Skip to content
Snippets Groups Projects
Pi_Receiver.py 13 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
#!/usr/bin/env python3

'''
    =========================================================================================
 
        CS408 Environmental Monitoring Independent of Existing Infrastructure
        Copyright (C) 2021 Callum Inglis

        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 2 of the License, or
        (at your option) any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License along
        with this program; if not, write to the Free Software Foundation, Inc.,
        51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

        Contact: Callum.Inglis.2018(at)uni.strath.ac.uk

    =========================================================================================

    Libraries Used
        https://github.com/raspberrypi-tw/lora-sx1276

    =========================================================================================
'''

# Usage: python Pi_Receiver.py -f 433 -b BW125 -s 7

from argparse import ArgumentError
import sys 
sys.path.insert(0, '../')     

from time import sleep, time
import json
import requests # pip3 install requests
import random

from SX127x.LoRa import *
from SX127x.LoRaArgumentParser import LoRaArgumentParser
from SX127x.board_config import BOARD
import SX127x.packer as packer

DEBUG = 1
API_URL = "https://emiei.4oh4.co/api"
MAX_TX_RESERVATION_TIME = 2 # Seconds

BOARD.setup()

parser = LoRaArgumentParser("Continous LoRa receiver.")

# Recived from Sensors when they have data to send
class RxHello(object):
    def __init__(self, uid, reservationTime, messageID):
        self.uid = uid
        self.reservationTime = min(reservationTime, MAX_TX_RESERVATION_TIME)
        self.messageID = messageID
        self.gatewayUid = getserial()
        self.okTransmit = False

    def setOK(self, okToTransmit):
        self.okTransmit = okToTransmit

    # Python Object to JSON Object
    def ToJson(self):
        return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True)

class SensorResponse(object):
    def __init__(self, sensorMetadata, data):
        self.sensorMetadata = SensorMetadata(**sensorMetadata)
        self.sensorReading = SensorReading(**data)

    # Python Object to JSON Object
    def ToJson(self):
        return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True)

    def sendToApi(self):
        headers = {'Content-Type': 'Application/json'}
        response = requests.post(API_URL + '/sensor/reading', data=self.ToJson(), headers=headers)#, verify=False) # Using self signed cert for now, sort later!
        if DEBUG > 1:
            print(response) # Ensure 200 Response!
        return

class SensorMetadata(object):
    def __init__(self, uid, messageID, samplePeriod):
        self.uid = uid
        self.messageID = messageID
        self.samplePeriod = samplePeriod
        self.sampleTime = round(time())

class SensorReading(object):
    def __init__(self, ppm, sht, co2):
        self.ppm = PPM(**ppm)
        self.sht = SHT(**sht)
        self.co2 = Co2(**co2)

class PPM(object):
    def __init__(self, p10, p25, p100):
        self.p10 = p10
        self.p25 = p25
        self.p100 = p100

class SHT(object):
    def __init__(self, temperature, humidity):
        self.temperature = temperature
        self.humidity = humidity

class Co2(object):
    def __init__(self, tmp):
        self.tmp = "Coming Soon!"

class Reply():
    ackStatus = False

    def __init__(self, remoteSensorID, replyMsgID):
        self.gatewayUid = getserial()
        self.uid = remoteSensorID
        self.replyMsgID = replyMsgID
        self.txAfterNReadings = None # We may update config of the sensor
        self.pollingFrequency = None # We may update config of the sensor

    def setAckStatus(self, ackStatus):
        self.ackStatus = ackStatus

    def updateConfigTxAfterNReadings(self, _nReadings):
        self.txAfterNReadings = _nReadings

    # Provide value in ms
    def updateConfigPollingFrequency(self, _pollingFrequency):
        self.pollingFrequency = _pollingFrequency

    def ToJson(self):
        return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True)

class LoRaReceiver(LoRa):
    def __init__(self, verbose=False):
        super(LoRaReceiver, self).__init__(verbose)
        self._id = "Base-01"
        self.set_mode(MODE.SLEEP)
        self.set_dio_mapping([0] * 6)
        self.rxHelloQueue = RxHelloQueue() # Track transmission requests

    def on_rx_done(self):
        ###if DEBUG > 1:
        ###    print("\n[+] Rx Done")

        self.clear_irq_flags(RxDone=1)
        payload = self.read_payload(nocheck=True)

        # Parse our data here!
        data = ''.join([chr(c) for c in payload])
        handleData(self.rxHelloQueue, data)

        self.reset_ptr_rx()
        self.set_mode(MODE.RXCONT)

    def on_tx_done(self):
        print("\nTxDone")
        # set RX
        self.set_dio_mapping([0,0,0,0,0,0])    # RX
        sleep(1)
        self.reset_ptr_rx()
        self.set_mode(MODE.RXCONT)
        self.clear_irq_flags(RxDone=1)

    def on_cad_done(self):
        print("\non_CadDone")
        print(self.get_irq_flags())

    def on_rx_timeout(self):
        print("\non_RxTimeout")
        print(self.get_irq_flags())

    def on_valid_header(self):
        print("\non_ValidHeader")
        print(self.get_irq_flags())

    def on_payload_crc_error(self):
        print("\non_PayloadCrcError")
        print(self.get_irq_flags())

    def on_fhss_change_channel(self):
        print("\non_FhssChangeChannel")
        print(self.get_irq_flags())

    def receive(self):
        self.reset_ptr_rx()
        self.set_mode(MODE.RXCONT)
        while True:
            if (self.get_mode() == MODE.TX):
                rssi_value = self.get_rssi_value()
                status = self.get_modem_status()

                # if DEBUG > 1:
                sys.stdout.flush()
                sys.stdout.write("\r%d %d %d" % (rssi_value, status['rx_ongoing'], status['modem_clear']))

    def transmit(self, _payload):
        global args
        self.tx_counter = 0
        self.write_payload(_payload)
        self.set_mode(MODE.TX)


class RxHelloQueue():
    def __init__(self):
        self.reserved = False
        self.sensorID = None
        self.reservedUntil = None


    def hasReservationExpired(self):
        print("Time Now: ", time(), ", Reserved Until: ", self.reservedUntil)
        if self.reserved and self.reservedUntil < time():
            self.reserved = False
            return True
        
        return False


    def canAccept(self):
        self.hasReservationExpired()

        if not self.reserved:
            return True

        # TODO Additional Logic Here!
        return False


    def acceptNew(self, sensorID, reservationDuration):
        if not self.canAccept:
            return False
        
        self.reserved = True
        self.sensorID = sensorID
        self.reservedUntil = time() + reservationDuration + 1

# Source: https://www.raspberrypi-spy.co.uk/2012/09/getting-your-raspberry-pi-serial-number-using-python/
def getserial():
  # Extract serial from cpuinfo file
  cpuserial = "0000000000000000"
  try:
    f = open('/proc/cpuinfo','r')
    for line in f:
      if line[0:6]=='Serial':
        cpuserial = line[10:26]
    f.close()
  except:
    cpuserial = "ERROR000000000"
 
  return cpuserial

# Upon succesfully receiving a message, send back an ack
def ackMsg(sensorResponse):
    data = Reply(sensorResponse.sensorMetadata.uid, sensorResponse.sensorMetadata.messageID)
    data.setAckStatus(True)

    # TODO Fetch config for this sensor from the API here
    # TODO Include Reply config updates here
    data.updateConfigPollingFrequency(10000)
    data.updateConfigTxAfterNReadings(6)

    if DEBUG > 1:
        print(data.ToJson())

    _length, _payload = packer.Pack_Str( data.ToJson() )

    payload = [int(hex(c), 0) for c in _payload]

    sleep(0.2) # Slight delay before transmitting, else too quick for sensor to start listening
    loraReceiver.transmit(payload)
    return

# When a sensor has data to transmit, it will send a TxHello. 
# Pick up that message here and decide if we can accept the message at this time.
def handleRxHello(rxHelloQueue, data):
    timeBegin = time()
    print("\n[?] Check for RX Hello")
    if DEBUG > 1:
        print("\nRaw data: %s" % data)

    try:
        rxHelloJson = json.loads(data)
        rxHello = RxHello(**rxHelloJson)
        print("    RX Hello Confirmed!")

    except Exception as e:
        print("    Not RX Hello")
        return False

    # TODO Check i'm not expecting any methods within rxHello.reservationTime seconds
    if not rxHelloQueue.canAccept():
        rxHello.setOK(False)
        print("[-] RX Hello \"No\" Reply Sent - Reserved by %s for %i seconds" % (rxHelloQueue.sensorID,  rxHelloQueue.reservedUntil - time()))
    
    else:
        rxHelloQueue.acceptNew(rxHello.uid, rxHello.reservationTime)

        # Send "OK" + UID
        rxHello.setOK(True) # We are happy for this sensor to send it's data
        print("[+] RX Hello \"OK\" Reply Sent - %s is permitted to send for %d seconds" % (rxHello.uid, rxHello.reservationTime))
    
    if DEBUG > 1:
       print(rxHello.ToJson())

    _length, _payload = packer.Pack_Str( rxHello.ToJson() ) # Send OK Back
    payload = [int(hex(c), 0) for c in _payload]

    sleep(0.2) # Slight delay before transmitting, else too quick for sensor to start listening
    loraReceiver.transmit(payload)
    sleep(0.2)
    return True


# handle SensorResponse data packet
def handleSensorResponsePacket(data):
    timeBegin = time()
    print("\n[?] Check for Sensor Response Data")
    if DEBUG > 1:
        print("\nRaw data: %s" % data)

    try:
        parsed = json.loads(data)
        p = SensorResponse(**parsed)

    except Exception as e:
        print("[-] Unable to Parse response, ignoring") # TODO Error handling, log increased error rates etc
        if DEBUG > 1:
            print("\tE: %e" % e)
        return False

    print("    Sensor Response Data Confirmed!")
    
    if DEBUG > 1:
        print("    Received Payload: %s" % p.ToJson())

    # TODO Validate response is valid and non-corrupt

    # TODO Ack / process here

    # TODO Transmit ACK
    print("    Sending Sensor Response Ack")
    ackMsg(p)
    print("[+] Sensor Response Ack Sent")

    # TODO Process response?

    # TODO Send To API
    print("    Sending Data to API")
    p.sendToApi()
    print("[+] Data Sent to API")

    sleep(0.2)
    return True



# Parse LoRa response, validate, save / transmit
def handleData(rxHelloQueue, data):
    print("\n===================\n[i] Packet Incoming")

    # Handle TxHello Transmission
    #       Rx - "I've got data to send!" + UID (From Sensor)
    #       If not expecting any messages, continue
    #       Tx - "OK" + UID (Back to Sensor) + TX_Auth_ID
    #
    #       <Do not Tx for 2s>
    if (handleRxHello(rxHelloQueue, data)): # Handled all OK
        return

    # Handle Payload Transmission
    #       Rx - Packet + UID + Tx_Auth_ID (From Sensor)
    #       Handle message, ack / nak appropriately
    #       Tx - Ack + UID + Next_Send_Interval
    #       Tx - Nak + UID
    #
    #       <Do not Tx for 3s>
    if (handleSensorResponsePacket(data)):
        return
        

    # T+3   Tx reserved window expires


# Setup Receiver
loraReceiver = LoRaReceiver(verbose=False)
args = parser.parse_args(loraReceiver)

loraReceiver.set_mode(MODE.STDBY)
loraReceiver.set_pa_config(pa_select=PA_SELECT.PA_BOOST)
#loraReceiver.set_rx_crc(True)
#loraReceiver.set_coding_rate(CODING_RATE.CR4_6)
loraReceiver.set_pa_config(max_power=300, output_power=500) # Default max_power = 10.8dB, output_power  = 4.2dB
#loraReceiver.set_lna_gain(GAIN.G1)
#loraReceiver.set_implicit_header_mode(False)
#loraReceiver.set_low_data_rate_optim(True)
#loraReceiver.set_pa_ramp(PA_RAMP.RAMP_50_us)
#loraReceiver.set_agc_auto_on(True)


print("Power:")
print(loraReceiver.get_pa_config(convert_dBm=True))

# Go Go Go!
print("[+] Receiver & API Gateway")
loraReceiver.set_agc_auto_on(1)
assert(loraReceiver.get_agc_auto_on() == 1)

try:
    loraReceiver.receive()

except KeyboardInterrupt:
    sys.stdout.flush()
    print("")
    sys.stderr.write("KeyboardInterrupt\n")

finally:
    sys.stdout.flush()
    print("")
    loraReceiver.set_mode(MODE.SLEEP)
    BOARD.teardown()