Smart Cities - Tiny House at Home

From Sensors in Schools
Jump to navigation Jump to search

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 sensor_1_wire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensor1(&sensor_1_wire);

void setup(void)
{
  // Start serial communication for debugging purposes
  Serial.begin(9600);
  // Start up the library
  sensors.begin();
}

void loop(void){ 
  // Call sensor1.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
  sensor1.requestTemperatures(); 
  
  Serial.print("Celsius temperature: ");
  // Why "byIndex"? You can have more than one IC on the same bus (pin). 0 refers to the first IC on the wire
  Serial.println(sensor1.getTempCByIndex(0)); 
  delay(1000);
}

Serial Monitor output

  • Temperature readings output to the Serial Monitor.