Sunday, December 15, 2019

Sending data from ESP to Android

Click here for an index of all of my stories

I have written about many projects that use an Android Phone or Tablet to command an ESP. There are several ways to do that.
You can use a dedicated APP written in Mit's App Inventor like this story explains: http://lucstechblog.blogspot.nl/2018/02/wifi-voice-commander.html or this story https://lucstechblog.blogspot.nl/2018/01/wifi-relay.html
You can also have a web-page on the ESP that has buttons and sliders which can be accessed from any browser including your phone's or tablet's like this story (programmed in LUA) https://lucstechblog.blogspot.nl/2016/05/esp8266-controlled-ledstrip.html or this story https://lucstechblog.blogspot.nl/2016/08/rain-sensor-using-esp8266.html or the Relay server https://lucstechblog.blogspot.nl/2016/09/esp-relayserver.html and of course the obvious thermometer: http://lucstechblog.blogspot.nl/2017/11/oh-no-not-another-wifi-thermometer.html

All these projects have one thing in common. The Android phone or Tablet sends data to the ESP. It is never the other way round.

Searching the internet I did not find any project in which an ESP8266 send data to an Android device where the Phone or Tablet is used to process the data or just to display it. And for a certain project (still in devellopment) that was just what I needed.

So I had to find it out for myself. And actually it turned out to be much easier as I expected.

There are 2 sides to this story The ESP side and the Android side.

The ESP side

I wanted a quick and dirty solution so I used ESP-Basic for this. If you want an introduction to lean to work with ESP-Basic read this:  https://lucstechblog.blogspot.nl/2017/03/back-to-basic-basic-language-on-esp8266.html

The basic (pun intended) point is that we want to have the ESP react on an incoming request. I did that before when I let the ESP react on a message send to it from IFTTT in this story: https://lucstechblog.blogspot.nl/2017/05/google-home-and-esp8266.html

And actually I did the same in Arduino language (C++) in this story:
https://lucstechblog.blogspot.nl/2018/01/wifi-relay.html

Both these stories let the ESP react on an incoming request but that is not what I wanted. I wanted the ESP to send data to the device that send a request. And that involves just a light modification of the code.



msgbranch [mytestbranch]
wait

[mytestbranch]
a = a+1
myreturn = "test number " & str(a)
print myreturn
msgreturn myreturn
wait

The msbrach just waits till the ESP receives a message. It is not important what the contents of the message is. We do not even check it.

When a message is received the program jumps to the [mytestbranch] routine.
In there the variable "a" is increased and a return message is send to the device that send the request.

Piece of cake.

The Android side

The Android program is made with Mit's APP-Inventor. If you do not have any experience with writing APP's for Android please take a look at APP-Inventor. It is easy to use and works like Scratch on the Raspberry or other block-oriented programming environments.



Start with designing a screen that contains a button and a label.
The label is the small green dot just below the button.

Now switch over to the block side and here is the program:



This part has a few flaws. It works but remember this is only a proof of concept.

The first block defines what happens when the button is clicked. And that is easy. A 'get' request is send to the ESP's IP adress. Finding the ESP's IP adress can be done in several ways. In ESP basic:

print ip()

will do the trick. Another way is to open your router's web page and look there for the IP number of the ESP. I described that in several stories here on this weblog. For example in this story:
https://lucstechblog.blogspot.nl/2018/02/esp-voice-control-with-esp-basic.html

You could make this program universal by adding a text field in which you can type the IP-adress. That way this small program can be usefull for getting information from a number of ESP's. Just be creative.

The next block is activated when a request has been received and a return message has been send.
The text in the label is then set to the response.

That is all folks.

Real world

The Basic program prints on the screen of it's web page what is send to the Android device. As you can see I have pressed 8 times on the button on my Android Phone.


The Android device, which is my phone but could also be a tablet or whatever, displays the last received data from the ESP.

Expanding the code

Here are several ideas to expand the code.

- Like stated above add a text field in the Android program/screen so you can fill in the ESP's IP-number
- Send multiple data to the Android Device
- Make this a two way conversation. For example the Android Device asks for certain information like a number, temperature, sensor data or whatever and let the ESP respond with the right information.
- Let the Android Device switch on a lamp, pump, motor etc and let the ESP respond if it is done so you are sure things work as you'd expect.

I think you can come up with a lot of projects and ideas for this.

Till next time
Have fun

Luc Volders

Friday, December 6, 2019

Dweet with Arduino

For an index of all my stories click this text

Oh no not Dweet again !!!
Well yes but due to popular demand.

Introduction

For those that do not know what Dweet is I give you a short introduction.

Dweet is a free service for IOT projects found at https://dweet.io/
Dweet is like Twitter for IOT projects. You send a message (sensor value or whatever) to Dweet.io and that message is stored for 24 hour. After that it is removed. Dweet.io remembers the last 5 messages you send to it. You can retrieve the message as many times as you like within those 24 hour. Dweet is totally anonymous. You even do not have to make an account for using the service. Dweet.io is used by many people all over the world. So how do you recognise which message is yours. You give your message a name called a 'thing' and a value. As long as you know the name of the 'thing' you can retrieve it's last 5 messages.

You can have multiple 'things' that all can Dweet and from each of them the last 5 messages are remembered for 24 hour.

You can have for example 3 ESP8266's which you give the thing names ESP1, ESP2, and ESP3. Each of these 'things' can send a message to Dweet containing sensor values. And if you want to know the value for the third ESP you just get the information for 'thing' ESP3 from Dweet.

I wrote 4 previous stories about Dweet.io on this weblog. The first described how to send and retrieve messages directly from your browser. Read that story here: 

https://lucstechblog.blogspot.com/2019/05/dweet-volatile-iot-cloud-service.html

The second story described how to send and retrieve Dweets with an ESP8266 using ESP-Basic. Read that story here:

https://lucstechblog.blogspot.com/2019/08/dweet-again.html

To get Dweeting with Android read this story:
https://lucstechblog.blogspot.com/2019/09/dweet-with-android.html 

And we can Dweet with the Raspberry:
https://lucstechblog.blogspot.com/2019/09/dweet-with-raspberry.html
 

So why another story ?

Popular demand like I said.
Well actually I received some mails in which people stated that they wanted to use Dweet with an ESP8266 but were not willing to switch to ESP-Basic. They wanted a solution in C++ (Arduino). Well here you go.

The Arduino Code



#include <ESP8266WiFi.h>

const char *ssid     = "ROUTERSNAME";
const char *password = "ROUTERSPASSWORD";

String thingName="Arduintemp";
const char* dweetsite = "dweet.io";

int stopval = 1;

void setup() 
  { 
  Serial.begin(9600); 
  Serial.println(); 
  Serial.println(); 
  Serial.print("Connecting to "); 
  Serial.println(ssid); 
  WiFi.begin(ssid, password);   
  int retries = 0; 
  while ((WiFi.status() != WL_CONNECTED) && (retries < 15)) 
    { 
    retries++; 
    delay(500); 
    Serial.print("."); 
    } 
  if(retries>14)
    { 
    Serial.println(F("WiFi conection FAILED")); 
    } 
  if (WiFi.status() == WL_CONNECTED) 
    { 
    Serial.println("WiFi connected"); 
    Serial.println("IP address: "); 
    Serial.println(WiFi.localIP()); 
    Serial.println("\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/");  
    } 
  Serial.println("Connected and ready to send");  
} 

void loop() 
  {
    dweetdata();
  }

void dweetdata()
  {
  WiFiClient client; 
  const int httpPort = 80; 
  if (!client.connect(dweetsite, httpPort)) 
    { 
    Serial.println("connection failed"); 
    return; 
    } 

  String dweetstring="GET /dweet/for/";
  dweetstring=dweetstring+String(thingName)+"?";
  dweetstring=dweetstring+"temp=";
  dweetstring=dweetstring+"12";
  dweetstring=dweetstring+" HTTP/1.1\r\n"+
      "Host: " + 
      dweetsite + 
      "\r\n" + 
      "Connection: close\r\n\r\n";
    
  client.print(dweetstring); 
  delay(10);//wait a bit for stability 
  while(client.available())
    { 
    String line = client.readStringUntil('\r'); 
    Serial.print(line); 
    } 
  Serial.println();
  Serial.println("Finished");
  while (stopval ==1)  
  {
    delay(10000);
  }
}


The program is straightforward and anybody who claims that Arduino code is their favorite should be able to decipher it easily.

I will highlite some bits though.

const char *ssid     = "ROUTERSNAME";
const char *password = "ROUTERSPASSWORD";

Fill in your own credentials otherwise you will have no connection to the internet.

The first part in the setup routine should give you no problems in understanding what is happening. It is the loop where you should pay attention to.

  String dweetstring="GET /dweet/for/";
  dweetstring=dweetstring+String(thingName)+"?";
  dweetstring=dweetstring+"temp=";
  dweetstring=dweetstring+"12";
  dweetstring=dweetstring+" HTTP/1.1\r\n"+
      "Host: " +
      dweetsite +
      "\r\n" +
      "Connection: close\r\n\r\n";
   
  client.print(dweetstring);

This is the part that actually builds the message and Dweets it.

String thingName="Arduintemp";

Thingname was defined at the start of the program.

const char* dweetsite = "dweet.io";

And so is dweetsite the name of the host.

dweetstring wil be the text that is Dweeted. The variable name will be "temp" and its fixed value in this example = "12"

Feel free to replace the 'thing' name with any name you like and do the same for the variable and it's value.

  while(client.available())
    {
    String line = client.readStringUntil('\r');
    Serial.print(line);
    }

These lines clear the wifi buffer.

  while (stopval ==1) 
  {
    delay(10000);
  }

And this makes sure that the routine is run only once.

If this is going to be used to send sensor information at a regular interval you could use Deepsleep here to save power. If you do not have any idea on what Deepsleep is read this story http://lucstechblog.blogspot.com/2018/07/esp-power-reduction-withn-deepsleep.html

The Result




Like I demonstrated in the first story about Dweet I just pulled the information from Dweet.io with my browser.

Famous Last Words

Well here you have it. Arduino Code to Dweet with the ESP. Now send me a mail with a link or description of the projects you made with this.Now go back to the ESP-Basic version and look how I get the same result with just a few lines of code. You choose !!

Till next time
Have fun !!!

Luc Volders