class ioAdafruitDash(): def __init__(self): self.mClient = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) def setupClient(self): # Setup the callback functions defined above. self.mClient.on_connect = connected self.mClient.on_disconnect = disconnected self.mClient.on_message = message # Connect to the Adafruit IO server. self.mClient.connect() # The first option is to run a thread in the background so you can continue # doing things in your program. self.mClient.loop_background() print 'Connecting.', while not self.mClient.is_connected(): print '.', time.sleep(.5) def update(self, sd): if not self.mClient.is_connected(): print 'Client not connected ... Check setupClient' return # print '--------update ---------' self.mClient.publish(feedTemp, sd.temp) self.mClient.publish(feedHumi, sd.humi) self.mClient.publish(feedPulse, sd.hbeat) self.mClient.publish(feedAccGyro, sd.Az)
def test_connect(self): # Create MQTT test client. client = MQTTClient(self.get_test_username(), self.get_test_key()) # Verify on_connect handler is called and expected client is provided. def on_connect(mqtt_client): self.assertEqual(mqtt_client, client) client.on_connect = on_connect # Connect and wait until on_connect event is fired. client.connect() self.wait_until_connected(client) # Verify connected. self.assertTrue(client.is_connected())
def test_connect(self): # Create MQTT test client. client = MQTTClient(self.get_test_username(), self.get_test_key()) # Verify on_connect handler is called and expected client is provided. def on_connect(mqtt_client): self.assertEqual(mqtt_client, client) client.on_connect = on_connect # Connect and wait until on_connect event is fired. client.connect() self.wait_until_connected(client) # Verify connected. self.assertTrue(client.is_connected())
def test_disconnect(self): # Create MQTT test client. client = MQTTClient(self.get_test_username(), self.get_test_key()) # Verify on_connect handler is called and expected client is provided. def on_disconnect(mqtt_client): self.assertEqual(mqtt_client, client) client.on_disconnect = on_disconnect # Connect and wait until on_connect event is fired. client.connect() self.wait_until_connected(client) # Now disconnect and wait until disconnection event occurs. client.disconnect() self.wait_until_connected(client, connect_value=False) # Verify diconnected. self.assertFalse(client.is_connected())
def test_secure_connect(self): """Test a secure (port 8883, TLS enabled) AIO connection """ # Create MQTT-Secure test client. client = MQTTClient(self.get_test_username(), self.get_test_key()) # Verify on_connect handler is called and expected client is provided. def on_connect(mqtt_client): self.assertEqual(mqtt_client, client) client.on_connect = on_connect # Connect and wait until on_connect event is fired. client.connect() self.wait_until_connected(client) # Verify connected. self.assertTrue(client.is_connected()) self.assertTrue(client._secure)
def test_insecure_connect(self): """Test an insecure (port 1883, TLS disabled) AIO connection """ # Create MQTT-Insecure (non-SSL) test client. client = MQTTClient(self.get_test_username(), self.get_test_key(), secure=False) # Verify on_connect handler is called and expected client is provided. def on_connect(mqtt_client): self.assertEqual(mqtt_client, client) client.on_connect = on_connect # Connect and wait until on_connect event is fired. client.connect() self.wait_until_connected(client) # Verify connected. self.assertTrue(client.is_connected()) # Verify insecure connection established self.assertFalse(client._secure)
if __name__ == "__main__": # Create an MQTT client instance. client = MQTTClient(username=AdafruitIOFeedUsername, key=AdafruitIOKey) # Setup the callback functions client.on_connect = on_connect client.on_disconnect = on_disconnect # Setup Control Vars client.messageSend = "0" # Connect to the Adafruit IO server. client.connect() client.loop_background() while not client.is_connected(): print("Esperando conexión") time.sleep(1) # Setup Threading, to publish message every 10 seconds hilo0 = threading.Thread(target=send_message, args=(client, )) hilo0.start() # Mod publish value while client.messageSend != "x": # char 'x' to exit client.messageSend = input("Nuevo valor para el tanque\n") client.loop_background(stop=True) client.disconnect()
s2 = pigpio.pi() s2.set_mode(RX, pigpio.INPUT) s2.bb_serial_read_open(RX, 9600, 8) """ Initialize running average for PM1.0, PM2.5, PM10, RH and VOC """ pm01_24h = CRR_AVG(24, jfile="pm01_24h") # daily average (retained) pm01_60m = CRR_AVG(60, pm01_24h) # hourly average pm01_60s = CRR_AVG(60, pm01_60m) # minutely average """ Initialize AQI object """ aqi = AQI() iaq = IAQ() print("Ready") while True: """ Check that AIO is connected """ if (not aio.is_connected()): aio._client.loop_stop() aio = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) aio.connect() aio.loop_background() """ 40001 Quality Control (3.14159265359) """ mb_set(40001, pi) """ Poll the BME680 """ if s1.get_sensor_data(): RH = s1.data.humidity wx = WX(T(s1.data.temperature), P(100.0 * s1.data.pressure), RH) iaq.rh(RH) """
def test_create_client(self): # Create MQTT test client. client = MQTTClient(self.get_test_username(), self.get_test_key()) # Verify not connected by default. self.assertFalse(client.is_connected())
def test_create_client(self): # Create MQTT test client. client = MQTTClient(self.get_test_username(), self.get_test_key()) # Verify not connected by default. self.assertFalse(client.is_connected())
# Setup the callback functions defined above. client.on_connect = connected client.on_disconnect = disconnected client.on_message = message # Connect to the Adafruit IO server. client.connect() client.loop_background() # Now send new values every 10 seconds. restart_time = time.monotonic() + 3600 while 1: # read sensor messages and fill the dictionary with new values #client.loop() #if (time.time() - last) >= 5.0: if read_rfm69_msg(): upload_feed_to_aio() if (time.monotonic() > restart_time): print('Regular restart') client.disconnect() sys.exit(1) if not client.is_connected(): print('Not connected - restart') sys.exit(1) last = time.time() #time.sleep(10) # scratchpad ####################################################################### # {"Z":"Dock","S":"P_bmp180","V":986.00,"R":""}