def mqtt_post(httpClient, httpResponse): """ Send test message to MQTT server """ settings_dict = httpClient.ReadRequestContentAsJSON() test_msg = ujson.dumps({'test-message': 200}) from mqtt_client import MQTT try: test_mqtt = MQTT(settings_dict) test_mqtt.publish(test_msg) except: httpResponse.WriteResponseInternalServerError() else: httpResponse.WriteResponseOk()
def mqtt_post(httpClient, httpResponse): """ Send test message to MQTT server """ settings_dict = httpClient.ReadRequestContentAsJSON() test_msg = {'test-message': 200} str_data = ujson.dumps(test_msg) from mqtt_client import MQTT try: client = MQTT(settings_dict) client.publish(str_data) except: print('Failed to send the message to the MQTT broker.') httpResponse.WriteResponseInternalServerError() else: print('The test message has been sent successfully.') httpResponse.WriteResponseOk()
def main(ini): loop = asyncio.get_event_loop() broker = ini.get("MQTT", "broker") certfile = ini.get("MQTT", "certfile") mqtt = MQTT(loop, broker) if certfile != "none": mqtt.set_certfile(certfile) dbHost = ini.get("postgres", "host") dbName = ini.get("postgres", "dbname") dbUser = ini.get("postgres", "user") dbPasswd = ini.get("postgres", "password") #run: tasks = [ mqtt.main_coro(), main_coro(mqtt, dbHost, dbName, dbUser, dbPasswd) ] loop.run_until_complete(asyncio.gather(*tasks))
def main(ini): loop = asyncio.get_event_loop() broker = ini.get("MQTT", "broker") certfile = ini.get("MQTT", "certfile") mqtt = MQTT(loop, broker) if certfile!="none": mqtt.set_certfile(certfile) sensorid = ini.getint("sensor", "ID") gpio_port = ini.getint("sensor", "GPIO_BCM_Port") #run: tasks = [ mqtt.main_coro(debug=DEBUG, stopLoop=True), sensor.maincoro(loop, mqtt, sensorid, gpio_port), init(mqtt) ] loop.run_until_complete(asyncio.gather(*tasks))#waits untill all tasks are complete loop.close()
ap_ip_addr = wifi.get_ap_ip_addr() logger.debug('AP IP address: ' + ap_ip_addr) # get the Station IP of ESP32 in the WLAN which ESP32 connects to if settings['wifi'].get('ssid'): sta_ip_addr = wifi.sta_connect(settings['wifi']['ssid'], settings['wifi']['pass'], verify_ap=True) if sta_ip_addr: logger.debug('STA IP address: ' + sta_ip_addr) # print current local date & time logger.debug('Date: ' + rtc.get_localdate()) logger.debug('Time: ' + rtc.get_localtime()) # initialize the MQTT module logger.debug('Initializing MQTT...') mqtt = MQTT(settings) # initialize the fermentation process logger.debug('Initializing main process logic...') main_process = Process(fermenter_temp_ctrl, process_tim, recovery, wifi, mqtt) # Set up HTTP server logger.debug('Initializing Web server...') web = HttpServer(main_process, wifi, rtc, settings) web.start() utime.sleep(3) if web.is_started(): logger.debug('HTTP service has started.') led.set_color('green') # 初始化全部完成后设置为绿色,表示处于待机状态 # Set up DNS Server
sg = round(gravity, 3) plato = round((-1 * 616.868) + (1111.14 * gravity) - (630.272 * gravity**2) + (135.997 * gravity**3), 1) if wifi.is_connected(): # 5.1. Send Specific Gravity data & battery level by MQTT if send_data_to_mqtt: from mqtt_client import MQTT hydrometer_dict = { 'temperature': temp, 'sg': sg, 'plato': plato, 'battery': battery_voltage } mqtt_data = ujson.dumps(hydrometer_dict) client = MQTT(settings) client.publish(mqtt_data) # 5.2. Send Specific Gravity data & battery level to Fermenter ESP32 by HTTP else: machine_id = int.from_bytes(machine.unique_id(), 'big') hydrometer_dict = { 'name': settings.get('apSsid'), 'ID': machine_id, 'temperature': temp, 'angle': tilt, 'battery': battery_voltage, 'fahrenheit': round(temp * 1.8 + 32, 1), 'currentGravity': sg, 'currentPlato': plato, 'batteryLevel': battery_percent, 'updateIntervalMs': int(settings['deepSleepIntervalMs'])