Temperature Monitored Greenhouse project: Difference between revisions
No edit summary |
|||
| (75 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
[[File:Mini-greenhouse Kara Lascaris 2 Aug 2023.png | 900px]] | |||
= | = Video lessons = | ||
* [https:// | * [https://www.dropbox.com/scl/fi/70oer7y0iqdzxcvaofv93/1.-temp-greenhouse.mp4?rlkey=f1yeouq33txy84awohdxrard6&dl=0 Introduction to temp-greenhouse lesson] | ||
* [https://www.dropbox.com/scl/fi/s71w35k7jsk0b4b07elsx/2.-Arduino-IDE-Installation.mp4?rlkey=v6848flv06dlns5fqkn9vi8qe&dl=0 Arduino IDE Installation] | |||
* [https://www.dropbox.com/scl/fi/yvdolcg8m9xl2um1x8dnl/3.-Arduino-library-installation.mp4?rlkey=n2ye2d92zfa122xbq2iwxx7zz&dl=0 Arduino library installation] | |||
* [https://www.dropbox.com/scl/fi/e3dxcryz0aaxmekrej7yz/4.-Onewire-temperature-sensor.mp4?rlkey=ogpja0csii4xe04dqiafu6lnb&dl=0 Onewire Temperature sensor coding on Arduino] | |||
* [https://www.dropbox.com/scl/fi/u0mnv8vanvu7sod5dxz8p/5.-Reading-data-using-python.mp4?rlkey=oj22oouwyhce44yrcgm8ykxdp&dl=0 Reading data using Python on the Raspberry Pi] | |||
* [https://www.dropbox.com/scl/fi/9uzo6l1ak82j7axe99etn/6.-Add-date_time-stamp.mp4?rlkey=vxk0g8orxv5ob6sqnkxqdgh11&dl=0 Adding date_time stamp using Python on the Raspberry Pi] | |||
= SMC Class Videos 24 July 2023 = | |||
1. Arduino IDE Installation | |||
[https://www.dropbox.com/scl/fi/bjxo1z3s84d65lmvwhixa/1.-SMC-Arduino-IDE-Installation.mp4?rlkey=nbi0d3xgpax34x1tkksslfjqa&dl=0 Arduino IDE Installation] | |||
2. Arduino library installation | |||
[https://www.dropbox.com/scl/fi/dhrbse8bd9erpnqrzh2tb/2.-SMC-Arduino-library-installation.mp4?rlkey=cdfgx44iq66zb1w5n3nq4vk2j&dl=0 Arduino library installation] | |||
3. Onewire Temperature Circuit using Arduino | |||
[https://www.dropbox.com/scl/fi/g2rzkr56b0mzqk8emj41o/3.-Onewire-Temperature-Circuit.mp4?rlkey=tdj4ds80tvdjn38n0qdmdqqxi&dl=0 Onewire Temperature Circuit using Arduino] | |||
4. Arduino onewire temperature sensor code | |||
[https://www.dropbox.com/scl/fi/91jiklpaf1uujr296ug4s/4.-Arduino-Onewire-Temperature-Code.mp4?rlkey=sr0sn4d87jgsanbekuhwmvrqa&dl=0 Arduino onewire temperature sensor code] | |||
5. Using Python to receive temperature data from the Arduino | |||
[https://www.dropbox.com/scl/fi/l7y4acnezop1xp5siqm09/5.-Using-Python-to-receive-data-from-Arduino.mp4?rlkey=qsk7ic7vr1tqah7v6dbudcj95&dl=0 Using Python to receive temperature data from the Arduino] | |||
6. Adding a Datetime stamp using Python | |||
[https://www.dropbox.com/scl/fi/kut2ebxjywprfbmeulccy/6.-Adding-Datetime-Stamp-using-Python.mp4?rlkey=vos7f7cdw15jxs210d0hs9pua&dl=0 Adding a Datetime stamp using Python] | |||
7. Saving data to a File using Python | |||
[https://www.dropbox.com/scl/fi/e7lzuq71cw96ms03fzjh5/7.-Saving-data-to-a-file-using-Python.mp4?rlkey=57iv9ag7j6gpvezsfdtr21yj0&dl=0 Saving data to a File using Python] | |||
= SMC Class Videos 31 July 2023 = | |||
1. Using Python to receive data from the Arduino | |||
[https://www.dropbox.com/scl/fi/tkou5i7dde3sxv4po6wwz/1.-Using-Python-to-receive-data-from-Arduino.mp4?rlkey=96rm5ivrcs0s248smgdcknk68&dl=0 Using Python to receive data from the Arduino] | |||
2. Adding a Datetime stamp using Python | |||
[https://www.dropbox.com/scl/fi/v9bwysz95m83j7ejyu1pq/2.-Adding-Datetime-Stamp-using-Python.mp4?rlkey=y6lm378bwelykwbpqzn1nqx9f&dl=0 Adding a Datetime stamp using Python] | |||
3. Saving data to a File using Python | |||
[https://www.dropbox.com/scl/fi/vp89ucdrs1azt1522b5ow/3.-Saving-data-to-a-file-using-Python.mp4?rlkey=3e4hy29dyun26k6uoj150e4yi&dl=0 Saving data to a File using Python] | |||
4. Dweeting data using Python | |||
[https://www.dropbox.com/scl/fi/awfkp86hzxpgakwvy5kpt/4.-Dweeting-data-using-Python.mp4?rlkey=ub2acx250tx50aokanbpquh20&dl=0 Dweeting data using Python] | |||
= Materials = | = Materials = | ||
| Line 12: | Line 53: | ||
* Arduino Uno + solderless breadboard | * Arduino Uno + solderless breadboard | ||
* Raspberry Pi | * Raspberry Pi | ||
= Install Arduino IDE Software = | |||
== Background == | |||
* The default Arduino IDE (Integrated Development Environment) installed on the Raspberry Pi is an older version. | |||
* This lesson explains how to install the latest version of the Arduino IDE. | |||
* The current version of Arduino IDE for the Raspberry Pi is '''1.8.19''' | |||
[[File:Screen Shot 2023-03-02 at 10.50.30 am.png | 900px]] | |||
== Online Resources == | |||
* https://www.raspberrypi-spy.co.uk/2020/12/install-arduino-ide-on-raspberry-pi/ | |||
* https://www.arduino.cc/en/software | |||
* https://roboticsbackend.com/raspberry-pi-arduino-serial-communication/#Arduino_code | |||
* https://youtu.be/jU_b8WBTUew | |||
[[File:Screen Shot 2023-02-26 at 4.30.43 pm.png | 900px]] | |||
== Installing the most current Arduino IDE == | |||
* We need to download the latest version of the Arduino IDE software compatible with the Raspberry Pi. | |||
* You can go directly the Arduino Download site and download the Linux ARM 32 bit version. | |||
* Alternatively, you can use the wget instruction. | |||
== Download the Arduino IDE Software == | |||
* Create a new folder for the Arduino IDE software. | |||
* In the Terminal enter the following commands. | |||
** '''mkdir Applications''' - make a new directory named Applications. | |||
** '''cd Applications''' - change directory. Enter into the directory Applications. | |||
[[File:Screen Shot 2023-02-26 at 4.36.35 pm.png | 900px]] | |||
* When in the directory the wget instruction can be entered to download the software. | |||
* The download takes approx 3 minutes. | |||
* Enter the following command in the Terminal | |||
<syntaxhighlight lang="linux"> | |||
wget https://downloads.arduino.cc/arduino-1.8.19-linuxarm.tar.xz | |||
</syntaxhighlight> | |||
[[File:Screen Shot 2023-02-26 at 4.52.37 pm.png | 900px]] | |||
* Alternatively you can also go to the Arduino website and download the file directly to the Raspberry Pi. | |||
[[File:Screen Shot 2023-02-26 at 4.30.43 pm.png | 900px]] | |||
== Unpack the Arduino IDE application == | |||
* Use the File Manager to copy the arduino-1.8.19-linuxarm.tar.xz from the '''Downloads''' directory to the '''Applications''' directory. | |||
[[File:Screenshot 2023-07-18 at 5.08.26 am.png | 900px]] | |||
* Go back to the '''Terminal'''. | |||
* To navigate to the Applications directory enter the commands '''cd ~''' to go to the home directory, then '''cd Applications''' to enter the Applications directory. | |||
[[File:Screenshot 2023-07-18 at 5.11.09 am.png | 900px]] | |||
* Enter the following command to unpack the package '''tar -xvf arduino-1.8.19-linuxarm.tar.xz''' | |||
** '''x''' - extracts the files | |||
** '''v''' - verbose mode so all extraction operations will be shown in the Terminal | |||
** '''f''' - specifies the name of the archive file we are extracting files from, in this case '''arduino-1.8.19-linuxarm.tar.xz''' | |||
* Refer to this tutorial [https://youtu.be/yws7HOndElA Linux tar Command Tutorial] | |||
<syntaxhighlight lang="linux"> | |||
tar -xvf arduino-1.8.19-linuxarm.tar.xz | |||
</syntaxhighlight> | |||
[[File:Screen Shot 2023-02-26 at 5.16.07 pm.png | 900px]] | |||
* Once the package in unpacked enter the newly created folder named '''arduino-1.8.19'''. | |||
* You can use the '''Tab''' key to complete the file name ('''cd arduino + Tab''') | |||
[[File:Screen Shot 2023-02-26 at 5.17.53 pm.png | 900px]] | |||
* The compressed Arduino file will take a few seconds for all files to be extracted. | |||
[[File:Screenshot 2023-07-18 at 5.13.44 am.png | 900px]] | |||
* Once all the files are extracted enter the command '''ls''' or '''ls -l''' to list all files in the current directory. | |||
* '''arduino-1.8.19''' is a directory and is coloured blue. | |||
[[File:Screenshot 2023-07-18 at 5.17.09 am.png | 900px]] | |||
* Enter the '''arduino-1.8.19''' by entering the command '''cd arduino-1.8.19''' | |||
* List all the contents of the directory with the command '''ls -l''' | |||
[[File:Screenshot 2023-07-18 at 5.21.46 am.png | 900px]] | |||
== Installing the Arduino IDE Application == | |||
* Once in the directory there is an '''install.sh''' scripting file that will complete the installation. | |||
* Start the install with the command '''sudo ./install.sh''' | |||
[[File:Screen Shot 2023-02-26 at 5.33.40 pm.png | 900px]] | |||
== Running the Arduino IDE Application == | |||
* The installation process adds an entry into the Raspberry Pi OS drop down menu. | |||
* You can find the Arduino IDE entry under the '''Programming''' heading. | |||
[[File:Screen Shot 2023-02-26 at 5.34.45 pm.png | 900px]] | |||
= Arduino Uno - One Wire Temperature sensor = | = Arduino Uno - One Wire Temperature sensor = | ||
== Frizing - Circuit diagram == | == Frizing - Circuit diagram == | ||
* In the Arduino code below the data line is connected to pin 4. | * In the Arduino code below the data line is connected to pin 4. | ||
[[File:Screenshot 2023- | [[File:Screenshot 2023-07-18 at 5.38.51 am.png | 900px]] | ||
== One Wire Library installation == | == One Wire Library installation == | ||
* Select '''Sketch > Include Library > Manage Libraries''' | * Select '''Sketch > Include Library > Manage Libraries''' | ||
* Wait for libraries to update. This may take 1-2 minutes. | * Wait for libraries to update. This may take 1-2 minutes. Be patient. | ||
[[File:Screenshot 2023-06-24 at 8.24.52 pm.png | 900px]] | [[File:Screenshot 2023-06-24 at 8.24.52 pm.png | 900px]] | ||
* The progress bar at the bottom of the Manage Libraries window will disappear when the libraries are fully updated. | |||
[[File:Screenshot 2023-07-18 at 5.43.27 am.png | 900px]] | |||
* In the search bar enter '''onewire''' and press Enter. | |||
* The search will take 10 seconds to complete. Be patient. | |||
* Install the '''One Wire''' library. | * Install the '''One Wire''' library. | ||
* Use the One wire library with the author '''Paul Stoffregen''' | * Use the One wire library with the author '''Paul Stoffregen''' | ||
* Click on the '''Install''' button to the right to install the library. | |||
[[File:Screenshot 2023-06-24 at 8.32.20 pm.png | 900px]] | [[File:Screenshot 2023-06-24 at 8.32.20 pm.png | 900px]] | ||
| Line 48: | Line 196: | ||
== Arduino Uno Code == | == Arduino Uno Code == | ||
* Copy the Arduino code below and paste it in the Arduino IDE. | |||
* Create a new directory on the Raspberry Pi using the '''File Manager''' in the home directory and name it '''temp-greenhouse''' | |||
* Save the Arduino file as '''one-wire-temperature''' | |||
[[File:Screenshot 2023-07-18 at 6.02.11 am.png | 900px]] | |||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
| Line 87: | Line 240: | ||
[[File:Screenshot 2023-06-24 at 9.23.58 pm.png | 900px]] | [[File:Screenshot 2023-06-24 at 9.23.58 pm.png | 900px]] | ||
= Delays in Arduino = | = Delays in Arduino = | ||
| Line 238: | Line 250: | ||
const long oneMinute = oneSecond * 60; | const long oneMinute = oneSecond * 60; | ||
const long fiveMinute = oneMinute * 5; | const long fiveMinute = oneMinute * 5; | ||
void setup() | void setup() | ||
| Line 256: | Line 266: | ||
Serial.println("delay for 5 minutes"); | Serial.println("delay for 5 minutes"); | ||
delay(fiveMinute); | delay(fiveMinute); | ||
Serial.println("Ready to start over"); | Serial.println("Ready to start over"); | ||
} | } | ||
| Line 265: | Line 271: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= | = Sending Temperature data to Raspberry Pi = | ||
* Data can be sent from the Arduino to the Raspberry pi using the Serial cable. | |||
* Open the Serial Monitor to view the data stream. | |||
* In this example the data is being updated every 10 seconds using '''delay(10000)'''. | |||
* When finalising the code on the Arduino make sure that the delay is set to five minutes '''delay(300000)''' | |||
== Arduino code | == Arduino code to send data to Raspberry Pi - image == | ||
[[File:Screenshot 2023- | [[File:Screenshot 2023-07-18 at 5.57.06 am.png | 900px]] | ||
== Arduino code to send data to Raspberry Pi - code == | |||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
| Line 279: | Line 290: | ||
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices | OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices | ||
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature sensor | DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature sensor | ||
float temperature; | |||
void setup() { | void setup() { | ||
| Line 331: | Line 300: | ||
void loop() { | void loop() { | ||
sensors.requestTemperatures(); | sensors.requestTemperatures(); | ||
Serial. | Serial.println(sensors.getTempCByIndex(0)); // Send data to Raspberry Pi | ||
delay(10000); // 300000 = Five minute delay, 10000 = 10 second delay | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= | = Python code on the Raspberry Pi to read Arduino data = | ||
* Open '''Thonny''' from the main Programming menu on the Raspberry Pi' | |||
* Copy and paste the code below. | |||
* Save the file in a new directory named '''temp-greenhouse''' | |||
* Save the file as '''python-data-collect1.py''' | |||
* | |||
* | |||
* | |||
* | |||
[[File:Screenshot 2023-07-18 at 6.07.31 am.png | 900px]] | |||
== Close the Arduino Serial Monitor before Running Python == | |||
* Before running the Python program in Thonny, make sure that the '''Arduino Serial Monitor''' is closed. | |||
* If the Arduino Serial monitor is operating - the python program will show an error. | |||
== Check the Arduino Serial Port Connection == | |||
* Check the Arduino IDE program to find out what serial port the Arduino is connected to. | |||
* Port details are available in many locations as highlighted below. | |||
* In this example the port is USB number 0 '''/dev/ttyUSB0''' | |||
[[File:Screenshot 2023-07-18 at 6.15.10 am.png | 900px]] | |||
* This same port number needs to be in the Python program in Thonny. | |||
[[File:Screenshot 2023-07-18 at 6.18.14 am.png | 900px]] | |||
== Python - code to constantly collect data from Arduino - code == | == Python - code to constantly collect data from Arduino - code == | ||
* The line '''print(line)''' prints all data received from the Arduino serial. | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
| Line 422: | Line 353: | ||
== Python - code to constantly collect data from Arduino - image == | == Python - code to constantly collect data from Arduino - image == | ||
[[File:Screenshot 2023- | [[File:Screenshot 2023-07-18 at 6.11.53 am.png | 900px]] | ||
= Stopping and Running code in Thonny = | |||
* Always click on the Stop button before running any new Python code. | |||
[[File:Screenshot 2023-07-18 at 6.58.08 am.png | 900px]] | |||
= Python - Adding a Datestamp - code = | |||
* It is important to note the exact date and time when receiving data. | |||
* In computer language we call this a '''datestamp''' | |||
* To record the datestamp we need to add the following code. | |||
** '''import datetime''' - python library to support datetime creation. | |||
** '''now = datetime.datetime.now() - code to collect the current datetime on the computer. The current datetime is stored in the object variable '''now''' | |||
** '''date_stamp = now.strftime("%Y-%m-%d %H:%M:%S")''' - reformat the object '''now''' to a human readable date format and store the result in the variable date_stamp. | |||
** '''print(f"Time is {date_stamp}')''' - print statement to combine text and variables. | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
| Line 443: | Line 386: | ||
now = datetime.datetime.now() | now = datetime.datetime.now() | ||
date_stamp = now.strftime("%Y-%m-%d %H:%M:%S") | date_stamp = now.strftime("%Y-%m-%d %H:%M:%S") | ||
print(f' | print(f'The current time is {date_stamp}') | ||
data = str(line) + "," + date_stamp + "\n" | data = str(line) + "," + date_stamp + "\n" | ||
| Line 452: | Line 395: | ||
== Python - Collecting data from Arduino and adding a datestamp - image == | == Python - Collecting data from Arduino and adding a datestamp - image == | ||
[[File:Screenshot 2023- | [[File:Screenshot 2023-07-18 at 7.01.01 am.png | 900px]] | ||
= Python - saving data to a file = | |||
* Now that we have the data from the Arduino and the current date and time its time to save the data to a file. | |||
* Saving the data allows us to import the data into a spreadsheet for analysis or graph the data to observe trends. | |||
* Copy and paste the code below in Thonny. | |||
* You can overwrite the existing code. | |||
* Save data we need to: | |||
** '''data = str(line) + "," + date_stamp + "\n"''' - add the Arduino data to the date_stamp data and also add a '''new line character - \n''' to the end of the code. | |||
** '''f = open('/home/pi/temp-greenhouse/greenhouse_data.txt','a')''' open a new file named '''greenhouse_data.txt''' | |||
** '''f.write(data)''' - write the data to the file. | |||
** '''f.close()''' - close the file. | |||
[[File:Screenshot 2023- | * After several data writes are completed open the file using the '''File Manager''' and check that file has stored the data. | ||
[[File:Screenshot 2023-07-18 at 7.04.00 am.png | 900px]] | |||
* Example of the data written to the text file '''greenhouse_data.txt''' | |||
[[File:Screenshot 2023-07-18 at 7.08.28 am.png | 900px]] | |||
== Python - saving data to a file - code == | == Python - saving data to a file - code == | ||
| Line 484: | Line 442: | ||
print(f'The data is = {data}') | print(f'The data is = {data}') | ||
f = open('/home/pi/ | f = open('/home/pi/temp-greenhouse/greenhouse_data.txt','a') | ||
f.write(data) | f.write(data) | ||
f.close() | f.close() | ||
print("The data has been written to the file greenhouse_data.txt") | |||
</syntaxhighlight> | </syntaxhighlight> | ||
= Python - Graphing data with Plotly = | |||
* Data stored in a text file can be plotted using a special python library called '''plotly''' | |||
* Before we can use plotly we need to install the python libraries '''plotly''' for graphing data and '''pandas''' to read and table data from the text file. | |||
== Installation of the plotly python package == | |||
* From the Raspberry Pi top bar menu click on the '''Terminal''' icon. | |||
* Open the Terminal and enter the command '''pip3 install plotly''' | |||
* '''pip''' is short for Pip Installs Python. | |||
* pip3 installs packages for python3 | |||
* The version of plotly that was installed is '''plotly-5.13.1''' | |||
* Other python packages may also be installed to support plotly. | |||
[[File:Screenshot 2023-07-18 at 7.14.41 am.png | 900px]] | |||
* To test that plotly has been installed correctly open the Thonny and enter - '''import plotly''' into the Python Shell window (the bottom window in Thonny). | |||
* '''Important''' - you need to Close and Re-open Thonny before the '''import plotly''' will be recognised. | |||
[[File:Screenshot 2023-07-18 at 7.16.38 am.png | 900px]] | |||
== Installing pandas python package to read data from text files == | |||
* To read data from our data.txt file we need to install another python package named '''pandas'''. | |||
* To install the pandas package open the Terminal and enter the following commands. | |||
** '''pip3 install pandas''' – will take up to 5-10 minutes to install. On an old laptop (2017) it took 30 minutes for pandas to install. | |||
[[File:Screen Shot 2023-03-23 at 4.06.33 am.png | 900px]] | |||
* Once pandas has been installed check to see if pandas can be imported in Thonny. | |||
* Use the command '''import pandas''' | |||
* If there is no error pandas has been installed correctly. | |||
=== Error with installation of pandas === | |||
* Sometime the '''import pandas''' statement produces an error because it is missing some libraries. | |||
[[File:Screenshot 2023-07-18 at 7.23.09 am.png | 900px]] | |||
* If this is the case install the following library. | |||
* Open the Terminal. | |||
* Enter the command '''sudo apt-get install libatlas-base-dev''' – extra missing software package to support pandas | |||
* Answer '''y''' yes to any questions about the installation. | |||
[[File:Screenshot 2023-07-18 at 7.25.17 am.png | 900px]] | |||
* To test that pandas has been installed correctly open the Python Shell and enter - '''import pandas''' | |||
* Remember to restart Thonny before testing. | |||
* If the pandas python package has installed correctly you should see no errors. | |||
[[File:Screenshot 2023-07-18 at 7.28.44 am.png | 900px]] | |||
== Modification of temperature_data.txt file == | |||
* Before pandas and plotly can use the data in the text file we need to add headers so that pandas knows the order of time and temperature data. | |||
* Open the '''File Manager''' | |||
* Double click on the file '''greenhouse_data.txt''' to open the file. | |||
* In the very top line add the following text '''temperature,date_time''' | |||
* This line will serve as a '''header''' so that plotly knows in which column the temperature and date_time data is located. | |||
* Save and close the file. | |||
[[File:Screenshot 2023- | [[File:Screenshot 2023-07-18 at 7.30.56 am.png | 900px]] | ||
== | == Python - Graphing data using Plotly - code == | ||
* Copy the following code to start graphing temperature data. | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
| Line 577: | Line 532: | ||
data = str(line) + "," + date_stamp + "\n" | data = str(line) + "," + date_stamp + "\n" | ||
print(f'The data is = {data}') | print(f'The data is = {data}') | ||
f = open('/home/pi/ | f = open('/home/pi/temp-greenhouse/greenhouse_data.txt','a') | ||
f.write(data) | f.write(data) | ||
f.close() | f.close() | ||
| Line 583: | Line 538: | ||
# import and plot data text file | # import and plot data text file | ||
df = pandas.read_csv('/home/pi/ | df = pandas.read_csv('/home/pi/temp-greenhouse/greenhouse_data.txt') | ||
fig = go.Figure(data = go.Scatter(mode='markers', | fig = go.Figure(data = go.Scatter(mode='markers', | ||
| Line 627: | Line 558: | ||
plotly.offline.plot(fig, | plotly.offline.plot(fig, | ||
filename="/home/pi/ | filename="/home/pi/temp-greenhouse/temperature_plot.html", | ||
auto_open=False) | auto_open=False) | ||
| Line 634: | Line 565: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[File:Screenshot 2023- | [[File:Screenshot 2023-07-18 at 7.36.26 am.png | 900px]] | ||
* To see the plotted temperature data open the File Manager. | |||
* Navigate to the directory '''temp-greenhouse''' | |||
* And click on the file '''temperature_plot.html''' | |||
[[File:Screenshot 2023- | [[File:Screenshot 2023-07-18 at 7.37.59 am.png | 900px]] | ||
* This will open the web browser on the Raspberry Pi and display a plot of the temperature data. | |||
[[File:Screenshot 2023-07-18 at 7.41.03 am.png | 900px]] | |||
= Python - Dweeting Data = | |||
* Data can be sent to the internet via a '''Dweet''' | |||
* Before sending dweets make sure you change the delay in the Arduino code to delay(300000) so that updates are only send once every five minutes. | |||
* Replace '''My_Dweet_address''' with your own made up address | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
try: | try: | ||
temperature = | print("Preparing dweet") | ||
dweet_dict = {} | |||
dweet_dict.update({"temperature": str(line)}) | |||
dweet_dict.update({"Time": str(date_stamp)}) | |||
url = "https://dweet.io/dweet/for/<My_Dweet_address>?" | |||
x = requests.post(url, json=dweet_dict) | |||
print(x.text) | |||
except: | except: | ||
print("Dweet failed") | |||
# check dweet with - https://dweet.io/get/latest/dweet/for/<My_Dweet_address> | |||
</syntaxhighlight> | </syntaxhighlight> | ||
= | == Dweet python code - full == | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
| Line 796: | Line 618: | ||
time.sleep(0.1) | time.sleep(0.1) | ||
if ser.in_waiting > 0: | if ser.in_waiting > 0: | ||
print("Data in") | |||
line = ser.readline().decode('utf-8').rstrip() | line = ser.readline().decode('utf-8').rstrip() | ||
print( | print(line) | ||
now = datetime.datetime.now() | now = datetime.datetime.now() | ||
date_stamp = now.strftime("%Y-%m-%d %H:%M:%S") | date_stamp = now.strftime("%Y-%m-%d %H:%M:%S") | ||
| Line 813: | Line 626: | ||
print(f'The data is = {data}') | print(f'The data is = {data}') | ||
f = open('/home/pi/temp-greenhouse/greenhouse_data.txt','a') | |||
f = open('/home/pi/ | |||
f.write(data) | f.write(data) | ||
f.close() | f.close() | ||
| Line 821: | Line 632: | ||
# import and plot data text file | # import and plot data text file | ||
df = pandas.read_csv('/home/pi/ | df = pandas.read_csv('/home/pi/temp-greenhouse/greenhouse_data.txt') | ||
fig = go.Figure(data = go.Scatter(mode='markers', | fig = go.Figure(data = go.Scatter(mode='markers', | ||
| Line 865: | Line 654: | ||
plotly.offline.plot(fig, | plotly.offline.plot(fig, | ||
filename="/home/pi/ | filename="/home/pi/temp-greenhouse/temperature_plot.html", | ||
auto_open=False) | auto_open=False) | ||
| Line 873: | Line 662: | ||
print("Preparing dweet") | print("Preparing dweet") | ||
dweet_dict = {} | dweet_dict = {} | ||
dweet_dict.update({"temperature": str( | dweet_dict.update({"temperature": str(line)}) | ||
dweet_dict.update({"Time": str(date_stamp)}) | dweet_dict.update({"Time": str(date_stamp)}) | ||
url = "https://dweet.io/dweet/for/ | url = "https://dweet.io/dweet/for/<My_Dweet_address>?" | ||
x = requests.post(url, json=dweet_dict) | x = requests.post(url, json=dweet_dict) | ||
print(x.text) | print(x.text) | ||
except: | except: | ||
print("Dweet failed") | print("Dweet failed") | ||
# check dweet with - https://dweet.io/get/latest/dweet/for/ | # check dweet with - https://dweet.io/get/latest/dweet/for/<My_Dweet_address> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= Freeboard IO - Public Dashboards = | = Freeboard IO - Public Dashboards = | ||
== Design Dashboard == | == Design Dashboard == | ||
[[File:Screenshot 2023-07- | [[File:Screenshot 2023-07-18 at 2.32.16 pm.png | 900px]] | ||
== Make Dashboard Public == | == Make Dashboard Public == | ||
* [https://freeboard.io/board/ | * [https://freeboard.io/board/YUogDB Temp-greenhouse Bundoora] | ||
[[File:Screenshot 2023-07- | [[File:Screenshot 2023-07-18 at 2.33.59 pm.png | 900px]] | ||
Latest revision as of 10:52, 2 August 2023
Video lessons
- Introduction to temp-greenhouse lesson
- Arduino IDE Installation
- Arduino library installation
- Onewire Temperature sensor coding on Arduino
- Reading data using Python on the Raspberry Pi
- Adding date_time stamp using Python on the Raspberry Pi
SMC Class Videos 24 July 2023
1. Arduino IDE Installation Arduino IDE Installation
2. Arduino library installation Arduino library installation
3. Onewire Temperature Circuit using Arduino Onewire Temperature Circuit using Arduino
4. Arduino onewire temperature sensor code Arduino onewire temperature sensor code
5. Using Python to receive temperature data from the Arduino Using Python to receive temperature data from the Arduino
6. Adding a Datetime stamp using Python Adding a Datetime stamp using Python
7. Saving data to a File using Python Saving data to a File using Python
SMC Class Videos 31 July 2023
1. Using Python to receive data from the Arduino Using Python to receive data from the Arduino
2. Adding a Datetime stamp using Python Adding a Datetime stamp using Python
3. Saving data to a File using Python Saving data to a File using Python
4. Dweeting data using Python Dweeting data using Python
Materials
List of material per experiment.
- Mini greenhouse
- Heatpad
- Seeds / soil
- Temperature probe one-wire
- Arduino Uno + solderless breadboard
- Raspberry Pi
Install Arduino IDE Software
Background
- The default Arduino IDE (Integrated Development Environment) installed on the Raspberry Pi is an older version.
- This lesson explains how to install the latest version of the Arduino IDE.
- The current version of Arduino IDE for the Raspberry Pi is 1.8.19
Online Resources
- https://www.raspberrypi-spy.co.uk/2020/12/install-arduino-ide-on-raspberry-pi/
- https://www.arduino.cc/en/software
- https://roboticsbackend.com/raspberry-pi-arduino-serial-communication/#Arduino_code
- https://youtu.be/jU_b8WBTUew
Installing the most current Arduino IDE
- We need to download the latest version of the Arduino IDE software compatible with the Raspberry Pi.
- You can go directly the Arduino Download site and download the Linux ARM 32 bit version.
- Alternatively, you can use the wget instruction.
Download the Arduino IDE Software
- Create a new folder for the Arduino IDE software.
- In the Terminal enter the following commands.
- mkdir Applications - make a new directory named Applications.
- cd Applications - change directory. Enter into the directory Applications.
- When in the directory the wget instruction can be entered to download the software.
- The download takes approx 3 minutes.
- Enter the following command in the Terminal
wget https://downloads.arduino.cc/arduino-1.8.19-linuxarm.tar.xz- Alternatively you can also go to the Arduino website and download the file directly to the Raspberry Pi.
Unpack the Arduino IDE application
- Use the File Manager to copy the arduino-1.8.19-linuxarm.tar.xz from the Downloads directory to the Applications directory.
- Go back to the Terminal.
- To navigate to the Applications directory enter the commands cd ~ to go to the home directory, then cd Applications to enter the Applications directory.
- Enter the following command to unpack the package tar -xvf arduino-1.8.19-linuxarm.tar.xz
- x - extracts the files
- v - verbose mode so all extraction operations will be shown in the Terminal
- f - specifies the name of the archive file we are extracting files from, in this case arduino-1.8.19-linuxarm.tar.xz
- Refer to this tutorial Linux tar Command Tutorial
tar -xvf arduino-1.8.19-linuxarm.tar.xz- Once the package in unpacked enter the newly created folder named arduino-1.8.19.
- You can use the Tab key to complete the file name (cd arduino + Tab)
- The compressed Arduino file will take a few seconds for all files to be extracted.
- Once all the files are extracted enter the command ls or ls -l to list all files in the current directory.
- arduino-1.8.19 is a directory and is coloured blue.
- Enter the arduino-1.8.19 by entering the command cd arduino-1.8.19
- List all the contents of the directory with the command ls -l
Installing the Arduino IDE Application
- Once in the directory there is an install.sh scripting file that will complete the installation.
- Start the install with the command sudo ./install.sh
Running the Arduino IDE Application
- The installation process adds an entry into the Raspberry Pi OS drop down menu.
- You can find the Arduino IDE entry under the Programming heading.
Arduino Uno - One Wire Temperature sensor
Frizing - Circuit diagram
- In the Arduino code below the data line is connected to pin 4.
One Wire Library installation
- Select Sketch > Include Library > Manage Libraries
- Wait for libraries to update. This may take 1-2 minutes. Be patient.
- The progress bar at the bottom of the Manage Libraries window will disappear when the libraries are fully updated.
- In the search bar enter onewire and press Enter.
- The search will take 10 seconds to complete. Be patient.
- Install the One Wire library.
- Use the One wire library with the author Paul Stoffregen
- Click on the Install button to the right to install the library.
- In this example the onewire library version 2.3.7 was installed.
Dallas Temperature Library installation
- After installing the onewire library now enter Dallas as the search term.
- In this instance version 3.9.0 of the DallasTemperature library was installed.
Arduino Uno Code
- Copy the Arduino code below and paste it in the Arduino IDE.
- Create a new directory on the Raspberry Pi using the File Manager in the home directory and name it temp-greenhouse
- Save the Arduino file as one-wire-temperature
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is conntec to the Arduino digital pin 4
#define ONE_WIRE_BUS 4
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
void setup(void)
{
// Start serial communication for debugging purposes
Serial.begin(9600);
// Start up the library
sensors.begin();
}
void loop(void){
// Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
sensors.requestTemperatures();
Serial.print("Celsius temperature: ");
// Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
Serial.println(sensors.getTempCByIndex(0));
delay(1000);
}
Serial Monitor output
- Temperature readings output to the Serial Monitor.
Delays in Arduino
- To make delays more human readable add the following code to your Arduino sketch.
- Try this example obtained from O'Rielly creating delays
const long oneSecond = 1000; // a second is a thousand milliseconds
const long oneMinute = oneSecond * 60;
const long fiveMinute = oneMinute * 5;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("delay for 1 millisecond");
delay(1);
Serial.println("delay for 1 second");
delay(oneSecond);
Serial.println("delay for 1 minute");
delay(oneMinute);
Serial.println("delay for 5 minutes");
delay(fiveMinute);
Serial.println("Ready to start over");
}
Sending Temperature data to Raspberry Pi
- Data can be sent from the Arduino to the Raspberry pi using the Serial cable.
- Open the Serial Monitor to view the data stream.
- In this example the data is being updated every 10 seconds using delay(10000).
- When finalising the code on the Arduino make sure that the delay is set to five minutes delay(300000)
Arduino code to send data to Raspberry Pi - image
Arduino code to send data to Raspberry Pi - code
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4 // Data wire is conntected to the Arduino digital pin 4
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature sensor
float temperature;
void setup() {
Serial.begin(9600);
// Start up the library
sensors.begin();
}
void loop() {
sensors.requestTemperatures();
Serial.println(sensors.getTempCByIndex(0)); // Send data to Raspberry Pi
delay(10000); // 300000 = Five minute delay, 10000 = 10 second delay
}
Python code on the Raspberry Pi to read Arduino data
- Open Thonny from the main Programming menu on the Raspberry Pi'
- Copy and paste the code below.
- Save the file in a new directory named temp-greenhouse
- Save the file as python-data-collect1.py
Close the Arduino Serial Monitor before Running Python
- Before running the Python program in Thonny, make sure that the Arduino Serial Monitor is closed.
- If the Arduino Serial monitor is operating - the python program will show an error.
Check the Arduino Serial Port Connection
- Check the Arduino IDE program to find out what serial port the Arduino is connected to.
- Port details are available in many locations as highlighted below.
- In this example the port is USB number 0 /dev/ttyUSB0
- This same port number needs to be in the Python program in Thonny.
Python - code to constantly collect data from Arduino - code
- The line print(line) prints all data received from the Arduino serial.
#!/usr/bin/env python3
import serial
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.reset_input_buffer()
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
print(line)
Python - code to constantly collect data from Arduino - image
Stopping and Running code in Thonny
- Always click on the Stop button before running any new Python code.
Python - Adding a Datestamp - code
- It is important to note the exact date and time when receiving data.
- In computer language we call this a datestamp
- To record the datestamp we need to add the following code.
- import datetime - python library to support datetime creation.
- now = datetime.datetime.now() - code to collect the current datetime on the computer. The current datetime is stored in the object variable now
- date_stamp = now.strftime("%Y-%m-%d %H:%M:%S") - reformat the object now to a human readable date format and store the result in the variable date_stamp.
- print(f"Time is {date_stamp}') - print statement to combine text and variables.
#!/usr/bin/env python3
import serial
import datetime
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.reset_input_buffer()
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
print(line)
now = datetime.datetime.now()
date_stamp = now.strftime("%Y-%m-%d %H:%M:%S")
print(f'The current time is {date_stamp}')
data = str(line) + "," + date_stamp + "\n"
print(f'The data is = {data}')
Python - Collecting data from Arduino and adding a datestamp - image
Python - saving data to a file
- Now that we have the data from the Arduino and the current date and time its time to save the data to a file.
- Saving the data allows us to import the data into a spreadsheet for analysis or graph the data to observe trends.
- Copy and paste the code below in Thonny.
- You can overwrite the existing code.
- Save data we need to:
- data = str(line) + "," + date_stamp + "\n" - add the Arduino data to the date_stamp data and also add a new line character - \n to the end of the code.
- f = open('/home/pi/temp-greenhouse/greenhouse_data.txt','a') open a new file named greenhouse_data.txt
- f.write(data) - write the data to the file.
- f.close() - close the file.
- After several data writes are completed open the file using the File Manager and check that file has stored the data.
- Example of the data written to the text file greenhouse_data.txt
Python - saving data to a file - code
#!/usr/bin/env python3
import serial
import datetime
import time
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.reset_input_buffer()
while True:
time.sleep(0.1) # added a small delay in loop
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
print(line)
now = datetime.datetime.now()
date_stamp = now.strftime("%Y-%m-%d %H:%M:%S")
print(f'This python program was run at this time {date_stamp}')
data = str(line) + "," + date_stamp + "\n"
print(f'The data is = {data}')
f = open('/home/pi/temp-greenhouse/greenhouse_data.txt','a')
f.write(data)
f.close()
print("The data has been written to the file greenhouse_data.txt")
Python - Graphing data with Plotly
- Data stored in a text file can be plotted using a special python library called plotly
- Before we can use plotly we need to install the python libraries plotly for graphing data and pandas to read and table data from the text file.
Installation of the plotly python package
- From the Raspberry Pi top bar menu click on the Terminal icon.
- Open the Terminal and enter the command pip3 install plotly
- pip is short for Pip Installs Python.
- pip3 installs packages for python3
- The version of plotly that was installed is plotly-5.13.1
- Other python packages may also be installed to support plotly.
- To test that plotly has been installed correctly open the Thonny and enter - import plotly into the Python Shell window (the bottom window in Thonny).
- Important - you need to Close and Re-open Thonny before the import plotly will be recognised.
Installing pandas python package to read data from text files
- To read data from our data.txt file we need to install another python package named pandas.
- To install the pandas package open the Terminal and enter the following commands.
- pip3 install pandas – will take up to 5-10 minutes to install. On an old laptop (2017) it took 30 minutes for pandas to install.
- Once pandas has been installed check to see if pandas can be imported in Thonny.
- Use the command import pandas
- If there is no error pandas has been installed correctly.
Error with installation of pandas
- Sometime the import pandas statement produces an error because it is missing some libraries.
- If this is the case install the following library.
- Open the Terminal.
- Enter the command sudo apt-get install libatlas-base-dev – extra missing software package to support pandas
- Answer y yes to any questions about the installation.
- To test that pandas has been installed correctly open the Python Shell and enter - import pandas
- Remember to restart Thonny before testing.
- If the pandas python package has installed correctly you should see no errors.
Modification of temperature_data.txt file
- Before pandas and plotly can use the data in the text file we need to add headers so that pandas knows the order of time and temperature data.
- Open the File Manager
- Double click on the file greenhouse_data.txt to open the file.
- In the very top line add the following text temperature,date_time
- This line will serve as a header so that plotly knows in which column the temperature and date_time data is located.
- Save and close the file.
Python - Graphing data using Plotly - code
- Copy the following code to start graphing temperature data.
#!/usr/bin/env python3
import serial
import datetime
import plotly
import plotly.graph_objects as go
import pandas
import time
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.reset_input_buffer()
while True:
time.sleep(0.1)
if ser.in_waiting > 0:
print("Data in")
line = ser.readline().decode('utf-8').rstrip()
print(line)
now = datetime.datetime.now()
date_stamp = now.strftime("%Y-%m-%d %H:%M:%S")
data = str(line) + "," + date_stamp + "\n"
print(f'The data is = {data}')
f = open('/home/pi/temp-greenhouse/greenhouse_data.txt','a')
f.write(data)
f.close()
print("Data saved")
# import and plot data text file
df = pandas.read_csv('/home/pi/temp-greenhouse/greenhouse_data.txt')
fig = go.Figure(data = go.Scatter(mode='markers',
x=df.date_time,
y=df.temperature,
marker=dict(
color="LightSkyBlue",
size=20)
)
)
fig.update_layout(title="Temperature in Greenhouse",
xaxis_title = 'Time',
yaxis_title = 'Temperature',
font=dict(
size=20,
color="RebeccaPurple"
)
)
plotly.offline.plot(fig,
filename="/home/pi/temp-greenhouse/temperature_plot.html",
auto_open=False)
print("Temperature data plotted")
- To see the plotted temperature data open the File Manager.
- Navigate to the directory temp-greenhouse
- And click on the file temperature_plot.html
- This will open the web browser on the Raspberry Pi and display a plot of the temperature data.
Python - Dweeting Data
- Data can be sent to the internet via a Dweet
- Before sending dweets make sure you change the delay in the Arduino code to delay(300000) so that updates are only send once every five minutes.
- Replace My_Dweet_address with your own made up address
try:
print("Preparing dweet")
dweet_dict = {}
dweet_dict.update({"temperature": str(line)})
dweet_dict.update({"Time": str(date_stamp)})
url = "https://dweet.io/dweet/for/<My_Dweet_address>?"
x = requests.post(url, json=dweet_dict)
print(x.text)
except:
print("Dweet failed")
# check dweet with - https://dweet.io/get/latest/dweet/for/<My_Dweet_address>
Dweet python code - full
#!/usr/bin/env python3
# dweet data
import serial
import datetime
import plotly
import plotly.graph_objects as go
import pandas
import time
import requests
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.reset_input_buffer()
while True:
time.sleep(0.1)
if ser.in_waiting > 0:
print("Data in")
line = ser.readline().decode('utf-8').rstrip()
print(line)
now = datetime.datetime.now()
date_stamp = now.strftime("%Y-%m-%d %H:%M:%S")
data = str(line) + "," + date_stamp + "\n"
print(f'The data is = {data}')
f = open('/home/pi/temp-greenhouse/greenhouse_data.txt','a')
f.write(data)
f.close()
print("Data saved")
# import and plot data text file
df = pandas.read_csv('/home/pi/temp-greenhouse/greenhouse_data.txt')
fig = go.Figure(data = go.Scatter(mode='markers',
x=df.date_time,
y=df.temperature,
marker=dict(
color="LightSkyBlue",
size=20)
)
)
fig.update_layout(title="Temperature in Greenhouse",
xaxis_title = 'Time',
yaxis_title = 'Temperature',
font=dict(
size=20,
color="RebeccaPurple"
)
)
plotly.offline.plot(fig,
filename="/home/pi/temp-greenhouse/temperature_plot.html",
auto_open=False)
print("Temperature data plotted")
try:
print("Preparing dweet")
dweet_dict = {}
dweet_dict.update({"temperature": str(line)})
dweet_dict.update({"Time": str(date_stamp)})
url = "https://dweet.io/dweet/for/<My_Dweet_address>?"
x = requests.post(url, json=dweet_dict)
print(x.text)
except:
print("Dweet failed")
# check dweet with - https://dweet.io/get/latest/dweet/for/<My_Dweet_address>