|
| 1 | +import time |
| 2 | +import board |
| 3 | +import busio |
| 4 | +from digitalio import DigitalInOut |
| 5 | +from adafruit_espatcontrol import adafruit_espatcontrol |
| 6 | +from adafruit_espatcontrol import adafruit_espatcontrol_requests as requests |
| 7 | + |
| 8 | + |
| 9 | +# Get wifi details and more from a settings.py file |
| 10 | +try: |
| 11 | + from settings import settings |
| 12 | +except ImportError: |
| 13 | + print("WiFi settings are kept in settings.py, please add them there!") |
| 14 | + raise |
| 15 | + |
| 16 | + |
| 17 | +# With a Metro or Feather M4 |
| 18 | +resetpin = DigitalInOut(board.D5) |
| 19 | +rtspin = DigitalInOut(board.D9) |
| 20 | +uart = busio.UART(board.TX, board.RX, timeout=0.1) |
| 21 | + |
| 22 | +# With a Particle Argon |
| 23 | +""" |
| 24 | +RX = board.ESP_TX |
| 25 | +TX = board.ESP_RX |
| 26 | +resetpin = DigitalInOut(board.ESP_WIFI_EN) |
| 27 | +rtspin = DigitalInOut(board.ESP_CTS) |
| 28 | +uart = busio.UART(TX, RX, timeout=0.1) |
| 29 | +esp_boot = DigitalInOut(board.ESP_BOOT_MODE) |
| 30 | +from digitalio import Direction |
| 31 | +esp_boot.direction = Direction.OUTPUT |
| 32 | +esp_boot.value = True |
| 33 | +""" |
| 34 | + |
| 35 | + |
| 36 | +esp = adafruit_espatcontrol.ESP_ATcontrol(uart, 115200, reset_pin=resetpin, |
| 37 | + run_baudrate = 460800, rts_pin=rtspin, debug=True) |
| 38 | +print("Resetting ESP module") |
| 39 | +esp.hard_reset() |
| 40 | +requests.set_interface(esp) |
| 41 | + |
| 42 | +print("Connected to AT software version", esp.get_version()) |
| 43 | + |
| 44 | +counter = 0 |
| 45 | +while True: |
| 46 | + try: |
| 47 | + # Connect to WiFi if not already |
| 48 | + while not esp.is_connected: |
| 49 | + print("Connecting...") |
| 50 | + esp.connect(settings) |
| 51 | + print("Connected to", esp.remote_AP) |
| 52 | + # great, lets get the data |
| 53 | + print("Posting data...", end='') |
| 54 | + data=counter |
| 55 | + feed='test' |
| 56 | + payload={'value':data} |
| 57 | + response=requests.post("https://io.adafruit.com/api/v2/"+settings['aio_username']+"/feeds/"+feed+"/data",json=payload,headers={bytes("X-AIO-KEY","utf-8"):bytes(settings['aio_key'],"utf-8")}) |
| 58 | + print(response.json()) |
| 59 | + response.close() |
| 60 | + counter = counter + 1 |
| 61 | + print("OK") |
| 62 | + except (RuntimeError, adafruit_espatcontrol.OKError) as e: |
| 63 | + print("Failed to get data, retrying\n", e) |
| 64 | + continue |
| 65 | + response = None |
0 commit comments