Advertisement
metalx1000

ESP8266 WiFi Manager HTTP client Example

Aug 11th, 2020
948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
  2.  
  3. //needed for library
  4. #include <DNSServer.h>
  5. #include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager
  6. #include <WiFiClient.h>
  7. #include <ESP8266HTTPClient.h>
  8.  
  9. char start_url[]="http://192.168.1.150:7777/startup.html";
  10. char loop_url[]="http://192.168.1.150:7777/loop.html";
  11.  
  12. //amount to wait before loop start
  13. int wait=10000;
  14.  
  15. //amount to wait each loop
  16. int loop_wait=5000;
  17.  
  18. void request();
  19.  
  20. void setup() {
  21.     WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
  22.  
  23.     // put your setup code here, to run once:
  24.     Serial.begin(115200);
  25.    
  26.     // WiFi.mode(WiFi_STA); // it is a good practice to make sure your code sets wifi mode how you want it.
  27.  
  28.     //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
  29.     WiFiManager wm;
  30.  
  31.     //reset settings - wipe credentials for testing
  32.     //wm.resetSettings();
  33.  
  34.     // Automatically connect using saved credentials,
  35.     // if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
  36.     // if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
  37.     // then goes into a blocking loop awaiting configuration and will return success result
  38.  
  39.     bool res;
  40.     // res = wm.autoConnect(); // auto generated AP name from chipid
  41.     res = wm.autoConnect("PumpAP"); // anonymous ap
  42.     // res = wm.autoConnect("AutoConnectAP","password"); // password protected ap
  43.  
  44.     if(!res) {
  45.         Serial.println("Failed to connect");
  46.         // ESP.restart();
  47.     }
  48.     else {
  49.         //if you get here you have connected to the WiFi    
  50.         Serial.println("connected...yeey :)");
  51.         request(start_url);
  52.     }
  53.   delay(wait);
  54. }
  55.  
  56. void request(char *url){
  57.     WiFiClient client;
  58.  
  59.     HTTPClient http;
  60.  
  61.     Serial.print("[HTTP] begin...\n");
  62.     if (http.begin(client, url)) {  // HTTP
  63.  
  64.  
  65.       Serial.print("[HTTP] GET...\n");
  66.       // start connection and send HTTP header
  67.       int httpCode = http.GET();
  68.  
  69.       // httpCode will be negative on error
  70.       if (httpCode > 0) {
  71.         // HTTP header has been send and Server response header has been handled
  72.         Serial.printf("[HTTP] GET... code: %d\n", httpCode);
  73.  
  74.         // file found at server
  75.         if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
  76.           String payload = http.getString();
  77.           Serial.println(payload);
  78.         }
  79.       } else {
  80.         Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  81.       }
  82.  
  83.       http.end();
  84.     } else {
  85.       Serial.printf("[HTTP} Unable to connect\n");
  86.     }
  87.  
  88.  
  89.  
  90. }
  91.  
  92. void loop() {
  93.   request(loop_url);
  94.   delay(loop_wait);  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement