def init_wifi(reset: bool = False): if WIFI_SETUP_FLAG not in os.listdir(): from wifi import wifi_connect, disable_wifi_ap from wifi_cfg import SSID, PASSWORD from config import HOSTNAME disable_wifi_ap() wifi_connect(SSID, PASSWORD, hostname=HOSTNAME) f = open(WIFI_SETUP_FLAG, 'w') f.write(SSID) f.close() if reset: machine.reset()
def test_wifi_connect(self): wifi.wifi_connect('foo', 'bar')
mqttServer = "mqttServer" mqttPort = "1883" mqttKeepAlive = 60 mqttUsername = "******" mqttPassword = "******" mqttTopic = "mqttTopic" mqttClientID = "123456" wifissid = "wifissid" wifiPassword = "******" relayPin = Pin(5, Pin.OUT) mqttTopic1 = "sensors/environmental/new/temperature,humidity" mqttTopic2 = "heater" mqttTopic3 = 'tempset' wifi_connect(wifissid, wifiPassword) relayFlag = 0 heaterFlag = 0 temperature = b'90' setTemperature = b'0' def subscribeCallBack(topic, message): global heaterFlag global temperature global setTemperature if topic == b'heater': if message == b'0': heaterFlag = 0 if message == b'1': heaterFlag = 1
To use it, the parameters in the file need to be set (WIFI_SID, WIFI_PW, BROKER) and the file renamed to 'main.py' which will be run when the ESP8266 is booted. Mpfshell is recommended for transferring MicroPython files to the ESP8266. """ from thingflow import Scheduler from adxl345_upy import ADXL345_upy from mqtt_writer import MQTTWriter from wifi import wifi_connect import os # Params to set WIFI_SID="" WIFI_PW="" SENSOR_ID="accelerometer" BROKER='' wifi_connect(WIFI_SID, WIFI_PW) sensor = ADXL345_upy(sensor_id=2) writer = MQTTWriter(SENSOR_ID, BROKER, 1883, 'sensor/1') sched = Scheduler() frequency = 5 #Hz sched.schedule_sensor(sensor, 1/frequency, writer) try: os.stat('stop-sampling.txt') print("Saw stop-sampling.txt, will skip rest") except: print("Starting sensor sampling") sched.run_forever()
from wifi import wifi_connect import urequests as requests wifi_connect() res = requests.get("http://192.168.1.228:9998/") print(res.text) myobj = {'value':'somevalue'} post = requests.post("http://192.168.1.228:9998/posts",json = myobj) print(post.text)
import bme280
import gc import machine from wifi import wifi_connect from api import api_send from logs import logs_error from measures import Measures from settings import WIFI_SSID, WIFI_PASSWORD, COUNT_FOR_AVERAGE, SENDING_DELTA wdt = machine.WDT(timeout=180000) try: wifi_connect(WIFI_SSID, WIFI_PASSWORD) while True: measures = Measures(COUNT_FOR_AVERAGE, SENDING_DELTA) data = measures.get_averaged_measures() del (measures) gc.collect() api_send(data) wdt.feed() gc.collect() except Exception as e: logs_error(str(e))