Pages

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

Friday, September 20, 2019

Dweet with Android

You can find an index of all my stories by clicking this text

For one reason or another I am still fascinated by Dweet. The possibillity to send and retrieve data anonymous intrigues me. Even more as that data is volatile. It disappears in 24 hours so the timespan to retrieve it is short. I see possibillities however have not yet a firm plan for using Dweet.

With Dweet you can send data from one or multiple ESP's, and you can retrieve it with one or multiple ESP's. It would be nice if you could Dweet with various apparatus and not only with ESP's. And actually you can.

In this story I am going to show you how you can send and retrieve data to Dweet with a self-build Android APP. In an upcoming story we are going to Dweet with a Raspberry Pi.

For those who do not have a clue on what I am ravelling about read the first two stories on Dweet.io: http://lucstechblog.blogspot.com/2019/05/dweet-volatile-iot-cloud-service.html and 

THIS One: http://lucstechblog.blogspot.com/2019/08/dweet-again.html

App Inventor

Building APP's for Android phones and tablets is much easier as you might think. The MIT offers a complete setup which is free to use. The only thing you have to do is to get a free account and you can start devellopping Android App's no strings attached. You can even distribute them on Google Playstore and earn some mony with your APP's.

App inventor is a block-oriented programming technique just like Scratch on the Raspberry Pi or KBIDE for the ESP32 or Ardublock for the Arduino's.

I have used App Inventor form various projects on this web-log. Just look for them in my index-page: http://lucstechblog.blogspot.nl/p/index-of-my-stories.html

So start by pointing your web-browser to the App-Inventor http://appinventor.mit.edu/explore/
page. Start an account, look at the tutorials and documentation and start coding your own APP's.

Sending a Dweet



Let's look at the Designer screen. The App has 2 Labels that indicate what you need to fill into the textboxes. Next it has 1 button that will initiate the code that will send the Dweet and there is a hidden Web attribute.

In the first Textbox called Thingname you will fill in the name of your 'Thing' and in the second Textbox called Thingvalue you just fill in the valuen that you want to be Dweeted. The value can be a figure or a text. Whatever you want. This is just for testing purposes, we are not going to send real data.

Press the button and the data is send to Dweet.io

Let's have a look at the program block.


First we initialise a global variable called "Testval"

The program waits till the button is pressed. When that happens the variable "Testval" is filled with a string that contains all the information to send a Dweet. The first line is

https://dweet.io/dweet/for/

Then we add the value found in the textbox "Thingname"

?val=

is the next part of the line and the last part is the value found in the textbox "Testval"

Suppose we filled the name Dweettest in as Thingname and 125 as Thingvalue the complete text in the variable Testval would become:

https://dweet.io/dweet/for/Dweettest?val=125

And that is exactly what Dweet expects to receive. In the green part of the block the URL is set to get "Testval" which has the value https://dweet.io/dweet/for/Dweettest?val=125
And lastly the function is executed.





This is how it looks on the Android simulator and that's also how it will look on your phone's or tablet's screen.

Does it work ?

Well the only thing you have to do is to fill in the following URL in your browser:

https://dweet.io/get/latest/dweet/for/Dweetest and the result will be shown:





Receiving a Dweet.

So sending works. Now let's see if we can retrieve Dweets.

I build a seperate APP for that. You could combine it in one APP with two screens (one for sending and one for retrieving) or even do both on the same screen.



As you can see the top of the screen has a textbox in which you can fill in the Things name. Then there is the button that will perform the action when pressed. And further there are two textboxes that will display the received Dweet and an analysed version.
Lastly the listbox will show the actual received values.
There is one hidden component and that is the web component called Dweetconnect

When the screen has been setup you can move over to programming the blocks. This is more complicated.


Clicking the image will enlarge it which will make it easier to replicate the blocks for your own purposes.

First two variables are defined called DweetJson and Initval. These will be used to distill the information from the Dweet.

Next the program waits till the button is pressed and then it gets the Dweet for whatever is filled in into the Thingname textbox.

Then the real magic begins. Initval is set to the information which is in the "content" part of the Dweet. That is where the actual values are stored.

And the last part is where DweetJson is filled with the actual content.


As you can see it actually works as expected. When the name Dweettest is filled in in the Thingname field and the button is pressed the value 125 which was send previously is retrieved.

A practical example.

Remember the Thermometer http://lucstechblog.blogspot.nl/2017/11/oh-no-not-another-wifi-thermometer.html In the previous story I altered the software so it sends information to a webpage, Thingspeak and Dweet.io

If everything works well we should be able to retrieve the last temperature.



Above is the information on the thermometers web-page


And if you fill in "Lucstemp2018" as the Thingsname the temperature in my work-room is retrieved.

How about multiple fields ??



Here you can see the information that is found with my program that ping's my wifi devices like I described in THIS STORY and that I had the information Dweeted in this story.


And this is what it looks in my simple Android APP.

That's it for now. I bet you can find some usefull purpose for this. This POC (proof of concept) demonstrated how using Dweet.io it is easy to send information from an ESP to Android or from Android to an ESP.

Beware: keep in mind that this information is publically available for anyone who is actively looking for it. So be carefull with what you Dweet and do not use obvious names for your Thing.

Till next time
Have fun

Luc Volders