Main-1.py:
Code: Select all
from machine import UART, Pin, I2C, PWM, SPI
from esp8266 import ESP8266
import time, sys, utime
from secret import AP_SSID, AP_Pwd, DataBase_URL, DataBase_Secret, Firebase_ProjectName, Web_API
from PicoLCD import LCD_1inch14
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("RPi-Pico MicroPython Ver:", sys.version)
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
#Credentials
SSID=AP_SSID
Pass=AP_Pwd
DB_URL=DataBase_URL
DB_Secret=DataBase_Secret
## Create On-board Led object
led=Pin(25,Pin.OUT)
## Create an ESP8266 Object
esp01 = ESP8266()
esp8266_at_ver = None
'''
Test communication to ESP8266
'''
print("Communication: ",esp01.startUP())
while (esp01.startUP() == True):
break;
print("StartUP",esp01.startUP())
#print("ReStart",esp01.reStart())
print("StartUP",esp01.startUP())
print("Echo-Off",esp01.echoING())
print("\r\n\r\n")
'''
Print ESP8266 AT comand version and SDK details
'''
esp8266_at_ver = esp01.getVersion()
if(esp8266_at_ver != None):
print(esp8266_at_ver)
'''
set the current WiFi in SoftAP+STA
'''
esp01.setCurrentWiFiMode()
#apList = esp01.getAvailableAPs()
#for items in apList:
# print(items)
#for item in tuple(items):
# print(item)
print("\r\n\r\n")
'''
Connect with the WiFi
'''
print("Try to connect with the WiFi..")
while (1):
if "WIFI CONNECTED" in esp01.connectWiFi(SSID,Pass):
print("ESP8266 connect with the WiFi..")
break;
else:
print(".")
time.sleep(2)
print("\r\n\r\n")
print("Now it's time to start Communication to Ubidots\r\n")
while(1):
led.toggle()
time.sleep(1)
'''
Going to do HTTP Post Operation with industrial.api.ubidots.com/post
'''
post_json="UbidotsESP8266/1.0|POST|BBFF-5zUpQ1TOUpcMyYitTu75gaitbNOGjT|sennah=>tempC:21.42667,tempF:70.56801|end" #"{\"name\":\"Noyel\"}"
httpCode, httpRes = esp01.doHttpPost("industrial.api.ubidots.com","/post","RPi-Pico", "application/json",post_json,port=9012)
print("------------- www.httpbin.org/post Post Operation Result -----------------------")
print("HTTP Code:",httpCode)
print("HTTP Response:",httpRes)
print("--------------------------------------------------------------------------------\r\n\r\n")
#break
Code: Select all
Traceback (most recent call last):
File "<stdin>", line 97, in <module>
File "esp8266.py", line 496, in doHttpPost
File "httpParser.py", line 35, in parseHTTP
IndexError: list index out of range
Line 97: httpCode, httpRes = esp01.doHttpPost("industrial.api.ubidots.com","/post","RPi-Pico", "application/json",post_json,port=9012)
Line 496 (The specific line is inbetween the ๐): def doHttpPost(self,host,path,content_type,content,user_agent="RPi-Pico",port=9012):
"""
This fucntion use to complete a HTTP Post operation
Parameter:
host (str): Host URL [ex: get operation URL: www.httpbin.org/ip. so, Host URL only "www.httpbin.org"]
path (str): Get operation's URL path [ex: get operation URL: www.httpbin.org/ip. so, the path "/ip"]
user-agent (str): User Agent Name [Default "RPi-Pico"]
content_type (str): Post operation's upload content type [ex. "application/json", "application/x-www-form-urlencoded", "text/plain"
content (str): Post operation's upload content
post (int): HTTP post number [Default port number 80]
Return:
HTTP error code & HTTP response[If error not equal to 200 then the response is None]
On failed return 0 and None
"""
if(self._createTCPConnection(host, port) == True):
self._createHTTPParseObj()
postHeader="POST "+path+" HTTP/1.1\r\n"+"Host: "+host+"\r\n"+"User-Agent: "+user_agent+"\r\n"+"Content-Type: "+content_type+"\r\n"+"Content-Length: "+str(len(content))+"\r\n"+"\r\n"+content+"\r\n";
#print(postHeader,len(postHeader))
txData="AT+CIPSEND="+str(len(postHeader))+"\r\n"
retData = self._sendToESP8266(txData)
if(retData != None):
if ">" in retData:
retData = self._sendToESP8266(postHeader, delay=2)
print(".......@@",retData)
self._sendToESP8266("AT+CIPCLOSE\r\n")
print(self.__httpResponse)
retData=self.__httpResponse.parseHTTP(retData)
๐ return retData, self.__httpResponse.getHTTPResponse() ๐
else:
return 0, None
else:
return 0, None
else:
self._sendToESP8266("AT+CIPCLOSE\r\n")
return 0, None
Line 35(Same as previous, with ๐):
def parseHTTP(self, httpRes):
"""
This funtion use to parse the HTTP response and return back the HTTP status code
Return:
HTTP status code.
"""
#print(">>>>",httpRes)
if(httpRes != None):
retParseResponse=str(httpRes).partition("+IPD,")[2]
#print(">>>>>>>>>>>>>>>>>",retParseResponse)
retParseResponse=retParseResponse.split(r"\r\n\r\n");
#print(">>>>>>>>>>>>>>>>>",retParseResponse[0])
๐ self.__httpResponse = retParseResponse[1]๐
#print(">>>>>>>>>>>>>>>>>???",retParseResponse[1])
self.__httpHeader=str(retParseResponse[0]).partition(":")[2]
#print("--",self.__httpHeader)
for code in str(self.__httpHeader.partition(r"\r\n")[0]).split():
if code.isdigit():
self.__httpErrCode=int(code
if(self.__httpErrCode != 200):
self.__httpResponse=None
return self.__httpErrCode
else:
return 0