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