Skip to content
An ESP8266 project that uses UDP to send messages to a server. The primary use is for testing code before committing to a project.
C++ JavaScript C
Branch: master
Clone or download
Latest commit 95de966 Feb 13, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
data config file updates Feb 13, 2018
mdimg README image update Feb 13, 2018
nodejs minor clean up, and changes to match docs Feb 13, 2018
.gitignore initial commit Nov 22, 2017
APPRUN.md major README updates Feb 13, 2018
AppCfgData.cpp added setDebugMute(bool) Nov 23, 2017
AppCfgData.h added setDebugMute(bool) Nov 23, 2017
ConfigData.cpp initial commit, configuration file base class Nov 22, 2017
ConfigData.h initial commit, configuration file base class Nov 22, 2017
ESP8266-udp.ino modified to match docs Feb 13, 2018
ESP8266_INO_FUNC.md wip: misc docs, misc updates (draft) Nov 24, 2017
ESP8266_UDP_FUNC.md wip: misc docs, misc updates (draft) Nov 24, 2017
LICENSE Initial commit Nov 22, 2017
MultiCastCfgData.cpp added public enable flag Feb 12, 2018
MultiCastCfgData.h added public enable flag Feb 12, 2018
NODE_TESTAPP.md major README updates Feb 13, 2018
OUTPUT_EXAMPLE_01.md major README updates Feb 13, 2018
OUTPUT_EXAMPLE_02.md major README updates Feb 13, 2018
PCKTSENDER_EXAMPLE.md wip: misc docs, misc updates (draft) Nov 24, 2017
README.md README update Feb 13, 2018
SrvCfgData.cpp removed `recvport` and renamed `sendport` to `port` Feb 13, 2018
SrvCfgData.h removed `recvport` and renamed `sendport` to `port` Feb 13, 2018
WifiCfgData.cpp general clean up Nov 23, 2017
WifiCfgData.h general clean up Nov 23, 2017
connectWiFi.cpp added multi-cast capability under #ifdef USE_MCAST Feb 10, 2018
connectWiFi.h iniital commit, WiFi connection class Nov 22, 2017
esp8266-ino.cpp removed `recvport` and renamed `sendport` to `port` Feb 13, 2018
esp8266-ino.h removed all #ifdef USE_MCAST, replaced some with use of a config flag Feb 12, 2018
esp8266-udp.cpp removed `recvport` and renamed `sendport` to `port` Feb 13, 2018
esp8266-udp.h removed all #ifdef USE_MCAST, replaced some with use of a config flag Feb 12, 2018
udp-defs.h removed #define USE_MCAST, replaced with config Feb 12, 2018

README.md

ESP8266-udp

An ESP8266 project that uses UDP to send messages to a server.

Overview

This project demonstrates the use of UDP on an ESP8266 (NodeMCU v1.0). It acts as a client and connects with a server. Software components included here come from some of my previous work, such as ESP8266-config-data-V2.

History

I wanted to test the UDP capabilities of the ESP8266 platform before I implemented code in a project. This application and its accompanying Node.js applications will do just that. The areas I was intereseted in were -

  • UDP one & two way communication between a device and a server.
  • UDP multi-cast communication from devices to one or more servers, including replies from servers.

Running the Application

Please read through this section and the files it links to at least once before proceeding. There are configuration changes you will need to make before running the applications.

A Server for Testing

You will need a server capable of responding to the UDP packets sent from the sketch. A second ESP8266 could be set up as a server by using one of the many UDP examples found online. A good one is Arduino/doc/esp8266wifi/udp-examples on Github. Please note that a Packet Sender example specific to this project will be provide later.

My preferred test server is NodeJS running a simple script. That's because I'm also developing a server to handle incoming data from sensors that will be on my network. And I've got experience writing Node.js applications.

NodeJS Testing Code

In my current development set up I use Visual Studio Code to run and debug the UDP server code. However the examples found in the nodejs folder in this repository can be run from the command line -

  • server-udp.js - A simple server that listens for UDP packets containing text. After a packet is recevied it replies to the sender.
    • client-udp.js - A simple UDP client that sends packets containing text.
  • multi-udp.js - A multi-cast UDP server that listens for messages on 224.0.0.1 and then replies to the sender via their IP address.

To see information and an example on using the scripts with this sketch right-click here and open in a new tab or window.

Build and Run

In order for the sketch to run on your network and use your UDP server some minor modifications must be made. To see some instructions on modifying it right-click here and open in a new tab or window.

Design Details

This sketch behaves as a client and not as a server as shown in most ESP8266 UDP examples. In those examples the ESP8266 responds to a UDP packet with a reply. In this sketch it behaves as a client and sends a packet to a server and then it expects a reply from the server.

UDP Packet Flow

The sketch continuously alternates between sending a UDP packet and waiting for a reply. After a packet is sent a delay of 1 second occurs before it attempts to receive a reply.


loop() Flow Chart

Code Details

The main sketch file - ESP9266-udp.ino, intentionally does not have a lot of code in it. Instead the code you would normally see is grouped into two types -

esp8266-udp Functions : UDP initialization, send, and receive. To view function descriptions please right-click here and open in a new tab or window.

esp8266-ino Functions : Miscellaneous and reusable functions. To view function descriptions please right-click here and open in a new tab or window.

Future Modifications

Just some things I may experiment with. This section will get updated as I work on them.

JSON Data via UDP

Create functions that can take an object and render them as JSON strings. Then send the JSON data to the server and expect a JSON response. The response will then be parsed into an object for use in the application. Possible uses are -

  • Obtaining configuration data.
  • Registering a device with a server.
  • Sending device/sensor data to a server.

Links and References

NodeMCU

UDP

NodeJS

Recommended Reading

To fully understand ArduinoJson and how to properly determine the appropriate size of the buffer needed for your JSON data I recommend that you read the following -



© 2017 James Motyl
You can’t perform that action at this time.