ESP32 Wifi post request?

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
13en
Posts: 3
Joined: Thu Nov 07, 2019 6:00 pm

ESP32 Wifi post request?

Post by 13en » Thu Nov 07, 2019 6:13 pm

Hello Everyone! I'm trying to post data from my BME280 sensor to my websites API which worked absolutely fine with c++ but i can't seem to nail it with MP

error

Code: Select all

Ready to download this file,please wait!
.......
download ok
e
>>> 
Full code, The Sensor on it's own works absolutely fine, the Wifi Works checked by using station.isconnected()

Code: Select all

from machine import Pin, I2C
from time import sleep
import network
import BME280
import urequests

# ESP32 - Pin assignment
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=10000)

station = network.WLAN(network.STA_IF)
station.active(True)
station.connect("...", "...")

logger = "http://reep.com/api"

while True:
  bme = BME280.BME280(i2c=i2c)
  temp = bme.temperature
  hum = bme.humidity
  pres = bme.pressure
  # uncomment for temperature in Fahrenheit
  #temp = (bme.read_temperature()/100) * (9/5) + 32
  #temp = str(round(temp, 2)) + 'F'
  print('Temperature: ', temp)
  print('Humidity: ', hum)
  print('Pressure: ', pres)
  
  url = logger+"?temperature="+str(temp)+"&humidity="+str(hum)

    print ("url: "+url)

    response = urequests.get(url)

    response.close()

    time.sleep(60)


  sleep(5)

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ESP32 Wifi post request?

Post by jimmo » Thu Nov 07, 2019 8:51 pm

What error do you get?

I don't know if it's an issue with copying into the forum but the indentation is wrong for everything from the print(url...) line onwards.

13en
Posts: 3
Joined: Thu Nov 07, 2019 6:00 pm

Re: ESP32 Wifi post request?

Post by 13en » Thu Nov 07, 2019 11:21 pm

Hey Jimmo, I noticed that after!

The only error i receive is this

Code: Select all

Ready to download this file,please wait!
.......
download ok
e

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ESP32 Wifi post request?

Post by jimmo » Thu Nov 07, 2019 11:35 pm

Sorry I don't follow - the code you posted wouldn't print that output?

13en
Posts: 3
Joined: Thu Nov 07, 2019 6:00 pm

Re: ESP32 Wifi post request?

Post by 13en » Thu Nov 07, 2019 11:52 pm

I'm not sure what's going on either. Here's the latest error after trying to run the code

Code: Select all

exec(open('main.py').read(),globals()m
 Nx atic rx buf'ioTemnelist index X5>>> I (603174) wifi: new:<6,0>, old:<1,0>, ap:<255,255>, sta:<6,0>, prof:1
I (6i:[
full script

Code: Select all

from machine import Pin, I2C
from time import sleep
import network
import BME280
import urequests

# ESP32 - Pin assignment
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=10000)

station = network.WLAN(network.STA_IF)
station.active(True)
station.connect("ssid", "password")

logger = "http://website.com/api"

while True:
  bme = BME280.BME280(i2c=i2c)
  temp = bme.temperature
  hum = bme.humidity
  pres = bme.pressure
  # uncomment for temperature in Fahrenheit
  #temp = (bme.read_temperature()/100) * (9/5) + 32
  #temp = str(round(temp, 2)) + 'F'
  print('Temperature: ', temp)
  print('Humidity: ', hum)
  print('Pressure: ', pres)
  
  url = logger+"?temperature="+str(temp)+"&humidity="+str(hum)

  print ("url: "+url)

  response = urequests.get(url)

  response.close()


  sleep(5)

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ESP32 Wifi post request?

Post by jimmo » Fri Nov 08, 2019 2:03 am

I think somethings going wrong with the way you're copying the files to the device -- it doesn't look like your main,py is actually running. (Are you using an IDE that's responsible for that exec() line?) I've seen a few people with this issue on the forum...and it always involves this exec(open('main.py'...) etc.

Can you try using pyboard.py [1] or rshell [2] or ampy [3]

[1] https://github.com/micropython/micropyt ... pyboard.py
[2] https://github.com/dhylands/rshell
[3] https://github.com/scientifichackers/ampy

I personally use pyboard.py -- my workflow is either:

Code: Select all

$ pyboard.py --device <device> filename.py
(which will run filename.py on the device and print any output)

or

Code: Select all

$ pyboard.py --device <device> -f cp main.py :main.py
(then connect with a serial terminal, hit ctrl-d to soft reset)

Post Reply