Smart Cities - Tiny House at Home (Alternate way)

From Sensors in Schools
Revision as of 00:28, 1 December 2024 by Jeremy (talk | contribs) (→‎For Linux)
Jump to navigation Jump to search

Required Items

Installing Thonny IDE

Follow installation steps for your os

For Windows/Mac

visit the thonny website and download the latest windows installer https://thonny.org/

For Linux

Installer (installs private Python 3.10 on x86_64, uses existing python3 elsewhere)

bash <(wget -O - https://thonny.org/installer-for-linux)

Re-using an existing Python installation

pip3 install thonny

Connect Sensors To Pico W

The final assembly should look something like this:

Daisy chain the piicodev modules together then plug into 3V3, SDA/SCL and GND

Temp Sensor

RTC

Connect the onewire temperature sensor into the raspberry pi pico (GND, PWR and SIG (I chose pin 28 for my SIG pin))

OneWire adapter board

OneWire - Pico (GP28)

Thonny IDE Setup

Steps to setup Thonny for use with pico w

Connect Raspberry Pi Pico

Connect the pico to your computer using a micro-USB to USB-A cable

Open Thonny IDE

Click 'Configure Interpreter' or the hamburger menu on the bottom right

Select 'MicroPython (Raspberry Pi Pico)'

Install Required Packages

Ensure Raspberry pi pico is connected

Select 'Tools' -> 'Manage packages...'

Search for and install 'network' 'onewire' 'piicodev' 'smbus2'

This should install the required libraries and put them in a folder called 'lib'

Programming The Pico W

Code and explanation of some sections of the code

main.py

from machine import *
from utime import sleep
import json
import socket
import network
import urequests
import onewire, ds18x20
from PiicoDev_RV3028 import PiicoDev_RV3028
from PiicoDev_TMP117 import PiicoDev_TMP117

led = Pin("LED", Pin.OUT) #Assign led
rtc = PiicoDev_RV3028() #Assign RTC
tempSensor = PiicoDev_TMP117(asw=[1,0,0,0]) #Assign Precision Temp Sensor
onewire_sensor = ds18x20.DS18X20(onewire.OneWire(Pin(28))) #Find Onewire Temp Sensor
onewire_scan = onewire_sensor.scan() #Assign Onewire Temp Sensor

# Network setup
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
try:
    wlan.connect(ssid, password)
    timeout = 0
    while not wlan.isconnected():
        print('Waiting for connection...')
        sleep(1)
        timeout += 1
        if timeout > 20:
            raise Exception("WiFi connection timeout")
    print('Network config:', wlan.ifconfig())
except Exception as e:
    print(f"WiFi connection error: {e}")
    machine.reset()

def getData(whichOne):
    if whichOne == 0:
        onewire_sensor.convert_temp() #Gets Temperature
        sleep(1) #Allow time for temp conversion
        outdoor_temp = onewire_sensor.read_temp(onewire_scan[0])
#        print(f"Onewire Temperature: {outdoor_temp}°C")
        return outdoor_temp
    elif whichOne == 1:
#        print(f"Precision Temperature: {tempSensor.readTempC()}°C")
        return tempSensor.readTempC()
    else:
        print(f'incorrect input: {whichOne}, value should be 0 or 1')

def postData():
    # Create data dictionary with both temperatures and timestamp
    data = {
        'outdoor_temp': getData(0),
        'indoor_temp': tempSensor.readTempC(),
        'timestamp': rtc.timestamp()
    }
    # Convert to JSON and set headers
    json_data = json.dumps(data)
    headers = {'Content-Type': 'application/json'}
    # Make the POST request
    response = urequests.post(
        'https://dweet.io/dweet/for/TinyHouse',
        data=json_data,
        headers=headers
    )
    out = response.text
    response.close()
    return out

while True:
    led.on()
    print(postData())
    led.off()
    sleep(60)

Explanations

Still need to add

Testing

If everything is setup correctly you should se a similar output in the Shell

MPY: soft reboot
72
Network config: ('192.168.100.130', '255.255.255.0', '192.168.100.219', '192.168.100.219')
{"this":"succeeded","by":"dweeting","the":"dweet","with":{"thing":"TinyHouse","created":"2024-11-30T05:58:41.887Z","content":{"outdoor_temp":23.5625,"timestamp":"2024-11-30 04:58:33 PM","indoor_temp":24.04688},"transaction":"fe7831d5-2423-4154-a2e8-29480c4130af"}}