Smart Cities - Tiny House at Home: Difference between revisions

From Sensors in Schools
Jump to navigation Jump to search
No edit summary
Line 51: Line 51:
/home/<username>/env/bin/python3.11 -m idlelib.idle
/home/<username>/env/bin/python3.11 -m idlelib.idle
</syntaxhighlight>
</syntaxhighlight>
= Arduino Uno - One Wire Temperature sensor =
== Frizing - Circuit diagram ==
* In this circuit diagram the data line is connected to pin 2.
* In the Arduino code below the data line is connected to pin 4.
[[File:Screenshot 2023-06-24 at 9.20.39 pm.png | 900px]]
== One Wire Library installation ==
* Select '''Sketch > Include Library > Manage Libraries'''
* Wait for libraries to update. This may take 1-2 minutes.
* In the search bar enter '''onewire''' and press Enter.
[[File:Screenshot 2023-06-24 at 8.24.52 pm.png | 900px]]
* Install the '''One Wire''' library.
* Use the One wire library with the author '''Paul Stoffregen'''
[[File:Screenshot 2023-06-24 at 8.32.20 pm.png | 900px]]
* In this example the onewire library version 2.3.7 was installed.
[[File:Screenshot 2023-06-24 at 8.48.39 pm.png | 900px]]
== Dallas Temperature Library installation ==
* After installing the onewire library now enter '''Dallas''' as the search term.
[[File:Screenshot 2023-06-24 at 8.53.33 pm.png | 900px]]
* In this instance version 3.9.0 of the DallasTemperature library was installed.
[[File:Screenshot 2023-06-24 at 9.04.40 pm.png | 900px]]
== Arduino Uno Code ==
<syntaxhighlight lang="c++">
#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);
}
</syntaxhighlight>
== Serial Monitor output ==
* Temperature readings output to the Serial Monitor.
[[File:Screenshot 2023-06-24 at 9.23.58 pm.png | 900px]]

Revision as of 23:10, 16 November 2024

Raspberry Pi OS

  • Starting with a fresh install of the Raspberry Pi Debian Bookworm OS with desktop (64 bit).
  • Use the Raspberry Pi Imager to install the OS on a 16MB micro SD card.
  • Complete the set up and updates on the Raspberry Pi.

Install the real Python IDE (not thonny) and the MQTT broker

  • Open the Terminal and enter these commands
sudo apt update
sudo apt -y full-upgrade 
sudo apt install -y idle3 arduino

Install Virtual Environment

  • These days Python must run in a virtual environment so that each user gets their own Python
  • The virtual environment has to be activated before each use
  • The linux prompt then changes from:
  • <username>@home:~ $ to (env) <username>@home:~ $
  • Create a virtual environment for Python called env in your /home/<username> directory.
    • In Linux (and other operating systems), a virtual environment is an isolated Python environment that allows you to install and manage Python packages separately from the system-wide Python installation.
    • This ensures that your Python projects have their own dependencies and versions of libraries without affecting or being affected by other projects or the global Python environment.
  • Enter the commands in the Terminal.
cd ~
python3 -m venv env


  • Activate the virtual environment with the commands.
cd ~/env
source bin/activate
  • Dectivate the virtual environment with the command.
deactivate

Configure the Python Idle3 shortcut to auto-activate the Python virtual environment

  • Click Raspberry | Programming
  • Right Click IDLE (using Python-3.11)
  • Click Properties | Desktop Entry
  • In the Command: field enter
/home/<username>/env/bin/python3.11 -m idlelib.idle

Arduino Uno - One Wire Temperature sensor

Frizing - Circuit diagram

  • In this circuit diagram the data line is connected to pin 2.
  • 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.
  • In the search bar enter onewire and press Enter.

  • Install the One Wire library.
  • Use the One wire library with the author Paul Stoffregen

  • 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

#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.