Krejzi_Dark

esp

Jun 10th, 2021 (edited)
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266HTTPClient.h>
  2. #include <ESP8266WiFiMulti.h>
  3.  
  4. ESP8266WiFiMulti wifiMulti;
  5.  
  6. // REPLACE with your Domain name and URL path or IP address with path
  7. const char* serverName = "http://*********************";
  8.  
  9. const uint32_t connectTimeoutMs = 5000;
  10.  
  11. String str;
  12.  
  13. void setup() {
  14.   Serial.begin(115200);
  15.  
  16.   Serial.begin(115200);
  17.   // Set in station mode
  18.   WiFi.mode(WIFI_STA);
  19.  
  20.   // Register multi WiFi networks
  21.   wifiMulti.addAP("1111", "pass11");
  22.   wifiMulti.addAP("2222", "pass22");
  23.   wifiMulti.addAP("3333", "pass33");
  24.   while (WiFi.status() != WL_CONNECTED) {
  25.     delay(500);
  26.     Serial.print(".");
  27.   }
  28. }
  29.  
  30. void loop() {
  31.   if (Serial.available()) {
  32.     str = Serial.readString();
  33.    
  34.     //Check WiFi connection status
  35.     if (wifiMulti.run(connectTimeoutMs) == WL_CONNECTED) {
  36.       Serial.print("Connected ");
  37.       Serial.println(WiFi.localIP());
  38.       HTTPClient http;
  39.  
  40.       // Your Domain name with URL path or IP address with path
  41.       http.begin(serverName);
  42.  
  43.       // Specify content-type header
  44.       http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  45.  
  46.       // Prepare your HTTP POST request data
  47.       String httpRequestData = str;
  48.  
  49.       Serial.print("httpRequestData: ");
  50.       Serial.println(httpRequestData);
  51.  
  52.       // Send HTTP POST request
  53.       int httpResponseCode = http.POST(httpRequestData);
  54.  
  55.       if (httpResponseCode > 0) {
  56.         Serial.print("HTTP Response code: ");
  57.         Serial.println(httpResponseCode);
  58.       }
  59.       else {
  60.         Serial.print("Error code: ");
  61.         Serial.println(httpResponseCode);
  62.       }
  63.       http.end();
  64.     }
  65.     else {
  66.       Serial.println("WiFi Disconnected");
  67.     }
  68.   }
  69. }
Add Comment
Please, Sign In to add comment