Friday, September 27, 2019

Dweet with Raspberry

For a complete index of all stories click this text


Introduction

This is my fourth story on Dweet but for those who have no knowledge about this service I will do 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.io 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.

For example you can have 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 3 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

And then I wrote a story on sending and retrieving Dweets with your Android phone using an dedicated app written with App Inventor. Re-read that story here:
https://lucstechblog.blogspot.com/2019/09/dweet-with-android.html

In this story I am going to show you how to send and retrieve Dweets with the Raspberry Pi. So when this story is completed you will have the means to send data using Dweet to any of your devices.

Raspberry Pi-Zero






I doubt that this small wonder needs an introduction. There are 2 versions. One with Wifi and Bluetooth and one without.
The Raspberry Pi Zero is more expensive as an ESP8266. The ESP costs about 3 Euro and the Raspberry around 11 euro. You do need a SD card to operate it which adds to the costs. However the Raspberry is a full Linux computer which is much faster as an ESP and has more memory and I/O ports. It can be equipped with a screen, keyboard and mouse like a full-fledged computer. However that not needed for IOT projects.

In this story I am not going to tell you how to set up a Raspberry. Neither am I going to tell you how you can use it headless (without monitor, keyboard and mouse) you can find all that information in about a 1000 stories on the web. In doubt go to the Raspberry Pi's official website and start there: https://www.raspberrypi.org/

Dweet with the Raspberry

For Dweeting with the Raspberry Pi I am going to use Python3. It is a bit more complicated as Dweeting with an ESP programmed in ESP-Basic but still no rocket-science. If you know the principles of programming in Python and are familiar with lists and dictionaries you can Dweet. If you need to learn Python I recommend SAMS book: https://lucstechblog.blogspot.nl/2017/08/learn-python.html

There is a Python library for working with Dweet called Dweepy. I have looked at it and find working with it not really easier as working without that library. So I am not going to use Dweepy but work directly from Python.

You do need 2 libraries however being REQUESTS and JSON. Both these libraries are included in the Raspberry's version of Python so nothing has to be installed.



Fire up your Raspberry and go to the Applications menu and start Python3 idle. I'll start with demonstrating how to Dweet with direct commands and put it all in a small program which you can modify for yourself later on.

Sending a Dweet with Python Idle

First thing to do is to import the REQUESTS library for internet information exchange and the JSON library to parse Dweet's answers.

>>> import requests
>>> import json


Now we need a value which we are going to send to Dweet. Let's use a fake temperature value and use that:

>>> temperature is 17.5

Dweet works with strings and the temperature value is a floating point variable. So lets change that:

>>> tempstring = str(temperature)

We now have the value that we are going to Dweet. Now lets make up a name for our 'thing'. For examples sake I call it Pyttemp.

Dweet says on its documentatio page that to send a Dweet you need to use the following format:

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

So let us translate that to Python:

>>> s = requests.get('https://dweet.io/dweet/for/Pyttemp?temp='+tempstring)

I put the command in the variable s so the answer of Dweet will not be printed on the screen.
That's all.

Go to your browser and in the URL field fill in the command for retrieving the Dweet from your thing which is in general form:

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

and in our particular case:

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

And there is your Dweet.

So this works. Easy !!

Retrieving a Dweet with Python Idle

Now we know that the Dweet has successfully been send we can retrieve it. This invloves a bit more work.

First we need Python to collect the Dweet. From the browser we just did that with the following command:

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

in Python this is translated in:

>>> r = requests.get('https://dweet.io/get/latest/dweet/for/Pyttemp')

The prompt will return but nothing is displayed. So lets see if the dweet was indeed retrieved:

>>> print (r)
<Response [200]>

Hey !! we do get a response but not what we expected. Actually the response tells us that the Dweet is indeed retrieved succesfully (code 200). However we want to get the actual information.
That is where the JSON library jumps in.

The JSON library parses the data from the JSON information we get back from Dweet.io. This is achieved by the following command:

>>> a=r.json()

Now we can print the information

>>> print (a)
{'by': 'getting', 'the': 'dweets', 'this': 'succeeded', 'with': [{'content': {'temp': 17.5}, 'created': '2018-02-18T14:47:14.080Z', 'thing': 'Pyttemp'}]}
And there it is. Next step is to distill only the information we need (Pyttemp) from this.

so let's see what a is:

>>> type (a)
<class 'dict'>

The variable a is a dictionary. That opens possibillities. We can retrieve the individual dictionary entries. First we examine what the entries are:

>>> for f in a: print (f)

by
the
this
with


We are interested in what is in the 'with'part. The following line retrieves that from the dictionary:

>>> b=a['with']

We now have retrieved part of the response. Let's have a look:

>>> print (b)
[{'thing': 'Luctemp182', 'created': '2018-02-18T10:28:46.312Z', 'content': {'temp': 19.5}}]

>>> type (b)
<class 'list'>

So b is a list. We have to get the first part out:

>>> b[0]
{'thing': 'Luctemp182', 'created': '2018-02-18T10:28:46.312Z', 'content': {'temp': 19.5}}

b[0] is a dictionary if all is well:

>>> type (b[0])
<class 'dict'>

From this dictionary we need to retrieve the 'content' entry which by the way is a dictionary in itself:

>>> c = b[0].get('content')

so the last step is:

>>> d=c.get('temp')

There we are:

>>> print (d)
19.5

Wow, Pfoeee !!!!!




The program

So it works in idle. Let's make a program out of it.



Start the program Editor Geany and paste the following code in:






import requests
import json
# next line dweets information
# it takes a floating point and converts it to a string
temperature = 19.5
tempstring = str(temperature)
s = requests.get('https://dweet.io/dweet/for/Pyttemp?temp='+tempstring)
# next line retrieves the dweeted information
r = requests.get('https://dweet.io/get/latest/dweet/for/Pyttemp')


a=r.json()
print (a)
b= (a.get('with'))
c=b[0]
d=c.get('content')
e=d.get('temp')
print() 
print()
print (e)


Run the program and:


We've done it.

Concluding

I do admit that this is a lot more complicated as Dweeting with an ESP8266 programmed with ESP-Basic. However this opens a lot of new possibillities. The Raspberry is cheap and has loads of IO ports. It's multitasking capabilities can have it Dweeting in the background while it is doing other tasks as well.

Till next time
Have fun !!!!

Luc Volders