Arduino Esp8266 Post Data to Website

364,083

156

108

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

4 People Made This Project!

Recommendations

  • Fandom Contest

    Fandom Contest
  • Beauty Tips Contest

    Beauty Tips Contest
  • Classroom Science Contest

    Classroom Science Contest

108 Discussions

0
None
albint2

4 weeks ago

I followed all the instructions but i was not able to send data from esp8266 to the database
can anyone help me out

0
None
youthresult

4 weeks ago

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 .

0
None
ibrahimatiker26

Question 2 months ago

serial monitor is null ? nothing happens

0
None
devAhsan

2 months ago

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?
0
None
furkan19635

4 months ago

Hi. Can you help me? I stayed here. I cant writing datas to web site.

Ekran G&ouml;r&uuml;nt&uuml;s&uuml; (281).pngEkran G&ouml;r&uuml;nt&uuml;s&uuml; (282).pngEkran G&ouml;r&uuml;nt&uuml;s&uuml; (280).png
0
None
kosdakan

Question 8 months ago

Hi,
I tried this project with sensor HC-SR04 in two places. Τhe one of them have been operating normally for
over six months and only one of them stop send data after few days. In this place internet has some "cuts". Is it due to poor
internet connection the above problem?

0
None
AdnanG14

8 months ago

Hello fellas

I tried this project I made a simple website with database table using 000webhost

I wrote the get.php and made the index.php

so I got a table and successfully entered random values to it.

all I need is to upload the temp , hum , values to this website .

when i used the previous code it gave me (Module reset) in the serial monitor

what should I do

0
None
MohammadddM

Question 11 months ago on Step 6

i want to have a http request in testing module status via serial monitor but esp returns this response:

AT+CIPSTART="TCP","readsensor.epizy.com",80

CONNECT

OK

AT+CIPSEND=85

OK

>

Recv 85 bytes

SEND OK

+IPD,1099:HTTP/1.1 200 OK

Server: nginx

Date: Mon, 16 Jul 2018 09:46:19 GMT

Content-Type: text/html

Content-Length: 870

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("4750527d0ec3cc0eed76933c5346b85c");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://readsensor.epizy.com/esp_post.php?temperature=40&humidity=40&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>CLOSED

Why does this happen?

0
None
GadepalliA

1 year ago

No details getting printed in Serial Monitor even on setting the Arduino RESET pin to GND and ESP8266 RESET pin to GND.

1
None
Mohammud Israt AllyK

Question 1 year ago on Introduction

Hi Khalil. I am a beginner and I am trying to use part of your tutorial for my final year project. You mentioned to use the above code in the image in a 'httppost.php' file but on the image i saw esppost.php as the name. Then u also wrote about esp8266.php as to which i can't see any php code to be written for it. Which to upload and what write in them? Could you please clear up my doubts asap?

0
None
ebeed79

1 year ago

Great instructable. I like the way you put all the information together. ESP8266 is my best chip to use in all new projects. Thanks for posting

0
None
PabloA133

Tip 1 year ago

Do not forget about wiring the RESET or RST pin to GROUND or GND if you are using ARDUINO, very sure that a lot of you are aware of this situation but I wasted all my morning trying to know why the serial monitor did not work, and it was just this wire!!!

0
None
verticalfarmwebsite

Question 1 year ago

Hello,

After following every single step, I get this error message when I try to load the website. Moreover, for some reason my Serial Monitor is not printing anything.


Fatal error: Uncaught Error: Function name must be a string in /storage/ssd3/597/5266597/public_html/esppost.php:3 Stack trace: #0 {main} thrown in /storage/ssd3/597/5266597/public_html/esppost.php on line 3


1 answer
0
None
bryan8404304

Answer 1 year ago

*Update, I was able to post the data now in my web site but the Content-Length is still 0 in the response in the serial monitor.

0
None
AkashS88

Question 1 year ago

Hi

I am doing a project on IOT , using Arduino and ESP8266-01 module. Core Idea of my project is to transmit real time sensor values to a website. 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 . Is there any solution to connect my website to the ESP.

1 answer
0
None
MayankV8AkashS88

Answer 1 year ago

try to remove www. from the website in code

0
None
Yusryl

Question 1 year ago

Hi.. I'm new to this page. I followed all the steps and yet I still get no reaction from my ESP8266 and the web. I have created the web and yet it still blanks and display nothing when opened. Please help me

2 answers
0
None
YusrylYusryl

Answer 1 year ago

By the way Im not quite sure on where to find the server and urI