WeMos D1 mini with DHT11 Plotting in MegunoLink over UDP

Have you ever been late for a meeting and tripped on a micro USB cable, simultaneously loosing a critical data set and messing up your hair? No? Let’s keep it that way by sending Arduino data to a computer using UDP over WiFi!

This project will remove annoying cables from your data collecting life while still giving you live data and an easy setup. How is this possible you may ask?

Well a UDP connection over WiFi can be used like a wireless serial port. So you can connect an ESP8266 WiFi board to any computer running MegunoLink.

I used a WeMos D1 mini ESP8266 WiFi/microcontroller.

Read on for all the details you need to make the switch to wireless data collection.

Built with MegunoLink

The UDP connection to the WiFi Arduino, and the graphs of the data it sends, are all done with MegunoLink — our companion software for Arduino developers.

MegunoLink lets you visualize serial data with graphs and tables, and quickly build user interfaces for your Arduino projects.

Download MegunoLink and give it a go for your next project!

Wireless Connection

What is UDP?

UDP stands for User Datagram Protocol. It is a network communication protocol designed back in 1979 by John Postel. UDP provides “…a procedure for application programs to send messages to other programs with a minimum of protocol mechanism” (IEN-88). The downside is that UDP packets have no guaranteed delivery, but in practice the chance of loosing a packet is pretty small.

So for practical purposes, UDP is an easy way to send data between a microcontroller, such as an Arduino, and a computer across a local network. Just like a serial port…but no wires!

Just how easy is it? Well, unlike the Transport Control Protocol (TCP), UDP messages can be broadcast to all devices on a local network rather than needing a specific address. This means you don’t have to worry about static IP addresses or name resolution to connect two devices, just use the Broadcast IP Address of “255.255.255.255” and every device on the local network should get the message.

Of course not every device is going to want your messages. So UDP has ports to create communication channels between the sender and the receiver.

A port is just a number between 0 and 65535 that the sender and receiver agree on. So for our purposes, all you have to do is pick a port number for the Arduino to use to send data to and type the same port number into MegunoLink so it knows where to listen.

Pick a number, it’s that simple*.

* Ok, not quite. Many numbers are already in use or are reserved but any number between 49152 to 65535 should be fine. You can check the status of any port number on the port number registry.

MegunoLink UDP Connections

MegunoLink can receive and transmit data over UDP as easily as it can over serial (RS232), an XBee Series 2 connection or even a text file.

To create a UDP connection, just:

  1. create a Connection Manger Visualizer (if you don’t have one open already),
  2. click the “Add Connection” drop-down button,
  3. select “UDP”,
  4. type your chosen UDP port number into the “Receive Port” box and,
  5. hit “Connect”!
Screenshot of UDP entry in the MegunoLink connection manager

To open a UDP Connection simply check the receive port number matches the sending one in your Arduino sketch and click “Connect”

UDP connections through MegunoLink can be used for remote control of systems as well as monitoring. Just choose another port number for transmitting data in the other direction to establish two-way communication.

Check out these links for more information:

Arduino UDP over WiFi

Sending UDP messages over WiFi is easy with the WiFiUDP library included with the ESP8266 core for Arduino (install instructions later in this post).

It functions just like the UDP components of the Ethernet library included with the Arduino IDE. After some includes, a declaration and a quick setup which are all covered later in the later guts of this post all you need to do to send data is:

  1. call .beginPacket(…) supplying the destination IP address (or 255.255.255.255 for broadcast) and your chosen port number,
  2. call .write() or .print() to add the data you want to send to the packet, and
  3. call .endPacket() to send the packet when you’re done.

Nice.

ESP8266 WiFi Connection Manager

Screenshots showing the three step process of configuring the WiFi connection

Configuring the ESP8266 WiFi connection is easy with the captive portal generated by the WiFiManager library

A common challenge with WiFi devices is joining the network and setting up the password. The awesome WiFiManager library by tzapu makes this really easy for Arduino’s using the ESP8266 module.

Once the library is installed (details below), it will create a hot spot you can connect to with your phone or table. A web-page will popup to configure the Arduino’s WiFi connection when you join the network. This method of having a page open after connecting to a WiFi access point is known as “captive portal” and is often used to present a login page before giving access to the wider internet in hotels.

All you need to do to use this, besides includes and a declaration covered later, is add a single line to your setup() function as shown above. Simple!

The configuration is saved to EEPROM so you don’t have to input it again, even if you loose power or upload a new program version. The manager will start a configuration access point up again for you if the saved WiFi connection cannot be established.

Data Plotting

Temperature and Humidity Sensing

Today’s project is measuring temperature and humidity using a DHT11 sensor on a shield for the WeMos D1 mini. The DHT11 measures temperature from 0 to 50 °C with a resolution of 1°C and humidity from 20 to 90% RH with a resolution of 1%.

I’m going to graph the data with MegunoLink.

A DHT11 on a WeMos D1 mini Shield

Embedded Exponential Filter

Unfortunately this sensor has a low-resolution and noisy measurements. An exponential filter is an easy way to solve this problem with the Arduino. This filter smooths measurements by combining new values (xn) with the previously filtered value (yn – 1) to calculate the new filtered value (yn):
yn = w × xn + (1 – w) ex× yn – 1

The previous and new values are combined with a weighting factor w. The weight must be between 0 and 1.

Weights close to 1 will make the filter favor new values and respond faster to changes, but will reduce the smoothing. Values close to 0 will favor previous values removing lots of noise, but it will respond slowly to changes. Just Find the balance that works for your situation by tweaking the value.

You don’t have to implement the filter yourself though. We’ve included a Exponential Filter in our Megunolink Arduino library.

Screenshot of a monitor visualizer displaying some received raw and filtered measurements

The MegunoLink Exponential Filter library allows us to see both the raw measurements and a cleaned version

I sent both raw measurements and filtered values over UDP for plotting to see the effect of the filter.

I used an initial value of 25°C for the temperature filer and 50%RH for the humidity filter. This makes the filter a bit quicker to settle when the program starts. You can change the initial values in the filter declaration.

MegunoLink Time Plot

This Time Plot shows how the exponential filter (lines) reduces the effect of small fluctuations in measurements (points)

MegunoLink’s Time Plot Visualizer provides a simple way to plot the temperature and humidity measurements sent by the ESP8266 sensor. MegunoLink looks for messages surrounded by braces (‘{‘ … ‘}’) like:
{TIMEPLOT|DATA|Raw Temperature|T|26.84}.

Here:

  • TIMEPLOT|DATA| signals the start of a message for the time-plot;
  • Raw Temperature is the series name for the data,
  • |T| tells MegunoLink to use the current time for the x-axis and
  • 26.84 is the temperature measurement we’d like to plot.

So if the current temperature is stored in a variable name temperature and udp is a WiFiUDP variable, you can send temperature to MegunoLink using:

But that’s quite a concoction to remember. So we built a plotting library for Arduino which takes care of the details. To use the library #include "MegunoLink.h" at the top of your program. Then you can:

Here:

  • udp is the WiFiUDP variable for sending data through the ESP8266’s UDP connection,
  • Raw Temperature is the series name for the data, as before,
  • temperature is the variable containing a temperature measurement.

Check out our introduction to plotting data with MegunoLink or the detailed Time Plot documentation for more information.

Plot from Arduino sketches with MegunoLink

MegunoLink Project

In MegunoLink you can build window layouts that include graphs, tables, logging, interface panels and more. These interfaces can be saved in a project file so you can use them again or share them.

UDP Monitor Interface in MegunoLink

The MegunoLink UDP Monitor Interface designed for this project

The MegunoLink project created for this project can be found in our Arduino-ESP8266-UDP-Monitor github repository along with the matching Arduino sketch.

It should allow you to get up and running with little effort, just follow the UDP connection instructions given earlier and the plot will format itself based on the data from the Arduino program. You can read more about how to customise the interface or create your own in the article about Interface Panels.

Build an interface panel for your Arduino sketch

Data Logging

Screenshot of MegunoLink "Log to File" and "Monitor" visulaizers with a Notepad window containg the same data overlaid

Any connection can be logged to a text file in MegunoLink in addition to plotting or other visualizers

Simply add a “Log to File” visualizer to log the data as well as plotting it. Find out more in the documentation on Logging Data.

Well that’s all the cool stuff you can do with this project, here’s some details on how to buy and install everything.

Hardware

ESP8266: Compact WiFi/Microcontroller Module

All of this would have been harder and more expensive to do with a traditional Arduino board with a WiFi shield. The ESP8266 module on offers a microcontroller capable of many simple tasks combined with convenient WiFi in a single compact, cheap module. Thanks to the ESP8266 core for Arduino and tzapu’s WiFiManager library, connecting an ESP8266 to WiFi and a UDP connection are easy. Just choose a dev board with it on and away you fly.

WeMos D1 Mini

WeMos D1 mini

A WeMos D1 mini with headers attached

I used a WeMos D1 mini development board with ESP8266 module in this article. The WeMos D1 mini is available on AliExpress for $USD4.00 from the original manufacture. WAVGAT has a clone, the D1 Mini for only $USD2.85.

Of course there are many ESP8266 development boards out there, all of which should work fine to send UDP data to MegunoLink. Some options are:

Post a comment below if you find another board that fits the bill, or let us know if any don’t make the grade.

One nice aspect of the D1 mini family are the handy shields available for them. Including a DHT11 temperature and humidity sensor used here. You can find the full selection in D1 mini Shields on AliExpress.

DHT11: Basic Temperature and Humidity

If your looking for something other than the D1 mini shield with the DHT11 mentioned above, a DHT11 on a breakout board with the necessary data line pull-up resistor such as the DHT11 Temperature And Relative Humidity Sensor Module can be found for as little as USD$1.70 at the time of writing from AliExpress. If you’re fine with adding your own resistor, the sensor by its self can be found for USD$0.70 on AliExpress or as the DHT11 basic temperature-humidity sensor + extras for USD$5 on Adafruit.

Alternatively you could use the more precise DHT22 sensor also supported by the sensor library.

Software

MegunoLink

MegunoLink is a tool to support Arduino development. Use MegunoLink to build an interface for your project including graphs, buttons, charts, tables and more. Download a free trial of MegunoLink to get started with wireless temperature recorder.

ESP8266 Core for Arduino Installation

Screenshot of the Arduino Boards Manager

Installing the ESP8266 core for Arduino is done nicely by the Arduino IDE Boards Manager

You can install the ESP8266 core for Arduino any one of four the ways described on its GitHub page. In this case I used the first method, searching for and installing the “esp8266” entry in the Arduino Boards Manager (Tools>Board>Boards Manager…). After installation you should be able to select your board from the board list and upload.

One thing to be careful with is pin numbers. For the WeMos D1 mini the GPIO numbers of the ESP8266 are different to the “D1” “D2” numbers marked on the board. To use the ESP8266 numbers, just use the number (eg. “2”) in the Arduino IDE. To use the D1 mini numbers, just put a “D” in front (eg. “D4”).

WiFiManager

Screenshot of the Arduino Library Manager

Installing the WiFiManager library is simple through the Arduino IDE Library Manager

tzapu’s WiFiManager library can also be installed from GitHub or using the Arduino library manager. In the library manager, (Sketch>Include Library>Manage Libraries…) just search for and install the “WiFiManager” library.

Just add the three includes and global variable declaration for a WiFiManager at the top of your sketch to use the library. Call wiFiManager.autoConnect() in setup() to initialize the WiFi connection and create the captive portal for configuring it.

WiFiUDP

The WiFIUDP library is included in the ESP8266 core for Arduino. Simply add the two includes, definitions and declaration shown below to the top of your program. In setup() call the udp.begin(…) method to initialize UDP communications.

MegunoLink Exponential Filter Library

Installing the MegunoLink Arduino libraries is quickly done through the Arduino integration setup in Megunolink

The exponential filter library is one of the many useful libraries provided with MegunoLink. Simply setup the Arduino Integration using the option in MegunoLink shown above. More details can be found in the article on Arduino Integration.

Once installed, just add the includes and declarations shown above to the top of your program with the values you want for the weight (0-100) and initial value.

DHT11 Library

Several libraries are available for the DHT11 and its close relations. In this case I chose to use the Adafruit DHT-sensor-library. You’ll need the Adafruit Unified Sensor library as well. Both are available on GitHub and can also be installed with the Arduino Library Manager.

Put the include, definitions and variable declaration shown above in the top of your sketch, remembering to change the pin number to whatever is needed for your board.

Start up the library in your setup as above.

Take measurements in your program as above.

Full Example Code

So here’s the full example that brings together all the parts. You can get the latest version of the ESP8266 temperature and humidity monitor from our GitHub library.

The GitHub library also includes a MegunoLink project to plot temperature and humidity with ESP8266 Arduinos.

With that I leave you to cut the cord, and discover wireless data collection with MegunoLink. Let us know how you get on with a comment below. If you have any trouble, head on over to the MegunoLink forum to post your questions.

Subscribe for MegunoLink and Arduino Tips, Tricks and Projects
Recommended Posts

Leave a Comment

Start typing and press Enter to search