Pages

Friday, January 11, 2019

ESP - ESP Communication with WGET and MSGBRACNH

For a complete index of all my stories click this text

The general purpose of this story is a way to send data directly between ESP-8266's. So without the need of sending the data to a computer and having an other ESP retrieving it. No just direct communication between ESP's
There are many ways of communication between wifi devices. UDP is one way. And here I present you with another way.

The general line of this story may sound familiar because I have used this method before for communicating between an Android Phone/Tablet and an ESP8266

Send and Receive data with WGET and MSGBRANCH


Despite the fact that many people loathe it I am going to use my favorite ESP8266 devellopment environment for this tutorial: ESP-Basic.

ESP-Basic is very easy to use, interactive and has loads of drivers build in. Therefore it is very easy and fast to build IOT project's with it. It also is stable as I have made a thermometer with the softawre in ESP-Basic that is running non-stop from July 2017 without any flaws.

For an introduction and tutorial on ESP-Basic click here.
http://lucstechblog.blogspot.nl/2017/03/back-to-basic-basic-language-on-esp8266.html

Sending data

To send data we will use the wget command. The program is actually very simple.


 ' =================================  
 ' msg send/receive with msgbrach  
 ' and wget  
 ' sending routine  
 ' Luc Volders  
 ' http://lucstechblog.blogspot.nl/  
 ' =================================  
 cls  
 counter = 0  
 button "send it" , [sendit]  
 wait  
 
[sendit]   
 counter = counter + 1  
 tosend = "192.168.1.80/msg?data=there-you-are-"  
 tosend = tosend & str(counter)  
 wget(tosend)  
 wait  


Make sure you replace the IP number with the number your receiving ESP has. You can find the IP numbers of your ESP in your router.

I have used a variable with the name counter that is incremented every time the button is pressed. The variable is then transformed into a string as we can only send strings. The sentence that is actually send is:

there-you-are-X

With X being the number of times you clicked the button.

You might wonder why the "-"'s are placed between words. This is because this way of sending data does not send anything after a space. So I replaced all spaces with "-".

The important part is:

tosend = "192.168.1.80/msg?data=there-you-are-"

The receiving program will look for the phrase "data=" and knows that what follows this phrase is the data you are looking for. You can replace "data=" with anything you like but remember to alter it in the same phrase in the receiving program.

Receiving data

The receiving part is run on a second ESP. Obvious as we want to communicate between two or more ESP's.

 ' =================================  
 ' msg send/receive with msgbrach  
 ' and wget  
 ' receive routine  
 ' Luc Volders  
 ' http://lucstechblog.blogspot.nl/  
 ' =================================  
 cls  
 receiveddata = ""  
 memclear  
 msgbranch [getit]  
 textbox receiveddata  
 wait 
 
 [getit]  
 receiveddata = msgget("data")  
 receiveddata = replace(receiveddata , "-" , " ")  
 wait   


It can't get any simpler as this.
The program waits till it gets some data and then jump to the [getit] routine.

receiveddata = replace(receiveddata , "-" , " ")

This is the only tricky part in the whole program. This line makes sure that the "-" are transferred back into spaces. And then the vriable is placed in the textbox on the screen.

That's all folks.

Check, check !!!

Just like sending/receiving data with UDP there is a problem here. You can never be sure wether the data you are sending is actually received by the other ESP. We need a check for that.

 ' =================================  
 ' msg send/receive with msgbrach  
 ' and wget  
 ' sending routine with feedback  
 ' Luc Volders  
 ' http://lucstechblog.blogspot.nl/  
 ' =================================  
 cls  
 counter = 0  
 returntext = ""  
 msgbranch [check]  
 button "send it" , [sendit]  
 wprint "<br><br>"  
 wprint "returned text "  
 textbox returntext  
 wait 
 
 [sendit]   
 counter = counter + 1  
 tosend = "192.168.1.80/msg?data=there-you-are-"  
 tosend = tosend & "00" & str(counter)  
 a = wget(tosend)  
 wait 
 
 [check]  
 returntext = msgget("back")  
 returntext = right(returntext,3)  
 wait  


This is the sending program which has some extra routines to check wether the sended data is received well at the other side.

The program has the [sendit] button and accompanying routine for sending the data which is basically the same as in the previous send program.

The program also has a message-branch routine called [check]. What it does is basically wait till it receives some information from the other ESP8266. Whatever is received back is placed in the textbox on the screen. In this example I just take the last 3 received characters which will be the number from the variable counter but received back from the other ESP.

 ' ====================================  
 ' msg send/receive with msgbrach  
 ' and wget  
 ' receive routine with sending feedack  
 ' Luc Volders  
 ' http://lucstechblog.blogspot.nl/  
 ' ====================================  
   
 cls  
 receiveddata = ""  
   
 memclear  
 msgbranch [getit]  
 textbox receiveddata  
 wait  
   
 [getit]  
 receiveddata = msgget("data")  
 receiveddata = replace(receiveddata , "-" , " ")  
   
 'send confirmation  
 tosend = "192.168.1.75/msg?back="  
 reccounter = right(receiveddata, 3)  
 tosend = tosend & reccounter  
 a=wget(tosend)  
   
 wait  


The program for the receiving ESP is the same as the previous receive program except for some lines in the [getit] routine.

After we received the data and the "-"are replaced for spaces we are going to send back what we just received. Not everything though. We send the last 3 characters, which is the received number, back to the ESP that originally send it.
Make sure you replace the IP number with the number from the other ESP.

The sequence

When you click the button on ESP 1 the text "there-you-are-001" is send to ESP 2.

ESP 2 will filter the "-" out and sets the received text "there you are 001" in the textbox on the screen.

ESP 2 will then take the last 3 charactes of the received text "001" and sends them back to ESP 1

ESP 1 will receive the number and put it in the textbox on it's screen.

When you click the button on ESP 1 again the counter is incremented with 1 and the sequence repeats

So to actually check if the sended information is indeed received well you only have to compare the value of the counter with the value that is received back.


And this is how that looks on your screen. As you can see I opened two browser windows each pointing to the right IP adress so I have both the sending and receiving screen open and can check if everythings goes as expected.

Expansion

This is a simple example which can easily be expanded to send all kinds of sensor data like temperature, color codes, switch settings etc etc etc. Use your imagination.

Android, IFTTT and Google Home

Often we not only want to send data between ESP's but also between your Android Phone or Tablet and an ESP8266. Well like said in the intro of this story we have done that before.

Just look at these stories:

http://lucstechblog.blogspot.nl/2018/02/esp-voice-control-with-esp-basic.html

http://lucstechblog.blogspot.nl/2017/05/google-home-and-esp8266.html

In the second story we have Google Home triggering IFTTT and IFTTT will send a similar message to the receiving ESP. Adapt it to your own needs and you will have a plethora of possibillities.

Till next time
Have fun

Luc Volders