def setupCloud(): IOT.on_message = on_message #make certain that the device & it's features are defined in the cloudapp IOT.connect() if hasLISIPAROI: IOT.addAsset(ToggleLISIPAROIId, "LISIPAROI", "Control the light on the camera", False, "boolean") IOT.addAsset( PreviewId, "Preview", "Show/close a preview on the monitor that is connected to the RPI", True, "boolean") IOT.addAsset(RecordId, "Record", "Start/stop recording the video stream on sd-card", True, "boolean") IOT.addAsset(PictureId, "Picture", "take a picture (max resoution) and store on sd-card", True, "boolean") IOT.addAsset(StreamServerId, "Stream server", "set the ip address of the server that manages the video", True, "string") # get any previously defined settings streamer.streamServerIp = IOT.getAssetState(StreamServerId) if streamer.streamServerIp: streamer.streamServerIp = streamer.streamServerIp['state']['value'] logging.info("sending stream to: " + streamer.streamServerIp) else: logging.info("no stream endpoint defined") IOT.subscribe() #starts the bi-directional communication # set current state of the device IOT.send("false", ToggleLISIPAROIId) IOT.send("false", PreviewId) IOT.send("false", RecordId)
def tryConnect(): global IsConnected try: networkCheckCount = 0 while ( Network.isConnected() == False and networkCheckCount < 5 ): # we check a number of times to give the network more time to start up. networkCheckCount = networkCheckCount + 1 sleep(2) if Network.isConnected() == False: logging.error("failed to set up network connection") else: # make certain that the device & it's features are defined in the cloudapp IOT.connect() # IOT.addAsset(TempSensorPin, TempSensorName, "temperature", False, "number", "Secondary") # IOT.addAsset(WaterLevelSensorPin, WaterLevelSensorName, "Water level", False, "number", "Secondary") IOT.addAsset(LightsRelaisPin, LightsRelaisName, "Turn the lights on/off", True, "boolean", "Primary") IOT.addAsset(WaterRelaisPin, WaterRelaisName, "Turn the water flow on/off", True, "boolean", "Primary") IOT.addAsset( ConfigSeasonId, ConfigSeasonName, "Configure the season", True, "{'type': 'string','enum': ['grow', 'flower']}", "Config", ) try: season = IOT.getAssetState(ConfigSeasonId) except: logging.exception("failed to get asset state") LoadConfig( season ) # load the cloud settings into the appbefore closing the http connection. otherwise this call fails. IOT.subscribe() # starts the bi-directional communication sleep( 2 ) # wait 2 seconds until the subscription has succeeded (bit of a hack, better would be to use the callback) IsConnected = True IOT.send( str(LightRelaisState).lower(), LightsRelaisPin ) # provide feedback to the platform of the current state of the light (after startup), this failed while loading config, cause mqtt is not yet set up. IOT.send(str(WaterRelaisState).lower(), WaterRelaisPin) except: logging.exception("failed to set up the connection with the cloud") IsConnected = False
def tryConnect(): global IsConnected try: networkCheckCount = 0 while Network.isConnected( ) == False and networkCheckCount < 5: # we check a number of times to give the network more time to start up. networkCheckCount = networkCheckCount + 1 sleep(2) if Network.isConnected() == False: logging.error("failed to set up network connection") else: #make certain that the device & it's features are defined in the cloudapp IOT.connect() #IOT.addAsset(TempSensorPin, TempSensorName, "temperature", False, "number", "Secondary") #IOT.addAsset(WaterLevelSensorPin, WaterLevelSensorName, "Water level", False, "number", "Secondary") IOT.addAsset(LightsRelaisPin, LightsRelaisName, "Turn the lights on/off", True, "boolean", "Primary") IOT.addAsset(WaterRelaisPin, WaterRelaisName, "Turn the water flow on/off", True, "boolean", "Primary") IOT.addAsset(ConfigSeasonId, ConfigSeasonName, "Configure the season", True, "{'type': 'string','enum': ['grow', 'flower']}", 'Config') try: season = IOT.getAssetState(ConfigSeasonId) except: logging.exception('failed to get asset state') LoadConfig( season ) # load the cloud settings into the appbefore closing the http connection. otherwise this call fails. IOT.subscribe() #starts the bi-directional communication sleep( 2 ) # wait 2 seconds until the subscription has succeeded (bit of a hack, better would be to use the callback) IsConnected = True IOT.send( str(LightRelaisState).lower(), LightsRelaisPin ) # provide feedback to the platform of the current state of the light (after startup), this failed while loading config, cause mqtt is not yet set up. IOT.send(str(WaterRelaisState).lower(), WaterRelaisPin) except: logging.exception("failed to set up the connection with the cloud") IsConnected = False
def setupCloud(): IOT.on_message = on_message #make certain that the device & it's features are defined in the cloudapp IOT.connect() if hasLISIPAROI: IOT.addAsset(ToggleLISIPAROIId, "LISIPAROI", "Control the light on the camera", False, "boolean") IOT.addAsset(PreviewId, "Preview", "Show/close a preview on the monitor that is connected to the RPI", True, "boolean") IOT.addAsset(RecordId, "Record", "Start/stop recording the video stream on sd-card", True, "boolean") IOT.addAsset(PictureId, "Picture", "take a picture (max resoution) and store on sd-card", True, "boolean") IOT.addAsset(StreamServerId, "Stream server", "set the ip address of the server that manages the video", True, "string") # get any previously defined settings streamer.streamServerIp = IOT.getAssetState(StreamServerId) if streamer.streamServerIp: streamer.streamServerIp = streamer.streamServerIp['state']['value'] logging.info("sending stream to: " + streamer.streamServerIp) else: logging.info("no stream endpoint defined") IOT.subscribe() #starts the bi-directional communication # set current state of the device IOT.send("false", ToggleLISIPAROIId) IOT.send("false", PreviewId) IOT.send("false", RecordId)
IOT.ClientId = "" IOT.ClientKey = "" sensorPrev = False doorBell = 2 # The Pin number of the Shield, also used to construct the AssetID #set up the pins grovepi.pinMode(doorBell, "INPUT") #callback: handles values sent from the cloudapp to the device #make certain that the device & it's features are defined in the cloudapp IOT.connect() IOT.addAsset(doorBell, "DoorBell", "DoorBell", False, "bool") IOT.subscribe() #starts the bi-directional communication #main loop: run as long as the device is turned on while True: try: if grovepi.digitalRead(doorBell) == 1: if sensorPrev == False: print("DoorBell activated") IOT.send("true", doorBell) sensorPrev = True elif sensorPrev == True: print("DoorBell deactivated") IOT.send("false", doorBell) sensorPrev = False sleep(.3)
grovepi.digitalWrite(Led, 1) IOT.send("true", Led) #provide feedback to the cloud that the operation was succesful elif value == "false": grovepi.digitalWrite(Led, 0) IOT.send("false", Led) #provide feedback to the cloud that the operation was succesful else: print("unknown value: " + value) else: print("unknown actuator: " + id) IOT.on_message = on_message #make certain that the device & it's features are defined in the cloudapp IOT.connect() IOT.addAsset(Pir, "PIR", "PIR SENSOR", False, "boolean") IOT.addAsset(Led, "LED", "Light Emitting Diode", True, "boolean") IOT.subscribe() #starts the bi-directional communication #main loop: run as long as the device is turned on while True: try: if grovepi.digitalRead(Pir) == 1: if Pir_Prev == False: print("PIR activated") IOT.send("true", Pir) Pir_Prev = True elif Pir_Prev == True: print("PIR deactivated") IOT.send("false", Pir) Pir_Prev = False sleep(.3)
value = value.lower() if value == "true": print "ON" grovepi.digitalWrite( LED, 1 ) if value == "false": print "OFF" grovepi.digitalWrite( LED, 0 ) # ignore unkown ids and values if __name__ == '__main__': grovepi.pinMode( LED, 'output' ) IOT.DeviceId = "b8klgjaOEbLaCvncEX421VM" IOT.ClientId = "itirockz" IOT.ClientKey = "rcwenbi3ssg" IOT.on_message = on_message IOT.connect() IOT.addAsset( LED, "LED", "Light Emitting Diode", True, "boolean" ) IOT.subscribe() while True: time.sleep( .1 )