0

Adafruit IO Metro ESP32-S2
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Adafruit IO Metro ESP32-S2

by jfabernathy on Wed Jan 06, 2021 8:37 am

I'm new to using Adafruit MCUs and am trying to find some resources. My goal is to have the S2 grab sensor data and post it wirelessly to a server. Hopefully using MQTT, but I understand MQTT is not quite ready for the S2. So in the meantime I'm just playing at getting some form of communication between the S2 and my network servers via WiFi.

I went through the examples @ https://learn.adafruit.com/adafruit-metro-esp32-s2/circuitpython-internet-test and the one after that for getting the date and time. Both work and I understand them.

The only example I've found of data being sent out from the S2 is something I found on github that I got the pointer to on Discord support. The code is below but I can't find the link again. EDIT found the link: https://github.com/adafruit/Adafruit_CircuitPython_AdafruitIO/blob/master/examples/adafruit_io_http/adafruit_io_simpletest.py

So my question is where is this piece of code sending it's data to and who is sending back the response? I would like that to be some server in my house, but I'm stuff figuring out what's happening here.


Code: Select all | TOGGLE FULL SIZE
# adafruit_circuitpython_adafruitio usage with native wifi networking
import ssl
from random import randint
import adafruit_requests
import socketpool
import wifi
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError

# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
# source control.
# pylint: disable=no-name-in-module,wrong-import-order
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

# Set your Adafruit IO Username and Key in secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
aio_username = secrets["aio_username"]
aio_key = secrets["aio_key"]

print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])


pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
# Initialize an Adafruit IO HTTP API object
io = IO_HTTP(aio_username, aio_key, requests)

try:
    # Get the 'temperature' feed from Adafruit IO
    temperature_feed = io.get_feed("temperature")
except AdafruitIO_RequestError:
    # If no 'temperature' feed exists, create one
    temperature_feed = io.create_new_feed("temperature")

# Send random integer values to the feed
random_value = randint(0, 50)
print("Sending {0} to temperature feed...".format(random_value))
io.send_data(temperature_feed["key"], random_value)
print("Data sent!")

# Retrieve data value from the feed
print("Retrieving data from temperature feed...")
received_data = io.receive_data(temperature_feed["key"])
print("Data from temperature feed: ", received_data["value"])

jfabernathy
 
Posts: 76
Joined: Thu Dec 17, 2020 10:58 pm

Re: Adafruit IO Metro ESP32-S2

by brubell on Wed Jan 06, 2021 11:57 am

So my question is where is this piece of code sending it's data to and who is sending back the response? I would like that to be some server in my house, but I'm stuff figuring out what's happening here.


This code sends data to io.adafruit.com's http api (https://io.adafruit.com/api/docs/#adafruit-io-http-api).

You may want to use the Adafruit_Requests library to send "raw" HTTP requests to your home server instead, the code does not rely on Adafruit IO: https://github.com/adafruit/Adafruit_Ci ... n_Requests

brubell
 
Posts: 1381
Joined: Fri Jul 17, 2015 10:33 pm

Re: Adafruit IO Metro ESP32-S2

by jfabernathy on Wed Jan 06, 2021 12:14 pm

looks promising. Thanks, I'll work with this for a while.

jfabernathy
 
Posts: 76
Joined: Thu Dec 17, 2020 10:58 pm

Re: Adafruit IO Metro ESP32-S2

by jfabernathy on Wed Jan 06, 2021 8:59 pm

brubell wrote:
So my question is where is this piece of code sending it's data to and who is sending back the response? I would like that to be some server in my house, but I'm stuff figuring out what's happening here.


This code sends data to io.adafruit.com's http api (https://io.adafruit.com/api/docs/#adafruit-io-http-api).

You may want to use the Adafruit_Requests library to send "raw" HTTP requests to your home server instead, the code does not rely on Adafruit IO: https://github.com/adafruit/Adafruit_Ci ... n_Requests

I may be missing something, but I can't get any of these to run on my Metro ESP32-S2. The ones that look close import socket which doesn't exist and if I change to socketpool, I get runtime errors

jfabernathy
 
Posts: 76
Joined: Thu Dec 17, 2020 10:58 pm

Re: Adafruit IO Metro ESP32-S2

by jfabernathy on Wed Jan 06, 2021 9:17 pm

I was able to fix one of the http examples. Here's my code.
Code: Select all | TOGGLE FULL SIZE
# adafruit_requests usage with a CPython socket
import socketpool
import ssl
import adafruit_requests as requests
import wifi
import secrets

# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

print("Connecting to %s"%secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!"%secrets["ssid"])
print("My IP address is", wifi.radio.ipv4_address)

socket = socketpool.SocketPool(wifi.radio)
https = requests.Session(socket, ssl.create_default_context())

TEXT_URL = "https://httpbin.org/get"
JSON_GET_URL = "https://httpbin.org/get"
JSON_POST_URL = "https://httpbin.org/post"

# print("Fetching text from %s" % TEXT_URL)
# response = requests.get(TEXT_URL)
# print("-" * 40)

# print("Text Response: ", response.text)
# print("-" * 40)
# response.close()

print("Fetching JSON data from %s" % JSON_GET_URL)
response = https.get(JSON_GET_URL)
print("-" * 40)

print("JSON Response: ", response.json())
print("-" * 40)

data = "31F"
print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
response = https.post(JSON_POST_URL, data=data)
print("-" * 40)

json_resp = response.json()
# Parse out the 'data' key from json_resp dict.
print("Data received from server:", json_resp["data"])
print("-" * 40)

json_data = {"Date": "July 25, 2019"}
print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
response = https.post(JSON_POST_URL, json=json_data)
print("-" * 40)

json_resp = response.json()
# Parse out the 'json' key from json_resp dict.
print("JSON Data received from server:", json_resp["json"])
print("-" * 40)

jfabernathy
 
Posts: 76
Joined: Thu Dec 17, 2020 10:58 pm

Re: Adafruit IO Metro ESP32-S2

by jfabernathy on Wed Jan 06, 2021 9:31 pm

I also found a fix for the previously commented out section. This is what I ended up with:
Code: Select all | TOGGLE FULL SIZE
# adafruit_requests usage with a CPython socket
import socketpool
import ssl
import adafruit_requests as requests
import wifi
import secrets

# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

print("Connecting to %s"%secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!"%secrets["ssid"])
print("My IP address is", wifi.radio.ipv4_address)

socket = socketpool.SocketPool(wifi.radio)
https = requests.Session(socket, ssl.create_default_context())

TEXT_URL = "https://httpbin.org/get"
JSON_GET_URL = "https://httpbin.org/get"
JSON_POST_URL = "https://httpbin.org/post"

print("Fetching text from %s" % TEXT_URL)
response = https.get(TEXT_URL)
print("-" * 40)
print("Text Response: ", response.text)
print("-" * 40)
response.close()

print("Fetching JSON data from %s" % JSON_GET_URL)
response = https.get(JSON_GET_URL)
print("-" * 40)

print("JSON Response: ", response.json())
print("-" * 40)

data = "31F"
print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
response = https.post(JSON_POST_URL, data=data)
print("-" * 40)

json_resp = response.json()
# Parse out the 'data' key from json_resp dict.
print("Data received from server:", json_resp["data"])
print("-" * 40)

json_data = {"Date": "July 25, 2019"}
print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
response = https.post(JSON_POST_URL, json=json_data)
print("-" * 40)

json_resp = response.json()
# Parse out the 'json' key from json_resp dict.
print("JSON Data received from server:", json_resp["json"])
print("-" * 40)

jfabernathy
 
Posts: 76
Joined: Thu Dec 17, 2020 10:58 pm

Re: Adafruit IO Metro ESP32-S2

by brubell on Fri Jan 08, 2021 11:23 am

Awesome - glad you got it working.

If you want, you could submit a pull request to https://github.com/adafruit/Adafruit_Ci ... ests/pulls with the ESP32-S2 requests code for others to use.

brubell
 
Posts: 1381
Joined: Fri Jul 17, 2015 10:33 pm

Re: Adafruit IO Metro ESP32-S2

by jfabernathy on Fri Jan 08, 2021 11:35 am

brubell wrote:Awesome - glad you got it working.

If you want, you could submit a pull request to https://github.com/adafruit/Adafruit_Ci ... ests/pulls with the ESP32-S2 requests code for others to use.

I would love to do that but have no idea what you are asking. I have a github account, but really don't know how to "submit a pull request" or what that means. when I wrote code we used IBM punch cards :-)

jfabernathy
 
Posts: 76
Joined: Thu Dec 17, 2020 10:58 pm

Re: Adafruit IO Metro ESP32-S2

by brubell on Fri Jan 08, 2021 11:40 am

jfabernathy wrote:
brubell wrote:Awesome - glad you got it working.

If you want, you could submit a pull request to https://github.com/adafruit/Adafruit_Ci ... ests/pulls with the ESP32-S2 requests code for others to use.

I would love to do that but have no idea what you are asking. I have a github account, but really don't know how to "submit a pull request" or what that means. when I wrote code we used IBM punch cards :-)


We have a whole guide about this process here - https://learn.adafruit.com/contribute-t ... and-github

If you have questions, post up on discord.gg/adafruit in the #help-with-git channel

brubell
 
Posts: 1381
Joined: Fri Jul 17, 2015 10:33 pm

Re: Adafruit IO Metro ESP32-S2

by jfabernathy on Fri Jan 08, 2021 11:43 am

okay, I tried to do the pull request. I have not idea what I did. Just hope it wasn't the wrong thing.

jfabernathy
 
Posts: 76
Joined: Thu Dec 17, 2020 10:58 pm

Re: Adafruit IO Metro ESP32-S2

by brubell on Fri Jan 08, 2021 12:01 pm

Cool, thanks, we'll continue this discussion over there...

brubell
 
Posts: 1381
Joined: Fri Jul 17, 2015 10:33 pm

Please be positive and constructive with your questions and comments.


cron