Rx function moved into the CC1101, RAM savings implemented

This commit is contained in:
2020-11-04 14:19:53 +01:00
parent 553778740d
commit fb7f14deb7
7 changed files with 107 additions and 94 deletions

BIN
WiFi.mpy

Binary file not shown.

28
WiFi.py
View File

@@ -1,20 +1,22 @@
import network
import utime
_DEBUG = False
try:
_DEBUG = DEBUG
except:
_DEBUG = False
class NetSet(object):
def __init__(self, wifi_type):
self.sta = network.WLAN(network.STA_IF)
self.ap = network.WLAN(network.AP_IF)
if wifi_type == 'infra':
if _DEBUG:
print("Disabling AP")
print(b"Disabling AP")
self.ap.active(False)
if _DEBUG:
print("Activating INFRA")
print(b"Activating INFRA")
self.sta.active(True)
self.sta.isconnected() # False, it should be # Comments by Yoda
self._SSID = None
@@ -22,10 +24,10 @@ class NetSet(object):
self._TIMEOUT = None
elif wifi_type == 'ap':
if _DEBUG:
print("Disabling INFRA")
print(b"Disabling INFRA")
self.ap.active(True)
if _DEBUG:
print("Activating AP")
print(b"Activating AP")
self.sta.active(False)
self.sta.isconnected() # False, it should be # Comments by Yoda
self._SSID = None
@@ -34,17 +36,17 @@ class NetSet(object):
def connectInfraGo(self, _timeout):
if _DEBUG:
print("Connecting to infra")
print(b"Connecting to infra")
self.sta.connect(self._SSID, self._PASS)
if _DEBUG:
print("Let's wait for the network to come up")
print(b"Let's wait for the network to come up")
while not (self.sta.isconnected()):
if _timeout > 0:
print("Trying... {} more times".format(_timeout))
print(b"Trying... {} more times".format(_timeout))
utime.sleep_ms(1001)
_timeout -= 1
else:
print("Out of retrys")
print(b"Out of retrys")
return False
network_config = self.sta.ifconfig()
return network_config
@@ -64,7 +66,7 @@ class NetSet(object):
else:
return False
except Exception as e:
print("ERROR: Network configuration failed with: {}".format(e))
print(b"ERROR: Network configuration failed with: {}".format(e))
return False
def connectAp(self):
@@ -88,10 +90,10 @@ class NetSet(object):
print(b"WARNING: Fucked up option, make it better")
except Exception as e:
if _DEBUG:
print("WARNING: Errors in INFRA config, still going for AP")
print(b"WARNING: Errors in INFRA config, still going for AP")
return False
if _DEBUG:
print("CONFIG DICT: {}".format(self.config_dict))
print(b"CONFIG DICT: {}".format(self.config_dict))
self._SSID = self.config_dict['_SSID']
self._PASS = self.config_dict['_PASS']
self._TIMEOUT = int(self.config_dict['_TIMEOUT'])

Binary file not shown.

View File

@@ -12,7 +12,6 @@ gc.collect()
class CC1101(object):
def __init__(self):
self.debug = 1
self.hspi = machine.SPI(1, baudrate=600000, polarity=0, phase=0)
self.PA_TABLE = [0xC0, 0xC0, 0xC0, 0xC0,
0xC0, 0xC0, 0xC0, 0xC0]
@@ -21,7 +20,7 @@ class CC1101(object):
self.FREQ_1 = [0x62, 0x65, 0x67, 0x64, 0x66]
self.FREQ_0 = [0xE2, 0x40, 0x9D, 0x11, 0x6F]
self.CRC_TABLE = [
self._CRC_TABLE = [
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
@@ -54,11 +53,11 @@ class CC1101(object):
0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0]
self.BUFFER_SIZE = 16
#self.BUFFER_SIZE = 16
self.DAVIS_PACKET_LENGTH = 10
self.rxBuffer = []
self.rxBufferIndex = 0
self.rxBufferLength = 0
#self.rxBuffer = []
#self.rxBufferIndex = 0
#self.rxBufferLength = 0
self.hopIndex = 0
self.freqComp = [0, 0, 0, 0, 0]
@@ -248,10 +247,11 @@ class CC1101(object):
addr = regAddr | self.READ_BURST
ss.off()
self.hspi.write(bytes([addr]))
rd_buffer = []
for i in range(0, size):
rd_result = self.hspi.read(1, 0x00)
rd_buffer.append(hex(rd_result[0]))
rd_buffer = bytearray(size)
#for i in range(0, size):
# rd_result = self.hspi.read(1, 0x00)
# rd_buffer.append(hex(rd_result[0]))
self.hspi.readinto(rd_buffer)
ss.on()
return rd_buffer
@@ -344,8 +344,8 @@ class CC1101(object):
self.sidle()
self.cmdStrobe(self.CC1101_SFRX) # Flush Rx FIFO
self.cmdStrobe(self.CC1101_SFTX) # Flush Tx FIFO
self.rxBuffer = [0x00] * self.BUFFER_SIZE
self.rxBufferLength = self.rxBufferIndex = 0
#self.rxBuffer = [0x00] * self.BUFFER_SIZE
#self.rxBufferLength = self.rxBufferIndex = 0
def rx(self):
self.cmdStrobe(self.CC1101_SRX)
@@ -373,8 +373,7 @@ class CC1101(object):
def calcCrc(self, _buffer):
crc = 0x0000
for i in range(0, len(_buffer)):
crc = ((crc << 8) ^ self.CRC_TABLE[(crc >> 8) ^ (_buffer[i])]) % 65536
# DEBUG print("CRC: {}".format(crc))
crc = ((crc << 8) ^ self._CRC_TABLE[(crc >> 8) ^ (_buffer[i])]) % 65536
return crc
def readRssi(self):
@@ -395,3 +394,41 @@ class CC1101(object):
else:
error = value >> 1
return error
def reverseBits(self, data):
data = "{:08b}".format(data)
z = ""
for i in range(len(data),0,-1):
z = z + (data[i-1])
return int(z, 2)
def rxPacket(self):
data_length = self.readRegister(self.CC1101_RXBYTES)
#data = ""
if data_length & 0x7f == 15:
data = bytearray(self.DAVIS_PACKET_LENGTH)
data = self.readBurst(self.CC1101_RXFIFO, self.DAVIS_PACKET_LENGTH)
self.rssi = self.readRssi()
self.lqi = self.readLQI()
freqEst = self.readStatus(self.CC1101_FREQEST)
freqError = self.calcFreqError(freqEst)
if DEBUG:
print(b"FERROR: {} (EST: {})".format(freqError, freqEst))
print(b"FCOMP: {}".format(davis.freqComp))
if self.freqComp[self.hopIndex] + freqEst <= 255:
self.freqComp[self.hopIndex] = self.freqComp[self.hopIndex] + freqEst
else:
self.freqComp[self.hopIndex] = 255
self.flush()
self.hop()
self.rx()
data_int = [self.reverseBits(int(item)) for item in data]
crc = self.calcCrc(data_int[:8])
if crc != 0x0000:
print(b"Corrupt data CRC: {}".format(crc))
return False
else:
print(b"Data OK, CRC: {}".format(crc))
return data_int
else:
return False

Binary file not shown.

View File

@@ -1,22 +1,24 @@
import urequests
import machine
_DEBUG = False
try:
_DEBUG = DEBUG
except:
_DEBUG = False
def send_to_influx(host, port, db, user, password, davis_unit_id, wind, measurement, name, value, tags):
post = "http://{}:{}/write?db={}".format(host, port, db)
if _DEBUG:
print("SENDING TO: {}".format(post))
print(b"SENDING TO: {}".format(post))
if measurement is False:
return (False, "ERROR measurement set False")
return (False, b"ERROR measurement set False")
if measurement is None:
data = "wind,type=speed,davis_id={_davis_id} value={_speed}\n wind,type=direction,davis_id={_davis_id} value={_direction}".format(
_davis_id = davis_unit_id,
_speed=wind['speed'],
_direction=wind['direction'])
if _DEBUG:
print("SEND WIND only: {}")
print(b"SEND WIND only: {}")
else:
for tag in tags.keys():
measurement = "{},{}={}".format(measurement, tag, tags[tag])
@@ -28,32 +30,32 @@ def send_to_influx(host, port, db, user, password, davis_unit_id, wind, measurem
_speed=wind['speed'],
_direction=wind['direction'])
if _DEBUG:
print("POST_DATA: {}".format(data))
print(b"POST_DATA: {}".format(data))
try:
return (True, urequests.post(post, data=data))
except Exception as e:
if e.args[0] == 103:
machine.reset()
else:
return (False, "ERROR sending data to influx: {}".format(e))
return (False, b"ERROR sending data to influx: {}".format(e))
def raw_send_to_influx(host, port, db, user, password, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, rssi, lqi):
post = "http://{}:{}/write?db={}".format(host, port, db)
if _DEBUG:
print("SENDING TO: {}".format(post))
print(b"SENDING TO: {}".format(post))
data = "data b0={_b0},b1={_b1},b2={_b2},b3={_b3},b4={_b4},b5={_b5},b6={_b6},b7={_b7},b8={_b8},b9={_b9},rssi={_rssi},lqi={_lqi}".format(
_b0=b0, _b1=b1, _b2=b2, _b3=b3,
_b4=b4, _b5=b5, _b6=b6, _b7=b7,
_b8=b8, _b9=b9, _rssi=rssi, _lqi=lqi)
if _DEBUG:
print("POST_DATA: {}".format(data))
print(b"POST_DATA: {}".format(data))
try:
return (True, urequests.post(post, data=data))
except Exception as e:
if e.args[0] == 103:
machine.reset()
else:
return (False, "ERROR sending RAW data to influx: {}".format(e))
return (False, b"ERROR sending RAW data to influx: {}".format(e))
def reverseBits(data):
data = "{:08b}".format(data)
@@ -65,7 +67,7 @@ def reverseBits(data):
class davisDecoder(object):
def __init__(self, weather_db, stat_db, raw_db):
__name__ = u'Davis value decoder class'
__name__ = 'Davis value decoder class'
self.weather_influx_db = weather_db
self.stat_influx_db = stat_db
self.raw_influx_db = raw_db

84
main.py
View File

@@ -5,20 +5,7 @@ import WiFi
import machine
gc.collect()
#_SSID = 'BastArt'
#_PASS = '3 litry Kvasaru!'
#_TIMEOUT = 15
_DEBUG = True
#
#_INFLUX_HOST = '192.168.1.2'
#_INFLUX_PORT = '8086'
#_INFLUX_USER = 'ventil'
#_INFLUX_PASS = '3 litry Kvasaru!'
#
#_INF_DB_WEATHER = 'weather'
#_INF_DB_STATUS = 'status'
#_INF_DB_RAW = 'raw'
DEBUG = True
wifi_con = WiFi.NetSet('infra')
wifi_con.readNetworkConfig()
@@ -27,9 +14,11 @@ ips = wifi_con.connectInfra(
wifi_con._PASS,
wifi_con._TIMEOUT)
if _DEBUG:
if DEBUG:
print("IPCONF: {}".format(ips))
gc.collect()
davis = cc1101_davis.CC1101()
davis.setRegisters()
davis.setFrequency(davis.hopIndex)
@@ -38,38 +27,21 @@ decoder = davis_decode.davisDecoder(
wifi_con._INF_DB_STATUS,
wifi_con._INF_DB_RAW)
gc.collect()
# Main receive loop
interpacket_time = 0
while True:
data_length = davis.readRegister(davis.CC1101_RXBYTES)
data = ""
if data_length & 0x7f == 15:
data = davis.readBurst(davis.CC1101_RXFIFO, 10)
rssi = davis.readRssi()
lqi = davis.readLQI()
freqEst = davis.readStatus(davis.CC1101_FREQEST)
freqError = davis.calcFreqError(freqEst)
if _DEBUG:
print("FERROR: {} (EST: {})".format(freqError, freqEst))
print("FCOMP: {}".format(davis.freqComp))
if davis.freqComp[davis.hopIndex] + freqEst <= 255:
davis.freqComp[davis.hopIndex] = davis.freqComp[davis.hopIndex] + freqEst
else:
davis.freqComp[davis.hopIndex] = 255
hop = davis.hopIndex
davis.flush()
davis.hop()
davis.rx()
data_int = [davis_decode.reverseBits(int(item)) for item in data]
crc = davis.calcCrc(data_int[:8])
if crc != 0x0000:
print("Corrupt data CRC: {}".format(crc))
continue
else:
print("Data OK, CRC: {}".format(crc))
try:
data_int = davis.rxPacket()
except Exception as e:
raise e
print(b"Rx EXCEPTION: {}".format(e))
continue
if data_int:
header = decoder.davis_id(data_int[0])
decoder.DecodePacket(data_int)
data_prn = "{:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5}".format(
data_prn = b"{:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5}".format(
data_int[0],
data_int[1],
data_int[2],
@@ -80,15 +52,15 @@ while True:
data_int[7],
data_int[8],
data_int[9])
print("{_data:60} HOP: {_hop:<5} RSSI: {_rssi:<5} LQI: {_lqi:<5} {_last}s since".format(
_rssi=rssi,
_hop=hop,
print(b"{_data:60} HOP: {_hop:<5} RSSI: {_rssi:<5} LQI: {_lqi:<5} {_last}s since".format(
_rssi=davis.rssi,
_hop=davis.hopIndex,
_data=data_prn,
_lqi=lqi,
_lqi=davis.lqi,
_last=interpacket_time / 10))
if _DEBUG:
print("Header: {} Wind: {}".format(header, decoder.wind))
print("{}: {}/{} ({})".format(
if DEBUG:
print(b"Header: {} Wind: {}".format(header, decoder.wind))
print(b"{}: {}/{} ({})".format(
decoder.measurement,
decoder.name,
decoder.value,
@@ -110,7 +82,7 @@ while True:
decoder.value,
decoder.tags)
except Exception as e:
print("ERROR: Data send 'urequest': {}".format(e))
print(b"ERROR: Data send 'urequest': {}".format(e))
try:
(raw_sent_ok, raw_data_sent) = davis_decode.raw_send_to_influx(
wifi_con._INFLUX_HOST,
@@ -128,16 +100,16 @@ while True:
data_int[7],
data_int[8],
data_int[9],
rssi,
lqi)
davis.rssi,
davis.lqi)
except Exception as e:
print("ERROR: Data send 'urequest': {}".format(e))
print(b"ERROR: Data send 'urequest': {}".format(e))
if _DEBUG:
if DEBUG:
if sent_ok:
print("DATA SEND: {}".format(data_sent.status_code))
print(b"DATA SEND: {}".format(data_sent.status_code))
else:
print("DATA SEND FAIL: {}".format(data_sent))
print(b"DATA SEND FAIL: {}".format(data_sent))
interpacket_time = 0
else:
interpacket_time += 1