Temperature sensor: Difference between revisions
Jump to navigation
Jump to search
| Line 257: | Line 257: | ||
=== pymakr.conf === | === pymakr.conf === | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
{ | |||
"address": "COM10", | |||
"username": "micro", | |||
"password": "python", | |||
"sync_folder": "", | |||
"sync_file_types": [ | |||
"py", | |||
"txt", | |||
"log", | |||
"json", | |||
"xml", | |||
"html", | |||
"js", | |||
"css", | |||
"mpy", | |||
"pem", | |||
"cet", | |||
"crt", | |||
"key" | |||
], | |||
"sync_all_file_types": false, | |||
"open_on_start": true, | |||
"safe_boot_on_upload": false, | |||
"py_ignore": [], | |||
"fast_upload": false | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 19:33, 1 January 2022
Temperature sensor
Applications
Temperature is one of the easiest sensor projects to start with. Applications include:
- Monitoring the temperature of a smart bee hive, rabbit hutch, chick coop, compost heap, worm farm, aquarium or wetland.
- Experimenting with concepts related to energy efficient building construction.
- Temperature compensation for other sensors (sensor values may change with changes in temperature).
- Making a weather station.
Types of temperature sensors
The are two common temperature sensors that are used.
- An internal temperature and humidity sensor is the DHT22. This is included in all sensor project to monitor temperature and humidity conditions within the sensor housing. If the temperature gets too hot or too cold within the sensor housing this may shorten the life of the equipment and the batteries.
- An external temperatures sensor is the DS18B20. External sensors are typically water proof and have a longer cable. They can be used to monitor a range of interesting discovery learning projects.
DHT22 Internal temperature sensor
DS18B20 External temperature sensor
Software installation requirements
Parts list
| Item description | Number | Supplier | URL | Cost | Notes |
|---|---|---|---|---|---|
| 10K resistor | 2 | Core Electronics | TBA | $10 | Get pack of 10 |
| 10K resistor | 2 | Core Electronics | TBA | $10 | Get pack of 10 |
| 10K resistor | 2 | Core Electronics | TBA | $10 | Get pack of 10 |
Electronic circuit construction
- Atom with Pymakr package installed
MicroPython code for Pycom LoPy4 microcontroller
- There are several different code segments used on the Pycom LoPy4 microcontroller to monitor temperature.
- boot.py - bootstrap program that runs first.
- main.py - main program where program code is contained
- pymakr.conf - configuration file for Atom and Pymakr package
- lib - directory that contains additional library files
- config.py - list of static variables
- dth.py - library for DHT22 internal temperature sensor
- lora.py - library for LoRa. Contains private keys
- onewire.py - onewire library to communicate with DS18B20 external temperature sensor
boot.py
# boot.py -- run on boot-up
import pycom
from machine import UART
import machine
import os
from network import Bluetooth
from network import WLAN
if pycom.heartbeat() == True:
pycom.heartbeat(False)
if pycom.wifi_on_boot() == True:
pycom.wifi_on_boot(False)
#wlan = WLAN()
#wlan.deinit()
bluetooth = Bluetooth()
bluetooth.deinit()
uart = UART(0, baudrate=115200)
os.dupterm(uart)
machine.main('main.py')
main.py
# main.py
import binascii #module that makes conversion between binary and Ascii
import config
from dth import DTH
import gc # garbage collection
from lora import LoraAU915
import machine
from machine import Pin
from machine import ADC
from onewire import DS18X20
from onewire import OneWire
import pycom
import network
import socket
import time
import ustruct
import utime
#INIT EVERYTHING
sleep_duration = 600 # in seconds (10 minutes)
pycom.rgbled(config.GREEN)
# power up sensors
sensor_power = Pin(config.POWER_PIN, mode=Pin.OUT) # power for all sensors
sensor_power.value(1) # power up
time.sleep(2) # wait 2 seconds to stabilise
print("starting main")
pycom.rgbled(config.OFF)
# voltage divider setup to monitor battery voltage
adc = ADC()
time.sleep(2) # wait 2 seconds to stabilise
# garbage collection enabled
gc.enable()
# config LoRa communications
s = LoraAU915.setupLora()
####################################
### LOOP IF NO NEED TO SLEEP #######
####################################
while True:
sensor_power.value(1) # power up
time.sleep(1)
pycom.rgbled(config.ORANGE)
time.sleep(1)
pycom.rgbled(config.OFF)
# TEMPERATURE AND HUMIDITY OF AMBIENT AIR
j=0
th = DTH(config.DHT22_DATA,1)
time.sleep(1)
while j < 5:
result = th.read()
if result.temperature != 0:
print("temperature is not zero - breaking from while loop")
break
time.sleep(1)
j += 1
print("Error code %d" % result.error_code)
print(j)
print("Temperature = = %.2f C" % result.temperature)
print("Humidity = %.2f %%" % result.humidity)
print("Error code final %d" % result.error_code)
# BATTERY
meanbatt=0
j=0
while j < 5:
utime.sleep(0.2)
batt = adc.channel(attn=3, pin=config.VOLTMETER)
meanbatt += batt.voltage()/1000
j += 1
batt_volt = (meanbatt/j)*config.BATCOEFF #multiplcator coef to adjust the real value of the battery voltage
print("Battery voltage = %.2f V" % batt_volt)
#DS18B20 TEMPERATURE SENSOR
#https://docs.pycom.io/tutorials/hardware/owd/#app
#Also note - needed to add a 10k resistor pull up on the board
#Internal pull up resistor was not sufficient
#used this reference for Raspberry Pi circuit which includes 5k to 10k pull up resistor
#https://www.circuitbasics.com/raspberry-pi-ds18b20-temperature-sensor-tutorial/
ow = OneWire(Pin(config.DS18B20_DATA))
temp = DS18X20(ow)
soil_temp = temp.start_conversion()
time.sleep(1)
soil_temp = temp.read_temp_async()
time.sleep(1)
if soil_temp != None:
print("Soil temperature = %.2f C" % soil_temp)
else:
print("No data received from soil temp probe")
pycom.rgbled(config.RED)
time.sleep(1)
pycom.rgbled(config.OFF)
soil_temp = 0.0
soil_temp_t = int(soil_temp*100)
print(soil_temp_t)
batt_volt_t = int(batt_volt*100)
print(batt_volt_t)
sens_temp = int(result.temperature*100)
print(sens_temp)
sens_humi = int(result.humidity*100)
print(sens_humi)
# converting 2 byte values into single bytes for transmission
soil1 = int(soil_temp_t//256)
soil2 = int(soil_temp_t%256)
batt1 = int(batt_volt_t//256)
batt2 = int(batt_volt_t%256)
temp1 = int(sens_temp//256)
temp2 = int(sens_temp%256)
humi1 = int(sens_humi//256)
humi2 = int(sens_humi%256)
print("sending to TTN")
s.setblocking(True)
print(soil1,soil2,batt1,batt2,temp1,temp2,humi1,humi2)
s.send(bytes([soil1,soil2,batt1,batt2,temp1,temp2,humi1,humi2]))
s.setblocking(False)
# Save LoRa settings before deep sleep
LoraAU915.saveLora()
print("going to sleep")
sensor_power.value(0) # power down
# note that batt_volt has been multiplied by 100 for transmission
if (batt_volt > 4.0):
# to minimise writes to onboard memory and for teaching
sleep_duration = 600 # sleep for 10 minutes
print("Time delay sleep only")
time.sleep(sleep_duration) # sleep in seconds
else:
sleep_duration = 3600 # sleep for 60 minutes
print("Sleep using deepsleep")
machine.deepsleep(1000 * sleep_duration) # deepsleep in ms
pymakr.conf
{
"address": "COM10",
"username": "micro",
"password": "python",
"sync_folder": "",
"sync_file_types": [
"py",
"txt",
"log",
"json",
"xml",
"html",
"js",
"css",
"mpy",
"pem",
"cet",
"crt",
"key"
],
"sync_all_file_types": false,
"open_on_start": true,
"safe_boot_on_upload": false,
"py_ignore": [],
"fast_upload": false
}