ESP8266 /XAMPP-Converting arduino to Python

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
samerou
Posts: 38
Joined: Mon Feb 11, 2019 12:51 pm

ESP8266 /XAMPP-Converting arduino to Python

Post by samerou » Wed May 08, 2019 11:43 am

Hello ,

I'm working on a project that allows me to send specific data to a local Website (XAMPP server) but I tried this using arduino and it worked perfectly but when I tried to convert arduino into python i didn't succeed ,

Arduino Code :

Code: Select all

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
  void setup() {
    Serial.begin(115200);                                  //Serial connection
 WiFi.begin("SSID", "PASS");   //WiFi connection

while (WiFi.status() != WL_CONNECTED) {  //Wait for the WiFI connection completion
delay(500);
Serial.println("Waiting for connection");
HTTPClient http;    //Declare object of class HTTPClient

 String code = "1245564331";
 String url = "http://192.168.1.100:80/info.php?request=";
 http.begin(url+code);      //Specify request destination

 // http.addHeader("Content-Type", "text/plain");  //Specify content-type 
header
 int httpCode = http.POST("Message from ESP8266");   //Send the request

 String payload = http.getString();                  //Get the response 
payload
Serial.println(httpCode);   //Print HTTP return code
Serial.println(payload);    //Print request response payload
http.end();  //Close connection    
 }

}

 void loop() { 

 if(WiFi.status()== WL_CONNECTED){   //Check WiFi connection status
   }else{
 Serial.println("Error in WiFi connection");  
  }
 delay(30000);  //Send a request every 30 seconds
 }

Python code that bugged :

Code: Select all

import http.client
conn = http.client.HTTPConnection("http://192.168.8.100:80/info.php?request = sam")
conn.request("HEAD","/info.php?request = sam")
res = conn.getresponse()
print (res.status, res.reason)
# Result:
print("200 OK")
print("The pastebin URL is:%s"%pastebin_url) 


Anyone can help me in converting it ?

Best Regards ,

Polos

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

Re: ESP8266 /XAMPP-Converting arduino to Python

Post by jimmo » Wed May 08, 2019 12:26 pm

Hi,
Did you receive any error message? What did your print statement show?
I notice the URL is "/info.php?request = sam", the spaces around the = are probably not what you want.
Also, the HTTPConnection should be made to the host (and the port should be a separate argument), then request is for the path and querystring.

Code: Select all

import http.client
conn = http.client.HTTPConnection("192.168.8.100", port=80)
conn.request("HEAD","/info.php?request=sam")

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

Re: ESP8266 /XAMPP-Converting arduino to Python

Post by samerou » Wed May 08, 2019 1:30 pm

jimmo wrote:
Wed May 08, 2019 12:26 pm
Hi,
Did you receive any error message? What did your print statement show?
I notice the URL is "/info.php?request = sam", the spaces around the = are probably not what you want.
Also, the HTTPConnection should be made to the host (and the port should be a separate argument), then request is for the path and querystring.

Code: Select all

import http.client
conn = http.client.HTTPConnection("192.168.8.100", port=80)
conn.request("HEAD","/info.php?request=sam")

Hello ,

Thank you jimmo for your reply On my PC it worked but on my ESP8266 it keeps saying error in line 1 " import http.client"
ImportError : no module named 'http'

I think I should have lib http.client on my ESP but I don't how to solve it


thank you again

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

Re: ESP8266 /XAMPP-Converting arduino to Python

Post by jimmo » Wed May 08, 2019 1:34 pm

Very few libraries are included by default on MicroPython (in order to save space). Also not all Python libraries have MicroPython equivalents.

There's a minimal http.client in micropython-lib, see github.com/micropython/micropython-lib

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

Re: ESP8266 /XAMPP-Converting arduino to Python

Post by samerou » Wed May 08, 2019 1:45 pm

jimmo wrote:
Wed May 08, 2019 1:34 pm
Very few libraries are included by default on MicroPython (in order to save space). Also not all Python libraries have MicroPython equivalents.

There's a minimal http.client in micropython-lib, see github.com/micropython/micropython-lib
it didn't work I tried to change even http.client with Socket :( :

Code: Select all

import socket
conn = socket.getaddrinfo("192.168.8.100", 80)[0][-1]
conn.request("HEAD","/info.php?request=sameur")
res = conn.getresponse()
print (res.status, res.reason)
# Result:
print("200 OK")
#print("The paste

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

Re: ESP8266 /XAMPP-Converting arduino to Python

Post by jimmo » Wed May 08, 2019 1:52 pm

What happened, what error did you get when you tried to use http.client from micropython-lib

That's not quite how the socket API works, see http://docs.micropython.org/en/latest/e ... k_tcp.html for an example.

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

Re: ESP8266 /XAMPP-Converting arduino to Python

Post by samerou » Wed May 08, 2019 2:22 pm

jimmo wrote:
Wed May 08, 2019 1:52 pm
What happened, what error did you get when you tried to use http.client from micropython-lib

That's not quite how the socket API works, see http://docs.micropython.org/en/latest/e ... k_tcp.html for an example.
This an error that is displayed :cry: , so Micropython like you said is quiet different , and I don't know how to modify it solo

and which part should be modified .

All the errors are about unknown command for micropython


https://ibb.co/j8B75MS

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

Re: ESP8266 /XAMPP-Converting arduino to Python

Post by samerou » Sat May 11, 2019 11:55 pm

I tried another solution for posting a http request in order to fill XAMPP tables but without success and Arduino code still working but micropython until now no solution

What I did is adding this urequests.py https://github.com/micropython/micropyt ... ts.py#L103 in my ESP8266 then I tried this

Code: Select all

import urequests
response = urequests.post("http://192.168.137:80/info.php?request=try")
error is this
urequests.post("http://192.168.137.1:80/info.php?request=inchllh")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "urequests.py", line 230, in post
File "urequests.py", line 200, in request
File "urequests.py", line 115, in request
Stuck in this :(

Post Reply