Home Internet of Things

ESP8266 Building Block #1A

The Center of Things


In this building block we will add communication between the Refrigerator Monitor of Block #1 and a central monitor. This monitor will collect data from any number of the block #1 devices, analyze the data and post TEXT messages to your smart phone if the data is not acceptable. Before we can make this central monitor we need to add some capability to the original Block 1 monitor to let it talk to the world.


In addition, we will add a couple of features that will be included in all future projects. These features are OTA or Over The Air flash of the ESP8266 and TELNET debug.


OTA allows the flashing of the firmware into an ESP8266 without any USB or other serial connection. It is all done via WiFi. Works great and allows the ESP8266 to be located in virtually anywhere that your local WiFi network touches.


TELNET debug is very handy when you break the USB connection. Without USB there would be no way to get debug information out of the ESP8266. With TELNET installed in the ESP8266 you can get data displayed on your PC.


This building block is unique in that all you need is a NODEMCU ESP8266 break out board. You can power it from the USB (necessary until the OTA is installed). After OTA is installed it only needs power. No other connections are required at this time.


Go to http://firebottleradio.com/iot/block1a.ino

to get a copy of the sketch and we will look at the changes that have been made to make OTA and TELNET work.


What has changed??


The answer is... a lot!! Basically, three major changes:


  1. Addition of code to support OTA or Over The Air flash of the ESP8266.

  2. Addition of code to support TELNET debugging of the code.

  3. Addition of code to broadcast (via UDP) the temperature information.


Get the sketch downloaded and installed into the Arduino IDE and take a look at the code.


OTA


Before you start using OTA there are a few things that must be done. Go to:

http://esp8266.github.io/Arduino/versions/2.0.0/doc/ota_updates/ota_updates.html

and select the ARDUINO IDE option and read the setup instructions. You MUST install PYTHON 2.7 (NOT 3.x) for OTA to work.


A number of changes have been made in the sketch to add OTA. Some of them are unique to OTA and some will also be used by TELNET and UDP.


  1. In the sketch before setup()

#include <ESP8266mDNS.h>

#include <ESP8266WiFi.h>

#include <WiFiUdp.h>

#include <ArduinoOTA.h>

//ssid and password needed for UDP, OTA, and TELNET

const char* ssid = "PUT YOUR SSID HERE";

const char* password = "PUT YOUR PASSWORD HERE";

//

//these are OTA only

#include <WiFiUdp.h>

#include <ArduinoOTA.h>

//

  1. A long block of code in setup() marked

    //

    //OTA only until //end of OTA

    // Port defaults to 8266

    ...until...

    //end of OTA

    //

  2. and these lines at the beginning of loop()

    //OTA only

    ArduinoOTA.handle();

    //end of OTA

All of this makes you sketch announce itself to the network and from that the Arduino IDE will add this node to the list of network nodes (TOOLS > PORTS > Network ports. This node's network port can now be selected for flash. It works just like flashing through USB except no USB connection is required. When you have more than one node set up for OTA you need to adjust the node names and passwords in the setup() OTA code to make each one unique. Be sure to take not of the IP addresses assigned to each node that you add (on the Network Ports list). There is a bug in IDE 1.8.1 that gets the node names confused. If you have two or three nodes it works fine. If you have 10 nodes as I have then some of the names are duplicated. However, the IP address will be correct. If you keep a list of IP addresses as they are assigned then you can sort it out. This has been reported and will no doubt will be fixed.


Just to make sure I don't flash the wrong node I use the OTA option to have a password on the flash process. Look at the OTA code in setup() and you will see a line that has a password. I set this to something related to the node name. That way, if I pick the wrong node from the list it will refuse the flash.


TELNET


TELNET is an application on your PC that can communicate with another TELNET equipped computer. We have added code to the sketch to support TELNET. Go to:

https://github.com/JoaoLopesF/ESP8266-RemoteDebug-Telnet


to read about and download the TELNET Remote Debug Library. Install the library in your Arduino IDE libraries.


There are a number of changes needed to the sketch to make TELNET Remote Debug work:


  1. These will already be in the top of the sketch if you added OTA

#include <ESP8266mDNS.h>

#include <ESP8266WiFi.h>

//ssid and password needed for UDP, OTA, and TELNET

const char* ssid = "PUT YOUR SSID HERE";

const char* password = "PUT YOUR PASSWORD HERE";

//

  1. Add these lines before setup()

//comment this out to remove TELNET

#define TELNET


#ifdef TELNET

#define PRNT Debug

#endif


#ifndef TELNET

#define PRNT Serial

#endif


#ifdef TELNET

//this is for telnet only

#include "RemoteDebug.h" //https://github.com/JoaoLopesF/RemoteDebug

RemoteDebug Debug;

#define HOST_NAME "refrig1"

#endif

//


  1. In the sketch in setup()

    #ifdef TELNET

    //telnet only

    String hostNameWifi = HOST_NAME;

    hostNameWifi.concat(".local");


    WiFi.hostname(hostNameWifi);

    if (MDNS.begin(HOST_NAME))

    {

    Serial.print("* MDNS responder started. Hostname -> ");

    Serial.println(HOST_NAME);

    }

    MDNS.addService("telnet", "tcp", 23);

    Debug.begin(HOST_NAME); // Initiaze the telnet server

    Debug.setResetCmdEnabled(true); // Enable the reset command

    Serial.println("* Arduino RemoteDebug Library");

    Serial.println("*");

    #endif

  1. At the very end of loop()

    //

    #ifdef TELNET

    // required for telnet

    Debug.handle();

    // Give a time for ESP8266

    yield();

    //

    #endif


In theory you can TELNET to your ESP8266 by using the HOST_NAME “refrigx”. However, I find that this just does not work and you need to use the IP address. You will need to change the “PUT YOUR SSID HERE” and “PUT YOUR PASSWORD HERE” to your Wifi SSID and PASSWORD (pass phrase).


After this code is installed and compiled and flashed into your ESP8266 fire it up while connected to your PC with a USB cable. Use the Serial Monitor to watch it start up. Watch for the IP address. Something like 10.0.0.123. This will be the same IP address that OTA uses.


Open a DOS window (command prompt) on your PC and type “telnet 10.0.0.123” use the IP address that was printed at start up. If you get an error then telnet is probably not enabled on your PC. Go here:

https://www.rootusers.com/how-to-enable-the-telnet-client-in-windows-10/


to see how to enable TELNET.


If you have successfully TELNET into your node you will get a page of instructions. You don't need to do anything else, just stand by and anything that is printed from the node will appear on the screen.


The TELNET print command in the sketch is exactly like the Serial.print() command except that it is Debug.print(). In the sketch I have preprocessor code that will change all of the token PRNT.print commands to Debug.print if TELNET is enabled and to Serial.print commands if telnet is NOT enabled. This way it will work with or without TELNET.


With both OTA and TELNET enabled you never again need to directly connect your EMC8266 to your PC. Very handy!!


UDP


UDP stands for User Datagram Protocol. This is a very simple way to send messages from one computer to another. It also allows a computer to broadcast messages to no one in particular. They are just sent out to a dummy IP address and port and any other computer listening on that port will get the message. The down side is that there is no way for the listener to know if a message is missed or to get correct data if the message is garbled. However, in this type environment where we are sending a new message every few seconds this is not a problem.


To enable send of UDP messages we need to add a few lines of code:


  1. Add these lines near the top of the sketch. If you have OTA or TELNET they will already be there:

    #include <ESP8266WiFi.h>

    #include <ESP8266mDNS.h>

    #include <WiFiUdp.h>

  2. just before startup() add

    //next three lines required for UDP

    WiFiUDP Udp;

    unsigned int localUdpPort = 4210; // local port to listen on

    char incomingPacket[255]; // buffer for incoming packets


Now it gets a little more complicated... You need to build a string with the message that you want to send. In this case the message is the temperature in the fresh food and freezer. You can do this almost any way you want. However, if you make more than one temperature monitor be sure that there is something that makes the message from each on unique. I chose a string that looks like:

"RF=xx.xx RZ=-xx.xx"

For ease of sorting the messages out at the receiving end I change the first letter of the message for each different monitor. The next one may be "QF=xx.xx RZ=-xx.xx”. The two letters on the freezer temperature are just there to make the message look good. Look at the code in the sketch and you will see how I pack the data from the temperature sensors into the UDP packet. When the packet is ready do the following to sent it on its way:


Udp.beginPacket(address, port);

Udp.write(packet);

Udp.endPacket();


Any computer listening on the port specified will get the packet and can decode it. The address is set to 255.255.255.255 so any computer can listen to it.


We now have a refrigerator monitor that not only checks the temperature and lights LEDs so that you will know if it is safe it also broadcasts the data to another computer. If you have more than one refrigerator or freezer you can make one of these for each of them.


We have added the talking part to the monitor but of course, at this point there is nothing that is listening to the broadcasts. That is BUILDING BLOCK #2. Click here to take a look: http://firebottleradio.com/iot/block2.html