For an index to all my stories click this text
After my story about Dweet.cc and Freeboard I got some mails from readers of that wanted to know how to send data to Dweet.cc and retrieve data from Dweet.cc with MicroPython.
Well here we go.
Sending a dweet to Dweet.cc
The API call I used to send a temperature value to Dweet.cc with the thingname lucstechblog is as follows:
https://dweet.cc/dweet/for/lucstechblog?temperature=25
To use that API call in a MicroPython program you can use the following code:
import network import urequests # Router credentials ssid = "YOUR_ROUTER_NAME" pw = "PASSWORD" print("Connecting to wifi...") # wifi connection wifi = network.WLAN(network.STA_IF) # station mode wifi.active(True) wifi.connect(ssid, pw) # wait for connection while not wifi.isconnected(): pass # wifi connected print("Connected. IP: ",str(wifi.ifconfig()[0], "\n")) # Replace 'lucstechblog' with your actual thing name # Replace valuename with the name of the value you are sending # Replace value with the actual value thingname = 'lucstechblog' valuename = 'temperature' value = 25 value = str(value) url = 'https://dweet.cc/dweet/for/' url = url + thingname url = url + "?" url = url + valuename url = url + "=" url = url + value response = urequests.get(url) print(response.text) response.close()
That is it.
Put this code in Thonny and press the "run" button. And you will get the next response in the shell:
Of course your IP address will be different.
The response clearly states that the command had succeeded.
I will break the most important part of the code down here for you.
thingname = 'lucstechblog' valuename = 'temperature' value = 25 value = str(value)
These are the variables that need to be send to Dweet.cc
I used lucstechblog for the thing name but please use your own thing name. You can choose any name you want but best practice is to make it relevant.
Please be aware that we are sending a public dweet. So anybody in the world that sends a dweet with the same thing name will alter your value.
Then there is a valuename. I used temperature but use something significant like "kitchen-temperature" or "mancave-temperature". For a single dewwt this looks irrelevant but wait till you build a complete home automation system with this.
And then there is the value itself.
If the value is a number (integer or floating point) like in this example you need to convert it into a string like this example shows.
url = 'https://dweet.cc/dweet/for/' url = url + thingname url = url + "?" url = url + valuename url = url + "=" url = url + value
This is the code that combines all variables into the api call. Just make sure to incorporate the "?" after the thingname and the "=" after the valuename.
I split the code up like this so you can easily put this in your own program and replace the variable names into your own names.
Retrieving a value from Dweet.cc
The code for retrieving a value from the Dweet.cc server is:
https://dweet.cc/get/latest/dweet/for/my-thing-name
In this example it would be:
https://dweet.cc/get/latest/dweet/for/lucstechblog
Let's translate this in a MicroPython program.
import network import urequests import ujson # Router credentials ssid = "YOUR_ROUTER_NAME" pw = "PASSWORD" print("Connecting to wifi...") # wifi connection wifi = network.WLAN(network.STA_IF) # station mode wifi.active(True) wifi.connect(ssid, pw) # wait for connection while not wifi.isconnected(): pass # wifi connected print("Connected. IP: ",str(wifi.ifconfig()[0], "\n")) # Replace 'my-thing-name' with your actual thing name thing_name = 'lucstechblog' url = 'https://dweet.cc/get/latest/dweet/for/' url = url + thing_name response = urequests.get(url) print(response.text) response.close()
And here is the important part for this program:
url = 'https://dweet.cc/get/latest/dweet/for/' url = url + thing_name
I splitted the api call in two parts. The first part is the general part and the second part adds your thingname. That is the variable you just created.
And this would gain the following response:
So this proves that it worked.
Decoding the JSON response.
So now we know that this works. However the response is JSON code. And unfortunately that is not direct usable in our program.
Suppose you want to display the temperature on an oled screen, or build a physical thermometer pointer with a servo, or use a bunch of neopixels to display the value like an old-school thermometer.
The JSON code would certainly not be suitable as a value we can work with. We have to decode it.
Here is the complete program that rertrieves the dweet and extracts the value.
import time import network import urequests import ujson # Router credentials ssid = "Ziggo2903181" pw = "ptzbB2ohKbgs7agp" print("Connecting to wifi...") # wifi connection wifi = network.WLAN(network.STA_IF) # station mode wifi.active(True) wifi.connect(ssid, pw) # wait for connection while not wifi.isconnected(): pass # wifi connected print("Connected. IP: ",str(wifi.ifconfig()[0], "\n")) # Replace 'my-thing-name' with your actual thing name thing_name = 'lucstechblog' #url = 'http://192.168.178.62:8000/get/latest/dweet/for/Kit-pow' #url = 'http://dweet.me:3333/get/latest/yoink/from/Man-temp' url = 'https://dweet.cc/get/latest/dweet/for/' url = url + thing_name response = urequests.get(url) print(response.text) print ("============================================") # Parse the JSON string data = ujson.loads(response.text) # Extract the temperature value tempvalue = data["with"][0]["content"]["temperature"] # Display the result print("Temperature is:", tempvalue)
The important parts are these:
data = ujson.loads(response.text)
We create a variable that decodes the JSON response in its individual parts
tempvalue = data["with"][0]["content"]["temperature"]
There we extract the temperature value and put it into the tempvalue variable.
print("Temperature is:", tempvalue)
And this is where we print the actual value in the shell.
If your JSON code looks different you can use a JSON decoder of which there are several on the internet.
Now the value is stored into the tempvalue variable we can use it for our project.
Decoding JSON structures is a bit puzzling.
Click here to buy my book on the Raspberry Pi Pico W
But then you could buy my book on MicroPython with the Raspberry Pi Pico W that explains it in detail and shows how to get help from a JSON decoder.
Till next time
Have fun
Luc Volders