Initial State

We will continue the building of the weather station with Initial State, a website that acts as data storage and data analysis for IoT projects using Arduino, Raspberry Pi, and similar. There are other services like Initial State as well, but not all of them support Sense HAT. The first step here is to create an account with Initial State; I will explain everything in the following images. Visit https://www.initialstate.com/, and sign up for your account. The following image is the INITIAL STATE website page. Click on the SIGN IN link in the top-right corner:

Click on the register link and fill out the registration form that you can see next with your Email and Password. You can also request a two-step verification using your mobile phone number. It is a good practice to add another security layer to your account:

When you fill out the form, you can click on the Stay signed in option to log in automatically. You will be redirected to the dashboard, which should look similar to the following image:

Next, click on the email address in the top-right corner and scroll to the bottom of the page, where you can find an option that allows you to create a new key. The button should look as follows:

Click on Create A New Key, and then write down the key that was generated. Now, go back to your Raspberry Pi Zero W board and open the terminal window. We now need to install the Initial State Python streamer. Before executing the command to grab the Initial State, you can verify the website with https://get.initialstate.com/python.

Then, you can simply run the following code:

curl -sSL https://get.initialstate.com/python -o - | sudo bash  

To get data from Initial State to your Raspberry Pi Zero W via SSH deny the download of the example code, and, after the installation is complete, go and edit your weather station file. You can do this with the following code:

sudo vim weather_script.py  

The previous code should be inserted below the line:

import sys 

Add the following line of code that will include the Initial State streamer package to your script:

from ISStreamer.Streamer import Streamer 

Now, we can use this package to set up your Python script and connect it to the Initial State API before the line:

sense = SenseHat() 

Add the following line of code so that you can create the streamer and initialize the connection. You have to replace YOUR_KEY_HERE with the appropriate key that you received at the bottom of the Initial State website before:

logger = Streamer(bucket_name="Sense Hat Sensor Data", access_key="YOUR_KEY_HERE") 

Next, go through all of your code and find all your instances of:

print( 

All instances of print should be replaced with the following code:

logger.log(

This change means that you print messages and information to the Initial State server and not your screen. Finally, your code should look as follows:

#!/usr/bin/python
from sense_hat import SenseHat
import time
import sys
from ISStreamer.Streamer import Streamer

sense = SenseHat()
logger = Streamer(bucket_name="Sense Hat Sensor Data", access_key="YOUR_KEY_HERE")
sense.clear()

try:
while True:
temp = sense.get_temperature()
temp = round(temp, 1)
logger.log("Temperature C",temp)

humidity = sense.get_humidity()
humidity = round(humidity, 1)
logger.log("Humidity :",humidity)

pressure = sense.get_pressure()
pressure = round(pressure, 1)
logger.log("Pressure:",pressure)

time.sleep(1)
except KeyboardInterrupt:
pass

Once you have done this, save and exit the file and run your new script file with the following code:

python weather_script.py  

The new code will directly send the data back to the Initial State server. If you get any errors, check your internet connection. Now, go back to the Initial State website and open the dashboard. You can see the new data at the top-left side. Select your data and then click on Tiles to view an awesome graph of your data.

The data is now visualized, as shown in the following image:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.104.230