def main(): arduino = Sensor('/dev/ttyACM0') # Create a Sensor object arduino.flushInput() # Flushing input to clean it before doing things arduino.wait_helo() print(arduino.state()) # Asking the sensor its current state time.sleep(1) print(arduino.deactivate()) # Putting the sensor in Deactivate state time.sleep(1) print(arduino.alarm()) # Putting the sensor in Alarm state time.sleep(3) print(arduino.deactivate()) while (True): # Waiting for events # This will block until the Sensor sends something # [:-4] is used to get rid of '\r\n' event = arduino.readline()[:4] print(event) # Printing the 4 character code of the event arduino.flushInput() # Cleaning input
def on_message_device(self, message): """ Callback method. Call when you receive a message from the arduio board. Parse the json and send it to the central com. """ data = {} data["id"] = "UJ01" data["type"] = "0" data["state"] = message.decode("UTF-8") self.websocket.send(json.dumps(data)) def __init__(self, address, port, device): # Declare datas self.central_address = address self.central_port = port self.device = device # Declare serial port listener and server listener self.websocket = webSocketManager.WebSocketManager( self.central_address, self.central_port, self.on_message_cc) self.serial_com = SerialCommunication(device, self.on_message_device) self.serial_com.start() # Just a testing main : to remove after if __name__ == '__main__': arduino = Sensor('/dev/ttyACM0') arduino.flushInput() rasp = RaspManager("192.168.1.155", "8100", arduino)