send HTTP Request ESP8266

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
User avatar
jimmo
Posts: 429
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: send HTTP Request ESP8266

Post by jimmo » Sat May 18, 2019 1:20 am

Just to confirm something -- you don't need to put anything in /lib. urequests is built-in to the firmware.

Here's a demo, starting from a brand new ESP8266 module.

Code: Select all

jimmo@jimmo-x240: ~
11:17 $ esptool --port /dev/ttyUSB0 erase_flash
esptool.py v2.6
Serial port /dev/ttyUSB0
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
MAC: 5c:cf:7f:19:a3:02
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 10.1s
Hard resetting via RTS pin...

jimmo@jimmo-x240: ~
11:17 $ esptool --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 ~/downloads/esp8266-20190125-v1.10.bin 
esptool.py v2.6
Serial port /dev/ttyUSB0
Connecting.......
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
MAC: 5c:cf:7f:19:a3:02
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Flash params set to 0x0040
Compressed 615388 bytes to 399928...
Wrote 615388 bytes (399928 compressed) at 0x00000000 in 9.3 seconds (effective 528.7 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

jimmo@jimmo-x240: ~
11:18 $ miniterm.py /dev/ttyUSB0 115200
--- Miniterm on /dev/ttyUSB0  115200,8,N,1 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
MicroPython v1.10-8-g8b7039d7d on 2019-01-26; ESP module with ESP8266
Type "help()" for more information.
>>> import urequests
>>> help(urequests)
object <module 'urequests'> is of type module
  Response -- <class 'Response'>
  patch -- <function patch at 0x3ffef0f0>
  __name__ -- urequests
  request -- <function request at 0x3ffef0c0>
  delete -- <function delete at 0x3ffef100>
  get -- <function get at 0x3ffeefe0>
  head -- <function head at 0x3ffef000>
  post -- <function post at 0x3ffeeff0>
  put -- <function put at 0x3ffef0e0>
  usocket -- <module 'lwip'>
>>> 

Christian Walther
Posts: 52
Joined: Fri Aug 19, 2016 11:55 am

Re: send HTTP Request ESP8266

Post by Christian Walther » Sat May 18, 2019 9:29 am

samerou wrote:
Fri May 17, 2019 11:32 pm

Code: Select all

>>> help(urequests)
object <module 'urequests'> is of type module
  __name__ -- urequests
  __path__ -- /lib/urequests
>>> 
So you’re not getting the built-in module, but one from the filesystem. I can think of two possible reasons for that.

1. You don’t have the built-in module, so you must be running a different firmware than I am. I am pretty sure that I used the official stable 1.10 release, and its boot output is

Code: Select all

MicroPython v1.10-8-g8b7039d7d on 2019-01-26; ESP module with ESP8266
What’s yours? And what does

Code: Select all

help('modules')
say, does it list urequests?

2. Your sys.path is different from mine. What is it? Run

Code: Select all

import sys
sys.path
It should be

Code: Select all

['', '/lib', '/']
where the empty string means built-in modules, so that these are found before the ones in /lib. Yours possibly has /lib before the empty string. Is there anything in boot.py that changes it?

samerou
Posts: 38
Joined: Mon Feb 11, 2019 12:51 pm

Re: send HTTP Request ESP8266

Post by samerou » Sat May 18, 2019 4:33 pm

Christian Walther wrote:
Sat May 18, 2019 9:29 am
samerou wrote:
Fri May 17, 2019 11:32 pm

Code: Select all

>>> help(urequests)
object <module 'urequests'> is of type module
  __name__ -- urequests
  __path__ -- /lib/urequests
>>> 
So you’re not getting the built-in module, but one from the filesystem. I can think of two possible reasons for that.

1. You don’t have the built-in module, so you must be running a different firmware than I am. I am pretty sure that I used the official stable 1.10 release, and its boot output is

Code: Select all

MicroPython v1.10-8-g8b7039d7d on 2019-01-26; ESP module with ESP8266
What’s yours? And what does

Code: Select all

help('modules')
say, does it list urequests?

2. Your sys.path is different from mine. What is it? Run

Code: Select all

import sys
sys.path
It should be

Code: Select all

['', '/lib', '/']
where the empty string means built-in modules, so that these are found before the ones in /lib. Yours possibly has /lib before the empty string. Is there anything in boot.py that changes it?
Hello ,

After testing

Code: Select all

help('modules') 
see Output

Code: Select all

 help('modules')
__main__          hashlib           socket            urandom
_boot             inisetup          ssl               ure
_onewire          io                struct            uselect
_webrepl          json              sys               usocket
apa102            lwip              time              ussl
array             machine           ubinascii         ustruct
binascii          math              ucollections      utime
btree             micropython       ucryptolib        utimeq
builtins          neopixel          uctypes           uzlib
collections       network           uerrno            webrepl
dht               ntptime           uhashlib          webrepl_setup
ds18x20           onewire           uheapq            websocket
errno             os                uio               websocket_helper
esp               port_diag         ujson             zlib
flashbdev         random            uos
framebuf          re                upip
gc                select            upip_utarfile
Plus any modules on the filesystem


and this is the output of sys.path same as you

Code: Select all

>>> import sys
>>> sys.path
['', '/lib', '/']
and this is my boot.py

Code: Select all






import os 
try:
  import usocket as socket
except:
  import socket

import ussl as ssl
from machine import Pin
import network

import esp
esp.osdebug(None)

import gc
gc.collect()

ssid = 'itserv'
password = 'azerty98769876'

station = network.WLAN(network.STA_IF)

station.active(True)
station.connect(ssid, password)

while station.isconnected() != False:
  pass

print('Connection successful')
print(station.ifconfig())

led = Pin(2, Pin.OUT)
I think I should reinstall firmware ?

samerou
Posts: 38
Joined: Mon Feb 11, 2019 12:51 pm

Re: send HTTP Request ESP8266

Post by samerou » Sat May 18, 2019 5:24 pm

I flashed my esp8266 and now I can see urequests there with the get function :

Code: Select all

>>> help('modules')
__main__          http_client       socket            upip
_boot             http_client_ssl   ssd1306           upip_utarfile
_onewire          http_server       ssl               upysh
_webrepl          http_server_ssl   struct            urandom
apa102            inisetup          sys               ure
array             io                time              urequests
binascii          json              uasyncio/__init__ urllib/urequest
btree             lwip              uasyncio/core     uselect
builtins          machine           ubinascii         usocket
collections       math              ucollections      ussl
dht               micropython       ucryptolib        ustruct
ds18x20           neopixel          uctypes           utime
errno             network           uerrno            utimeq
esp               ntptime           uhashlib          uzlib
example_pub_button                  onewire           uheapq            webrepl
example_sub_led   os                uio               webrepl_setup
flashbdev         port_diag         ujson             websocket
framebuf          random            umqtt/robust      websocket_helper
gc                re                umqtt/simple      zlib
hashlib           select            uos
Plus any modules on the filesystem
and now After executing this line

Code: Select all

>>> response = urequests.get('http:/192.168.137.1:80/info.php?requests=sam')
I get this error :

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "urequests.py", line 112, in get
  File "urequests.py", line 53, in request
OSError: -2



Edit : I did a big mistake when I flashed my esp8266 It deleted all my files :o :o :o :o :o :o :o is there a way to recover them ????

Christian Walther
Posts: 52
Joined: Fri Aug 19, 2016 11:55 am

Re: send HTTP Request ESP8266

Post by Christian Walther » Sat May 18, 2019 5:38 pm

samerou wrote:
Sat May 18, 2019 5:24 pm
I flashed my esp8266 and now I can see urequests there with the get function :
Glad we got that one solved!

Code: Select all

>>> response = urequests.get('http:/192.168.137.1:80/info.php?requests=sam')
I don’t know if that’s the source of the error, but you’re missing a slash in that URL. Should be http://192....
Edit : I did a big mistake when I flashed my esp8266 It deleted all my files :o :o :o :o :o :o :o is there a way to recover them ????
Oops! If you did esptool.py erase_flash as recommended, then no. If not, then you might be able to dump the whole flash and recover something from it, but I don’t know if that is possible or how to do it (you could try experimenting with esptool.py read_flash).

By the way, if you’re always connecting to the same WiFi network, you don’t need to do that in boot.py. The ESP8266 remembers the last network and automatically reconnects.

samerou
Posts: 38
Joined: Mon Feb 11, 2019 12:51 pm

Re: send HTTP Request ESP8266

Post by samerou » Sat May 18, 2019 5:57 pm

Christian Walther wrote:
Sat May 18, 2019 5:38 pm
samerou wrote:
Sat May 18, 2019 5:24 pm
I flashed my esp8266 and now I can see urequests there with the get function :
Glad we got that one solved!

Code: Select all

>>> response = urequests.get('http:/192.168.137.1:80/info.php?requests=sam')
I don’t know if that’s the source of the error, but you’re missing a slash in that URL. Should be http://192....
Edit : I did a big mistake when I flashed my esp8266 It deleted all my files :o :o :o :o :o :o :o is there a way to recover them ????
Oops! If you did esptool.py erase_flash as recommended, then no. If not, then you might be able to dump the whole flash and recover something from it, but I don’t know if that is possible or how to do it (you could try experimenting with esptool.py read_flash).

By the way, if you’re always connecting to the same WiFi network, you don’t need to do that in boot.py. The ESP8266 remembers the last network and automatically reconnects.


I changed the URL as http:// but this is the error

Code: Select all

re= urequests.get('http://192.168.137.1:80//info.php?request=sam')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "urequests.py", line 112, in get
  File "urequests.py", line 100, in request
  File "urequests.py", line 58, in request
OSError: [Errno 103] ECONNABORTED
I tested the URL on the browser and it worked perfectly but with the urequests still no

Christian Walther
Posts: 52
Joined: Fri Aug 19, 2016 11:55 am

Re: send HTTP Request ESP8266

Post by Christian Walther » Sat May 18, 2019 7:21 pm

Now there’s one slash too many in the other place, maybe your browser is smart enough to strip that but urequests isn’t?

Try

Code: Select all

urequests.get('http://192.168.137.1:80/info.php?request=sam')
Since it looks like this is a web server running on your local network, you could also take a look at its logs and see what the difference is between the successful and the failed request.

Post Reply