How to send data to webserver using ESP8266

Hi.

I've been trying to send some scanned values from my barcode scanner to my apache webserver. I can't for the life of me figure out how to do this with the Arduino IDE.
Basically the barcode scanner needs to scan a barcode, and then send that barcode to the server. The website I have created for this is a website with a submit button and a text field for the barcode. The goal is for me to scan a value, and then program (using Arduino IDE) some code which can submit that barcode into the text field of my website and press the submit button. I am using the post form to submit the barcodes to the database.

I have tried looking at tutorials for HTTP post requests but I couldn't get any of them to work. Any help is therefore greatly appreciated.

you do not call a web page or fill something there. you need to call the web server with a HTTP request the same way the page does when you submit

Juraj:
you do not call a web page or fill something there. you need to call the web server with a HTTP request the same way the page does when you submit

How do I find the HTTP request of the submit button?

No need, just press F12 in any modern browser (Chrome, Firefox, ...) and go to the network tab.

You have created a browser page hosted on an Apache server. That browser page has a field for the barcode number and a submit button. Is that right ?

  1. Does that work in that if you enter a bar code number in the field and press submit, that data gets into the database ?

  2. If that works, then you have to issue an HTTP GET request on your Arduino of the format
    "GET /barCodeSave.php?barCodeNumber=0123456789 HTTP/1.1"
    which would be more or less what your existing web pages does in the background when you click submit.

Look up "ESP8266 Arduino Web Client" to find examples.

6v6gt:
You have created a browser page hosted on an Apache server. That browser page has a field for the barcode number and a submit button. Is that right ?

  1. Does that work in that if you enter a bar code number in the field and press submit, that data gets into the database ?

  2. If that works, then you have to issue an HTTP GET request on your Arduino of the format
    "GET /barCodeSave.php?barCodeNumber=0123456789 HTTP/1.1"
    which would be more or less what your existing web pages does in the background when you click submit.

Look up "ESP8266 Arduino Web Client" to find examples.

Well. When I press the button it issues a POST request and not a GET request. Is it possible to issue a POST request on Arduino?

Slashdash:
Well. When I press the button it issues a POST request and not a GET request. Is it possible to issue a POST request on Arduino?

Yes. For example: ESP8266: HTTP POST Requests - techtutorialsx

6v6gt:
Yes. For example: ESP8266: HTTP POST Requests - techtutorialsx

I can't get this to work. I don't get how this can work. Doesn't it need some information such as

barcode=12345689&submit=Scan

How does the program in the link you send me know where to send the post request.

Extract from the linked example:

   HTTPClient http;    //Declare object of class HTTPClient
 
   http.begin("http://192.168.1.88:8085/hello");      //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

Change:

http.begin("http://192.168.1.88:8085/hello");      //Specify request destination

to match your environment say:

http.begin("http://myserver.com/barCodeSave.php");

Change:

int httpCode = http.POST("Message from ESP8266");   //Send the request

to match your environment say:

int httpCode = http.POST("barcode=12345689&submit=Scan");

If that does not help, post the full code you are attempting to use.

6v6gt:
Extract from the linked example:

   HTTPClient http;    //Declare object of class HTTPClient

http.begin("http://192.168.1.88:8085/hello");      //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






Change: 



http.begin("http://192.168.1.88:8085/hello");      //Specify request destination




to match your environment say:



http.begin("http://myserver.com/barCodeSave.php");





Change:



int httpCode = http.POST("Message from ESP8266");  //Send the request




to match your environment say:



int httpCode = http.POST("barcode=12345689&submit=Scan");





If that does not help, post the full code you are attempting to use.

Sorry for the late response. Thank you very much for you help, it works! I now have another problem. Is there any way I can make the esp8266 read some text from the website, and then store that text in a variable or print it in the serial monitor?

I'm pleased you had some success with it.

Doesn't this, from the linked example, write a response to the serial console:

  String payload = http.getString();                  //Get the response payload

  Serial.println(httpCode);   //Print HTTP return code
  Serial.println(payload);    //Print request response payload

If not, what, if anything, is your web server sending back in response to your http.POST command ?

6v6gt:
I'm pleased you had some success with it.

Doesn't this, from the linked example, write a response to the serial console:

  String payload = http.getString();                  //Get the response payload

Serial.println(httpCode);  //Print HTTP return code
  Serial.println(payload);    //Print request response payload




If not, what, if anything, is your web server sending back in response to your http.POST command ?

It does in HTML. I don't mind the HTML, but I would like to store a specific part of the HTML code as a variable.