Friday, August 9, 2019

Dweet again

For an index to all of my stories click this text

As you will recall as a frequent reader of this weblog my previous story introduced Dweet. A kind of cloud-based IOT messaging system to which you can send data using an API call. Next you can retrieve that data with another API call. All data is send publicly (unless you use a paid account) and can be retrieved by anyone if they happen to know the credentials. You can re-read that story here:
https://lucstechblog.blogspot.com/2019/05/dweet-volatile-iot-cloud-service.html

Months ago I wrote a story on how I build a thermometer. I am no fan of YAT (yet another Thermometer) however they have one big advantage: they are a source for data that can be used for testing. So look at the schematics and build yourself a version.  You can find all details here: http://lucstechblog.blogspot.nl/2017/11/oh-no-not-another-wifi-thermometer.html

Next step was to send the data from the thermometer to thingspeak. Read that story here: http://lucstechblog.blogspot.nl/2017/11/thingspeak.html

And then some time ago I wrote a story on how you could use the ping command to check if someone was at home or if all your network devices were working properly. You can re-read that story here: https://lucstechblog.blogspot.com/2019/05/ping-ping-anybody-home.html

And now we are going to send all this information to Dweet.

And again for rapid prototyping and quick results I am going to use ESPBasic as the devellopment environment. Read all primary information about installing and using ESPBasic here: http://lucstechblog.blogspot.nl/2017/03/back-to-basic-basic-language-on-esp8266.html

Sending thermometer values to Dweet

For starters I urge you to re-read the story about the thermometer and study the program thoroughly. Re-read the story here:
http://lucstechblog.blogspot.nl/2017/11/thingspeak.html

From the Dweet story we remember that the general API call is:

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

Let's adapt that for sending the thermometer value with ESPBasic.

First have a look at the most important lines from the Basic program: the lines that actually get the temperature:
test = temp(0)
test = ((int(test*10))/10)


The lines above get the temperature from a to an ESP connected temperature sensor (DS18B20) and round that to 1 decimal.

Next step is to look at the example I used in the story about Dweet. I send a fake temperature using the following line:

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

So the complete Dweet API call for the thermometer value would be:

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

Let's adapt that to a real ESPBasic command:

tosend$ = "dweet.io/dweet/for/Lucstemp2018?temp="&str(test)
helper$= wget(tosend$,80)
helper$ = ""


As you know by now Dweet wil send information back in the form of JSON code when you send information to the service. That is why I used the lines:

helper$= wget(tosend$,80)
helper$ = ""


This way the information that is send back will not be displayed on the screen but captured in a variable which is cleared immediately afterwards so the memory will not be cluttered up.

So here is the complete Thermometer program that sends the temperature to a webpage, Thingspeak and as well as to Dweet:


 tel=0  
 Timer 5000, [start]  
   
 wprint "<!DOCTYPE html>"   
 wprint "<html> <body>"  
 wprint |<body style="background-color:greenyellow;">|  
 wprint |<H1><span style="color: red;">|  
 wprint " A thermometer"  
 wprint "</H1>"  
 wprint "</span>"  
 wprint |<H2><span style="color: blue;">|  
 wprint "Temperature is now "  
 textbox test  
   
 a = "background-color:greenyellow;"  
 a = a & "display:block;width:80px;"  
 a = a & "border-style: hidden;"  
 a = a & "font-size: 22px;"  
 a = a & "font-weight: bold;"  
 a = a & "color: fuchsia ;"  
 cssid htmlid(), a  
 wprint "</span>"  
 wprint "</H2>"  
 Wait  
   
 [start]  
 test = temp(0)  
 test = ((int(test*10))/10)  
 tel=tel+1  
 if tel = 360 then  
  tel = 0  
  gosub [thing]  
 endif  
 wait  
   
 [thing]  
 ' send to Thingspeak  
 sendthing = str(test)  
 SENDTS("MP8Y1YLJUWZ4RTVR", "1", sendthing)  
   
 'send to Dweet  
 tosend$ = "dweet.io/dweet/for/Lucstemp2018?temp="&str(test)  
 helper$= wget(tosend$,80)  
 helper$ = ""  
 return  


I made some tests and then retrieved the data from Dweet.

The Dweet documentation discusses an API call to get the last send data. The call's general form is:

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

Adapting that for my Themometer that would be:

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

I just put that in my browser and here is the result:


Sending Ping information to Dweet.

One of the previous stories on this weblog featured a method to test wether someone was at home and test if your network attached devices were up and running. I urge you to re-read that story and examine the source code thoroughly as we are going to use that to send the network status and the information about who is home to Dweet. So first re-read the original story here:
https://lucstechblog.blogspot.com/2019/05/ping-ping-anybody-home.html


The web-page the ESP makes looks like the page above.
Again this is done in ESPBasic.

Let's examine the part in the code were the program tests wether I am home or not by Ping-ing my phone's IP-adress.

lucphone = ping("192.168.1.67")
if lucphone = 1 then
io(po,D2,1)
lucphone = "at home"
else
io(po,D2,0)
lucphone = "he's not in"
endif

Well that part is easy. If lucphone = 1 then I am in and else I am out.

If I am going to send this info to Dweet I need to send the variable lucphone to Dweet. However the variable is used in some text. And I just want to send the plain data being 0 or 1. Why ? Just keep following this weblog it'll be explained in an upcoming story.
To do that alter this part of the program as follows:

lucphone = ping("192.168.1.67")
lucphone2=lucphone
if lucphone = 1 then
io(po,D2,1)
lucphone = "at home"
else
io(po,D2,0)
lucphone = "he's not in"
endif

As you can see I preserved the value of lucphone in a new variable lucphone2. And that is what we can Dweet.

Maybe you recall from the Dweet story that every sensor is called a 'thing' and needs a name. As I am not using the Thermometer ESP for this, but a separate ESP I am going to give this ESP another name being Lucshome.

For just Dweeting my phone info the API call looks like this:

http://dweet.io/dweet/for/Lucshome?phone=lucphone2

Let's transorm this to proper ESPBasic code:

tosend$ = "http://dweet.io/dweet/for/Lucshome?phone="&str(lucphone2)
a$= wget(tosend$,80)

However we do want to send all the information from our Ping program. So we have to create extra variables for each test just like we did for lucphone. So add the proper lines in the program for therm2, domo2 and elsphone2.

And then we get the following complete program:


io(po,D0,0)
io(po,D1,0)
io(po,D2,0)
io(po,D4,0)

timer 1000, [testping]

wprint |<body style="background-color:powderblue;">|
wprint |<H1><span style="color: red;">|
wprint "Anybody Home ??"
wprint "</H1>"
wprint "<h3>Thermometer  "

textbox therm
cssid htmlid(), "background-color:powderblue;border-style: hidden;font-size: 16px;display:block;width:80px"

wprint "<br>"
wprint "Domoticz         "
textbox domo
cssid htmlid(), "background-color:powderblue;border-style: hidden;font-size: 16px;display:block;width:80px"

wprint "<br>"
wprint "Luc's phone      "
textbox lucphone
cssid htmlid(), "background-color:powderblue;border-style: hidden;font-size: 16px;display:block;width:80px"

wprint "<br>"
wprint "Els phone        "
textbox elsphone
cssid htmlid(), "background-color:powderblue;border-style: hidden;font-size: 16px;display:block;width:80px"

wprint "<br>"
button "QUIT", [progend]
wait

[testping]
therm = ping("192.168.1.65")
therm2=therm
if therm = 1 then
io(po,D0,1)
therm = "available"
else
io(po,D0,0)
therm = "off line"
endif


domo = ping("192.168.1.66")
domo2=domo
if domo = 1 then
io(po,D1,1)
domo = "available"
else
io(po,D1,0)
domo = "Domoticz off line"
endif

lucphone = ping("192.168.1.67")
lucphone2=lucphone
if lucphone = 1 then
io(po,D2,1)
lucphone = "at home"
else
io(po,D2,0)
lucphone = "he's not in"
endif

elsphone = ping("192.168.1.72")
elsphone2=elsphone
if elsphone = 1 then
io(po,D4,1)
elsphone = "available"
else
io(po,D4,0)
elsphone = "she's out"
endif

tosend$ = "dweet.io/dweet/for/Lucshome?therm="&str(therm2)&"&domo="&str(domo2)&"&lucphone="&str(lucphone2)&"&elsphone="&str(elsphone2)
a$= wget(tosend$,80)

wait

[progend]
io(po,D0,0)
io(po,D1,0)
io(po,D2,0)
io(po,D4,0)
end


And it works:




Caveats and pitfalls

I really enjoy playing with Dweet. However there are some serious caveats and pitfalls that you must consider.

Ever heard of number stations ?

These are radiofrequencies on which at certain times a day a list of numbers were send. It is a widely known phenomenon. Number stations were short wave transmissions alledgedly send to spies. Just Google "number stations" and a whole new world will open up to you. In the modern information age there are twitter accounts which are the equivalent of number stations. Look for example to these:
https://twitter.com/googuns_staging?lang=en
https://twitter.com/SierraGolf5
https://twitter.com/cynthiafortune
https://twitter.com/ZuluTango4
https://twitter.com/station_15a
https://twitter.com/ns111042

Well you can imagine that it is easy to use Dweet for sending secret information. Moreover since you can Dweet from a TOR browser so your IP adress can not be localised. Besides that the secret information can be disguised as genuine sensor readings. And after 24 hour the information is ereased !!! So nearly impossible to trace.

More down to earth.

In this story I Dweeted and used Lucstemp2018 and Lucshome as my 'things'.
Anybody reading this weblog knows that now. So anybody can put the following line in their browser and look at my data.

http://dweet.io/get/dweets/for/Lucstemp2018

Even worse. Anybody who has read this entry and the previous one can alter any Dweet they see at the Dweet webpage. So be carefull and responsible with this free service and do not spoil it for others.

Till next time
Have fun

Luc Volders