def w_connect(): from time import sleep_ms from utils.wifi_connect import WiFiConnect sleep_ms(200) w = WiFiConnect() if w.connect(): print("--- WiFi: OK") else: print("--- WiFi: Connect error, check configuration") print('Network config:', w.sta_if.ifconfig()) return w
def w_connect(): led.value(1) from utils.wifi_connect import WiFiConnect sleep(1) w = WiFiConnect() if w.connect(): print("WiFi: OK") else: print("WiFi: Connect error, check configuration") led.value(0) print('Network config:', w.sta_if.ifconfig()) return w
def wifi(comm="on"): global _wc # TODO: Remove depend libraries or document them if _wc is None: from utils.wifi_connect import WiFiConnect _wc = WiFiConnect() if comm == "on": if _wc.connect(): print("WiFi: OK") else: print("WiFi: Connect error, check configuration") if comm == "scan": staactive = _wc.sta_if.active() if not staactive: _wc.sta_if.active(True) from ubinascii import hexlify print("networks:") print('-' * SEPARATOR_WIDTH) nets = [[ item[0].decode('utf-8'), hexlify(item[1], ":").decode(), item[2], item[3], item[4] ] for item in _wc.sta_if.scan()] for net in nets: print(str(net)) print('-' * SEPARATOR_WIDTH) _wc.sta_if.active(staactive) if comm == "off": try: _wc.sta_if.disconnect() _wc.sta_if.active(False) except Exception as e: print("Exception: {0}".format(e))
elif data == '0': plc_set(OUT3,1) def mqtt_send_temp(): try: temp = lm.get_temp() print("- Temp: ", temp) c.publish("octopus/device/{0}/temp".format(m.client_id),str(temp)) # topic, message (value) to publish except: print("mqtt_send_temp() Err.") print("--- wifi_connect >") net = WiFiConnect() net.connect() print("--- mqtt_connnect >") # c = mqtt_connect_from_config(esp_id) m = MQTT.from_config() c = m.client c.set_callback(mqtt_handler) c.connect() c.subscribe("octopus/device/{0}/#".format(m.client_id)) print("testing blink") simple_blink()setup() print("send alive message") c.publish("octopus/device", m.client_id) # topic, message (value) to publish
def setup(): printOctopus() print("Hello, this will help you initialize your ESP") print("ver: " + ver + " (c)octopusLAB") print("Press Ctrl+C to abort") # TODO improve this # prepare directory if 'config' not in uos.listdir(): uos.mkdir('config') run = True while run: sele = setupMenu() if sele == "q": print("all OK, press CTRL+D to soft reboot") run = False if sele == "w": from utils.wifi_connect import WiFiConnect w = WiFiConnect() sel_w = wifiMenu() if sel_w == "a": wifi_ssid = input("SSID: ") wifi_pass = input("PASSWORD: "******"r": wifi_ssid = input("SSID: ") w.remove_network(wifi_ssid) if sel_w == "s": print("Saved wifi networks") for k, v in w.config['networks'].items(): print("SSID: {0}".format(k)) if sele == "cw": print("Connect WiFi >") from utils.wifi_connect import WiFiConnect w = WiFiConnect() if w.connect(): print("WiFi: OK") else: print("WiFi: Connect error, check configuration") if sele == "cl": print("Connect LAN >") import network if "ETH_CLOCK_GPIO17_OUT" in dir(network): lan = network.LAN(mdc=machine.Pin(23), mdio=machine.Pin(18), phy_type=network.PHY_LAN8720, phy_addr=1, clock_mode=network.ETH_CLOCK_GPIO17_OUT) else: lan = network.LAN(mdc=machine.Pin(23), mdio=machine.Pin(18), phy_type=network.PHY_LAN8720, phy_addr=1) lan.active(1) retry = 0 while not lan.isconnected() or lan.ifconfig()[0] is '0.0.0.0': retry += 1 time.sleep_ms(500) if retry > 20: break if lan.isconnected(): print("LAN: OK") else: print("LAN: Connect error, check cable or DHCP server") if sele == "sd": shutil() deplUrl = "https://octopusengine.org/download/micropython/stable.tar" deploy(deplUrl)
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 utils.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
def setup(): mainOctopus() print("Hello, this will help you initialize your ESP") print("ver: " + ver + " (c)octopusLAB") print("Press Ctrl+C to abort") # TODO improve this # prepare directory if 'config' not in uos.listdir(): uos.mkdir('config') run = True while run: sele = setupMenu() if sele == "q": print("Setup - quit >") time.sleep_ms(1000) print("all OK, press CTRL+D to soft reboot") run = False if sele == "si": #system_info() from utils.sys_info import sys_info sys_info() if sele == "ds": print("Device setting:") print(" board_type | soc_type (system on the board)") i = 0 for di in devices: print(str(i) + ": " + str(di[0]) + " | " + str(di[1])) i = i + 1 print() sd = input("select: ") #print(str(devices[int(sd)])) print("> " + str(devices[int(sd)][0]) + " | " + str(devices[int(sd)][1])) dc = {} dc['board_type'] = str( devices[int(sd)][0] ) #input("Board type ('oLAB RobotBoard1' or 'oLAB IoTBoard1'): ") dc['soc_type'] = str(devices[int( sd)][1]) #input("SoC type ('esp32' or 'esp8266'): ") print("Writing to file config/device.json") with open('config/device.json', 'w') as f: ujson.dump(dc, f) # ujson.dump(wc, f, ensure_ascii=False, indent=4) if sele == "ios": print("I/O setting:") # io menu ioMenu() if sele == "w": from utils.wifi_connect import WiFiConnect w = WiFiConnect() sel_w = wifiMenu() if sel_w == "a": wifi_ssid = input("SSID: ") wifi_pass = input("PASSWORD: "******"r": wifi_ssid = input("SSID: ") w.remove_network(wifi_ssid) if sel_w == "s": print("Saved wifi networks") for k, v in w.config['networks'].items(): print("SSID: {0}".format(k)) if sele == "cw": print("Connect WiFi >") from utils.wifi_connect import WiFiConnect w = WiFiConnect() if w.connect(): print("WiFi: OK") else: print("WiFi: Connect error, check configuration") if sele == "cl": print("Connect LAN >") import network if "ETH_CLOCK_GPIO17_OUT" in dir(network): lan = network.LAN(mdc=machine.Pin(23), mdio=machine.Pin(18), phy_type=network.PHY_LAN8720, phy_addr=1, clock_mode=network.ETH_CLOCK_GPIO17_OUT) else: lan = network.LAN(mdc=machine.Pin(23), mdio=machine.Pin(18), phy_type=network.PHY_LAN8720, phy_addr=1) lan.active(1) retry = 0 while not lan.isconnected() or lan.ifconfig()[0] is '0.0.0.0': retry += 1 time.sleep_ms(500) if retry > 20: break if lan.isconnected(): print("LAN: OK") else: print("LAN: Connect error, check cable or DHCP server") if sele == "mq": print("mqtt setup >") try: print() from utils.mqtt import mqtt mqtt() except: print("Err.mqtt() or 'utils.mqtt.py' does not exist") if sele == "st": print("Time setting >") rtc = machine.RTC() print(str(rtc.datetime())) setdatetime = input( "input 6 numbers - format: RRRRR,M,D,wd,h,m > ") + (",0,0") dt_str = setdatetime.split(",") print(str(dt_str)) dt_int = [int(numeric_string) for numeric_string in dt_str] rtc.init(dt_int) print(str(rtc.datetime())) if sele == "sd": shutil() deplUrl = "https://octopusengine.org/download/micropython/stable.tar" deploy(deplUrl) if sele == "sde": shutil() deplUrl = "https://octopusengine.org/download/micropython/examples.tar" deploy(deplUrl) if sele == "sdp": shutil() deplUrl = "http://iot.petrkr.net/olab/latest.tar" deploy(deplUrl) if sele == "sdo": shutil() deplUrl = "https://octopusengine.org/download/latest.tar" deploy(deplUrl) if sele == "sdh": shutil() deplUrl = "https://octopusengine.org/download/hydroponics.tar" deploy(deplUrl) if sele == "sdg": shutil() deplUrl = "https://iot.petrkr.net/olab/micro-gui.tar" deploy(deplUrl) if sele == "wr": print("under reconstruction <") import esp esp.osdebug(None) import webrepl webrepl.start() if sele == "ftp": import ftp