Friday, October 27, 2023

Migrating from one internet provider to another

For an index to all my stories click this text

A short story here because I have a lot of work at hand.

I just transferred to a new internet provider. The reason is money. Not that I was not satisfied with my old provider but it was getting expensive. I had a separate telephone landline, an internet connection and a television subscription. Each with a different provider.
I switched to one provider that supplies all. Their combined services are a lot cheaper. And they provide faster internet access for a combined lower price. A drawback is of course that when the connection dies I loose all: telephone, internet and television.

Migrating from one internet provider to another means re-programming all your devices. Not only do you get a different router with new login codes, but all your network devices get new IP numbers.

So I have to reprogram our mobile phones and my Google Home Assistant. But I also have to re-program the internet connections in each of my ESP's and Raspberry Pico's.

Then I realised that I not only have to re-program their internet connections. I also have to re-program all the programs that connect to my Domoticz home automation system. Why ?? Well because the programs in the microcontroller point to the internal IP address of my Domoticz system and that had of course changed.

This means getting physical access to the controllers. Attaching them to my PC through USB and re-programming their settings. And believe me: this is a lot easier when your microcontrollers run MicroPython then when they are programmed with the Arduino IDE (C++). The latter means editing the programs, recompiling and sending them to the ESP's.

When your micro's are programmed with MicroPython you just connect them to your PC, load the program from the controller into your PC with Thonny, edit the IP numbers and credentials, and write them back. Done in no-time !!!

And I have to re-program my Domoticz system. As the Microcontrollers get new IP addresses Domoticz is sending commands to non-existant IP numbers.

And then there is that other matter.
My new provider (Ziggo) does not provide a fixed IP address for my router. To the outside world I have a dynamic IP address..............
This means that my IP address to the outside world might change any minute.

No problem ??? Well yes !!!
I want to access my Domoticz system (and some other devices) when I am not at home. And for that I need to know my routers IP-Address.

So this story is actually just a small warning.
Think real good before you migrate from one internet provider to another......There is a lot of work involved.

I am also thinking about migrating from Domoticz to Home Assistant.
So a few stories coming up:
- How to handle a dynamic IP address
- Migrating from Domoticz to Home Assistant or not ???

Till next time
have fun


Luc Volders

Friday, October 13, 2023

ESPEasy part 1: IOT without programming

For an index to all my stories click this text

A few year ago on an electronics fair I stumbled upon some guys who were controlling ESP8266's with something called ESP Easy. I looked into it and forgot about it. ESP Easy was at that time immature and had limited purpose for me.

But boy has that changed !!!
ESP Easy is now a mature product with no less then 7 developers working at the software and 3 guys working on the documentation. It still has some quircks but overall it is becoming a mature and very usable project. It is well maintained and updates regular appear.  However I have to say that, despite 3 people working at it, the documentation is really lousy.

If you need a quick way to add some sensors or actuators to your home automation system make sure to check this out !!

What we are going to do.

In this story we are going to make a switch in Domoticz that sets a led on or off on an ESP8266 without a line of programming !!!

What is ESP-Easy

ESP Easy started out as a project to, in an easy way, attach sensors and actuators to an ESP8266 with the purpose to send the data to Domoticz. Further more the idea was that you did not need a lot of programming knowledge to get started. Everything could be done with a few mouse-clicks and some easy to understand commands. The on-line manual says it like this: So the idea was to have something plug and play to hook up sensors to Domoticz.

Well in a way they have succeeded in this. ESP Easy has a nice looking interface. No drivers need to be installed (all are aboard). Hundreds of sensors and actuators work direct out of the box. For simple tasks no programming is involved. And for some more complex tasks a simple programming language (called rules) is build in.
ESP Easy does not only connects to Domoticz (by HTTP and MQTT) but also to Thingspeak, Home Assistant, OpenHab, PiDome MQTT, Emoncms and it has generic HTTP and UDP.
The build-in Generic HTTP makes it possible to send commands to many other services and even to other ESP's.

ESP-Easy has its own installer. You only have to connect the ESP8266 (or ESP32) to your computer with an USB cable and start the install software. A few minutes later you are set to go.

Just have a look at the list of sensors and actuators and be impressed:
https://www.letscontrolit.com/wiki/index.php?title=Devices

Any Flaws ???

Yes there are some quircks.
As far as I have tested the software works flawlessly. The problems are in the documentation.

First there is a easy to use web-based installer that replaces the old installation procedure. However there is no mention (well I could not find it) in the documentation. Don't worry it is covered here !!

Not all pages are real-time updated. It took me a while to realise this, and therefore was at first confused about the workings of a simple switch.

It took me some time to figure out how to send data over HTTP. Again a lack in the documentation. And the same happened when I wanted to attach a TM1637 7 segment display. I'll explain these later on in this story.

Nevertheless when you get used to the way of explanation in the documentation it is a very usefull project.

Installing ESP Easy


Installing the ESP Easy software is incredibly easy........if you know how.

The documentation sends you to the Github pages where you can find all published versions. You can download one of these and use the supplied installation program. Look for a direct installable binary in the releases section: https://github.com/letscontrolit/ESPEasy/releases

The installation program is however not easy to use and has been modified from previous versions which were much better. So we are going to take another road.

By chance I blundered upon a webinstaller. You can find it here:
https://td-er.nl/ESPEasy/latest/

This is the easiest way to install ESPeasy on an ESP8266 or ESP32.

To use this you need to address this webinstaller with Chrome or MicroSoft Edge. The installer will not work with Firefox !!



This is how the page looks.



Click on the drop-down menu and a long list with available versions appears. Some versions are specifically designed for a purpose like the Climate, Energy or Neopixel versions. You can find the pro's and con's of each version in the Documentation (just click on the highlighted word Documentation). But for general purposes just use the Normal 4M version.



Connect your ESP8266 or ESP32 with a USB cable to your computer and click the blue CONNECT button.
A pop-up window appears in which the USB ports are listed. I had two ESP's attached at the moment but wanted to install ESPEasy to the one connected on COM8. Next click on the blue Connect button.



A new pop-up window appears. Click on "INSTALL NORMAL 4M".



Press the boot button on the ESP8266 or ESP32 and installation begins.



After a few minutes the installation finishes. Then press NEXT.



Press again on the blue CONNECT button and again a pop-up window appears. This time you can choose between installing another ESP, configure the Wifi settings or view the logs. This time choose the Wifi settings.
The drop down menu shows all networks in your environment. Choose your router sname and fill in the password. Then click CONNECT again.



Back in the main screen again choose CONNECT and this time choose to view the log files in the pop-up screen.
Halfway the screen you can see the text:

2655 : Info : WIFI : DHCP IP : 192.168.178.211 (ESP-Easy)

This is the IP-address of your ESP-Easy. Of course that number will be different in your case.



Now open your browser on your PC and connect to the IP adress of ESP Easy. The IP address on this webpage is different from the one described above. That is because I did multiple tests and installs when writing this text.

That is all there is to the software installation. Now let us move on to using ESPEasy.
Lets make something simple for starters. Something where no programming for is needed !!! We are going to build a virtual button in the home automation system Domoticz and when that button is pressed a led attached to an ESP8266 will go on. In the real world you would replace that led by a relay, motor or whatever home automation project you have in mind.

If you do not have a Domoticz home automation system, no worries. You can give commands to ESPEasy from your browser !!! Just read on and in the next stories you will find more practical examples.

Breadboard setup




The breadboard setup is simple. just a led with a current delimiting resistor on D1 and a button on D5.

Sending a command to ESP-Easy.

There are several ways to communicate with ESP Easy. In the beginning of this story I mentioned that you can connect an ESP8266 with ESP Easy to almost any home automation system you can bring up. Communication is two way and can be done with simple HTTP commands or through an MQTT broker. We'll stick to HTTP commands here.

On the ESP Easy website in the documentation section look for GPIO : 

https://www.letscontrolit.com/wiki/index.php?title=GPIO

The command for setting a GPIO pin on is:  
http://<espeasyip>/control?cmd=GPIO,number,1

And for setting the GPIO pin off the command is:
http://<espeasyip>/control?cmd=GPIO,number,0

You need to substitute the word 'number' with the GPIO pin.



We are used to use D1 to D8 for the IO pins when programming the ESP8266 in the Arduino software. However ESP Easy uses the GPIO notation which has a different numbering as you can see in the list above.
Our led is attached to D7. Our first ESP8266 with ESPEasy has IP number 192.168.1.85 So the command to set the led on will be:

http://192.168.1.85/control?cmd=GPIO,13,1

Type this in a fresh tab in your browser, press enter,  and the led will go on. Replace the 1 at the end with 0 and the led will go off again. Remember to replace the IP number with the one your ESP8266 has.


Building a button in Domoticz.

Actually this was already covered in an earlier story on this weblog. You can re-read that here. So I am going over this in a brief way.



In the Domoticz settings menu choose hardware and add a new Dummy virtual switch and call it ESP EAsy led



When done click on the Make Virtual sensor button (Maak virtuele sensor). Call it ESP Easy and as sensor type choose Switch (Schakelaar).



Now switch over to the Domoticz Switches tab and your newly build switch is there.



Click on "Aanpassen" and alter the settings to your liking. The most important part are the Aan actie (On action) in which we fill in the HTTP command that switches the led on:
http://192.168.1.85/control?cmd=GPIO,13,1

And in the Uit actie (Off action) fill in the appropriate command for setting the led off.

Don't forget to save (Opsaan) the settings. And you're done !!!

Done !!

Indeed that is all. We just made a switch in Domoticz that sets a led on or off on our ESP8266 without 1 line of programming !!!

There is more.

Indeed there is. ESP-Easy is chock full with drivers for servo's, temperature sensors, rotary encoders, neopixels, ldr's, pir's, 7 segment displays, oled and tft displays etc. etc. etc. Like said before in this story: have a good look at the documentation and be amazed. The possibilities are immens.

Go ahead and look around in ESP-Easy and play with the options. This story just scratched the surface.

Next time I am going to show how to send data (like temperature values) from ESP Easy to Domoticz and how to send data from one ESP8266 to another ESP8266 both equipped with ESPEasy.

Till that time
Have fun

Luc Volders
















 

Friday, October 6, 2023

ESP8266 send data to a webpage

For an index to all my stories click this text.

Normally when you run a webserver on your ESP you can adress that ESP to send commands to it. The ESP has a webserver that sends a webpage to your device (pc, tablet, phone). To command several ESP's that way is not easy. You need to re-open the browser on your device to get the webpage's from each individual ESP.

In a previous story I adressed this problem and showed how you could build a webpage with Javascript that adressed several ESP's and send commands to them. That webpage runs on your PC, tablet or phone. So NOT on the ESP itself. The ESP's are running a webserver that actually does not serve a webpage. Only the framework is used to receive commands from the webpage on your PC. Yoy can re-read that story here http://lucstechblog.blogspot.com/2022/05/control-multiple-esp-webservers.html

Getting information back.

As this works great I have used it in several of my projects. But it is a one-way-road. You can send only information to the ESP's. Well actually no. You can also get information from the ESP's back like a temperature or a confirmation that the information has been received.

Simply said: Set a switch on the webpage ON and the chosen ESP will set a led, relay, motor, fan etc etc on. Then the ESP will send back a confirmation that the led is indeed set on. Another example is to send a request to the ESP and the ESP sends the temperature or any other value or sensor reading back.

To achieve this the ESP sends a webpage back to the webpage it was getting the request from (the PC, tablet, phone). And that webpage that is send is just one line of text with the information we need.

Sounds complicated and actually it is not. Bear with me, and this story shows how to do this.

I am gong to show you how to send a random value to the Javascript page on your PC. To make it a bit lifelike I am going to pretend that that random value is a temperature reading from a sensor.

The hardware setup

Nothing special here.



Just an ESP8266 Wemos D1 mini with a current delimiting resistor attached to I/O pin D5. This is exactly the same setup as used in the previous story: http://lucstechblog.blogspot.com/2022/05/control-multiple-esp-webservers.html

The ESP8266 code

Actually this is also basically the same code as the code used in the previous story: http://lucstechblog.blogspot.com/2022/05/control-multiple-esp-webservers.html with some minor expansions.
Please re-read that story because I am only going to discus the alterations.

here is the full code:

// ======================================================
// Webserver program that has a simple webpage and the 
// program only handles a received ON-OFF command by 
// putting a led on or off.
// command is IPADRESS/?cmd=ON
// command is IPADRESS/?cmd=OFF
//
// value is IPADRESS/?TEMP
//
// this command prints a random value in the
// Serial monitor and sends that value
// back to the Javascript page that accessed the server.
// for that see Fetch-webserv2-4.html
// http://lucstechblog.blogspot.com/
// ======================================================

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

ESP8266WebServer server(80);

// Your routers credentials
const char* ssid = "XXXXXXXXXXXXXXXXXXX";
const char* password = "YYYYYYYYYYYYYYYYY";

// ==========================================
// initial variables
// ========================================== 

String ledcmd;
String value;

const int analogInPin = A0;

// =========================================
// Here is the HTML page
// =========================================

String getPageON()
  {
  String page = "The led is set ON";
  return page;
  }

String getPageOFF()
  {  
  String page = "The led is set OFF";
  return page;
  }  

String getPageTEMP()
  {  
  String page = "The temperature is ";
  value = String(random(25));
  page += value;
  Serial.println(page);
  return page;
  }   



// ==================================================
// Handle submit form
// ==================================================
void handlecmdreceived()
{  
   if (server.hasArg("cmd"))
      {
      ledcmd = server.arg("cmd");
      Serial.print("Thereceived command is:             ");
      Serial.println(ledcmd);
      if (ledcmd == "ON")
        {
           digitalWrite(D5, HIGH);
           server.sendHeader("Access-Control-Allow-Origin", "*");
           server.send(200, "text/html", getPageON());       //Response to the HTTP request
        } 
      if (ledcmd == "OFF")
        {
           digitalWrite(D5, LOW);
           server.sendHeader("Access-Control-Allow-Origin", "*");
           server.send(200, "text/html", getPageOFF());       //Response to the HTTP request
        }
        
      }
      
      if (server.hasArg("TEMP"))
           {
           server.sendHeader("Access-Control-Allow-Origin", "*");
           server.send(200, "text/html", getPageTEMP());       //Response to the HTTP request
           }  
}  


// ===================================================
// Handle root
// ===================================================
void handleRoot() 
{   
  if (server.args() ) 
    {
    handlecmdreceived();
    } 
}


// ===================================================
// Setup
// ===================================================
void setup()
{
  delay(1000);
  Serial.begin(9600);

  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid,password);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
  server.on("/", handleRoot);

  server.begin();
  delay(500);

  pinMode(D5, OUTPUT);
  digitalWrite(D5,LOW);

  randomSeed(analogRead(analogInPin));
}


// ===================================================
// Loop
// ===================================================
void loop()
{  
  server.handleClient(); 
  delay(50);
}

If you are going to copy this code replace the XXXXX and YYYY with your own routers name and password.

I am only going to discus the expansions from the previous code here in some detail.

const int analogInPin = A0;


This line makes the program aware that the analog pin on the ESP8266 is A0. We are not going to do anything special with this analog pin. It is only used to get a seed for the random function.

String getPageON()
  {
  String page = "The led is set ON";
  return page;
  }

String getPageOFF()
  {  
  String page = "The led is set OFF";
  return page;
  }


The variable page will hold the very simple webpage we are going to send back to the requesting webpage on your PC.
Three different webpages will be build in this example. The first page is a confirmation that "The led is set ON"
The second page is a confirmation that the "The led is set OFF"

String getPageTEMP()
  {  
  String page = "The temperature is ";
  value = String(random(25));
  page += value;
  Serial.println(page);
  return page;
  } 


This is the third webpage which will send the fake temperature value to the HTML page with Javascript.
The string variable value will get a random value between 0 and 25. This value is put into the page string together with the text "The temperature is "

On your PC there will be a webpage with a Javascript program that will send the commands:
fetch('http://ESP8266-IP-ADRESS/?cmd=ON')
fetch('http://ESP8266-IP-ADRESS/?cmd=OFF')

To set the led on or off. What the ESP8266 has to do is to analyse that incoming command and send a confirmation that the command has been received and processed.

      if (ledcmd == "ON")
        {
           digitalWrite(D5, HIGH);
           server.sendHeader("Access-Control-Allow-Origin", "*");
           server.send(200, "text/html", getPageON());       //Response to the HTTP request
        } 



And this is where the magic happens.

If the received command is ON then first the led is set ON.

Next a header is send to the requesting webpage with the following text: "Access-Control-Allow-Origin", "*"

Normally we do not use this if the webserver is coming from the same ESP as where the requests are coming from. But in this case we are using a PC to send commands to the ESP8266 and that PC is expecting an answer. The PC and the ESP8266 have different IP adresses and therefore this is absolutely necessary cause this tells the Javascript code on the requesting page to accept information from another IP adress. Without this command Javascript will not accept any information coming from the ESP8266.

And lastly the server.send command sends the string build in the getPageON() routine.

The same code with some minor alterations is used when the OFF command is received, and also when the TEMP command is received.

How to adress the webserver

Program your ESP with this software and open the Serial monitor.



My ESP got the IP number 192.168.1.83

The general command to set the led on is:

IPADRESS/?cmd=ON

and to set the led off:

IPADRESS/?cmd=OFF

To get the fake temperature made by the random function use this command:

IPADRESS/?TEMP

A first test.



To test wether everything works as expected open a webbrowser on your PC or phone and type in the adress bar: 192.168.1.83/?cmd=ON and the led should go on.

You need to replace 192.168.1.83 with the IP number your ESP displayed in the serial monitor. Typing this command will put the text The led is set ON on your webpage. And of course the led on the ESP8266 will go ON.



The command 192.168.1.83/?cmd=OFF will set the led of and also put the text The led is set OFF on the webpage.


Putting http://192.168.1.83/?TEMP in the browsers adress bar will set the text The temperature is 10 on your webpage. The number 10 will be different every time you give this command. It will be the random generated value from 0 to 25.

If this all works then we have accomplished sending a command to the ESP8266 and receiving information back !!!!

Control the ESP8266 from a webpage

Now that we can control the ESP8266 from our browser the next step is to build a simple webpage that controlls the ESP8266.

The webpage consists of some html code and some Javascript. Nothing to fancy to keep things as simple as possible. Basically this is the same code as used in the previous story. Left out are the commands for a second ESP and added are the code for displaying the fake random temperature values.

<!DOCTYPE html>
<html>
<body>

<h2>Control for ESP webserver</h2>

The ESP led is <output id="ledstat1"> ?? </output>
<br>
<br>
<button onclick="putledon()">ESP Led ON</button>
<br>
<br>
<button onclick="putledoff()">ESP Led Off</button>
<p>=======================================</p>
<h2>Temperature</h2>
<button onclick="gettemp()">Get Temperature</button>
<p id="gettemp"></p>

<script>

var rectext;
var first;
var second;
var led1stat;

function putledon() 
{
     fetch("http://192.168.1.83/?cmd=ON").then(function(response) 
      {
     return response.text().then(function(text) 
         {
         [first, second]=text.split("set ");
         document.getElementById("ledstat1").innerHTML = second;
         });
      });
  }

function putledoff() 
{
     fetch("http://192.168.1.83/?cmd=OFF").then(function(response) 
      {
     return response.text().then(function(text) 
         {
         [first, second]=text.split("set ");
         document.getElementById("ledstat1").innerHTML = second;
         });
      });
  }

function gettemp() 
  {
     fetch("http://192.168.1.83/?TEMP").then(function(response) 
      {
     return response.text().then(function(text) 
         {
         [first, second]=text.split("is ");
         document.getElementById("gettemp").innerHTML = second;        
         });
      });
     
  }

</script>
</body>
</html>


It is beyond the intention of this story to give you a complete course on HTML and Javascript but I will highlight some important parts in this code which are different from the code of the previous story.

Firts (of course) you will need to exchange the ESP's IP-adress for the one you found in the serial monitor when the ESP8266 boots.

Copy the code and paste it on your PC in a text editor like notepad and then save this code on your computer as espcontrol.html or any name you want to use. The code will be saved as a webpage.

Clicking that file will open your default web-browser and show the just created webpage.

That webpage is obviously NOT running on your ESP but running on your PC (or Raspberry or Phone or tablet etc). But it will send and receive commands from the ESP.

function putledon() 
{
     fetch("http://192.168.1.83/?cmd=ON").then(function(response) 
      {
     return response.text().then(function(text) 
         {
         [first, second]=text.split("set ");
         document.getElementById("ledstat1").innerHTML = second;
         });
      });
  }



The fetch command is a lot more complicated as the command used in the previous story. That is because the ESP8266 is sending an answer back which has to be processed.

After the fetch command is send to the ESP8266 a response is received and that response is stored in the variable text.

Next step is to split the at the word "set " (don't forget the space behind set). This is because the ESP sends the sentence The led is set ON and we need the part after set.

At this moment the variable second contains the value ON or OFF and that is placed on the webpage at the <output> tag which has the ID ledstat1.

Almost the same code is used for the command to set the led OFF and the command to get the temperature.

The webpage in Real Life

Here is how the page will look in your browser.



As you can see initially the state of the led is unknown and no temperature information is available.



And after pressing the ESP led ON button and Get Temperature button this is what the page looks. The state of the led is now displayed and the temperature is fetched.

So there we have it. An independend webpage that runs on your PC, Raspberry Pi, Phone or Tablet that sends commands to your ESP8266 and receives information back.

Adapt this to your own needs.

Again look to the previous story: http://lucstechblog.blogspot.com/2022/05/control-multiple-esp-webservers.html
Studying that story combined with the code from this story makes it possible to command several ESP's from a central webpage running on your PC, Raspberry, Phone or Tablet.

The previous story also showed how to send not just ON/OFF commands to the ESP8266 but also how to send values to the ESP8266. Use that to dim a led/lamp or to set a servo at a certain position, or control the speed of a motor, build a remote controlled car etc. etc.

You can combine : http://lucstechblog.blogspot.com/2019/08/esp-webserver-tutorial-part-4-slider.html with this story to put sliders in your webpage and send their value to the ESP8266.

The ESP8266 code can easily be altered to get real temperature values from a Dallas DS18B20 for example, or door sensors or light sensors etc. etc. etc.

Multiple sensors can be attached to one ESP or like stated before you can have one webpage control a multitude of ESP's.

Javascript is a very extensive language and the program can be made so that the ESP's are polled automatically for example every 30 seconds to get the status of important sensors.



If you are interested in Javascript please consider buyimg my book. The book is not a course or tutorial on Javascript but contains more than 500 tips for working with time and dates, sorting, objects, html manipulation etc. etc. etc. The book is available through your local bookshop and Amazon. 

Amazon link: Javascript tips by Luc Volders

Click on the picture of the book or on the previous link to order this book. As I do not have advertisements on these pages a donation or buying one of my books is the best way to support me.

Future plans

Wel I am using this in a real-life project which I am building at the moment and that will be a furure story.

Another thing coming up is how to easily convert an HTML page with Javascript into an Android App.

So a lot of things coming up. Stay tuned and have fun

Luc Volders