Friday, May 3, 2019

Dweet: A volatile IOT cloud service

For an index to all my stories click this line

In a previous story which you can read here I wrote about Thingspeak. Thingspeak is a service where you send (sensor) data which is then displayed on a webpage. The advantage of this is that you can check your data from anywhere in the world. This way you do not have to open a port on your router to get to your data which may present some vulnerability.

Dweet is a similar service only with a twist.



The name Dweet is derived from Twitter where you tweet messages. Dweet is Data tWEETing. You can send as many data sets to dweet as you like but only the last five are stored for 24 hour. Next to that Dweet does not offer a website where you can check your data. Data has to be retrieved by an API call and is send back in JSON format. You even do not have to make an account on the Dweet service. You just Dweet your data and that's that.

Sounds complicated ??? Well it is not. Read on.

So why should you use Dweet.

First you do not have to make an account or anything. You can use the service without logging in or any other means of identifying yourself. So Dweeting is pretty anonymous. Next you can send as many Dweets as you like. A Dweet can contain up to 2000 characters. So thats an awfull lot of sensor data you can send. Besides that you can have multiple microcontrollers each sending Dweets under a different name. All those Dweets can be retrieved by one or more different microcontrollers even by multiple users.

I presume this all sounds hazy. So let's start with some examples that make everything clear for you.

Let's start !

Suppose you have a sensor reading that you want to share. For simplicity's sake let's say it is a temperature value of 25.

Dweet is meant to be used for the IOT (Internet Of Things). So to distinguish your Dweet amongst the thousands of other Dweets send every minute, think up for an unique name for your Thing. Keep it simple so you can remember it easily. For example. My name is Luc I, I want to Dweet the temperature and it is 2018. Lets call my Thing: Lucstemp2018

Now we have a name (Lucstemp2018) and an example sensorvalue (25). Let's Dweet that.

The Dweet.io home pages tells us to dweet you simply have to call an URL like:

https://dweet.io/dweet/for/my-thing-name?hello=world

In our example that would be:

https://dweet.io/dweet/for/Lucstemp2018?temp=25

Just put that in your Browser and you will get the following message back:


 
As you can see the Dweet has succeeded, the name of our Thing is Lucstemp2018 and the data what we are looking for is called temp and the value is 25.

Let's see if we can retrieve the information. For retrieving we use again an API call. The home page tells us again the general format which is:

https://dweet.io/get/latest/dweet/for/my-thing-name

That's easy to implement for our example. The Api Call should be:

https://dweet.io/get/latest/dweet/for/Lucstemp2018

Again put this in your browser and you will get the following result:



So we succeeded retrieving the data. The Thing name is the same and the data is 25.

ERROR !!!!



If something in the name is wrong or you are trying to retrieve data that was send more as 24 hours ago you will get an error message looking like the above picture.

Sending and retrieving multiple data.

Suppose you want to send multiple data. For example not only the temperature but also a notification if a door is open, a window is open and if a light switch is on or off.

Well that is easy. You just add multiple data by combining them with the & sign.

Lets try with the temperature and the indication of a light switch. We send a 1 if the switch is on and a 0 if the switch is off.
You could send the words ON and OFF however I will combine Dweet in the future with another service called Freeboard and for that purpose it is easy to represent on with 1 and off with 0.

Here is the Dweet, put it again in your browser:

https://dweet.io/dweet/for/Lucstemp2018?temp=18&switch=1



And here is the confirmation that we succeeded. As you can see two variables have been saved being temp and switch.

Lets see if we can retrieve that. The command for retrieving is the same one we used before:

https://dweet.io/get/latest/dweet/for/Lucstemp2018



There it is: we have retrieved the data.

Remember that I told you that a Dweet can contain up to 2000 characters. Therefore you can not only send sensor data but also complete texts. An example could be:

https://dweet.io/dweet/for/Mymessage?text=This is an example of what can be send with Dweet


And bingo, this worked. Without any hassle we created a new Thing which is called Mymessage and the variable is called text. This does not erease or interfere with the previous thing which still exists (as long as there have not waited 24 hours)

To retrieve the text you use the next call:

https://dweet.io/get/latest/dweet/for/Mymessage



And this is the result.



In case you are wondering...........
YES this story was written on 17 November 2017 at almost 1 o'clock in the night.

The purpose of all this.

So what's the purpose of this. What can we do with it.

Suppose you are at work and want your loved ones at home to know that you are on your way home. This can be done by having an ESP8266 at your work sending a Dweet and at home an ESP can be polling the latest Dweets so they know when you are coming home. As you might have seen in the previous examples Dweet automatically adds the date and time to a Dweet. So your loved ones even know when you have left work.

Let's look at another example.
Attach at holliday cottage a door or drawer sensor to an ESP and let it Dweet when the door or drawer is opened. An ESP at home can then check for Dweets and see if the door/drawer were opened and blink a light or give an audible signal or even send a message to your Phone.

"Hoho" is what I can here you think. The examples show how to Dweet from your browser. But your talking about Dweeting with an ESP.  So let's do that.

Dweeting from a program.

We can do this the hard way (the C++ way) or the easy way. Well I am lazy so let's take the easy way. Let's use my favorite devellopment tool: ESPBasic.

I am using ESPBasic here as it is (in my humble opinion) THE most easy developping environment for the ESP series. You can read a detailed instruction on how to install ESPBasic and take the first steps here: http://lucstechblog.blogspot.nl/2017/03/back-to-basic-basic-language-on-esp8266.html





' =================================================
' Dweet for Luc's weblog
' First send a fake temperature and switch
' =================================================

tosend$ = "dweet.io/dweet/for/Lucstemp2018?temp=18&switch=1"
print wget(tosend$,80)
delay 2000

' =========================================
' now lets see if we can retrieve the Dweet
' =========================================

request$ = wget("dweet.io/get/latest/dweet/for/Lucstemp2018",80)
print request$

You can see there isn't much to it.

The sending part.

tosend$ = "dweet.io/dweet/for/Lucstemp2018?temp=18&switch=1"

First we make a string containing the api call.

print wget(tosend$,80)

Next print wget sends it onto the internet.

The retrieving part.

request$ = wget("dweet.io/get/latest/dweet/for/Lucstemp2018",80)
print request$

Just as simple as the sending part.

Suppose you want to distill the temperature out of the answer that is returned.

reqfound$ = JSON(request$, "temp")
print reqfound$

Takes care of it all.
The JSON parser distills the value of the temp variable for you.

Here is the complete code that first sends a Dweet, then waits 2 seconds and then retrieves it after which the required data is distilled:



' =================================================
' Dweet for Luc's weblog
' First send a fake temperature and switch
' =================================================
wprint "<br>"
wprint "Sending the Dweet<br>" wprint "=================<br>" tosend$ = "dweet.io/dweet/for/Lucstemp2018?temp=18&switch=1" wprint wget(tosend$,80)
wprint "<br><br>"
delay 2000 ' ========================================= ' now lets see if we can retrieve the Dweet ' ========================================= wprint "Receiving the Dweet<br>" wprint "===================<br>" request$ = wget("dweet.io/get/latest/dweet/for/Lucstemp2018",80) wprint request$
wprint "<br><br>"
' ========================================= ' now lets see if we can retrieve the Dweet ' ========================================= wprint "The temperature is<br>" wprint "==================<br>" reqfound$ = JSON(request$, "temp")

This is how it looks in the output window:




There's more

Just go to Dweet's home page and you will find a few more API calls which you can use. There is also a paid service that makes it possible to save the Dweets for a month. You can find the homepage here: https://dweet.io/

Ok guys and girls.
Time for you to do some experiments.
Play with this and wait for the next installment.

Have fun

Luc Volders