class ArduinoSerialInterface: # sc_macSerialPort = "/dev/tty.usbmodem1451" # sc_baudRate = 9600 # sc_timeout = 5 # seconds def __init__(self, serialPort="/dev/tty.usbmodem1451", baudRate=9600, timeout=5): self.__serialConnector = SerialConnect(serialPort, baudRate, timeout) self.__serialConn = self.__serialConnector.getSerialConnection() self.__serialConn.reset_input_buffer() # return only when ACK is received def serialWrite(self, message): ackMsg = "" while True: self.__serialConn.write(str.encode(message)) ackMsg = self.__serialConn.readline() # if MessageStruct.cs_ack in ackMsg.decode("utf-8"): # print(ackMsg.decode("utf-8")) # break # if more than one ACK is received self.__serialConn.reset_input_buffer() # non-blocking i/o. # only reads when some message is available in the serial buffer def serialRead(self): # time.sleep(1) # non-blocking i/o if self.__serialConn.in_waiting == 0: return 0 # sanitizing the reading # self.__serialConn.reset_input_buffer() # msg = self.__serialConn.readline() # self.__serialConn.reset_input_buffer() # return msg return str(self.__serialConn.readline().decode("utf-8") ) # def serialWrite(self, message): # # self.__serialConn.write(bytearray(message)) # self.__serialConn.write(str.encode(message)) # def serialRead(self): # if self.__serialConn.in_waiting == 0: # return "" # return str(self.__serialConn.readline().decode("utf-8") ) def getWaitingQueueByte(self): return self.__serialConn.in_waiting
class AppWindow(QtWidgets.QMainWindow): serialCom = SerialConnect() def __init__(self): super(AppWindow, self).__init__() self.ui = Ui_MainWindow() self.ui.setupUi(self) self.ui.btn_kl15.clicked.connect(self.btnKl15Clicked) self.ui.btn_SSM_A.clicked.connect(self.btnSSM_AClicked) self.ui.btn_Power.clicked.connect(self.btnPowerClicked) self.ui.btn_connect.clicked.connect(self.btnReconnectClicked) self.happyDog = QtGui.QPixmap( 'C:\ssm\Tools\PowerManager\images\ONMEME.jpg') self.sadDog = QtGui.QPixmap( 'C:\ssm\Tools\PowerManager\images\sleepMEME.jpg') self.serialCom.getArduinoIOstatus() self.initLblStatus() def initLblStatus(self): if self.serialCom.serialStatus is self.serialCom.comOn: #updated KL15 status if self.serialCom.kl15Status is self.serialCom.io_status_on: self.ui.lbl_kl15.setText(self.serialCom.io_status_on) self.ui.lbl_kl15.setStyleSheet('background-color:green') else: self.ui.lbl_kl15.setText(self.serialCom.io_status_off) self.ui.lbl_kl15.setStyleSheet('background-color:red') #update kl30 status if self.serialCom.kl30Status is self.serialCom.io_status_on: self.ui.lbl_SSM_A.setText(self.serialCom.io_status_on) self.ui.lbl_SSM_A.setStyleSheet('background-color:green') else: self.ui.lbl_SSM_A.setText(self.serialCom.io_status_off) self.ui.lbl_SSM_A.setStyleSheet('background-color:red') #update all power status if self.serialCom.kl15Status is self.serialCom.io_status_off and self.serialCom.kl30Status is self.serialCom.io_status_off: self.ui.lbl_Power.setText(self.serialCom.io_status_off) self.ui.lbl_Power.setStyleSheet('background-color:red') self.serialCom.rbtStatus = self.serialCom.io_status_off else: self.ui.lbl_Power.setText(self.serialCom.io_status_on) self.ui.lbl_Power.setStyleSheet('background-color:green') #if serial is not connected else: initLable = 'UNKNOWN' initColr = 'red' self.setALLlable(initColr, initLable) self.setComStatus() # kl15 button handeler def btnKl15Clicked(self): colr, text = self.serialCom.get_kl_15_Status() self.ui.lbl_kl15.setStyleSheet('background-color:' + colr) self.ui.lbl_kl15.setText(text) # ssm_A kl30 button handeler def btnSSM_AClicked(self): colr, text = self.serialCom.get_kl_30_SSM_A_Status() #colr2, text2 = self.serialCom.get_kl_30_SSM_B_Status() self.ui.lbl_SSM_A.setText(text) self.ui.lbl_SSM_A.setStyleSheet('background-color:' + colr) # all power button handeler def btnPowerClicked(self): colr, text = self.serialCom.getRebootStatus() self.setALLlable(colr, text) # reconnect button handeler def btnReconnectClicked(self): colr, comStat = self.serialCom.reconnectSerial() if comStat is self.serialCom.comOn: text = 'ON' else: text = 'OFF' self.setALLlable(colr, text) self.setComStatus() #update all the labels at the same time def setALLlable(self, colr, text): self.ui.lbl_kl15.setText(text) self.ui.lbl_kl15.setStyleSheet('background-color:' + colr) self.ui.lbl_SSM_A.setText(text) self.ui.lbl_SSM_A.setStyleSheet('background-color:' + colr) self.ui.lbl_Power.setText(text) self.ui.lbl_Power.setStyleSheet('background-color:' + colr) if text is self.serialCom.io_status_on: self.ui.lbl_miri.setPixmap(self.happyDog) elif text is self.serialCom.io_status_off: self.ui.lbl_miri.setPixmap(self.sadDog) #Set status of com button and the status bar def setComStatus(self): if self.serialCom.serialStatus is self.serialCom.comOn: self.ui.bar_ComStatus.setValue(100) self.ui.btn_connect.setText('CONNECTED') self.ui.lbl_miri.setPixmap(self.happyDog) else: self.ui.bar_ComStatus.setValue(0) self.ui.btn_connect.setText('RECONNECT') self.ui.lbl_miri.setPixmap(self.sadDog) #recheck the serial connection status in run time def updateComStatus(self): colr, comStat = self.serialCom.comSerialStatus() #self.serialCom.getArduinoStaus() if comStat is self.serialCom.comOff: text = 'UNKNOWN' self.setALLlable(colr, text) self.setComStatus()
def main(): # option to read in a file for sending data # consistent source to be able to compare the different protocols parser = argparse.ArgumentParser(description='SARA R5 Simulation Program') parser.add_argument("--com_port", default="COM6", help="Defines which COM port to use via serial") parser.add_argument( "--wait_time", default=8, help= "Defines how long to wait in seconds in between each data transfer", type=int) parser.add_argument("--protocol", default="mqtt", choices=['mqtt', 'coap', 'amqp', 'all'], help="Defines which protocol to simulate") parser.add_argument("--mqtt_topic", default="test", help="The topic to send data to via MQTT") parser.add_argument( "--init", action='store_true', help= "Use this switch if board needs to be setup and have the PDP context initialized" ) parser.add_argument( "--datafile", help="JSON events data to send via specified IoT protocol", required=True) parser.add_argument("--serial_number", help="The serial number associated with the device", required=True) args = parser.parse_args() # ingest the data file into a dictionary using the json library datafile = open(args.datafile, "r") json_data = json.load(datafile) datafile.close() # Pull payloads from the json input file payloads = None if "payloads" in json_data: payloads = json_data["payloads"] if payloads == None: print("[-] title 'payloads' not found in json data!") return # check if we want to test the AMQP protocol which has to be done via pc client if (args.protocol == "amqp") or (args.protocol == "all"): import pika # setup the channel connection with the setup user 'test_device' creds = pika.PlainCredentials('test_device', 'THISi5@T3stP@ssW0rD') connection = pika.BlockingConnection( pika.ConnectionParameters('44.238.142.208', 5672, '/', creds)) channel = connection.channel() # publish data to the events queue channel.queue_declare(queue='events') for payload in payloads["payload"]: print("Payload Description: " + payload["description"]) payload_chunks = get_payload_chunks(payload["events"], 1000) for chunk in payload_chunks: # append the serial number to the beginning of the json packet packet = {args.serial_number: chunk} # publish all of the events in the payload channel.basic_publish(exchange='', routing_key='events', body=str(packet)) # allow some time to sleep to gather current measurements time.sleep(args.wait_time) connection.close() # setup the serial connection only if we need to use mqtt or coap if (args.protocol != "amqp"): # setup the serial connection over the specified com port connection = SerialConnect(args.com_port) if (args.init): # Setup the board and verify its connection connection.Initialize_Board() connection.Setup_PDP_Context() connection.Basic_Up_Test() # MQTT testing if ((args.protocol == "mqtt") or (args.protocol == "all")): mqtt = MqttSaraR5( connection, "ec2-44-238-142-208.us-west-2.compute.amazonaws.com", 1883) mqtt.MQTT_Connect() # give some sleep time to be visible in the current consumption testing time.sleep(2) for payload in payloads["payload"]: print("Payload Description: " + payload["description"]) payload_chunks = get_payload_chunks(payload["events"], 1000) for chunk in payload_chunks: # append the serial number to the beginning of the json packet packet = {args.serial_number: chunk} # publish all of the events in the payload with qos level 1 mqtt.MQTT_Publish(topic=args.mqtt_topic, data=str(packet), qos=1) # allow some time to sleep to gather current measurements time.sleep(args.wait_time) mqtt.MQTT_Disconnect() # CoAP testing if ((args.protocol == "coap") or (args.protocol == "all")): # Simulate the CoAP protocol using the CoapSaraR5 class coap = CoapSaraR5(connection, "coap://44.238.142.208:5683/") coap.Coap_Configure() # give some sleep time to be visible in the current consumption testing time.sleep(2) count = 0 for payload in payloads["payload"]: print("Payload Description: " + payload["description"]) # maximum octet string is 512 bytes for the coap commands so must be limited here payload_chunks = get_payload_chunks(payload["events"], 400) for chunk in payload_chunks: # append the serial number to the beginning of the json packet packet = {args.serial_number: chunk} # the only resource configured right now is /test # use a count iterator to specify different resources for each payload (simulates a timestamp) resource = "test/{}-{}".format(args.serial_number, count) coap.Coap_POST(resource_uri=resource, data=str(packet), use_con=False) count += 1 # allow some time to sleep to gather current measurements time.sleep(args.wait_time)
# Initial testing script to invoke the IoT tests from SerialConnect import SerialConnect from MqttSaraR5 import MqttSaraR5 from CoapSaraR5 import CoapSaraR5 import time connection = SerialConnect("COM6") # Setup the board and the PDP context to connect to the internet # then test that the board can ping google.com #********************************************************************** # Only need to run this section once to get the board working, comment # this section out when board is operational on the network/internet connection.Initialize_Board() connection.Setup_PDP_Context() connection.Basic_Up_Test() #********************************************************************** # Simulate the MQTT protocol using the MqttSaraR5 class mqtt = MqttSaraR5(connection, "ec2-44-238-142-208.us-west-2.compute.amazonaws.com", 1883) mqtt.MQTT_Connect() mqtt.MQTT_Subscribe("test") mqtt.MQTT_Publish(topic="test", data="Hello World") mqtt.MQTT_Read_Data() mqtt.MQTT_Disconnect() # Simulate the CoAP protocol using the CoapSaraR5 class coap = CoapSaraR5(connection, "coap://44.238.142.208:5683/")
def __init__(self, serialPort="/dev/tty.usbmodem1451", baudRate=9600, timeout=5): self.__serialConnector = SerialConnect(serialPort, baudRate, timeout) self.__serialConn = self.__serialConnector.getSerialConnection() self.__serialConn.reset_input_buffer()