def ReadUSBData(CSVPath,ClientID,client): # Read serial data from USB port # # Define the PicAxe Divisors DivisorDict = dict.fromkeys(string.ascii_uppercase) for key in DivisorDict : DivisorDict[key] = 1 DivisorDict['A'] = 10 # Soil Moisture DivisorDict['B'] = 10 # Temperature DivisorDict['S'] = 10 # Kihi-02 Moisture DivisorDict['T'] = 10 # Kihi-02 Temperature USBport = 'USB0' if (USBport in PiSerial() ): DoRead = True logging.info("USB port found: "+ USBport ) else: Message = "Error looking for "+USBport CSV_Message = Message DoRead = ProcessError(CSVPath, ClientID, '', CSV_Message, Message) # Will try to read (twice) data anyway SerialDetails = { "DeviceName": "/dev/tty"+USBport, "ModuleType": "DRF126x", "BAUDrate": "2400", } # Could also include other parameters # parity = serial.PARITY_NONE, # stopbits = serial.STOPBITS_ONE, # bytesize = serial.EIGHTBITS, # Default location of serial port on Pi models 3 and Zero # SERIAL_PORT = "/dev/ttyS0" # Other serial ports are "/dev/ttyAMA0" & "/dev/serial0" # ModuleType can be Dorji DRF126x or DRF127x while DoRead : try : Value = GetSerialData(CSVPath,ClientID,SerialDetails) # logging.info("Serial Loop: %s", Value) Status = Value["Status"] if Status == 0 : Error = "Invalid_Read" Save2CSV (CSVPath, ClientID, 'Error', Error) else : # Status is OK, so write the data ... Error = Value["Error"] Channel = Value["Channel"] Data = Value["Data"] ClientID = Value["ClientID"] Save2CSV (CSVPath, ClientID, Channel, Data) Save2Cayenne (client, Channel, Data, DivisorDict[Channel]) Save2Cayenne (client, 'Stat', Status, 1) except : Message = "Exception reading LoRa Data from "+USBport CSV_Message = Message DoRead = ProcessError(CSVPath, ClientID, '', CSV_Message, Message)
from MQTTUtils import PiSerial serialDevices = PiSerial() print('Onboard port is:'+serialDevices['Onboard'] ) print('USB port(s):',end='') for Device in serialDevices.keys() : if 'USB' in Device: print(Device,end=',') print()
def GetSerialData(CSVPath,ClientID) : import struct import serial from MQTTUtils import ProcessError from MQTTUtils import PiSerial Eq = ' = ' CrLf = '\r\n' Qt = '"' # Variables specfic to reading sensor data DRF126x = False # must be DRF127x # DRF126x = True HEADIN = b':'b'0' # Set up the serial port. if ('USB0' in PiSerial() ): SERIAL_PORT = "/dev/ttyUSB0" else: SERIAL_PORT = "/dev/serial0" # SERIAL_PORT = "/dev/ttyAMA0" # Default location of serial port on Pi models 3 and Zero # SERIAL_PORT = "/dev/ttyS0" BAUDRATE=2400 # These values appear to be the defaults # parity = serial.PARITY_NONE, # stopbits = serial.STOPBITS_ONE, # bytesize = serial.EIGHTBITS, with serial.Serial(SERIAL_PORT, BAUDRATE) as ser: Sync = ser.read_until(HEADIN) if not(Sync==HEADIN): print( "Extra Sync text!", Sync, "**************") Save2Cayenne (client, 'Stat', 1, 1) Save2CSV (CSVPath, CayenneParam.get('CayClientID'), 'Sync-Error', Sync) PacketIn = ser.read(5) print( PacketIn, len(PacketIn), 'l' ) Device,Channel,Data,Cks=struct.unpack("<ccHB",PacketIn) if DRF126x : RSSI = ser.read(1) else: RSSI = 0 # Checksum processing CksTest = 0 for byte in PacketIn[0:5]: CksTest = CksTest ^ byte print(Device, Channel, Data, Cks, "RSSI = ", RSSI) Channel = str(Channel,'ASCII') SerialData = { "Device" : Device, "Channel" : Channel, "Data" : Data, "RSSI" : RSSI, "Status" : 1, "ClientID" : ClientID, "Error" : PacketIn, } # raise Exception('Test exception at line 96 of SensorLib.py') if CksTest == 0: print( 'Checksum correct!') else: print( '"Huston - We have a problem!" *******************************' ) SerialData = { "Status" : 0, } DataError(Device , Channel, \ "Checksums (recv/calc): "+str(Cks)+"/"+str(CksTest), PacketIn) return (SerialData)
LogPathFile = os.path.join(CSVPath, LOG_FILE) logging.basicConfig(filename=LogPathFile, level=logging.DEBUG) CurrentTime = datetime.datetime.now().isoformat() logging.debug(CrLf + '***** Starting at: {a}'.format(a=CurrentTime) + ' *****') # Cayenne authentication info. This should be obtained from the Cayenne Dashboard, # and the details should be put into the file listed above. # Read the Cayenne configuration stuff into a dictionary ConfigDict = toml.load(ConfPathFile) CayenneParam = ConfigDict.get('cayenne') # print (CayenneParam) # Set up the serial port. if ('USB0' in PiSerial()): SERIAL_PORT = "/dev/ttyUSB0" else: SERIAL_PORT = "/dev/serial0" # SERIAL_PORT = "/dev/ttyAMA0" # Default location of serial port on Pi models 3 and Zero # SERIAL_PORT = "/dev/ttyS0" BAUDRATE = 2400 # These values appear to be the defaults # parity = serial.PARITY_NONE, # stopbits = serial.STOPBITS_ONE, # bytesize = serial.EIGHTBITS,