def __init__( self, secrets_, *, esp01_pins=[], esp01_uart=None, esp01_baud=115200, pub_prefix="", debug=False ### pylint: disable=redefined-outer-name ): if esp01_uart: self.esp01_uart = esp01_uart else: self.esp01_uart = busio.UART(*esp01_pins, receiver_buffer_size=2048) self.debug = debug self.esp = adafruit_espatcontrol.ESP_ATcontrol(self.esp01_uart, esp01_baud, debug=debug) self.esp_version = "" self.wifi = None try: _ = [secrets_[key] for key in self.SECRETS_REQUIRED] except KeyError: raise RuntimeError("secrets.py must contain: " + " ".join(self.SECRETS_REQUIRED)) self.secrets = secrets_ self.io = None self.pub_prefix = pub_prefix self.pub_name = {} self.init_connect()
def SendData(self): global temp global humidity global air global motion global sound try: from secrets import secrets except ImportError: print("All secret keys are kept in secrets.py, please add them there!") raise # Initialize UART connection to the ESP8266 WiFi Module. RX = board.GP17 TX = board.GP16 uart = busio.UART( TX, RX, receiver_buffer_size=2048 ) # Use large buffer as we're not using hardware flow control. esp = adafruit_espatcontrol.ESP_ATcontrol(uart, 115200, debug=False) requests.set_socket(socket, esp) print("Resetting ESP module") esp.soft_reset() # Connect to WiFi print("Connecting to WiFi...") esp.connect(secrets) print("Connected!") yield while True: # Update the blynk datastream using HTTP GET requests requests.get("https://blynk.cloud/external/api/update?token=" + secrets["blynk_auth_token"] + "&v0=" + str(temp)) yield [pyRTOS.timeout(0.5)] # Let other tasks run requests.get("https://blynk.cloud/external/api/update?token=" + secrets["blynk_auth_token"] + "&v1=" + str(humidity)) yield [pyRTOS.timeout(0.5)] # Let other tasks run requests.get("https://blynk.cloud/external/api/update?token=" + secrets["blynk_auth_token"] + "&v2=" + str(air)) yield [pyRTOS.timeout(0.5)] # Let other tasks run requests.get("https://blynk.cloud/external/api/update?token=" + secrets["blynk_auth_token"] + "&v3=" + str(motion)) yield [pyRTOS.timeout(0.5)] # Let other tasks run requests.get("https://blynk.cloud/external/api/update?token=" + secrets["blynk_auth_token"] + "&v4=" + str(sound)) yield [pyRTOS.timeout(0.5)] # Let other tasks run # Reset the global variables if motion: motion = 0 if sound: sound = 0 yield [pyRTOS.timeout(0.5)] # Delay in seconds (Other task can run)
# With a Particle Argon RX = board.ESP_TX TX = board.ESP_RX resetpin = DigitalInOut(board.ESP_WIFI_EN) rtspin = DigitalInOut(board.ESP_CTS) uart = busio.UART(TX, RX, timeout=0.1) esp_boot = DigitalInOut(board.ESP_BOOT_MODE) esp_boot.direction = Direction.OUTPUT esp_boot.value = True status_light = None print("ESP AT commands") esp = adafruit_espatcontrol.ESP_ATcontrol(uart, 115200, reset_pin=resetpin, rts_pin=rtspin, debug=False) wifi = adafruit_espatcontrol_wifimanager.ESPAT_WiFiManager( esp, secrets, status_light) counter = 0 while True: try: print("Posting data...", end='') data = counter feed = 'test' payload = {'value': data} response = wifi.post("https://io.adafruit.com/api/v2/" + secrets['aio_username'] + "/feeds/" + feed +
# Debug Level # Change the Debug Flag if you have issues with AT commands debugflag = False # How Long to sleep between polling sleep_duration = 60 PIN_RX = board.GP1 PIN_TX = board.GP0 uart = busio.UART(PIN_TX, PIN_RX, baudrate=11520, receiver_buffer_size=2048) status_light = None print("ESP AT commands") esp = adafruit_espatcontrol.ESP_ATcontrol( uart, 115200, reset_pin=None, rts_pin=None, debug=debugflag ) wifi = adafruit_espatcontrol_wifimanager.ESPAT_WiFiManager( esp, secrets, status_light ) print("ESP-01 local time") TIME_API = "http://worldtimeapi.org/api/ip" the_rtc = rtc.RTC() response = None while True: try: print("Fetching json from", TIME_API)
try: from secrets import secrets except ImportError: print("All secret keys are kept in secrets.py, please add them there!") raise # Telegram API url. API_URL = "https://api.telegram.org" # Initialize UART connection to the ESP8266 WiFi Module. RX = board.GP17 TX = board.GP16 uart = busio.UART(TX, RX, receiver_buffer_size=2048) # Use large buffer as we're not using hardware flow control. esp = adafruit_espatcontrol.ESP_ATcontrol(uart, 115200, debug=False) requests.set_socket(socket, esp) print("Resetting ESP module") esp.soft_reset() while True: try: print("\nChecking WiFi connection...") while not esp.is_connected: print("Connecting...") esp.connect(secrets) print("\nSending Telegram message...") message = "Hello World from Circuitpython :)" get_url = API_URL
# With a Particle Argon """ RX = board.ESP_TX TX = board.ESP_RX resetpin = DigitalInOut(board.ESP_WIFI_EN) rtspin = DigitalInOut(board.ESP_CTS) uart = busio.UART(TX, RX, timeout=0.1) esp_boot = DigitalInOut(board.ESP_BOOT_MODE) from digitalio import Direction esp_boot.direction = Direction.OUTPUT esp_boot.value = True """ esp = adafruit_espatcontrol.ESP_ATcontrol(uart, 115200, reset_pin=resetpin, run_baudrate=460800, rts_pin=rtspin, debug=True) print("Resetting ESP module") esp.hard_reset() requests.set_interface(esp) print("Connected to AT software version", esp.get_version()) counter = 0 while True: try: # Connect to WiFi if not already while not esp.is_connected: print("Connecting...") esp.connect(settings)
resetpin = DigitalInOut(board.D5) # With a Particle Argon """ uart = busio.UART(board.ESP_RX, board.ESP_TX, timeout=0.1) resetpin = DigitalInOut(board.ESP_WIFI_EN) esp_boot = DigitalInOut(board.ESP_BOOT_MODE) from digitalio import Direction esp_boot.direction = Direction.OUTPUT esp_boot.value = True """ URL = "http://wifitest.adafruit.com/testwifi/index.html" print("ESP AT GET URL", URL) esp = adafruit_espatcontrol.ESP_ATcontrol(uart, 115200, run_baudrate=9600, reset_pin=resetpin, debug=False) print("Resetting ESP module") esp.hard_reset() requests.set_interface(esp) while True: try: print("Checking connection...") while not esp.is_connected: print("Connecting...") esp.connect(settings) # great, lets get the data print("Retrieving URL...", end='') r = requests.get(URL) print("Status:", r.status_code)