def w_connect(): from util.wifi_connect import read_wifi_config, WiFiConnect time.sleep_ms(1000) wifi_config = read_wifi_config() if Debug: print("config for: " + wifi_config["wifi_ssid"]) w = WiFiConnect() w.events_add_connecting(connecting_callback) w.events_add_connected(connected_callback) w.connect(wifi_config["wifi_ssid"], wifi_config["wifi_pass"]) if Debug: print("WiFi: OK")
def w_connect(): global wifi time.sleep_ms(1000) wifi_config = read_wifi_config() if Debug: print("config for: " + wifi_config["wifi_ssid"]) wifi = WiFiConnect(wifi_config["wifi_retries"] if "wifi_retries" in wifi_config else wifi_retries) wifi.events_add_connecting(connecting_callback) wifi.events_add_connected(connected_callback) wifi.events_add_timeout(connecting_timeout_callback) wifi.events_add_disconnected(disconnected_callback) wifi_status = wifi.connect(wifi_config["wifi_ssid"], wifi_config["wifi_pass"]) if Debug: print("WiFi: OK" if wifi_status else "WiFi: Error")
y = int(data.split('x')[1]) else: y = int(data) lcd.move_to(x, y) except Exception as e: print("Error parse message") print(e) print("wifi_config >") wifi_config = read_wifi_config() wifi = WiFiConnect(wifi_config["wifi_retries"] if "wifi_retries" in wifi_config else 250) wifi.events_add_connecting(connecting_callback) wifi.events_add_connected(connected_callback) wifi_status = wifi.connect(wifi_config["wifi_ssid"], wifi_config["wifi_pass"]) print("mqtt_config >") mqtt_clientid_prefix = read_mqtt_config()["mqtt_clientid_prefix"] mqtt_host = read_mqtt_config()["mqtt_broker_ip"] mqtt_root_topic = read_mqtt_config()["mqtt_root_topic"] mqtt_clientid = mqtt_clientid_prefix + esp_id c = MQTTClient(mqtt_clientid, mqtt_host, ssl=mqtt_ssl) c.set_callback(mqtt_sub) lcd.move_to(14, 0)
def mqtt(): mainOctopus() print("Hello, this will help you initialize MQTT client") print("ver: " + ver + " (c)octopusLAB") print("id: " + esp_id) print("Press Ctrl+C to abort") # TODO improve this # prepare directory if 'config' not in uos.listdir(): uos.makedirs('config') run = True while run: sele = setupMenu() if sele == "x": print("Setup - exit >") time.sleep_ms(2000) print("all OK, press CTRL+D to soft reboot") run = False if sele == "si": #system_info() from util.sys_info import sys_info sys_info() if sele == "cv": print("------- Set 0/1/str for settings ------") wc = {} wc['name'] = input("device (host)name/describe: ") wc['time'] = int(input("get time from server? [1/0]: ")) wc['mysql'] = int(input("send data to mysql db [1/0]: ")) if wc['mysql']: wc['mysqlURL'] = input("mysql Write URL: ") wc['mqtt'] = int(input("mqtt client [1/0]: ")) wc['influx'] = int(input("send data to influx db [1/0]: ")) if wc['influx']: wc['influxWriteURL'] = input("influx Write URL: ") wc['timer'] = int(input("timer: ")) print("Writing to file config/mqtt_io.json") with open('config/mqtt_io.json', 'w') as f: ujson.dump(wc, f) if sele == "ms": print("Set mqtt >") print() mq = {} mq['mqtt_broker_ip'] = input("BROKER IP: ") mq['mqtt_ssl'] = int(input("> SSL (0/1): ")) mq['mqtt_port'] = int(input("> PORT (1883/8883/?): ")) mq['mqtt_clientid_prefix'] = input("CLIENT PREFIX: ") mq_user = input("Username: "******"" else mq_user mq_pass = input("Password: "******"" else mq_pass mq['mqtt_root_topic'] = input("ROOT TOPIC: ") print("Writing to file config/mqtt.json") with open('config/mqtt.json', 'w') as f: ujson.dump(mq, f) def mqtt_sub(topic, msg): print("MQTT Topic {0}: {1}".format(topic, msg)) if sele == "mt": print("mqtt simple test:") print("wifi_config >") wifi = WiFiConnect(250) wifi.events_add_connecting(connecting_callback) wifi.events_add_connected(connected_callback) print("wifi.connect") wifi_status = wifi.connect() # url config: TODO > extern. print("mqtt_config >") mqtt_clientid_prefix = read_mqtt_config()["mqtt_clientid_prefix"] mqtt_host = read_mqtt_config()["mqtt_broker_ip"] mqtt_root_topic = read_mqtt_config()["mqtt_root_topic"] mqtt_ssl = read_mqtt_config()["mqtt_ssl"] mqtt_user = read_mqtt_config()["mqtt_user"] mqtt_pass = read_mqtt_config()["mqtt_pass"] mqtt_clientid = mqtt_clientid_prefix + esp_id c = MQTTClient(mqtt_clientid, mqtt_host, ssl=mqtt_ssl, user=mqtt_user, password=mqtt_pass) c.set_callback(mqtt_sub) print("mqtt.connect to " + mqtt_host) c.connect() """ # c.subscribe("/octopus/device/{0}/#".format(esp_id)) subStr = mqtt_root_topic+"/"+esp_id+"/#" print("subscribe (root topic + esp id):" + subStr) c.subscribe(subStr) """ mqtt_log_topic = mqtt_root_topic + "/log" print("mqtt log > " + mqtt_log_topic) print(mqtt_log_topic) # mqtt_root_topic_temp = "octopus/device" c.publish(mqtt_log_topic, esp_id) # topic, message (value) to publish