ESP 8266: sending data to the site using the Get request method

Hey! This is my first post here! I decided to write it to make it easier for all beginners to get acquainted with the ESP 8266.





So we have ESP-01.





And a USB-UART converter.





Personally, I have one, but that's not the point.

You can take other modules, only the connection diagram will change.

We connect for firmware:





Immediately it is necessary to say about the pitfalls.





GPIO0 EXT_RSBT 10.

, RTS DTR.





, ESP-01 – 3,3. 3,3.





3,3 USB-UART , 220 ESP-01.





lm1117-3,3. , ESP-01 ESPlorer Lua Arduino.





– Arduino IDE.

esp8266 .

.

, <> Generic ESP8266 Module.





.





#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h> 
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
boolean recievedFlag;
String strData, Link;

//     WIFI 
const char *ssid = "**************";                           //  
const char *password = "*************";                           //   
const char *host = "**********************************";       //   
const int httpsPort = 443;                                    //   HTTPS= 443  HTTP = 80
const char fingerprint[] PROGMEM = "5B:FB:D1:D4:49:D3:0F:A9:C6:40:03:34:BA:E0:24:05:AA:D2:E2:01";    //  

//     t  WIFI 
void setup() {
 delay(1000);                                                 //
 Serial.begin(9600);                                          //  UART
 WiFi.mode(WIFI_OFF);                                         //  
 delay(1000);                                                 //
 WiFi.mode(WIFI_STA);                                         // ESP   
 WiFi.begin(ssid, password);                                  //   
 Serial.println("");
 Serial.print("Connecting");                                  //  UART  
 //  
 while (WiFi.status() != WL_CONNECTED) {delay(500);
 Serial.print(".");}                                          //     UART 
 Serial.println("");                                          //  
 Serial.print("Connected to ");                               //  UART:
 Serial.println(ssid);                                        //  
 Serial.print("IP address: ");
 Serial.println(WiFi.localIP());                              //IP    ESP
}

//    
void transmit() {
 WiFiClientSecure httpsClient;                                //   WiFiClient
 Serial.println(host);                                        //  UART:    ,
 Serial.printf("Using fingerprint '%s'\n", fingerprint);      //  .
 httpsClient.setFingerprint(fingerprint);                     //    
 httpsClient.setTimeout(15000);                               //   (15 )
 delay(1000);                                                 //                                     
 Serial.print("HTTPS Connecting");                            //  UART:     
 int r=0;                                                     //    
 while((!httpsClient.connect(host, httpsPort)) && (r < 30))
 {delay(100);Serial.print(".");r++;}                          //        UART 
 if(r==30) {Serial.println("Connection failed");}             //      UART,     
 else {Serial.println("Connected to web");}                   //     UART,    
 Link = "/get.php?" + strData;                                //   GET 
 Serial.print("requesting URL: ");                            //  UART   GET 
 Serial.println(host+Link);                                   //  UART GET 
 httpsClient.print(String("GET ") + Link + " HTTP/1.1\r\n" +  
 "Host: " + host + "\r\n" + 
 "Connection: close\r\n\r\n");                                // GET   ESP
 Serial.println("request sent");                              //  UART  GET  
 while (httpsClient.connected())                              //   
 {String line = httpsClient.readStringUntil('\n');
 if (line == "\r") {Serial.println("headers received");break;}}
 Serial.println("reply was:");                                //  UART    
 Serial.println("==========");                                //    UART  
 String line;                                                 //     
 while(httpsClient.available()){                              //    
 line = httpsClient.readStringUntil('\n');                    
 Serial.println(line);}                                       //  UART    
 Serial.println("==========");                                //    UART  
 Serial.println("closing connection");}                       //  UART,      
 
// 
void loop() {
  while (Serial.available() > 0)                              //    UART      
  { strData +=(char)Serial.read();                            //    
    recievedFlag = true; delay(2); };                         //     
  if (recievedFlag)                                           //      
  { Serial.println(strData);                                  //   UART       
    transmit();                                               //    
    strData ="";                                              //     
    recievedFlag = false;}                                    //  
}

      
      



. .

: Wi-Fi / / UART.





:

const char fingerprint[] PROGMEM = "5B:FB:D1:D4:49:D3:0F:A9:C6:40:03:34:BA:E0:24:05:AA:D2:E2:01"; //



.





.. HTTPS « ».





: https://www.grc.com/fingerprints.htm





“Fingerprint Site”.





, .





:





! Esp .





«id=123456789» «». « » «9600 » .





, .





«https://www.000webhost.com».





. Get ( ).

.





« » …





… .





« ».





«public_html».





«New File» .





test.txt ( ).





«get.php» :





<?php
  $var1= $_GET['id'];
  
  $fileContent = "Registrated id= ".$var1."\n";
  
  $fileStatus = file_put_contents('test.txt',$fileContent,FILE_APPEND);
  if($fileStatus != false)
  {
     echo "SUCCESS: data written to file";
	}
	else
	{
	echo "FAIL: could not write to file";
	}
	?>
      
      



.

.





: «SUCCESS: data written to file».





test.txt….





.





test.txt . « /test.txt».





- .





. , , . , ..





. .. - . !








All Articles