Arduino Esp8266 Post Data to Website

460K178174

Intro: Arduino Esp8266 Post Data to Website

The ESP8266 WiFi Module is a self contained SOC with integrated TCP/IP protocol stack that can give any microcontroller access to your WiFi network. It offers a complete and self-contained Wi-Fi networking solution, allowing it to either host the application or to offload all Wi-Fi networking functions from another application processor.When ESP8266 hosts the application, and when it is the only application processor in the device, it is able to boot up directly from an external flash. It has integrated cache to improve the performance of the system in such applications, and to minimize the memory requirements.

For this project We are going to use The Esp8266-01 .

STEP 1: What You Will Need ??

To accomplish this simple yet amazing project you will need a few components that I personally got from Gearbest.com mainly thanks to their High-Quality products and also for their Free-shipping WORLDWIDE. I mean who wouldn't like that !! So the list of the components needed is down below:

  • Arduino Uno (you can get the official one or other clones ,same for this project)

Official 8.00 $ : http://www.gearbest.com/development-boards/pp_4772...

  • DHT11 Temperature Humidity Sensor Module 2.68 $ :

http://www.gearbest.com/sensors/pp_238712.html

  • 3X 220 Ohm resistors (Voltage divider, explaination in the circuit diagram below) from your local electronics-shop.
  • A few Male to Female jumper Wires 1.88 $ :

http://www.gearbest.com/diy-parts-components/pp_23...

  • A website : 0.00 $

Finally this project won't cost you any higher than 20 $. Isn't That Amazing ?!!

STEP 2: Circuit Diagram :

This project is based on a very simple Circuit that anyone can do it .

Power Consumption:

First, you need to keep in mind that the Esp8266 runs on 3.3V , connecting it to 5V could work but it is not recommanded.According to its Datasheet,the amperage needed for the Esp8266 to work correctly varies according to its state ( transmitting, receiving, deep sleep ,etc... ), and reaches its highest at around 200 mA while the Arduino's DC Current for 3.3V Pin is Only 50mA,it actually works but you won't get a perfect result.

So, there are plenty of other ways to power the module (using diodes, using Zener diode, voltage regulator). I believe that the best solution is to make an independant 5 to 3.3V power regulator circuit, you will need the following components :

  • LM1117 or LD1117 : A very common and cheap 3.3 V regulator that allows you to convert the 5V power used by the Arduino to the 3.3V needed by the Esp8266 module.
  • A 10 µF capacitor.

Then, when it comes to the TX and RX connections, you have to avoid connecting the TX of the Arduino directly to the RX of the Esp module since that could damage both of them, so two easy solutions you can use to solve this issue. The first one is to use a simple voltage divider with resistors (as in the diagram above) to drop down the 5V voltage from the TX Pin of the Arduino to 3.3 V or you can use a level shifter with a diode, they both do the job correctly and it's up to you to choose either one for your project.

STEP 3: Testing the Module :

If this is the First time you use your Esp8266 module, you probably should test it first by sending AT Commands manually to it . You can do that with an FTDI USB to Serial Converter like this one http://www.gearbest.com/other-accessories/pp_22726... or you can simply use your arduino.

If you're going to use your arduino you have to arrange your connection as in the image above :

Pin 0 Arduino (Rx pin) ---->> Rx Pin Module

Pin 1 Arduino (Tx pin) ---->> Tx Pin Module

And for the power supply use the power regulator circuit as described in the previous step of this article.

Then, you will have to upload into the arduino an empty code means ( void setup() {} void loop () {} ) and open the Serial monitor window to type the commands just remember that newer versions of the esp module use 9600 baudrate while older ones use 57600 or 115200 baud rates. The Esp8266 responds to a few commands called AT Commands, this is a list of the most basic ones to help you get going and you can find more complex ones just google it.

http://www.pridopia.co.uk/pi-doc/ESP8266ATCommands...

After checking out that your module is working correctly by responding positively to the commands sent you can finally move to the next step and start your HTTP post project.

STEP 4: Create a Website First

So first we need to create our own website and to do that there are plenty of free domain and hosting websites. I personally chose https://www.000webhost.com/ which sounds pretty much convenient for our simple task.

You can create a free website using their free subdomain service then just follow their instructions and you should receive an email that lists your FTP Username and FTP Password and your website will be fully functional within 24 hours. After you will actually need a software that allows you to send the files (php, HTML , images ...) from your computer to the host servers and to do that I used FileZilla you can download it from here. Before sending any file we need to make a file, so in this example we need to make a php file that we will name httppost.php using NotePad ++ ,which you can download from here, (check the image above for the code and a little explaination).After creating your php server file you are going to actually send that file to the servers that host your website and this is where FileZilla comes in,to accomplish that we first need to establish a connection between your computer and the host servers then you right-click on the file that you wish to send ,in this case esp8266.php, and then transfer.

STEP 5: Sending the Data

First you need to understand the basics of an HTTP.The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers.

HTTP works as a request-response protocol between a client and server. A web browser may be the client, and an application on a computer that hosts a web site may be the server. Example: A client (browser) submits an HTTP request to the server; then the server returns a response to the client. The response contains status information about the request and may also contain the requested content. On our example the esp8266 is the client and the server that is hosting our website is the server.

So performing an HTTP Post request has to be under a certain form:

POST /esppost.php HTTP/1.0

Host: serverconnect.site88.net

Accept: */*

Content-Length: "name1=value1&name2=value2".Length

Content-Type: application/x-www-form-urlencoded


name1=value1&name2=value2

You can find more explaination and informations in the Arduino sketch below.After uploading the Sketch , wait for a few seconds then refresh you webpage.

#include "SoftwareSerial.h"
String ssid ="yourSSID";

String password="yourPassword";

SoftwareSerial esp(6, 7);// RX, TX

String data;

String server = "yourServer"; // www.example.com

String uri = "yourURI";// our example is /esppost.php

int DHpin = 8;//sensor pin

byte dat [5];

String temp ,hum;

void setup() {

pinMode (DHpin, OUTPUT);

esp.begin(9600);

Serial.begin(9600);

reset();

connectWifi();

}

//reset the esp8266 module

void reset() {

esp.println("AT+RST");

delay(1000);

if(esp.find("OK") ) Serial.println("Module Reset");

}

//connect to your wifi network

void connectWifi() {

String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";

esp.println(cmd);

delay(4000);

if(esp.find("OK")) {

Serial.println("Connected!");

}

else {

connectWifi();

Serial.println("Cannot connect to wifi"); }

}

byte read_data () {

byte data;

for (int i = 0; i < 8; i ++) {

if (digitalRead (DHpin) == LOW) {

while (digitalRead (DHpin) == LOW); // wait for 50us

delayMicroseconds (30); // determine the duration of the high level to determine the data is '0 'or '1'

if (digitalRead (DHpin) == HIGH)

data |= (1 << (7-i)); // high front and low in the post

while (digitalRead (DHpin) == HIGH);

// data '1 ', wait for the next one receiver

}

} return data; }

void start_test () {

digitalWrite (DHpin, LOW); // bus down, send start signal

delay (30); // delay greater than 18ms, so DHT11 start signal can be detected

digitalWrite (DHpin, HIGH);

delayMicroseconds (40); // Wait for DHT11 response

pinMode (DHpin, INPUT);

while (digitalRead (DHpin) == HIGH);

delayMicroseconds (80);

// DHT11 response, pulled the bus 80us

if (digitalRead (DHpin) == LOW);

delayMicroseconds (80);

// DHT11 80us after the bus pulled to start sending data

for (int i = 0; i < 4; i ++)

// receive temperature and humidity data, the parity bit is not considered

dat[i] = read_data ();

pinMode (DHpin, OUTPUT);

digitalWrite (DHpin, HIGH);

// send data once after releasing the bus, wait for the host to open the next Start signal

}

void loop () {

start_test ();

// convert the bit data to string form

hum = String(dat[0]);

temp= String(dat[2]);

data = "temperature=" + temp + "&humidity=" + hum;// data sent must be under this form //name1=value1&name2=value2.

httppost();

delay(1000);

}

void httppost () {

esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.

if( esp.find("OK")) {

Serial.println("TCP connection ready");

} delay(1000);

String postRequest =

"POST " + uri + " HTTP/1.0\r\n" +

"Host: " + server + "\r\n" +

"Accept: *" + "/" + "*\r\n" +

"Content-Length: " + data.length() + "\r\n" +

"Content-Type: application/x-www-form-urlencoded\r\n" +

"\r\n" + data;

String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.

esp.print(sendCmd);

esp.println(postRequest.length() );

delay(500);

if(esp.find(">")) { Serial.println("Sending.."); esp.print(postRequest);

if( esp.find("SEND OK")) { Serial.println("Packet sent");

while (esp.available()) {

String tmpResp = esp.readString();

Serial.println(tmpResp);

}

// close the connection

esp.println("AT+CIPCLOSE");

}

}}

If everything went well you should see a result similar to the image above if not unplug then replug your Arduino and wait for a few seconds.

STEP 6: You Can Go Even Further

This isn't the end of this project ,there are plenty of other stuff you can do like for example I made an Android App that will display the data instead of displaying it on a website which is a little bit boring. So to do so the app send an HTTP Get request to the website, retrieve its HTML code as string then get both the Temperature and Humidity.You can find more explaination about HTTP POST and GET from an Android App in this Instructables article that I made a while ago .You can download the App's source code down below.

The ESP8266 is just a very awesome device !! . you can perform a lot of other stuff with it like (Home automation system , Mesh network, IP cameras streaming,etc...)

Thanks for reading my article, if you have any questions about anything just leave them in the comment section below or email me at : Khalilmerchaoui@gmail.com :D :D :D

114 Comments

Hi Khalil
My code is below:

Serial.println("sent data starting");

Serial1.println("AT+CIPSTART=0,\"TCP\",\"vendingmachine.rf.gd\",80\r\n");
delay(2500);
Oku=Serial1.readString();
Serial.println(Oku);

String veri = "raf1=22&raf2=244";
String GETRequest = "GET ";
GETRequest += "/index.php";
GETRequest += " HTTP/1.1\r\n";
GETRequest += "Host: ";
GETRequest += "vendingmachine.rf.gd";
GETRequest += "\r\n";
GETRequest += "Accept: */";
GETRequest += "*\r\n";
GETRequest += "Content-Length: ";
GETRequest += String(veri.length());
GETRequest += "\r\n";
GETRequest += "Content-Type: application/x-www-form-urlencoded\r\n";
GETRequest += "\r\n";
GETRequest += veri;
String sendCmd = "AT+CIPSEND=0,";
sendCmd += GETRequest.length();
Serial1.println(sendCmd);
delay(1500);
Oku=Serial1.readString();
Serial.println(Oku);
delay(1000);
Serial1.println(GETRequest);
delay(2500);
Oku=Serial1.readString();
Serial.println(Oku);


When I started the code, sending to me a response by ESP8266 as look at below:
Recv 153 bytes

SEND OK

+IPD,0,1360:HTTP/1.1 200 OK
Server: nginx
Date: Mon, 31 Jan 2022 07:05:38 GMT
Content-Type: text/html
Content-Length: 840
Connection: keep-alive
Vary: Accept-Encoding
Expires: Thu, 01 Jan 1970 00:00:01 GMT
Cache-Control: no-cache

<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("a2765f4cbbedab577b7a69353df8a91e");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://vendingmachine.rf.gd/index.php?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>HTTP/1.1 400 Bad Request
Server: nginx
Date: Mon, 31 Jan 2022 07:05:38 GMT
Content-Type: text/html
Content-Length: 150
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
+IPD,0,4:l>
0,CLOSED


where am i doing wrong?
Could you help me with this subject?
So I have a question about this module. Am I able to send and receive information through internet to the microcontroller? I'm currently working on a project where I need to send information to a microcontroller and receive other information at the same time, all through internet. Am I able to do that using this Esp8266 module?
I have hosted a website through a free hosting website. All the required HTML and PHP pages are uploaded and everything works fine, but the issue is that, I am not able to connect my ESP8266-01 to my website, it returns an “DNS Fail” Error message when I Post the command - AT+CIPSTART=4,"TCP","www.myaccountaccess.com",80 .One probable reason for the error is that, the website has a dynamic IP (as I am hosting it under a free-trial plan). Is there any way to connect my webpage (with Dynamic IP) to ESP module.
Yes. Use a dynamic DNS like https://www.noip.com/. It gives you a domain name that automatically maps to the latest IP address assigned to your webpage.
First thank you for sharing your projects!
But is it really that simple?
Needing a webpage hosting server in a data centre somewhere in the world
To display your room temperature sensor?? Does that mean that many millions ESP "clients" use web servers at high powered data-centres just to display temperature or moisture of sensors?
The code does not come simple to me it's abracadabra HTTP: syntax using lots of // {{}}[[]] &&&%%$$##@@ signs that have to be exactly in the "right" form to let it work.
like do////$$ 64568798dhsg12@$5738/llle Jkhjhdkjkj draw a nice picture
show [[{{]l||| hhhtttssss:: no yes =temperature / speed of light in parsec




No. You can host a local server as well if you don't need to see your data from anywhere in the world. And the local server can run on a hardware as simple as an ESP itself lol. You need a powerful server only when you need thousands of ESPs connected to it, sending their data.

I have published a similar article that gets rid of the old and naive HTTP syntax: The Easiest Way to Send Data From ESP8266 to a Website : 6 Steps - Instructables. I hope this helps you understand this better.
permission denied on my server :(
note: I intensionally replaced my server address for this purpose

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"&gt;
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://mydomainname/test.php">here</a>.</p&gt;
<hr>
<address>Apache/2.4.29 (Ubuntu) Server at projects.wci.com.ph Port 80</address>
</body></html>
CLOSED

hi thank you for this article but i am comfusing about pinmode of DHpin it schould be as input and not as output
Thanks for the nice article. When you use voltage divider is it necessary to ground using the two register? Is not the esp module ground in its internal circuitry?
hi. when i want to test esp8266 , and uploding file, this error come:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer

pls help me.
Hi! Nice post. You say to use the circuit layout where you connect RX and TX to arduino pins 0 and 1 but then in the sketch you write:
SoftwareSerial esp(6, 7);// RX, TX
So, should I type the following instead?
SoftwareSerial esp(0, 1);// RX, TX
I followed all the instructions but i was not able to send data from esp8266 to the database
can anyone help me out
I have hosted a website through a free hosting website. All the required HTML and PHP pages are uploaded and everything works fine, but the issue is that, I am not able to connect my ESP8266-01 to my website, it returns an “DNS Fail” Error message when I Post the command - AT+CIPSTART=4,"TCP","www.xyzwebsite.com",80 .One probable reason for the error is that, the website has a dynamic IP (as I am hosting it under a free-trial plan). Is there any way to connect my webpage (with Dynamic IP) to ESP module. The aim of the project is to log the sensor data in real time .
Hi. I am curious as to why you didnt use the voltage divider with the RX pin when first testing the ESP using AT commands?
Hi. Can you help me? I stayed here. I cant writing datas to web site.
More Comments