# If you ever want to make this code better, please do
# but for the love of God, please save this code first.
# It is written in circuitpython to prevent the need
# to compile code, and the RFM69 does not work with
# micropython.

import board
import analogio
import digitalio
from time import sleep
import math
import adafruit_rfm69
import busio

RoomNum = 10 # What room number is this in
SenderRoom = 10 # What room number reports to the receiver
RoomQuant = 12 # How many rooms are there
RecAddr = 254 # What's the address of the receiver
Radio_Freq_Mhz = 915.0 # What's the frequency of the radio
RetryAmount = 3 # How many times to retry sending the packet
min_okay = 12493 # Approx 20 deg C
max_okay = 9571 # Approx 26 deg C
led = digitalio.DigitalInOut(board.GP25) # Onboard LED
led.direction = digitalio.Direction.OUTPUT
CS = digitalio.DigitalInOut(board.GP13) # Pin 17
CS.direction = digitalio.Direction.OUTPUT
RESET = digitalio.DigitalInOut(board.GP6) # Pin 9
RESET.direction = digitalio.Direction.OUTPUT
SCK = board.GP10 # Pin 14
MOSI = board.GP11 # Pin 15
MISO = board.GP12 # Pin 16
spi = busio.SPI(SCK, MOSI, MISO)
rfm69 = adafruit_rfm69.RFM69(spi, CS, RESET, Radio_Freq_Mhz, baudrate=1000000)
#rfm69.reset()
rfm69.xmit_timeout = 10.0
#rfm69.freq_mhz = Radio_Freq_Mhz
rfm69.tx_power = 20
rfm69.node = RoomNum
rfm69.destination = RecAddr
#rfm69.encryption_key = (b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')

adcpin = board.GP26 # Pin 31
thermistor = analogio.AnalogIn(adcpin)

resend_time = 1

def ask_all(radio):
send_packet = ''
temp_list = []
room_packet = ''
for a in range(RoomQuant):
temp_list.append('')
for b in range(RoomQuant):
digitnum = str(b + 1)
if len(digitnum) == 1:
digitnum = '0' + str(b + 1)
if not int(digitnum) == RoomNum:
radio.send(bytes('query','utf-8'), destination = int(digitnum))
sleep(.5)
room_packet = radio.receive(timeout = 1.0)
if room_packet == None:
room_packet = str('0')
else:
room_packet = temp_retrieve()
temp_list[b] = digitnum+room_packet
for c in range(RoomQuant):
send_packet = send_packet + temp_list[c]
return send_packet

def temp_retrieve():
#Voltage dividor
Vin = 3.3 # Pin 35
Ro = 7500 # 10k resistor 10000 was initial setting, changed for calibration

#Steinhart constants
A = 0.00110683
B = 0.000238464
C = 0.00000006507394

# Get voltage value from ADC
adc = thermistor.value
print(adc)
Vout = (Vin/65535)*adc

# Calculate resistance
Rt = round((Vout * Ro) / (Vin - Vout),0)
# Rt = 10000 # Used for testing. Setting Rt to 10k equates approx 25C

# Steinhart - Hart equation
TempK = 1 / (A + (B * math.log(Rt)) + C * math.pow(math.log(Rt), 3))

# Convert from K to C
TempC = TempK - 273.15
TempF = (TempC * (9/5)) + 32
print(Rt, TempC, TempF)
return temp_check(Rt)

def temp_check(digis):
if digis < max_okay:
return '1'
elif digis >= max_okay and digis < min_okay:
return '2'
elif digis >= min_okay:
return '3'

temp_retrieve()
while True:
led.value = True
if SenderRoom == RoomNum:
tx_packet = ask_all(rfm69)
print(tx_packet)
for x in range(RetryAmount):
rfm69.send(bytes(tx_packet, 'utf-8'))
sleep(5)
else:
rx_packet = rfm69.receive(timeout = 10, keep_listening = False)
if not rx_packet == None:
tx_packet = temp_retrieve()
print(tx_packet)
sleep(.2)
led.value = False


Click the image for the full-sized version