Exercise: NodeMCU recieve OSC

Objective

Control a remote/wireless microcontroller from a computer connected to the same network. We will do this by using some python code on a computer to control the onboard LED of the NodeMCU microcontroller.

The NodeMCU has a wifi chip, so why don't we use it! There are many different libraries and strategies for sending data over wifi with the NodeMCU. However, one super versatile and friendly way to do this is by using the Open Sound Control (OSC) protocol. OSC was originally developed as an open source protocol for communication among audio hardware and software. However, it is also a great protocol for easy wifi communication among any piece of hardware or software that speaks OSC. An OSC packet is either a bundle or a message. Bundles are just groups of messages sent at one time. In this exercise we will just be sending a message. Each message has both an address and one or more pieces of data. The address allows both the client (sender) and the server (reciever) to identify the data and the address always starts with a "/" character. For example "/led", "/a/1", and "/a/2" are all addresses. The data can be an int, float, or string. If you want to read up on the full protocol, and additional features like timestamps and additional data types check out the full spec.

Another great thing about OSC is that there are lots of applications and libraries that implement the protocol. Some examples include: Ableton Live, Max, PureData, OpenFrameworks, Python, C++, Java, Arduino, Unity, etc; for a comprehensive list of software that can send and receive osc, look here. By learning how to send and receive OSC messages from/to our NodeMCU microcontroller, we effectively become capable of communicating between our physical computing inventions, and any of these powerful applications.

 

Required Software

  1. python-osc package
    1. For the OSC library, we need to be running python 3. If you are not sure what version of python you have, you can run this command in the terminal:

      $ python -V
      You can download the latest version here.
    2. The python library we will be using is pythonosc. Follow the installations in the link. You can also install pythonosc using the python package manager, called 'pip', by running the following command:

      $ pip install python-osc
      If you do not have pip installed (you can tell by typing 'pip' in the command line), then install it using these instructions.
  2. OSC Arduino Library: Download and install OSC for the Arduino IDE from here. The installation instructions are the same as those you followed to install drivers for the I2C ADC board from Adafruit, in the last exercise.
  3. Firewall: We need to make sure that our firewall isn't causing any issues. On mac, the firewall settings are under System Preferences -> Security & Privacy -> Firewall. On PC, it is under Control Panel -> System and Security -> Windows Firewall. You only need to allow the ports we will be using in the examples which are 8888 and 9999.

Steps and observations

  1. Load the example NodeMCU sketch and upload it.
  2. Grab the example python program and open it in your editor of choice.
  3. In order for your computer and the NodeCMU to communicate wirelessly, they must both be on the same wireless network, and know about one another's IP addresses. We need to change the IP address in the python program to match the IP address of the NodeMCU so it knows where to send the data. To find the Node's IP address, open up the serial monitor and hit reset on your NodeMCU (if you just see garbage, make sure to change the baud rate of the serial monitor to 115200). It should print out the ip address. Be careful when using this in projects, the Node's ip address might change between restarts
  4. Save the python sketch with the updated IP address and run it (remember, it's python3, not python2!) from the terminal by executing the following command from the folder that contains the python code:

    $ python3 ./osc_client.py
  5. Watch as the LED on your NodeMCU blinks as it recieves data from the computer. You will notice that the LED actually turns off when the computer sends a 1 and on when it sends a 0. This is normal, the built in LED on the NodeMCU is just weird that way.
  6. Adjust the python program so the LED blinks in a different pattern

Comments

The arduino library is written by the Center for New Music and Audio Technologies at UC Berkeley that wrote the osc protocol and has great documumentation. I would highly recommend checking it out, esspecially the sections on Receiving Data, Routing/Dispatching, and OSCBundles.

Other Files

  1. ESP8266ReceiveMsg.ino
  2. osc_client.py