Arduino esp8266 WiFi module sends HTTP request _wi-fi

Source: Internet
Author: User
Tags readline keep alive

In the past SCM programming process, probably most people are more familiar with the MCU WiFi module and the server to establish a socket connection. However, it may be a better option to establish an HTTP connection when we are confronted with the fact that our data requires a dynamic display of the Web page, a file upload, and so on, when the two parties do not need to stay connected online.

Since there is no prior relevant experience, so in writing HTTP protocol encountered a relatively large difficulty, after a few days of debugging found the root of the problem or the HTTP request format is not correct, the server reported 400 errors, the first thought is the JSON transmission format is not correct, after a while after a toss found that the problem has not been resolved.

Then began to suspect that using the wificlient in <ESP8266WiFi.h> could not send HTTP requests, and then used the https://github.com/esp8266/Arduino/blob/master/ Libraries/esp8266httpclient in the <esp8266httpclient.h>, the problem is still not resolved before beginning to query HTTP protocol server feedback information.

1XX: Indicates information--indicates that the request has been received and continues processing
2XX: Success
3XX: Redirect-requires further action to complete the request
4XX: Client Error--the request has a syntax error or a request cannot be implemented
5XX: Server-side Error-Server failed to implement legitimate request

After starting to compare to their own HTTP request format, found the problem, the data and request must be a separate line to identify, the problem finally resolved.

Attached below is the complete code for the HTTP client side of the esp8266 module

#include <ESP8266WiFi.h> const char* SSID = "...";

Const char* Password = "...";
const char * host = "...";

const int httpport = ...;
  void Setup () {serial.begin (9600);
  Delay (10);
  Serial.println ();
  Serial.println ();
  Serial.print ("Connecting to");
  
  SERIAL.PRINTLN (SSID);
  
  Wifi.begin (SSID, password);
    while (Wifi.status ()!= wl_connected) {delay (500);
  Serial.print (".");
  } serial.println ("");  
  Serial.println ("WiFi connected");
  Serial.println ("IP address:");
Serial.println (Wifi.localip ());
  } void Loop () {serial.print ("Connecting to");
  
  SERIAL.PRINTLN (host);
  
  Wificlient client;
    if (!client.connect (host, Httpport)) {serial.println ("Connection failed");
  Return
  } delay (10); String recv = ReadLine ()//Sends data received from the serial port to the server, ReadLine () method can design while (recv!= "0") {if (Recv.length () && R ECV!= "0" && recv!= "-1200") {//Match my project's judgment criteria String data = (string) "{\" heartbeat\ ": \" "+ recv+" \ "}";
      
      int length = Data.length ();
          String postrequest = (String) ("POST") + "/http/1.1\r\n" + "content-type:application/json;charset=utf-8\r\n" +
          "Host:" + Host + ":" + Httpport + "\ r \ n" + "Content-length:" + Length + "\ r \ n" +
       "Connection:keep alive\r\n\r\n" + data+ "\ r \ n";
       Serial.println (postrequest);
       Client.print (postrequest);
       Delay (100);
  Break
  } recv = ReadLine ();
} client.stop ();
    String ReadLine () {string str;
        while (Serial.available ()) {Char temp = serial.read ();
          if (temp!= ' \ n ') {str + = char (temp);
        Delay (2);
        }//delete temp;
    else Break;//delete temp;
return str; }
To sum up, the esp8266 module and the server to establish an HTTP connection and establish a socket connection is similar to the implementation, you can use the Https://github.com/ekstrand/ESP8266wifi in the <esp8266wifi.h > Establish a connection. Connect to the server through the Connect (host, port) method in Wificlient, and use the print or write method to send the data directly.

As for how to send the data through a soft serial port to the ESP8266 WiFi module, I wrote in my last blog post

http://blog.csdn.net/lrh_12580/article/details/52048079






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.