Smart Cities - Tiny House at Home (Alternate way)
Required Items
- Raspberry pi pico wh (wireless with headers)
- OneWire temperature sensor
- Other sensors:
- Breadboard to join sensors + required cables/jumper wires
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
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 (there are two)
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_sensor2 = ds18x20.DS18X20(onewire.OneWire(Pin(12))) #Find Onewire Temp Sensor
onewire_scan = []
onewire_scan.append(onewire_sensor.scan()) #[0] = onewire_sensor.scan() #Assign Onewire Temp Sensor
onewire_scan.append(onewire_sensor2.scan()) #[1] = onewire_sensor2.scan() #Assign Onewire Temp Sensor
print(onewire_scan)
# 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][0]) # Pass the first ROM address in the list
return outdoor_temp
elif whichOne == 1:
return tempSensor.readTempC()
elif whichOne == 2:
onewire_sensor2.convert_temp() #Gets Temperature
sleep(1) #Allow time for temp conversion
outdoor_temp2 = onewire_sensor2.read_temp(onewire_scan[1][0]) # Pass the first ROM address in the list
return outdoor_temp2
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(),
'outdoor_temp2': getData(2)
}
# 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
1. Initial Setup:
- The code starts by setting up three sensors:
- An LED light that can be turned on and off
- A real-time clock (RTC) to keep track of time
- Two temperature sensors: one precision sensor inside (TMP117) and one waterproof sensor outside (DS18X20)
2. WiFi Connection:
- The code tries to connect to WiFi using a username (ssid) and password
- If it can't connect within 20 seconds, it will restart the device
- Once connected, it shows the network information
3. getData Function:
- This function can read from either temperature sensor
- If you call it with 0, it reads the outdoor temperature
- If you call it with 1, it reads the indoor temperature
- If you call it with 2, it reads the second outdoor temperature sensor
- If you use any other number, it gives an error message
4. postData Function:
- This function collects four pieces of information:
- The outdoor temperature
- The indoor temperature
- The second outdoor temperature
- The current time from the clock
- It packages this information together
- It sends this data to a website called "dweet.io" where it can be stored and viewed
- The data is sent to a specific channel called "TinyHouse"
5. Main Loop:
- The program runs forever in a loop that:
- Turns the LED on
- Sends the temperature data
- Turns the LED off
- Waits for 60 seconds (1 minute)
- Repeats
Think of it like a weather station that:
- Takes temperature readings from inside and outside your house
- Adds a timestamp to know when the readings were taken
- Sends this information to the internet every minute
- Turns an LED on when it is sending data, so you know it's working
- Turns the LED off when it has finished sending data
Testing
If everything is setup correctly you should se a similar output in the Shell
MPY: soft reboot
72
Network config: ('192.168.*.*', '255.255.0.0', '192.168.*.*', '192.168.*.*')
{"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"}}




