Smart Cities - Tiny House at Home: Difference between revisions
(→Notes) |
No edit summary |
||
| Line 335: | Line 335: | ||
=Interpreting Data from Arduino on Raspberry using Python= | =Interpreting Data from Arduino on Raspberry using Python= | ||
<syntaxhighlight lang="bash"> | |||
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||
# dweet data | # dweet data | ||
| Line 349: | Line 350: | ||
if ser.in_waiting > 0: | if ser.in_waiting > 0: | ||
line = ser.readline().decode('utf-8').rstrip() | line = ser.readline().decode('utf-8').rstrip() | ||
</syntaxhighlight> | |||
Revision as of 03:27, 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 Server
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!
Setting up VNC Viewer
To install a VNC client on a Raspberry Pi 4 and use it to access another Raspberry Pi with a VNC server already running, follow these steps:
Install VNC Viewer on the Raspberry Pi 4
- Open a terminal on your Raspberry Pi 4.
- Install the RealVNC Viewer (the VNC client):
sudo apt update
sudo apt install realvnc-vnc-viewer
Confirm the installation by running:
vncviewer --version
This should display the version of VNC Viewer installed.
Get the IP Address of the Remote Raspberry Pi
- On the Raspberry Pi with the VNC server running, open a terminal.
- Find its IP address by running:
hostname -I
Note the first IP address (e.g., 192.168.1.102).
Use VNC Viewer to Access the Remote Raspberry Pi
Launch VNC Viewer on your Raspberry Pi 4 by typing:
vncviewer
- In the VNC Viewer window:
- Enter the IP address of the remote Raspberry Pi (e.g., 192.168.1.102).
- Click Connect.
- Log in to the remote Raspberry Pi:
- Enter the username and password of the remote Raspberry Pi.
- After successful authentication, you will see the remote Raspberry Pi's desktop.
Save Connection for Future Use
- VNC Viewer allows you to save connections for easy access later.
- Click File > New Connection in VNC Viewer and save the remote Pi's details.
Setting Static IP Address on Raspberry Pi
To set a fixed IP address on a Raspberry Pi using the Advanced Options > Edit Connections graphical interface, follow these steps:
Open the Network Connections Settings
- On your Raspberry Pi, open the desktop environment.
- Click on the network icon in the top-right corner of the screen (usually looks like Wi-Fi or two arrows).
- Select Advanced Options > Edit Connections.
Select the Connection to Configure
- In the Edit Connections window, you'll see a list of network connections.
- Find the connection you want to configure (e.g., your Ethernet or Wi-Fi connection).
- Select the connection and click Edit.
Set a Static IP Address
- Navigate to the IPv4 Settings tab.
- Change the Method from Automatic (DHCP) to Manual.
- Click the Add button to input the following details:
- Address: The desired static IP address (e.g., 192.168.1.100).
- Netmask: The subnet mask (usually 255.255.255.0 for home networks) or enter 24which is equivalent to 255.255.255.0.
- Gateway: The IP address of your router (e.g., 192.168.1.1).
- Enter the DNS server(s) under the DNS Servers field, separated by commas if multiple, or just enter the IP address of the router.
- For example, 8.8.8.8, 8.8.4.4 (Google DNS) or your router's IP address.
Save and Apply
- Click Save to save your changes.
- Restart the network connection:
- Disable and re-enable the connection from the network icon, or
- Reboot the Raspberry Pi for the changes to take effect.
Verify the Static IP
- Open a terminal and check your IP address:
hostname -I
Ensure it matches the static IP you configured. Test network connectivity:
ping 8.8.8.8
This will verify that your Pi can reach the internet.
Notes
- Choose a static IP outside your router's DHCP range to avoid conflicts with dynamically assigned IPs. You can find the range in your router's settings (typically via 192.168.1.1 in a browser).
- If you encounter connectivity issues, double-check the gateway and DNS server settings.
Interpreting Data from Arduino on Raspberry using Python
#!/usr/bin/env python3
# dweet data
import serial
import datetime
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:
line = ser.readline().decode('utf-8').rstrip()