Skip to content

Instantly share code, notes, and snippets.

@idriszmy
Created November 13, 2021 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idriszmy/ea3839b50146fc7dc638397a8365bcaa to your computer and use it in GitHub Desktop.
Save idriszmy/ea3839b50146fc7dc638397a8365bcaa to your computer and use it in GitHub Desktop.
Send internal temperature reading to Favoriot IoT platform.
################################################################################
# Example for Favoriot.
# Send internal temperature reading to Favoriot IoT platform.
#
# Hardware:
# - Maker Pi Pico or Maker Pi RP2040
# - ESP8266 WiFi module with Espressif AT Firmware v2.2.0 and above.
#
# Dependencies:
# - adafruit_requests
# - adafruit_espatcontrol
#
# Instructions:
# - Copy the lib folder to the CIRCUITPY device.
# - Modify the keys in secrets.py and copy to the CIRCUITPY device.
# - Make sure the UART pins are defined correctly according to your hardware.
#
#
# Author: Cytron Technologies
# Website: www.cytron.io
# Email: support@cytron.io
################################################################################
import time
import board
import digitalio
import microcontroller
import busio
import adafruit_requests as requests
import adafruit_espatcontrol.adafruit_espatcontrol_socket as socket
from adafruit_espatcontrol import adafruit_espatcontrol
import json
# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("All secret keys are kept in secrets.py, please add them there!")
raise
# Thingspeak API url.
API_URL = "https://apiv2.favoriot.com/v2/streams"
headers = {
"Content-Type": "application/json",
"apikey": secrets["favoriot_apikey"],
}
# Initialize LED and button.
led = digitalio.DigitalInOut(board.GP0)
led.direction = digitalio.Direction.OUTPUT
button = digitalio.DigitalInOut(board.GP20)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP
# 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:
# Make sure WiFi is connected.
while not esp.is_connected:
print("Connecting...")
esp.connect(secrets)
root = {}
root["device_developer_id"] = "MakerPiPico@Idriszmy"
data = {}
data["temperature"] = round(microcontroller.cpu.temperature, 1)
root["data"] = data
body = json.dumps(root)
print("\nData send:\n{}".format(body))
response = requests.post(API_URL, headers=headers, data=body)
print("Response:\n{}".format(response.text))
time.sleep(30)
except (ValueError, RuntimeError, adafruit_espatcontrol.OKError) as e:
print("Failed, retrying\n", e)
# Secret Keys.
secrets = {
"ssid" : "Your WiFi SSID",
"password" : "Your WiFi password",
"favoriot_apikey" : "Your Favoriot API key",
"favoriot_token" : "Your Favoriot token",
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment