Temperature Monitored Greenhouse project: Difference between revisions

From Sensors in Schools
Jump to navigation Jump to search
No edit summary
 
(37 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://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 =
List of material per experiment.
List of material per experiment.
Line 305: Line 351:
</syntaxhighlight>
</syntaxhighlight>


= Python - code to constantly collect data from Arduino - image =
== Python - code to constantly collect data from Arduino - image ==


[[File:Screenshot 2023-07-18 at 6.11.53 am.png | 900px]]
[[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 =
= Python - Adding a Datestamp - code =
Line 344: 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-06-25 at 7.03.31 am.png | 900px]]
[[File:Screenshot 2023-07-18 at 7.01.01 am.png | 900px]]


= Python - saving data to a file - 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.
* 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.
* Saving the data allows us to import the data into a spreadsheet for analysis or graph the data to observe trends.
Line 359: Line 410:
* After several data writes are completed open the file using the '''File Manager''' and check that file has stored the data.
* After several data writes are completed open the file using the '''File Manager''' and check that file has stored the data.


[[File:Screenshot 2023-06-25 at 7.12.09 am.png | 900px]]
[[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 387: Line 442:
             print(f'The data is =  {data}')
             print(f'The data is =  {data}')
          
          
             f = open('/home/pi/auto-greenhouse/greenhouse_data.txt','a')
             f = open('/home/pi/temp-greenhouse/greenhouse_data.txt','a')
             f.write(data)
             f.write(data)
             f.close()
             f.close()
Line 393: Line 448:


</syntaxhighlight>
</syntaxhighlight>


= Python - Graphing data with Plotly =
= Python - Graphing data with Plotly =
Line 407: Line 461:
* Other python packages may also be installed to support plotly.
* Other python packages may also be installed to support plotly.


[[File:Screen Shot 2023-03-23 at 3.55.06 am.png | 900px]]
[[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).
* 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:Screen Shot 2023-03-23 at 4.03.47 am.png | 900px]]
[[File:Screenshot 2023-07-18 at 7.16.38 am.png | 900px]]


== Installing pandas python package to read data from text files ==
== Installing pandas python package to read data from text files ==
Line 417: Line 472:
* To install the pandas package open the Terminal and enter the following commands.
* 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.
** '''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.
* Once pandas has been installed check to see if pandas can be imported in Thonny.
* Use the command '''import pandas'''
* Use the command '''import pandas'''
* If there is no error pandas has been installed correctly.
* If there is no error pandas has been installed correctly.
* If however there is an error try installing these additional libraries.
 
** '''sudo apt-get install libatlas-base-dev''' – extra missing software package to support pandas
=== 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'''
* 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.
* If the pandas python package has installed correctly you should see no errors.
* There are errors try installing this additional package
** '''sudo apt-get update''' – update all libraries on the Raspberry Pi
** '''sudo apt-get install libatlas-base-dev''' – extra missing software package to support pandas


[[File:Screen Shot 2023-03-23 at 4.06.33 am.png | 900px]]
[[File:Screenshot 2023-07-18 at 7.28.44 am.png | 900px]]


== Modification of temperature_data.txt file ==
== 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.
* 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-07-18 at 7.30.56 am.png | 900px]]


== Python - Graphing data using Plotly - code ==
== Python - Graphing data using Plotly - code ==
* Copy the following code to start graphing temperature data.
* Copy the following code to start graphing temperature data.


<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
Line 459: 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/auto-greenhouse/greenhouse_data.txt','a')
             f = open('/home/pi/temp-greenhouse/greenhouse_data.txt','a')
             f.write(data)
             f.write(data)
             f.close()
             f.close()
Line 492: Line 565:
</syntaxhighlight>
</syntaxhighlight>


[[File:Screenshot 2023-06-25 at 11.24.51 am.png | 900px]]
[[File:Screenshot 2023-07-18 at 7.36.26 am.png | 900px]]


= Arduino code - Soil Moisture with Temperature Compensation - image =
* 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-06-25 at 7.18.03 pm.png | 900px]]
[[File:Screenshot 2023-07-18 at 7.37.59 am.png | 900px]]


= Arduino code - Soil Moisture with Temperature Compensation - code =
* This will open the web browser on the Raspberry Pi and display a plot of the temperature data.


<syntaxhighlight lang="c++">
[[File:Screenshot 2023-07-18 at 7.41.03 am.png | 900px]]


#include <OneWire.h>
= Python - Dweeting Data =
#include <DallasTemperature.h>
* Data can be sent to the internet via a '''Dweet'''
#define ONE_WIRE_BUS 4  // Data wire is conntected to the Arduino digital pin 4
* 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.
OneWire oneWire(ONE_WIRE_BUS);  // Setup a oneWire instance to communicate with any OneWire devices
* Replace '''My_Dweet_address''' with your own made up address
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature sensor
const int sensorXAnalogPin = A4;
const int sensorYAnalogPin = A5;
const float knownResistor = 10.0;      // Constant value of known resistor in k Ohms
float resistance;
float resistanceCorrect;
float temperature;
 
void setup() {
  Serial.begin(9600);
  sensors.begin();
}
 
void loop() {
  // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
  sensors.requestTemperatures();
  temperature = sensors.getTempCByIndex(0);
  resistance = ReadSensor();
  resistanceCorrect = resistance * (1.0 + (temperature - 22.0) * 0.018);
  Serial.print(temperature);
  Serial.print(",");
  //Serial.print(resistance);
  //Serial.print(",");
  Serial.println(resistanceCorrect);
  delay(300000);  // 300000 five minutes,
}
 
</syntaxhighlight>
 
= Arduino - Soil Moisture with Temperature Compensation - Serial Monitor =
 
[[File:Screenshot 2023-06-25 at 7.22.53 pm.png | 900px]]
 
= Python - Splitting data and using try and except statements =


<syntaxhighlight lang="python">
<syntaxhighlight lang="python">


#!/usr/bin/env python3
# split data
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:
            line = ser.readline().decode('utf-8').rstrip()
            print(f"Data separated using commas {line.split(',')}")
            mylist = line.split(',')
             try:
             try:
                 temperature = float(mylist[0])
                 print("Preparing dweet")
                 soilMoisture = float(mylist[1])
                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:
                 temperature = 0.0
                 print("Dweet failed")
                soilMoisture = 0.0
             # check dweet with - https://dweet.io/get/latest/dweet/for/<My_Dweet_address>
 
            print(f'temperature is {temperature} and soil moisture is {soilMoisture}')
            time.sleep(1)
            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}')
 
            data = ""
            data = date_stamp + "," + str(temperature) + "," + str(soilMoisture) + '\n'
            f = open('/home/pi/auto-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/auto-greenhouse/greenhouse_data.txt')
 
            fig = go.Figure(data = go.Scatter(mode='markers',
                                                x=df.date_time,
                                                y=df.soil_moisture,
                                                marker=dict(
                                                    color="LightSkyBlue",
                                                    size=20)
                                                )
                                        )
            fig.update_layout(title="Soil Moisture in Greenhouse",
                                xaxis_title = 'Time',
                                yaxis_title = 'Soil Moisture',
                                font=dict(
                                    size=20,
                                    color="RebeccaPurple"
                                )
                            )
 
            plotly.offline.plot(fig,
                                filename="/home/pi/auto-greenhouse/soil_moisture_plot.html",
                                auto_open=False)
 
            print("Soil Moisture data plotted")
           
           
            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/auto-greenhouse/temperature_plot.html",
                                auto_open=False)
 
            print("Temperature data plotted")


</syntaxhighlight>
</syntaxhighlight>


= Python - Dweeting Data =
== Dweet python code - full ==


<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
Line 654: 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(f"Data separated using commas {line.split(',')}")
             print(line)
            mylist = line.split(',')
            try:
                temperature = float(mylist[0])
                soilMoisture = float(mylist[1])
            except:
                temperature = 0.0
                soilMoisture = 0.0
 
            print(f'temperature is {temperature} and soil moisture is {soilMoisture}')
            time.sleep(1)
             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 671: Line 626:
             print(f'The data is =  {data}')
             print(f'The data is =  {data}')


            data = ""
             f = open('/home/pi/temp-greenhouse/greenhouse_data.txt','a')
            data = date_stamp + "," + str(temperature) + "," + str(soilMoisture) + '\n'
             f = open('/home/pi/auto-greenhouse/greenhouse_data.txt','a')
             f.write(data)
             f.write(data)
             f.close()
             f.close()
Line 679: Line 632:
              
              
             # import and plot data text file
             # import and plot data text file
             df = pandas.read_csv('/home/pi/auto-greenhouse/greenhouse_data.txt')
             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.soil_moisture,
                                                marker=dict(
                                                    color="LightSkyBlue",
                                                    size=20)
                                                )
                                        )
            fig.update_layout(title="Soil Moisture in Greenhouse",
                                xaxis_title = 'Time',
                                yaxis_title = 'Soil Moisture',
                                font=dict(
                                    size=20,
                                    color="RebeccaPurple"
                                )
                            )


            plotly.offline.plot(fig,
                                filename="/home/pi/auto-greenhouse/soil_moisture_plot.html",
                                auto_open=False)


            print("Soil Moisture data plotted")
           
              
              
             fig = go.Figure(data = go.Scatter(mode='markers',
             fig = go.Figure(data = go.Scatter(mode='markers',
Line 723: Line 654:


             plotly.offline.plot(fig,
             plotly.offline.plot(fig,
                                 filename="/home/pi/auto-greenhouse/temperature_plot.html",
                                 filename="/home/pi/temp-greenhouse/temperature_plot.html",
                                 auto_open=False)
                                 auto_open=False)


Line 731: Line 662:
                 print("Preparing dweet")
                 print("Preparing dweet")
                 dweet_dict = {}
                 dweet_dict = {}
                 dweet_dict.update({"temperature": str(temperature)})
                 dweet_dict.update({"temperature": str(line)})  
                dweet_dict.update({"soilMoisture": str(soilMoisture)})
                 dweet_dict.update({"Time": str(date_stamp)})
                 dweet_dict.update({"Time": str(date_stamp)})
                 url = "https://dweet.io/dweet/for/3083-auto-greenhouse1?"
                 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/3083-auto-greenhouse1
             # check dweet with - https://dweet.io/get/latest/dweet/for/<My_Dweet_address>


           
</syntaxhighlight>
</syntaxhighlight>


= Freeboard IO - Public Dashboards =
= Freeboard IO - Public Dashboards =
== Create Datasource ==
[[File:Screenshot 2023-07-12 at 2.10.08 pm.png | 900px]]


== Design Dashboard ==
== Design Dashboard ==


[[File:Screenshot 2023-07-12 at 2.11.35 pm.png | 900px]]
[[File:Screenshot 2023-07-18 at 2.32.16 pm.png | 900px]]


== Make Dashboard Public ==
== Make Dashboard Public ==
* [https://freeboard.io/board/k6pB1B Temperature Controlled Mini-greenhouse Bundoora]
* [https://freeboard.io/board/YUogDB Temp-greenhouse Bundoora]


[[File:Screenshot 2023-07-12 at 2.24.28 pm.png | 900px]]
[[File:Screenshot 2023-07-18 at 2.33.59 pm.png | 900px]]

Latest revision as of 10:52, 2 August 2023

Video lessons

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

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>

Freeboard IO - Public Dashboards

Design Dashboard

Make Dashboard Public