#!/usr/local/bin/python import board import busio import digitalio import logging from flask import Flask import adafruit_rfm69 class LoRa: __SIGNAL_FREQUENCY = 915.0 __ENCRYPTION_KEY = b"\x01\x01\x01\x01\x01\x01\x01\x01\x02\x02\x02\x02\x02\x02\x02\x02" __PINS = { 'miso': board.MISO, 'mosi': board.MOSI, 'sck': board.SCK, 'cs': board.D22, 'rst': board.D27 } __PACKET_WAIT = 2 __MAX_ATTEMPTS = 5 def __init__(self): spi = busio.SPI(self.__PINS['sck'], MOSI=self.__PINS['mosi'], MISO=self.__PINS['miso']) cs = digitalio.DigitalInOut(self.__PINS['cs']) rst = digitalio.DigitalInOut(self.__PINS['rst']) self.lora = adafruit_rfm69.RFM69(spi, cs, rst, self.__SIGNAL_FREQUENCY) self.lora.encryption_key = self.__ENCRYPTION_KEY logging.debug(f'Init\'d LoRa board with freq: {self.lora.frequency_mhz}, bitrate: {self.lora.bitrate / 1000} kbit/s, f. deviation: {self.lora.frequency_deviation/1000} khz, and encryption key: {self.lora.encryption_key}') def receive_message(self): logging.debug('Waiting for message...') packets = self.lora.receive(timeout=self.__PACKET_WAIT) if packets: logging.debug(f'Got packets: {packets}') return packets return False def send_message(self, message): logging.debug(f'Sending message: {message}') packets = bytes(message, 'utf-8') self.lora.send(packets) def send_and_wait(self, message): attempt = 0 max_attempts = self.__MAX_ATTEMPTS waiting_for_response = True response = None while waiting_for_response: logging.debug(f'Attempt {attempt}...') attempt += 1 self.send_message(message) response = self.receive_message() if attempt > max_attempts or response: waiting_for_response = False return response class Master: def __init__(self): self.com = LoRa() logging.info('Pining slave') rsp = self.com.send_and_wait('ping|') print(f'Slave is {rsp}') def get_temp(self): temp = self.com.send_and_wait('iot_g_temp|') logging.info(f'Got temp from slave: {temp}ÂșC') if temp: return float("{:.1f}".format(float(temp.decode()))) return 0 def get_hmdt(self): hmdt = self.com.send_and_wait('iot_g_hmdt|') logging.info(f'Got hmdt from slave: {hmdt}') if hmdt: return int(hmdt.decode()) return 0 def run_pump(self, volume): status = self.com.send_and_wait(f'iot_pmp_ctrl|{str(volume)}') logging.info(status) return status.decode() if __name__ == "__main__": logging.root.setLevel(logging.DEBUG) master = Master() node = Flask(__name__) prev_temp = master.get_temp() prev_hmdt = master.get_hmdt() @node.route("/") def root(): return "IoT-ICL DE Weather Master Node running..." @node.route("/hmdt") def humidity(): global prev_hmdt humidity = master.get_hmdt() if humidity: prev_hmdt = humidity return {'success': True, 'value': humidity} return {'success': False, 'value': prev_hmdt} @node.route("/temp") def temp(): global prev_temp temp = master.get_temp() if temp: prev_temp = temp return {'success': True, 'value': temp} return {'success': False, 'value': prev_temp} node.run(host='0.0.0.0', port='3333', use_reloader=False)