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.

License

Notifications You must be signed in to change notification settings

jxmot/ESP8266-udp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

An ESP8266 project that uses UDP to send messages to a server. The primary use is for testing code before committing to a project.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published