def main(): """ Main loop """ print("Main loop started...") wifi_connect.connect() while True: while True: if button.value() == 0: break time.sleep_ms(20) print("Button pressed. Sending request...") r = requests.get(URL) print("Response status: {}".format(r.status_code)) print("Response data: {}".format(r.json())) # It's mandatory to close response objects as soon as you finished # working with them. On MicroPython platforms without full-fledged # OS, not doing so may lead to resource leaks and malfunction. r.close() time.sleep_ms(200)
def ensure_connection(context): """ will be called from: - main.py - watchdog_checks.check() Must return True if connected to WiFi. """ station = network.WLAN(network.STA_IF) # WiFi station interface if not station.active(): station.active(True) if station.isconnected(): print('Still connected:', station.ifconfig()) context.wifi_connected += 1 return True context.wifi_not_connected += 1 Pins.power_led.off() from wifi_connect import connect connected_time = connect(context, station) del connect del sys.modules['wifi_connect'] if context.wifi_first_connect_time is None: context.wifi_first_connect_time = connected_time return True
def main(): # set up Pin 26 is output # LED_PIN = 26 led = Pin(LED_PIN, Pin.OUT) led.value(0) addr = None # connect to WiFi then turn on the LED # addr = connect(config.SSID, config.PASS) led.value(1)
def __init__(self): # # connect to the WiFi network and setup the time # connect() # # setup the initial time # currentTime = cetTime() # cetTime returns (year,month,day,hours,minute,sec...) self.hours = currentTime[3] self.minutes = currentTime[4] time = self.hours = currentTime[3] * 100 + self.minutes # switch display on self.tm1637 = TM1637() self.tm1637.display_on() self.tm1637.write_dec(time, self.colon) # start a timer to interrupt every second timer = Timer(1) timer.init(period=1000, mode=Timer.PERIODIC, callback=self.set_clock)
import time from umqtt import MQTTClient import machine, ubinascii, gc, json from machine import I2C from machine import Pin from machine import DHT import network import wifi_connect as wlan wlan.connect() d = DHT(Pin(17), DHT.DHT2X) CLIENT_ID = ubinascii.hexlify(machine.unique_id()) gc.collect() _connected = True def conncb(task): print("[{}] Connected".format(task)) _connected = True def disconncb(task): print("[{}] Disconnected".format(task)) def subscb(task): print("[{}] Subscribed".format(task)) def pubcb(pub): print("[{}] Published: {}".format(pub[0], pub[1])) def datacb(msg): print("[{}] Data arrived from topic: {}, Message:\n".format(msg[0], msg[1]), msg[2]) mqtts = network.mqtt("myx", "iot.eclipse.org", secure=True) mqtts.config(clientid=CLIENT_ID,connected_cb=conncb,subscribed_cb=subscb, published_cb=pubcb, data_cb=datacb) time.sleep(2) gc.collect() def loop(): while True:
import gc import wifi_connect import scan_hotspots if (wifi_connect.connect()): scan_hotspots.scan_and_send() gc.collect() # TODO: go to deep sleep
from machine import I2C, Pin from dht12 import DHT12 from BH1750 import BH1750 import ssd1306 import time, json, machine, ubinascii import _thread as th from umqtt import MQTTClient import wifi_connect as wlan CLIENT_ID = ubinascii.hexlify(machine.unique_id()) client = None # OLED rst = Pin(16, Pin.OUT) rst.value(1) oledScl = Pin(15, Pin.OUT, Pin.PULL_UP) oledSda = Pin(4, Pin.OUT, Pin.PULL_UP) i2cOled = I2C(scl=oledScl, sda=oledSda, freq=450000) oled = ssd1306.SSD1306_I2C(128, 64, i2cOled, addr=0x3c) oled.fill(0) oled.text('SENSOR', 40, 5) oled.text('MicroPython', 10, 20) oled.text('Waiting...', 10, 35) oled.show() wlan.connect() oled.text('{0}'.format(wlan.get_ip()), 10, 50) oled.show() # MQTTClient time.sleep(3) def on_message(topic, msg): print(topic, msg)
# copyright U. Raich 26.1.2021 # This program is released under MIT license import utime as time from machine import SoftI2C, Pin from pcf8563 import PCF8563 T_WATCH_I2C0_SCL = 22 T_WATCH_I2C0_SDA = 21 from wifi_connect import connect, cetTime # # connect to WiFi and get time from ntp # connect() month_str = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ] month_short = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ] day_of_week_str = [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ] day_of_week_short = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] # # initialize I2C bus 0 on the t-watch
import network from wifi_connect import connect if __name__ == '__main__': # Connect or reconnect sta_if = network.WLAN(network.STA_IF) sta_if.disconnect() connect(station=sta_if) print('connected:', sta_if.isconnected())
import wifi_connect import client try: from config import mqtt_host except ImportError: mqtt_host = 'broker.hivemq.com' try: from config import robot_name except ImportError: robot_name = 'another robot' wifi_connect.connect() client.mqtt_drive(mqtt_host, robot_name=robot_name)
def main(): station = network.WLAN(network.STA_IF) WIFI_LED_PIN = 26 wifi_led = Pin(WIFI_LED_PIN, Pin.OUT) # check if no wifi connection # if not station.isconnected(): wifi_led.value(0) # get the host and port # HOST = wifi_connect.connect(ssid=config.SSID, password=config.PASS) PORT = 5001 # check if connection has been established # if station.isconnected(): wifi_led.value(1) # create the arm object # ARM_FLAG = 1 arm = Arm() Z_FLAG = 0 # turn on the LED to know it's ready to recv angles # TODO: this is a flag that is going to be sent to the PI # LED_PIN = 25 led = Pin(LED_PIN, Pin.OUT) led.value(ARM_FLAG) # flash blue led to check # sleep(0.5) led.value(1) sleep(0.5) led.value(0) sleep(0.5) # FIXME: find out the problem with the blue led # while True: # check if no wifi connection # if not station.isconnected(): print("wifi disconnected...") wifi_led.value(0) led.value(0) # this will loop foreever until wifi is here # HOST = wifi_connect.connect(ssid=config.SSID, password=config.PASS) else: wifi_led.value(1) # turn on the LED meaning the arm is ready to receive # led.value(ARM_FLAG) angles, addr = recv_on(host=HOST, port=PORT) print("angles are = {}".format(angles)) print("addr {}".format(addr)) # turn off the LED meaning the arm is moving # led.value(not ARM_FLAG) # move the arn # ARM_FLAG = arm.moveJoints(angles=angles) # store old angles for later after picking # old_angles = angles # if angles is not empty flag_z = True # if (len(angles) > 0): Z_FLAG = 1 else: Z_FLAG = 0 # if flag_z: # send Z to pi via socket # if Z_FLAG: # # num_samples = 10 mushroom_dist = 0 for _ in range(0, num_samples): mushroom_dist = mushroom_dist + get_distance(pin=27) mushroom_dist = mushroom_dist / num_samples print("dist: {}".format(mushroom_dist)) send_dist(mushroom_dist, addr=addr[0], port=6666) Z_FLAG = 0 print("sent Z, recomputing...") # get the angles for the new point with the Z dist # then move the arm to that point # sleep(1) angles, addr = recv_on(host=HOST, port=PORT) ARM_FLAG = arm.moveJoints(angles=angles) # call arm.pick to enable suction cup and pick the mushtom # TODO: find a way to know if we picked the thing up # arm.pick(angles=old_angles) print("GOT THE THING") # wait some time to be sure # sleep(2) # call arm.drop to move the arm to drop basket # arm.drop()
import untplib import time import wifi_connect as wlan wlan.connect() c = untplib.NTPClient() resp = c.request('pool.ntp.org', version=3, port=123) print("Offset is ", resp.offset) i = 0 while True: try: tm = time.localtime(time.time() + resp.offset) print('DATE TIME: {2:02d}/{1:02d}/{0} {3:02d}:{4:02d}:{5:02d}'.format( tm[0], tm[1], tm[2], tm[3] + 7, tm[4], tm[5])) time.sleep(1) i = i + 1 if i == 300: print('sync ntp time server...') resp = c.request('pool.ntp.org', version=3, port=123) i = 0 except untplib.NTPException as e: print(e)
import rsu_regular import untplib import time import wifi_connect as wlan rst = Pin(16, Pin.OUT) rst.value(1) scl = Pin(15, Pin.OUT, Pin.PULL_UP) sda = Pin(4, Pin.OUT, Pin.PULL_UP) i2c = I2C(scl=scl, sda=sda, freq=450000) oled = ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3c) rsu = Writer(oled, rsu_regular) oled.fill(0) rsu.set_textpos(5, 40) rsu.printstring('ESP32') rsu.set_textpos(25, 30) rsu.printstring('Waiting...') oled.show() wlan.connect() rsu.set_textpos(45, 2) rsu.printstring('IP: {}'.format(wlan.get_ip())) oled.show()time.sleep(3) client=untplib.NTPClient() resp = client.request('pool.ntp.org', version=3, port=123) print("Offset is ", resp.offset) i = 0 while True: try: tm = time.localtime(time.time() + resp.offset) print('DATE TIME: {2:02d}/{1:02d}/{0} {3:02d}:{4:02d}:{5:02d}'.format(tm[0],tm[1],tm[2],tm[3]+7,tm[4],tm[5])) oled.fill(0) rsu.set_textpos(5, 25)
import urequests import wifi_connect import time wifi_connect.connect() time.sleep(1) print('Start OTA') FILE_NAME = 'soil_sensor_mqtt.py' r = urequests.get('http://192.168.1.36:3000/' + FILE_NAME) if r.status_code == 200: f = open(FILE_NAME, 'w') f.write(r.content) f.close() import machine #machine.reset() else: print('http request error cde: ', r.status_code)
from upower import BtPower import time from machine import I2C, Pin import _thread as th import ssd1306 import BlynkLib import wifi_connect as wlan i2c = I2C(scl=Pin(22), sda=Pin(21)) oled = ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3c) oled.fill(0) oled.text('Power Energy', 20, 10) oled.text('Meter', 45, 30) oled.text('Waiting..', 30, 50) oled.show() wlan.connect() oled.text(wlan.get_ip(), 3, 35) blynk = BlynkLib.Blynk('04bf32882c1f4b758b253605c6793893', '27.254.63.34') sensor = BtPower(com=1, timeout=1) def loop(): while True: try: value = sensor.readAll() print('{0:.0f}V, {1:.2f}A, {2:.0f}W, {3:.0f}Wh'.format( value[0], value[1], value[2], value[3])) oled.fill(0) y = 15 oled.text('Power Meter', 20, 2) oled.text('V : {0:.0f}'.format(value[0]), 5, y) oled.text('V ', 128 - 20, y)
def main(): while True: wifi_connect.connect() if wifi_connect.connect(): print('not connect') break