Please select an option to see content specific to your location and shop online.

  • We are still shipping! We are actively monitoring the COVID-19 situation. Thank you for your continued support. We are still shipping! We are actively monitoring the COVID-19 situation. Thank you for your continued support.
  • Free delivery on orders over £60 Free delivery on orders over £60

Project

OKdo IoT Cloud

Introduction to internet of things with Python and AllThingsTalk Cloud

Scroll down

In this project, you will learn how to connect your Pi 4 to AllThingsTalk Cloud and send sensor data using Python. The OKdo cloud allows you to view your sensor data from any connected device like a browser or smartphone.

1. Get your Pi ready

  • Ensure that you have the latest version of Raspbian installed on your Pi. You can learn here how to do it with a micro SD card.
  • Due to current compatibility issues when using Sense Hat on the Pi 4, after flashing your micro SD card, you’ll need to open config.txt from it with a text editor, add the line shown below at the bottom of the file and save it:
hdmi_force_hotplug=1
dtoverlay=rpi-sense
  • If you’re using an OKdo Pi Kit, you’ll have to remove the fan, as the Sense HAT used in this project connects to all the GPIO pins.
  • Connect the Sense HAT to your Pi as shown below

  • Insert the freshly flashed micro SD card
  • Connect a network cable from your host computer to your Pi and boot up the Pi
  • The Sense HAT will flash white and then transition to multi-colour mode while the Pi 4 boots, then it should turn off. The standard desktop should appear on your monitor where you can run through the setup as directed.

2. Set up AllThingsTalk Maker Cloud account

On your host computer go to allthingstalk.com and sign up for a free account. Once that is complete it will take you to the Playground page where you can set up your devices

 

• Select Devices and click +Connect A Device

 

  • Select the Raspberry Pi option from the pop-up and give the device a name. Once a device has been created, you will now be asked to create your assets. These represent the sensors connected to your device
  • Create assets for temperature, pressure and humidity sensors, choosing sensor as the kind, giving them a name (case sensitive) and a profile type of number

 

 

3. Install AllthingsTalk SDK

OKdo uses the AllThingsTalk platform who provide a Python Library to make it easier to connect devices to the service.

  • On the Pi open a Terminal window, type the following command an press Return
git clone git://github.com/allthingstalk/python-sdk.git

This will clone AllThingsTalk Python SDK from Github

  • Change into the directory created by typing the following and pressing Return
cd python-sdk/
  • Install the Python library globally with the following command
sudo pip3 install .

Everything is now setup to begin coding and sending data

4. Set up Python IoT client

  • Open Thonny on your Pi  (Found under the Programming submenu) and enter the following:
#!/usr/bin/env python

from time import sleep
from sense_hat import SenseHat

sense = SenseHat()
sense.clear()

import allthingstalk as talk

class EnvironmentMonitor(talk.Device):
    temperature = talk.NumberAsset(unit='°C')
    humidity = talk.NumberAsset(unit='%')
    pressure = talk.NumberAsset(unit='mbar')


client = talk.Client('maker:4UIYP67meScIW1VeVyLXq8lzob9vKf9IgZIDrSF')
weather = EnvironmentMonitor(client=client, id='9E0myD30og7QLibUVGQChIuS')

while True:
    weather.temperature = sense.get_temperature()
    weather.humidity = sense.get_humidity()
    weather.pressure = sense.get_pressure()
    sleep(5)

5. Add keys

The Python client has place holders for the DEVICE TOKEN and DEVICE ID that must be replaced with the ones from your OKdo IoT Cloud account.

  • Go to your Okdo IoT Cloud account select Settings / Authentication and cut and paste the Device Token and Device ID into the Python code replacing everything inside the brackets

 

6. Run the code

  • Click Run in Thonny to test that everything is connecting and sending data. On the first run, Thonny will ask you to save the file that you just created. There is no output unless there is a problem
  • Go to the AllThingsTalk Cloud console

After a few seconds, the asset fields in your console should update every few seconds with your sensor readings

 

If the web page doesn’t update, refresh your browser to see the latest data. If all is well it should update every 5 seconds.

If that doesn’t work, try reloading the file into Thonny and running it again.

Summary

You should now have your Pi connected to the AllThingsTalk Cloud sending sensor readings for temperature, pressure and humidity every few seconds.

Using the principles learnt in this project you should be able to setup many different kinds of sensors connected to the Pi and send their data to the cloud.

Your data is accessible from other devices connected to the internet so you can access it from almost anywhere.

The OKdo service also has the ability to setup rules which can alert you if sensor readings reach a threshold value using web, email or push notifications.

Like what you read? Why not show your appreciation by giving some love.

From a quick tap to smashing that love button and show how much you enjoyed this project.