Smart Cities - Tiny House at Home: Difference between revisions

From Sensors in Schools
Jump to navigation Jump to search
No edit summary
Line 128: Line 128:


=Setting up VNC=
=Setting up VNC=
==Check if VNC is Already Installed==
In recent versions of Raspberry Pi OS, VNC comes pre-installed but might be disabled by default.
* Check Installation:
* Open a terminal on your Raspberry Pi.
* Run:
<syntaxhighlight lang="c++">
vncserver --version
</syntaxhighlight>
If VNC is installed, it will display the version. If not, install it (see step 3).
==Enable VNC on Raspberry Pi==
* Open the Raspberry Pi Configuration Tool:
* GUI: Go to Start Menu > Preferences > Raspberry Pi Configuration.
* CLI: Run:
<syntaxhighlight lang="bash">
sudo raspi-config
</syntaxhighlight>
* Navigate to:
* Interface Options > VNC > Enable
* Confirm and exit the configuration tool.
* Verify that the VNC service is running:
<syntaxhighlight lang="bash">
systemctl status vncserver-x11-serviced.service
</syntaxhighlight>
If not running, start it:
<syntaxhighlight lang="bash">
sudo systemctl start vncserver-x11-serviced.service
</syntaxhighlight>
==Install VNC (if not installed)==
If VNC isn't pre-installed:
* Update the package list:
<syntaxhighlight lang="bash">
sudo apt update
</syntaxhighlight>
Install VNC Server:
<syntaxhighlight lang="bash">
sudo apt install realvnc-vnc-server
</syntaxhighlight>
==Connect to the Raspberry Pi Using VNC Viewer==
Find the Raspberry Pi’s IP Address:
<syntaxhighlight lang="bash">
hostname -I
</syntaxhighlight>
* Note the first IP address (e.g., 192.168.1.100).
* Install VNC Viewer on your client device:
* Download VNC Viewer for Windows, macOS, or Linux.
* Open VNC Viewer on your client device and enter the Raspberry Pi’s IP address (e.g., 192.168.1.100).
* Log in with your Raspberry Pi's credentials:
<syntaxhighlight lang="bash">
Username: pi
Password: (default is raspberry, or your custom password).
</syntaxhighlight>
==Optional: Configure VNC==
* Resolution Adjustment:
Edit /boot/config.txt to set a virtual desktop resolution:
<syntaxhighlight lang="bash">
sudo nano /boot/firmware/config.txt
</syntaxhighlight>
Add or modify:
<syntaxhighlight lang="bash">
hdmi_force_hotplug=1
hdmi_group=2  # Set to 2 for DMT (monitor resolutions)
hdmi_mode=9  # Example: 800x600 @ 60Hz
</syntaxhighlight>
This sets a resolution of 800x600. Save and reboot:
<syntaxhighlight lang="bash">
sudo reboot
</syntaxhighlight>
==hdmi_force_hotplug=1==
* Forces the Raspberry Pi to output HDMI signals even if no HDMI display is detected at boot.
* This is essential when you're using VNC without a physical monitor connected.
Now you can access your Raspberry Pi 4 from any device on the same network!

Revision as of 02:56, 15 December 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 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
  sensor1.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.

Setting up VNC

Check if VNC is Already Installed

In recent versions of Raspberry Pi OS, VNC comes pre-installed but might be disabled by default.

  • Check Installation:
  • Open a terminal on your Raspberry Pi.
  • Run:
vncserver --version

If VNC is installed, it will display the version. If not, install it (see step 3).

Enable VNC on Raspberry Pi

  • Open the Raspberry Pi Configuration Tool:
  • GUI: Go to Start Menu > Preferences > Raspberry Pi Configuration.
  • CLI: Run:
sudo raspi-config
  • Navigate to:
  • Interface Options > VNC > Enable
  • Confirm and exit the configuration tool.
  • Verify that the VNC service is running:
systemctl status vncserver-x11-serviced.service

If not running, start it:

sudo systemctl start vncserver-x11-serviced.service

Install VNC (if not installed)

If VNC isn't pre-installed:

  • Update the package list:
sudo apt update

Install VNC Server:

sudo apt install realvnc-vnc-server

Connect to the Raspberry Pi Using VNC Viewer

Find the Raspberry Pi’s IP Address:

hostname -I
  • Note the first IP address (e.g., 192.168.1.100).
  • Install VNC Viewer on your client device:
  • Download VNC Viewer for Windows, macOS, or Linux.
  • Open VNC Viewer on your client device and enter the Raspberry Pi’s IP address (e.g., 192.168.1.100).
  • Log in with your Raspberry Pi's credentials:
Username: pi
Password: (default is raspberry, or your custom password).

Optional: Configure VNC

  • Resolution Adjustment:

Edit /boot/config.txt to set a virtual desktop resolution:

sudo nano /boot/firmware/config.txt

Add or modify:

hdmi_force_hotplug=1
hdmi_group=2  # Set to 2 for DMT (monitor resolutions)
hdmi_mode=9   # Example: 800x600 @ 60Hz

This sets a resolution of 800x600. Save and reboot:

sudo reboot


hdmi_force_hotplug=1

  • Forces the Raspberry Pi to output HDMI signals even if no HDMI display is detected at boot.
  • This is essential when you're using VNC without a physical monitor connected.

Now you can access your Raspberry Pi 4 from any device on the same network!