Sam WilliamsAlex Bucknall
Published © GPL3+

Fridge Sensor

This tutorial will help you create a fridge sensor to read the temperature, location, and battery level of a fridge.

IntermediateFull instructions provided2 hours3,278
Fridge Sensor

Things used in this project

Hardware components

SiPy
Pycom SiPy
×1
Pycom Pytrack
×1

Story

Read more

Code

main.py

Python
from pybytes import Pybytes

from onewire import OneWire, DS18X20
from pytrack import Pytrack
from L76GNSS import L76GNSS

from machine import Pin
from pycom import heartbeat,rgbled
from time import sleep

#INITIALISE PYBYTES
USERNAME = "example@email.io"                          
DEVICE_ID = "AABBCCDDEEFFGGHHIIJJKKLLMMNNOO"   
SERVER = "mqtt.pybytes.pycom.io"

pybytes = Pybytes(USERNAME, DEVICE_ID, SERVER)
pybytes.connect_wifi()

#INITIALISE LIBRARIES

py = Pytrack()
gps = L76GNSS(py, timeout=60)


py.setup_sleep(3600)

#INITIALISE VARIABLES

ow = OneWire(Pin('P9'))
temp_sensor = DS18X20(ow)

#FUNCTIONS

def get_temp():                         
    temp_value = None
    while temp_value == None:
        temp_sensor.start_convertion()  
        sleep(1)
        temp_value = temp_sensor.read_temp_async()
        sleep(1)

    return(temp_value)

def get_location():                     
    location = gps.coords()             
    return location

def read_battery():                     
    voltage = py.read_battery_voltage()
    return voltage

def main():
    temp = int(get_temp())
    location = get_location()
    if location != (None,None):
        latitude = location[0]
        longitude = location[1]
    else:
        latitude = 51.23
        longitude = -0.57

    voltPercent = int(((read_battery() - 3.3) / 1.7) * 100)

    pybytes.send_virtual_pin_value(False, 16, int(voltPercent))
    pybytes.send_virtual_pin_value(False, 15, int(temp))
    pybytes.send_custom_location(12, str(latitude), str(longitude))

    py.go_to_sleep()

main()

Credits

Sam Williams

Sam Williams

1 project • 3 followers
Alex Bucknall

Alex Bucknall

3 projects • 24 followers
Senior Engineer @ Arribada | Software / Hardware | PhD in Reconfigurable Computing

Comments