def startWifi(): import network import utime import machine machine.loglevel("wifi", machine.LOG_ERROR) wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(machine.nvs_getstr("wifiSSID"), machine.nvs_getstr("wifiPass")) while (not wlan.isconnected()): utime.sleep_ms(50) user = machine.nvs_getstr("networkLogin") password = machine.nvs_getstr("networkPassword") network.ftp.start(user=user, password=password) network.telnet.start(user=user, password=password)
msg = "" print("\n" + _cyan + "I [WiFi] event: {} ({}){}".format(info[0], info[1], msg) + _norm) if ShowWifi: # Enable (if supported) try: network.WLANcallback(wifi_cb) except: pass if ShowDebug: #enable system wide verbose information messages import machine machine.loglevel('*', machine.LOG_VERBOSE) machine.loglevel('[modnetwork]', machine.LOG_DEBUG) machine.loglevel('wifi', machine.LOG_DEBUG) machine.loglevel('tcpip_adapter', machine.LOG_DEBUG) machine.loglevel('event', machine.LOG_DEBUG) #---------------------------------------------- # Auto Connect to the network, starts autoconfig if needed import wifisetup wifisetup.auto_connect() #Note this section is not normally run, # but is provided as an example of how to force the autoconfiguration if False:
#minimal MQTT import network, sys, machine import utime import uos import gc machine.loglevel('[Mqtt client]', machine.LOG_DEBUG) myMqttClient = bytes("client_123", 'utf-8') thingspeakUrl = "mqtt.thingspeak.com" thingspeakUserId = "josverlinde" # EDIT - enter Thingspeak User ID thingspeakMqttApiKey = "ZWH8LQHCU7L9ETXA" # EDIT - enter Thingspeak MQTT API Key (Needs to be generated) machine.loglevel('[Mqtt client]', machine.LOG_DEBUG) client = network.mqtt( myMqttClient, #clientID thingspeakUrl, #server user=thingspeakUserId, #user password=thingspeakMqttApiKey)
#First Script to #minimize logging import machine machine.loglevel('*', machine.LOG_INFO)
mode = load_mode() idx = 78 msg = (b'{{ "idx": {}, "nvalue": 0, "svalue": "{}" }}'.format(idx, mode)) print(msg) print("Posting current heating mode to domoticz") if mode: client.publish(TOPIC, msg) # Publish sensor data to MQTT topic return True else: print("Error loading saved mode") return False print("Starting One wire module with embeded temperature sensor") if DEBUG: machine.loglevel('*', machine.LOG_DEBUG) if 4 == machine.wake_reason()[0]: print('Woke from a deep sleep') else: print("Woke up from hard reboot ?") utime.sleep(2) pin = machine.Pin(LED_BUILT_IN, machine.Pin.OUT) # Initialize the MOC3043 Pins alt_positive_pin = machine.Pin(ALT_POSITIVE_PIN, machine.Pin.OUT) alt_negative_pin = machine.Pin(ALT_NEGATIVE_PIN, machine.Pin.OUT) current_mode = default_mode() # the temperature device is on GPIO16 dat = machine.Pin(DS18B20_PIN, machine.Pin.IN)