Skip to content

Commit 902c7c2

Browse files
author
Noyel Seth
committed
[add] example code of HTTP Get/Post Operation using ESP8266 lib and RPi-Pico
1 parent 32bf050 commit 902c7c2

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

example/http-get-post/main.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from machine import UART, Pin
2+
import time
3+
from esp8266 import ESP8266
4+
5+
esp01 = ESP8266()
6+
esp8266_at_ver = None
7+
8+
led=Pin(25,Pin.OUT)
9+
10+
print("StartUP",esp01.startUP())
11+
#print("ReStart",esp01.reStart())
12+
print("StartUP",esp01.startUP())
13+
print("Echo-Off",esp01.echoING())
14+
print("\r\n\r\n")
15+
16+
'''
17+
Print ESP8266 AT comand version and SDK details
18+
'''
19+
esp8266_at_var = esp01.getVersion()
20+
if(esp8266_at_var != None):
21+
print(esp8266_at_var)
22+
23+
'''
24+
set the current WiFi in SoftAP+STA
25+
'''
26+
esp01.setCurrentWiFiMode()
27+
28+
#apList = esp01.getAvailableAPs()
29+
#for items in apList:
30+
# print(items)
31+
#for item in tuple(items):
32+
# print(item)
33+
34+
print("\r\n\r\n")
35+
36+
'''
37+
Connect with the WiFi
38+
'''
39+
print("Try to connect with the WiFi..")
40+
while (1):
41+
if "WIFI CONNECTED" in esp01.connectWiFi("ssid","pwd"):
42+
print("ESP8266 connect with the WiFi..")
43+
break;
44+
else:
45+
print(".")
46+
time.sleep(2)
47+
48+
49+
print("\r\n\r\n")
50+
print("Now it's time to start HTTP Get/Post Operation.......\r\n")
51+
52+
while(1):
53+
led.toggle()
54+
time.sleep(1)
55+
56+
'''
57+
Going to do HTTP Get Operation with www.httpbin.org/ip, It return the IP address of the connected device
58+
'''
59+
httpCode, httpRes = esp01.doHttpGet("www.httpbin.org","/ip","RaspberryPi-Pico", port=80)
60+
print("------------- www.httpbin.org/ip Get Operation Result -----------------------")
61+
print("HTTP Code:",httpCode)
62+
print("HTTP Response:",httpRes)
63+
print("-----------------------------------------------------------------------------\r\n\r\n")
64+
65+
66+
'''
67+
Going to do HTTP Post Operation with www.httpbin.org/post
68+
'''
69+
post_json="abcdefghijklmnopqrstuvwxyz" #"{\"name\":\"Noyel\"}"
70+
httpCode, httpRes = esp01.doHttpPost("www.httpbin.org","/post","RPi-Pico", "application/json",post_json,port=80)
71+
print("------------- www.httpbin.org/post Post Operation Result -----------------------")
72+
print("HTTP Code:",httpCode)
73+
print("HTTP Response:",httpRes)
74+
print("--------------------------------------------------------------------------------\r\n\r\n")
75+
#break
76+
77+

0 commit comments

Comments
 (0)