Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
- //needed for library
- #include <DNSServer.h>
- #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
- #include <WiFiClient.h>
- #include <ESP8266HTTPClient.h>
- char start_url[]="http://192.168.1.150:7777/startup.html";
- char loop_url[]="http://192.168.1.150:7777/loop.html";
- //amount to wait before loop start
- int wait=10000;
- //amount to wait each loop
- int loop_wait=5000;
- void request();
- void setup() {
- WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
- // put your setup code here, to run once:
- Serial.begin(115200);
- // WiFi.mode(WiFi_STA); // it is a good practice to make sure your code sets wifi mode how you want it.
- //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
- WiFiManager wm;
- //reset settings - wipe credentials for testing
- //wm.resetSettings();
- // Automatically connect using saved credentials,
- // if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
- // if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
- // then goes into a blocking loop awaiting configuration and will return success result
- bool res;
- // res = wm.autoConnect(); // auto generated AP name from chipid
- res = wm.autoConnect("PumpAP"); // anonymous ap
- // res = wm.autoConnect("AutoConnectAP","password"); // password protected ap
- if(!res) {
- Serial.println("Failed to connect");
- // ESP.restart();
- }
- else {
- //if you get here you have connected to the WiFi
- Serial.println("connected...yeey :)");
- request(start_url);
- }
- delay(wait);
- }
- void request(char *url){
- WiFiClient client;
- HTTPClient http;
- Serial.print("[HTTP] begin...\n");
- if (http.begin(client, url)) { // HTTP
- Serial.print("[HTTP] GET...\n");
- // start connection and send HTTP header
- int httpCode = http.GET();
- // httpCode will be negative on error
- if (httpCode > 0) {
- // HTTP header has been send and Server response header has been handled
- Serial.printf("[HTTP] GET... code: %d\n", httpCode);
- // file found at server
- if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
- String payload = http.getString();
- Serial.println(payload);
- }
- } else {
- Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
- }
- http.end();
- } else {
- Serial.printf("[HTTP} Unable to connect\n");
- }
- }
- void loop() {
- request(loop_url);
- delay(loop_wait);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement