Weather Station Project: Lesson 6

weather station project with raspberry pi lesson 6

Introduction

We need the JSON file from the previous lesson. This fill will be transferred to the Pi. It will be used by our Python script to send the sensor data to the Google Sheet.

I cover the process for transferring the file to the Pi using Mac and Windows. The transfer on these operating systems is done through the command line. Chromebook doesn’t a similar command for its command line. We will use our GitHub account to download the file onto the Pi.

There is a sample script that is part of the DHT package we downloaded in a previous lesson. We will modify this file to send the sensor data to the Google Sheet.

JSON file

In the previous lesson, we downloaded the JSON file for our Google Sheets API. You should have placed the file into a folder titled 'weatherFile'. This file needs to be copied to the Raspberry Pi.

We are using the terminal to transfer the file with a protocol called Secure Copy, scp.

Open a terminal window. Don't log in with SSH. In the terminal, we have access to navigate the contents of our computer. I will go over the process using MacOS first.

Mac SCP file transfer

The commands on MacOS are very similar to those on the Raspberry Pi. Open the terminal application. Type ls -l to see what folder you are in.

list the contents of the current directory

The content listed should be those of your home folder or directory. The home directory contains the Desktop folder.

the desktop folder in the home directory for the Pi account

If you don't see the Desktop folder then you are probably not in your home directory. Type cd ~ and press the Return key. The tilde character instructs the terminal to switch to the home directory.

Type cd Desktop to move into the desktop directory. The folder with the JSON file should be in your Desktop folder. Type scp -r weatherFile/ pi@192.168.1.63:. Replace the IP address with the one for your Raspberry Pi. Make sure to include the colon after the IP address. The colon instructs SCP to get the directory from the current location.

copy the JSON file to the Pi using the scp command

Provide the password for the Pi account. The folder and file within will be transferred to the home directory of your pi account.

prompt to enter the password for the Pi account

You will get a confirmation that the file was transferred.

confirmation for the transfer of the file

Windows CP file transfer

Open the Windows command prompt. Here is a hint if you don't remember. Type cmd in the search box. Type dir to get a listing of the files and directories in your home directory.

list the contents of the windows home folder with dir

There are several files and directories. We need to access the desktop directory. This is where we saved the JSON file inside a folder. Type cd Desktop and press the return key.

the desktop folder listed

Type dir to get a listing of the desktop contents. The folder with the JSON file is in the weatherFile directory. We will copy the directory and file over to the Pi.

the folder with the JSON file listed

Type scp -r weatherFile/ pi@192.168.1.63:.

the command line with the scp command

Enter the password for the Raspberry Pi.

prompt to enter the password for the pi account

The command prompt returns with information for the JSON file in the folder. This confirms the file and folder transfer is complete.

Chromebook file transfer

Chromebook don't have a way to transfer the file over to the Raspberry Pi like Mac and Windows. Well, that isn't entirely true. The latest Chromebook with access to Google Play has access to apps and extensions. Not all teachers and students have access to the latest products. I don't want to leave them out. So I have come up with an alternative.

We created a Github account in an earlier lesson. We used the account to download the sensor libraries and examples. We are going to upload our JSON file to Github from the Chromebook. We will then log into the Pi and download the file from Github.

Open the Chrome browser. Go to https://github.com and log into your account. We are going to create a repository to store our JSON file. Click the New button.

the GitHub repository listing and create new repository button

We need to provide a name for the repository. The repository name is the same as a directory or folder. This repository is tied to the account name for your Github account.

a repository as a directory in our github account

Name the repository weatherProject. Don't use spaces in the name.

weather project repository name

Provide a description for this repository. It isn't required. It is always a good idea to document your repositories. Repositories are usually a way to share your projects with the community. In this case, we don't want anyone to have access to your JSON file. Select the option to make this repository private.

describe the repository and set it to be a private repository

This is where we upload our JSON file. Click the link that reads uploading an existing file.

uploading an existing file to the repository

Click the choose your files link.

dialogue box with the choose file link

Open the weatherFile folder if you placed the file inside.

the folder with the JSON weather file

Select the weatherStaion JSON file and click the Open button.

the JSON file selected

The file is small. It doesn't take long to upload.

The JSON file in the upload cue

Click the Commit changes button.

commit the changes to save the file to the repository

The JSON file will appear in your weatherProject repository.

the JSON file in the repository project

The repository is part of the web link in the browser address bar. This information is the path to the repository. Write this path down somewhere. You will need it soon.

the path to the weather project repository

When downloading a repository we see the same path.

example of the repository path when downloading a repository

In the next step, we are going to log into the Raspberry Pi. Click the SSH extension and select your Pi connection.

connect to the pi and account using the SSH extension

Enter the password for your Pi account.

enter the account password when prompted

Type git clone followed by the link to your repository. The link begins with https://github.com. This is followed by your account name and the repository name. Press the return key.

the git clone command to fetch our weather data repository file

Enter the username for your Github account. We are prompted for the username because the repository is private. We need to log in to access the file. You are prompted to enter the account password after entering the account name.

prompt to enter your github username

The file downloads and the command prompt returns.

command prompt with information from repository

Type ls to verify the repository is in the home directory.

the weather repository listed in the home directory

The file is in a directory with the same name as the repository. We need to change the name to weatherFile. Type mv weatherProject weatherFile.

command to change the name of the directory

The mv command is the move command. This is how we rename files and directories in Linux. The directory is moved and named weatherFile. The old directory is removed.

Get a list of the directory. Confirm the directory name has changed.

confirm the directory name has changed

Weather File Directory

There are two directories in our home directory. We are going to create another directory to hold our project files. Type sudo mkdir weatherScript The mkdir command creates a directory.

create a directory to store our scripts for the project

We are going to copy the JSON file into this folder. Type sudo cp weatherFile/. We only need the file inside the directory. We don't remember the full name of the file but this is where Linux is helpful.

copy the JSON file to the new directory

The file begins with the word weather. Add the word weather to the command. Example, cp weatherFile/weather. Press the tab key on your keyboard once.

part of the JSON file name

Linux looks at files in the directory that begin with the word weather. If there is only one file then it completes the name of the file for us.

the rest of the JSON file name completed for us

Complete the command by typing weatherScript/. Example, cp weatherFile/weatherStation-33f21afa3c11 weatherScript/.

complete the command to copy the file

We don't get a confirmation. Type cd weatherScript/. Type ls -l to confirm the file is in the directory.

list the contents of the directory to confirm

Now we are going to work on the script to upload our data to the Google Sheet.

Google sensor script

The DHT library includes a simple test script. We used the script to verify the sensor is working. There is another example file in the directory with a sample script for uploading our data to Google. Type cd .. to return to the previous directory. The two dots refer to the directory above the current directory.

command to return to the previous directory

Type cd followed by the letters Ad. Press the tab key to complete the rest of the directory name.

the cd command to change to the adafruit directory

Press the Return key to move into the directory.

the completed adafruit directory

Type ls -l to view the files in the directory.

the contents of the adafruit directory listed

The sample file is in the examples directory. Type cd examples/ and then ls -l. The file we need is google-spreadsheet.py.

change directory to move into the examples directory

We are going to copy this file to our script directory. Type sudo cp google_spreadsheet.py ~/weatherScript/. This instructs Linux to return to the home directory and copy the file into the 'weatherScript' directory.

make a copy of the google spreadsheet example file

Type cd ~/weatherScript/ so we can begin working on the script. Get a list of the files in the directory. Write down the name of the JSON file. You will need to provide this name later.

Script Google authentication

The Google script includes the lines of code we need to publish our data. Type sudo nano google, and press the tab key to complete the file name. Press enter to begin editing the file.

edit the google spreadsheet file with nano

Use the down arrow to get to the import section. This is where we see all the hard work pay off. The import section imports all the libraries that are needed to communicate with the DHT sensor and Google Sheets. Some of the import names should look familiar, Adafruit_DHT, gspread, and the oauth2client.

resource and library imports in the python script

Move the cursor down to the DHT type line. Move the cursor to the end of the line. Change the sensor type from DHT22 to DHT11.

the cursor at the end of the dht sensor line

The pin connection for the DHT sensor is already at 23. We don't need to change that pin value.

updated dht sensor information and pin for sensor

Move the cursor down to the GDOCS line. We need to provide the name of the JSON file. We also need to provide the name of the Google Spreadsheet. This is where you are going to need to manually enter the complete name of the JSON file.

Move the cursor with the arrow keys to the dot of .json. Press the backspace or delete key to erase the example information. Type the name of your JSON file.

Type the Google spreadsheet name in the corresponding line.

place holders for the JSON file name and the google sheet name

The JSON file name begins with weatherStation. A hyphen and series of numbers and letters come after the name. Make sure to include the '.json' extension. This is important. The Google Sheet name is piweatherData. That is all the information we need to provide. Let's take a look at the code and learn what is going to happen.

updated place holder information

The code

The code will fetch data from the sensor at thirty-second intervals.

frequency at which the sensor is queried

The information from the JSON file and the spreadsheet name is used to access the Google Sheet. The "Try" section of the code tries to connect to the Google Sheet with the authentication information. This authentication information is in the JSON file. If there is no success we will get a message. That is where the "except" section returns the unable to connect message.

authentication information in the python script for the google spreadsheet

The code will try to get the data from the sensor. If it does not receive data it will wait for two-seconds to elapse and try again.

code to query the sensor information

If the sensor sends data to the program then the program will try to submit the information to the Google Sheet. It will append the data to the sheet by adding it to the first row. If it can't access the Google sheet it will wait and try again. If it can't access it at all, then it will report an error.

code to append the sensor information to the google spreadsheet

At the end of the loop, the script will announce that it published the sensor data to the Google Spreadsheet. It will wait for thirty-seconds and repeat the process. It will do this until we stop the program. The program is terminated by pressing the keyboard combination Control+X.

confirmation code that the information was written to the google spreadsheet

We need to save the file and exit nano before running the code. Press Control plus the letter 'o' to write the changes to the file. We are prompted to confirm the file name. Press the Return key to confirm. Press Control + x to exit the nano editor.

save the changes to the python script

To run the script type sudo python3 google_spreadsheet.py.

run the updated python script

The sensor will send data. We will get confirmation on the terminal too.

confirmation that sensor data was written to the google spreadsheet

Go to Google Sheets and open the spreadsheet. The Google Sheet is accepting data from the python script. Let the script run a few minutes.

the spreadsheet with temperature and humidity information from the sensor

The data will remain in the spreadsheet after we have stopped the Python script. Logging out of the Raspberry Pi SSH session will also terminate the script. Keeping the script going is part of a future lesson. Stay tuned.

Previous
Previous

Weather Station Project: Lesson 7

Next
Next

Weather Station Project: Lesson 5