__ESP8266 Part 10D

The LCD is working and the webapp is working as long as we know the ip address to go to. To get the IP address the ESP8266 has to be connected to a computer with the serial monitor open. We also have the network credentials hard coded which is not the best solution. To make this project stand alone we need a way of allowing it to connect to any network without reprogramming the ESP8266. We also need a way of knowing what ip address to go to once the device is connected.

Connect to any network

I had planned to simply add WiFi Manager and move on but I ran in to problems. With WIFi Manager included in the sketch things became unreliable and the sketch often crashed so and I started looking for a lighter version, this then lead me developing my own solution. A simpler connection portal that had one job; find networks and allow the user to connect to one of them.

WML is a function on a separate tab that can be dropped in to any sketch. I have played with this for a short while and the version below has worked well for me and has proved to be pretty reliable. The code is not unique, I took a lot of inspiration from the various WiFi Manager libraries but added my own style to the code. There is no auto connect functionality, this is handled natively by the ESP8266 any way, and it is up to the user/code writer to check for saved credentials before calling the function which is a technique I have been employing with WiFi Manager.

if (WiFi.SSID() != "")
{ call WiFi Manager}

Having such a simple connection function did produce one issue. There was no easy way to not connect to the saved network. This normally is not an issue but I have 3 networks and I could not easily change between them without reprogramming the ESP8266 (deleting the saved credentials) or turning off the network and letting the connection process fail so I need a way of activating the connection portal on command. I did this by adding another switch. If the switch is closed on start up the saved credentials are erased with a WiFi.disconnect() command.
It should be noted that I am not using the ESPWebserver library and instead relying on the request data to determine what page to serve or what action to take.

Get the ip address

The project has a LCD so the obvious choice is to display the local network ip address on the lcd. I can do this easily at start up but what happens if I need the ip address after the monitor has been running? I have used the same switch I am using to reset the saved network credentials. Once the device is running and showing the IOT monitor, closing the switch shows the local network ip address on the LCD.

Circuit

I have moved a couple of things and added a push button switch to pin D0.
On the NodeMCU pin D0 is connected to an onboard LED and when it is connected to GND the LED comes on. This is handy to show when the switch is closed. One thing to note; D0/GPIO16 is HIGH on startup so I am using normally HIGH rather than a normally LOW and the pin is pulled HIGH with a 10K resistor and connects to GND when the switch is closed.

[circuit]
[photo breadboard]
[photo breadboard]

WiFi Manager Lite

I have added a lot of Serial.print() and lcd.print() commands. This allows you to see what is happening as the code progresses. When using the WiFi Manager Lite function in other sketches these commands would not be present.

We can also use mDNS to connect.

Using Ap mode ans STA mode together.

ESP8266 gave only one radio, this means he channel used for ASP mode is dependant on the channel required for station mode. To allow for this use STA to connect to the local network first, let the channel number settle and then use AP mode.

 
 

mDNS

mDNS allows the use of local domain names. It can be very handy but it is not universally implemented, especially on Android devices. Using a webbrowser on a PC should work fine though.

If you need a refresher have a look at Connecting to an ESP8266 with unknown IP address using mDNS

Adding mDNS is fairly simple. Add the library and start the service.

At the top of the sketch where all the other include statements are add

#include <ESP8266mDNS.h>

and then some where bnetween connecting to the metwork and server.begin() statement add

  if (!MDNS.begin("esp8266")) 
  {
    Serial.println("Error setting up MDNS responder!");
    while (1) {  delay(1000);  }
  }
  Serial.println("mDNS responder started");
 
  // Add service to MDNS-SD
  MDNS.addService("http", "tcp", 80);

And finally in the main loop() function

MDNS.update();

Then we should be able to access, as long as you are using a device that supports mDNS, the ESP8266 server using the domain name esp8266.local.

 
 
 
 

 
 

Put the project in to a stand

I have a simple acrylic stand that I will use for this project. It is unlikely you will have the same stand and anything suitable can be used.

The stand has a hole for the LCD and holes to mount a 9cm x 5cm protoboard.

To move the project to the stand I need to transfer the components to the protoboard. Now that I have the LCD I do not need the status LED and I don’t really need the power LED, alkthough power LEDS are usually useful.

The eagle eyed may have notived that I have the temperature in Celcius and Faranheit but only display Celcius. To address this I will add the option to display the temperature is either by adding a small switch to the circuit. When the switch is in one position Celcius will be displayed and when the switch is in the other position Faranheit will be displayed.

 
 
 
 

http://www.martyncurrey.com/?wpdmdl=8966