As I mentioned in the previous posts, Node MCU is a tiny little board is based on ESP8266 WiFi module which is specifically designed for working with or without micro controllers which is specifically designed for IOT projects. Today I will how you how you can live stream and process Android Mobile Phone’s Sensor Data to node MCU using UDP.

UDP

The Transport layer in the OSI model manages data flow in an IP network. TCP and UDP are the two protocols working in this layer. Each has its own advantages and disadvantages as mentioned here. There is a trade off between speed and reliability. Since reliability is not an issue here, in this project we will be using UDP rather than TCP.

Setting Up Node MCU in Arduino IDE

The easiest way to program Node MCU is through Arduino IDE. Setting up Node MCU in arduino is very simple. You can find step by step instruction to set up Node MCU in Arduino IDE here. Once you have setup everything, please proceed to the the next step.

Setting up the Network

We will need a WiFi network to connect the listener (Node MCU) and the transmitter (Android Phone) to the same network. Best thing to do is set up a hot spot in your android phone.

By doing so, you can easily start and stop the network whenever you want. Make sure that you keep the listener with in the range of WiFi network.You can also use your home network to connect both devices. All that matters is that both the devices should be in the same network.

Video Tutorial

The Code

This is a simple arduino code that can be compiled and uploaded to Node MCU using Arduino IDE.

#include <ESP8266WiFi.h>

#include <WiFiUdp.h> const char* ssid = “rootsaid”;

const char* password = “testpassword”; WiFiUDP Udp;

unsigned int port = 5514;

char packet[255]; void setup()

{

Serial.begin(9600);

Serial.println(); Serial.printf(“Connecting to %s “, ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED)

{

delay(500);

Serial.print(“.”);

}

Serial.println(“Connection Successful”);

Udp.begin(port);

Serial.printf(“Listener started at IP %s, at port %dn”, WiFi.localIP().toString().c_str(), port);

} void loop()

{

int packetSize = Udp.parsePacket();

if (packetSize)

{

Serial.printf(“Received %d bytes from %s, port %dn”, packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());

int len = Udp.read(packet, 255);

if (len > 0)

{

packet[len] = 0;

}

Serial.printf(“UDP packet contents: %sn”, packet); }

}

When this code is uploaded, the Node MCU will automatically connect to the WiFi network, obtains and IP address and starts an UDP Listener in the above mentioned port (5514).

const char* ssid = “rootsaid”;

const char* password = “testpassword”;

Change the ssid to the WiFi name and password to WiFi network password.Now start the serial monitor.

Here you will see the status of Node MCU. Take a note of the IP address and Port.

Sending UDP packets

There are various Apps available in play store that can be used to send UDP data to other devices from your phone. Here, I will be using ‘Sensor UDP’ for this purpose. This application enables us to send real time sensor data to a remote device.Here enter the IP address of the Listener and Port number. Type something in the ‘text to send’ and click send.

If everything is done correctly, you will see the text you send in the Serial Monitor.

Here you can also choose the sensor values that can be sent using Sensor UDP. This will be streamed and can be viewed in the Serial monitor.

These data can be used for various other projects like controlling a robot, making a wireless mouse etc.

Rate the Project

Did you find this page useful? Help us to improve by rating this page.

Name Email Review Title Rating Review Content