Advertisement
kayduino

IoT Button HTTP

Apr 2nd, 2018
4,367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.66 KB | None | 0 0
  1. /*
  2.  POHL Dash Button NG V1
  3.  17.02.2018
  4.  by Kay Pohl
  5. */
  6.  
  7. /*
  8. ##############################################################################################
  9. ##############################################################################################
  10.  
  11.   _____            _           _      
  12.  |_   _|          | |         | |    
  13.    | |  _ __   ___| |_   _  __| | ___
  14.    | | | '_ \ / __| | | | |/ _` |/ _ \
  15.   _| |_| | | | (__| | |_| | (_| |  __/
  16.  |_____|_| |_|\___|_|\__,_|\__,_|\___|
  17.  
  18.  
  19. ##############################################################################################
  20. ##############################################################################################
  21. */                                  
  22.  
  23. #include <ESP8266WiFi.h>
  24. #include <ESP8266HTTPClient.h>
  25. #include <WiFiClient.h>
  26. #include <ESP8266WebServer.h>
  27. #include <ESP8266mDNS.h>
  28. #include <ESP8266HTTPUpdateServer.h>
  29.  
  30. /*
  31. ##############################################################################################
  32. ##############################################################################################
  33.  
  34.   _____        __ _            
  35.  |  __ \      / _(_)          
  36.  | |  | | ___| |_ _ _ __   ___
  37.  | |  | |/ _ \  _| | '_ \ / _ \
  38.  | |__| |  __/ | | | | | |  __/
  39.  |_____/ \___|_| |_|_| |_|\___|
  40.                                
  41.                                
  42. ##############################################################################################
  43. ##############################################################################################
  44. */  
  45.  
  46. #define LED 2               // GPIO2 - Led
  47. #define SETUP_BUTTON 12     // Webupdate Button
  48.  
  49. /*
  50. ##############################################################################################
  51. ##############################################################################################
  52.  
  53.   _    _               _                        
  54.  | |  | |             | |                      
  55.  | |__| | __ _ _ __ __| |_      ____ _ _ __ ___
  56.  |  __  |/ _` | '__/ _` \ \ /\ / / _` | '__/ _ \
  57.  | |  | | (_| | | | (_| |\ V  V / (_| | | |  __/
  58.  |_|  |_|\__,_|_|  \__,_| \_/\_/ \__,_|_|  \___|
  59.  
  60.                                                                        
  61. ##############################################################################################
  62. ##############################################################################################
  63. */  
  64.  
  65. ADC_MODE(ADC_VCC); //vcc read
  66.  
  67. /*
  68. ##############################################################################################
  69. ##############################################################################################
  70.  
  71.   _____                               _            
  72.  |  __ \                             | |          
  73.  | |__) |_ _ _ __ __ _ _ __ ___   ___| |_ ___ _ __
  74.  |  ___/ _` | '__/ _` | '_ ` _ \ / _ \ __/ _ \ '__|
  75.  | |  | (_| | | | (_| | | | | | |  __/ ||  __/ |  
  76.  |_|   \__,_|_|  \__,_|_| |_| |_|\___|\__\___|_|  
  77.                                                    
  78.                          
  79. ##############################################################################################
  80. ##############################################################################################
  81. */
  82. //Wifi
  83. const char* host = "dash-webupdate";
  84. const char* ssid = "xxx";
  85. const char* password = "xxx";
  86. //Server
  87. const String server_ip = "xxx0";
  88. const String server_port = "3777";
  89. const String server_webhook = "/hook/dash_buero/";
  90. const String server_add = "?VCC=";
  91. //Hardware
  92. boolean mSetupButton = LOW;  
  93. int ledState = HIGH;
  94. unsigned long previousMillis = 0;
  95. const long interval = 500;
  96.  
  97. /*
  98. ##############################################################################################
  99. ##############################################################################################
  100.  
  101.    _____      _              
  102.   / ____|    | |              
  103.  | (___   ___| |_ _   _ _ __  
  104.   \___ \ / _ \ __| | | | '_ \
  105.   ____) |  __/ |_| |_| | |_) |
  106.  |_____/ \___|\__|\__,_| .__/
  107.                        | |    
  108.                        |_|    
  109.  
  110. ##############################################################################################
  111. ##############################################################################################
  112. */
  113.  
  114. ESP8266WebServer httpServer(80);
  115. ESP8266HTTPUpdateServer httpUpdater;
  116.  
  117.  
  118. void setup() {
  119.  
  120. // Setup Webupdate Button
  121. pinMode(SETUP_BUTTON, INPUT_PULLUP);
  122. // LED Output
  123. pinMode(LED, OUTPUT); // Port als Ausgang schalten
  124.  
  125. // Connect with WIFI
  126. Serial.begin(115200);
  127.   WiFi.hostname("dash_buero");
  128.   WiFi.mode(WIFI_STA);
  129.   WiFi.begin(ssid, password);
  130.  
  131.   while (WiFi.status() != WL_CONNECTED) {
  132.  
  133.     delay(1000);
  134.     Serial.println("Connecting..");
  135.  
  136.   }
  137.  
  138.   Serial.println("Connected ...");
  139.  
  140. // Setup httpUpdater
  141.   MDNS.begin(host);
  142.  
  143.   httpUpdater.setup(&httpServer);
  144.   httpServer.begin();
  145.  
  146.   MDNS.addService("http", "tcp", 80);
  147.  
  148.   //Read Setup Button
  149.   if (digitalRead(SETUP_BUTTON) == HIGH) {
  150.       webhook();
  151.   }
  152.  
  153.   Serial.println("Webupdate started ...");
  154.  
  155. }
  156.  
  157. /*
  158. ##############################################################################################
  159. ##############################################################################################
  160.  
  161.   _                      
  162.  | |                      
  163.  | |     ___   ___  _ __  
  164.  | |    / _ \ / _ \| '_ \
  165.  | |___| (_) | (_) | |_) |
  166.  |______\___/ \___/| .__/
  167.                    | |    
  168.                    |_|    
  169.  
  170. ##############################################################################################
  171. ##############################################################################################
  172. */
  173.  
  174. void loop() {
  175.  
  176. //HTTPServer Handle Client
  177. httpServer.handleClient();
  178.  
  179. unsigned long currentMillis = millis();
  180.  
  181. if (currentMillis - previousMillis >= interval) {
  182.     // save the last time you blinked the LED
  183.     previousMillis = currentMillis;
  184.  
  185.     // if the LED is off turn it on and vice-versa:
  186.     if (ledState == LOW) {
  187.         ledState = HIGH;
  188.     }
  189.     else {
  190.         ledState = LOW;
  191.     }
  192.  
  193.     // set the LED with the ledState of the variable:
  194.     digitalWrite(LED, ledState);
  195. }
  196.  
  197.  
  198. }
  199.  
  200. /*
  201. ##############################################################################################
  202. ##############################################################################################
  203. __          __  _     _                 _
  204. \ \        / / | |   | |               | |
  205.  \ \  /\  / /__| |__ | |__   ___   ___ | | __
  206.   \ \/  \/ / _ \ '_ \| '_ \ / _ \ / _ \| |/ /
  207.    \  /\  /  __/ |_) | | | | (_) | (_) |   <
  208.    \ /  \/ \___|_.__/|_| |_|\___/ \___/|_|\_\
  209.  
  210. ##############################################################################################
  211. ##############################################################################################
  212. */
  213.  
  214. void webhook() {
  215.  
  216.     // Onboard LED ON
  217.     digitalWrite(LED, LOW);
  218.  
  219.     // Read internal VCC
  220.     float vdd = ESP.getVcc() / 1000.0;
  221.     vdd = vdd + 0.1;
  222.     String vdd_string;
  223.     vdd_string = String(vdd);
  224.  
  225.     // If Wifi is connected send Webhook and then goto deepsleep
  226.     // Check WiFi connection status
  227.     if (WiFi.status() == WL_CONNECTED) {
  228.  
  229.         // Declare an object of class HTTPClient
  230.         HTTPClient http;
  231.         delay(100);
  232.         // Set address
  233.         http.begin("http://"+server_ip+":"+server_port+server_webhook+server_add+vdd_string);
  234.  
  235.         //Send the request
  236.         int httpCode = http.GET();                                                                
  237.         //Close connection
  238.         http.end();  
  239.         //Print address
  240.         Serial.println("http://" + server_ip + ":" + server_port + server_webhook + server_add + vdd_string);
  241.  
  242.     }
  243.  
  244.     // Onboard LED OFF
  245.     digitalWrite(LED, HIGH);
  246.     // Go into DeepSleep
  247.     Serial.print("Good Night ...");
  248.     ESP.deepSleep(0);
  249.  
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement