🎁📬❄️📦Adafruit Holiday Shipping Deadlines 2019: Please place all UPS 2 Day orders by 11am Monday, December 16 📦❄️📬🎁
0

wifi post data to IO Feed encounter runtime error
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.

wifi post data to IO Feed encounter runtime error

by davidluca123 on Tue Nov 12, 2019 1:54 am

Hi, I am currently trying to post data collected by my adafruit to my account feed. However, I am encountering a runtime error when I try to post my data. I am currently trying to run the code following the "Internet Connect!" tutorial here https://learn.adafruit.com/adafruit-air ... et-connect

My code currently looks like this.

Code: Select all | TOGGLE FULL SIZE
import time
import board
import busio
from digitalio import DigitalInOut
import neopixel
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
from adafruit_io.adafruit_io import IO_HTTP
import adafruit_requests as requests
import adafruit_esp32spi.adafruit_esp32spi_socket as socket



print("ESP32 SPI webclient test")

# 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

# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.D13)
esp32_ready = DigitalInOut(board.D11)
esp32_reset = DigitalInOut(board.D12)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
"""Use below for Most Boards"""
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards

wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)

counter = 0

while True:
    try:
        print("Posting data...", end='')
        data = counter
        feed = 'welcome-feed'
        payload = {'value':data}
        response = wifi.post(
            "https://io.adafruit.com/api/v2/"+secrets['aio_username']+"/feeds/"+feed+"/data",
            json=payload,
            headers={"X-AIO-KEY":secrets['aio_key']})
        print(response.json())
        response.close()
        counter = counter + 1
        print("OK")
    except (ValueError) as e:
        print("Failed to get data, retrying\n")
        print(e)
        wifi.reset()
        continue
    response = None
    time.sleep(15)


However, I am getting this error message:
Code: Select all | TOGGLE FULL SIZE
  File "main.py", line 55, in <module>
  File "main.py", line 46, in <module>
  File "/lib/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py", line 221, in post
  File "adafruit_requests.py", line 275, in post
  File "adafruit_requests.py", line 236, in request
  File "adafruit_requests.py", line 190, in request
  File "/lib/adafruit_esp32spi/adafruit_esp32spi_socket.py", line 83, in connect
  File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 696, in socket_connect
  File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 624, in socket_open
  File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 319, in _send_command_get_response
  File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 308, in _wait_response_cmd
  File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 295, in _wait_response_cmd
  File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 277, in _check_data
RuntimeError: Expected 01 but got 00

where line 46 in "main.py" is "headers={"X-AIO-KEY":secrets['aio_key']}" in the wifi.post call.

I have tried running
Code: Select all | TOGGLE FULL SIZE
curl -F 'value=42' -H "X-AIO-Key: {io_key}" https://io.adafruit.com/api/v2/{username}/feeds/{feed_key}/data
in my terminal and was able to successfully post my data onto my feed through that. However, I cannot successfully get my main.py code to run on my Adafruit.

I am currently using a Feather M04 and an AirLift FeatherWing - ESP32 WiFi Co-Processor.
I will greatly appreciate it if you can help me understand why this error is happening or direct me on how to fix this issue.

davidluca123
 
Posts: 7
Joined: Sat Oct 19, 2019 2:30 am

Re: wifi post data to IO Feed encounter runtime error

by brubell on Wed Nov 13, 2019 11:36 am

Are you able to run the ESP32SPI simpletest code on your Feather+AirLift: https://github.com/adafruit/Adafruit_Ci ... pletest.py

Do you receive the same error?

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

Re: wifi post data to IO Feed encounter runtime error

by davidluca123 on Wed Nov 13, 2019 12:15 pm

Hi brubell, I think I am able to get data from the JSON_URL correctly. I get a syntax error when trying to print out my JSON file.
Here is my output:
Code: Select all | TOGGLE FULL SIZE
code.py output:
ESP32 SPI webclient test
ESP32 found and in idle mode
Firmware vers. bytearray(b'1.2.2\x00')
MAC addr: ['0x88', '0x7b', '0x11', '0x12', '0xcf', '0xa4']
   Brown-Guest      RSSI: -73
   eduroam      RSSI: -73
   Brown      RSSI: -74
   Brown-Guest      RSSI: -83
   eduroam      RSSI: -83
   Brown-Guest      RSSI: -83
   eduroam      RSSI: -83
   Brown      RSSI: -83
   Brown-Guest      RSSI: -84
   eduroam      RSSI: -85
Connecting to AP...
Connected to Brown-Guest    RSSI: -70
My IP address is ***.**.***.***
IP lookup adafruit.com: ***.**.**.***
Ping google.com: 65535 ms
Fetching text from http://wifitest.adafruit.com/testwifi/index.html
----------------------------------------
<html>
<head>
<meta http-equiv='refresh' content='1; url=http://wifitest.adafruit.com/testwifi/index.html?cmd=redirect&arubalp=12345'>
</head>
</html>

----------------------------------------

Fetching json from http://api.coindesk.com/v1/bpi/currentprice/USD.json
----------------------------------------
Traceback (most recent call last):
  File "code.py", line 57, in <module>
  File "adafruit_requests.py", line 132, in json
ValueError: syntax error in JSON

davidluca123
 
Posts: 7
Joined: Sat Oct 19, 2019 2:30 am

Re: wifi post data to IO Feed encounter runtime error

by brubell on Thu Nov 14, 2019 10:38 am

Could you update the adafruit_requests module to the latest version in the Adafruit CircuitPython Bundle?
https://github.com/adafruit/Adafruit_Ci ... hon_Bundle

Also please try updating ESP32SPI to the latest version as well.

- brent r.

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

Re: wifi post data to IO Feed encounter runtime error

by davidluca123 on Thu Nov 14, 2019 5:53 pm

Thanks for the reply. I have updated to the most recent version of adafruit_requests.py and adafruit_esp32spi from the Github. Unfortunately, I am still getting the error.

davidluca123
 
Posts: 7
Joined: Sat Oct 19, 2019 2:30 am

Re: wifi post data to IO Feed encounter runtime error

by davidluca123 on Fri Nov 15, 2019 2:42 am

Sorry, after digging more into the code, I think the requests.get() call actually might not be returning the correct value in the ESP32SPI simpletest code. From the output of the ESP32SPI simpletest code, it seems like the r = requests.get(TEXT_URL) did not get the the text content from the url but instead the just the buffer. When I printed out the content for the r = requests.get(JSON_URL) call, I also only get the buffer which is b"<html>\n<head>\n<meta http-equiv='refresh' content='1; url=http://api.coindesk.com/v1/bpi/currentprice/USD.json?cmd=redirect&arubalp=12345'>\n</head>\n</html>\n".

It seems like the correct output of the simplist code should be:
Image

davidluca123
 
Posts: 7
Joined: Sat Oct 19, 2019 2:30 am

Re: wifi post data to IO Feed encounter runtime error

by davidluca123 on Fri Nov 15, 2019 3:32 am

I am also trying to connect with MQTT to see if that will work, however, I seem to be getting an error as well.
Here is my MQTT code:
Code: Select all | TOGGLE FULL SIZE
import time
import board
import busio
from digitalio import DigitalInOut
import neopixel
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_minimqtt import MQTT



print("ESP32 SPI webclient test")

# 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

# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.D13)
esp32_ready = DigitalInOut(board.D11)
esp32_reset = DigitalInOut(board.D12)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
"""Use below for Most Boards"""
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
print(status_light)


wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
temperature_feed = secrets['aio_username'] + 'feeds/temperature'

# Define callback methods which are called when events occur
# pylint: disable=unused-argument, redefined-outer-name
def connected(client, userdata, flags, rc):
    # This function will be called when the client is connected
    # successfully to the broker.
    print('Connected to Adafruit IO! Listening for topic changes on %s' % onoff_feed)
    # Subscribe to all changes on the onoff_feed.
    client.subscribe(onoff_feed)
 
 
def disconnected(client, userdata, rc):
    # This method is called when the client is disconnected
    print('Disconnected from Adafruit IO!')
 
 
def message(client, topic, message):
    # This method is called when a topic the client is subscribed to
    # has a new message.
    print('New message on topic {0}: {1}'.format(topic, message))

wifi.connect()


mqtt_client = MQTT(socket,
                   broker='io.adafruit.com',
                   username=secrets['aio_username'],
                   password=secrets['aio_key'],
                   network_manager=wifi)
 
# Setup the callback methods above
mqtt_client.on_connect = connected
mqtt_client.on_disconnect = disconnected
mqtt_client.on_message = message#requests.set_socket(socket, esp)

#test_feed = io.send_data('welcome-feed', 42)

print('Connecting to Adafruit IO...')
mqtt_client.connect()

counter = 0

while True:
    # Poll the message queue
    mqtt_client.loop()
 
    # Send a new message
    print('Sending photocell value: %d...' % counter)
    mqtt_client.publish(temperature_feed, counter)
    print('Sent!')
    counter += 1
    time.sleep(1)


The error I am getting is :
Code: Select all | TOGGLE FULL SIZE
Traceback (most recent call last):
  File "main.py", line 74, in <module>
  File "adafruit_minimqtt.py", line 202, in connect
MMQTTException: Invalid broker address defined.


Sorry if I am bombarding you with many updates, I want to give you as much info as I can to help you in solving the problem.

davidluca123
 
Posts: 7
Joined: Sat Oct 19, 2019 2:30 am

Re: wifi post data to IO Feed encounter runtime error

by davidluca123 on Sun Nov 17, 2019 12:47 am

Solved the problem :) It had to do with my wifi configuration

davidluca123
 
Posts: 7
Joined: Sat Oct 19, 2019 2:30 am

Re: wifi post data to IO Feed encounter runtime error

by brubell on Mon Nov 18, 2019 11:26 am

davidluca123 wrote:Solved the problem :) It had to do with my wifi configuration


Excellent - do you know what the exact issue was, so I can help people solve this in the future?

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

Please be positive and constructive with your questions and comments.