SHARE
TWEET

Untitled

a guest Jan 20th, 2020 94 in 153 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <ArduinoOTA.h>
  3.  
  4. const char* ssid = "............."; // Your WLAN SSID
  5. const char* password = "............"; // Your WLAN Password
  6. const char* OTA_hostname = "button_coffee_pot"; // The hostname that you will find under network ports
  7. long buttonTimer = 0; //The nomal press time
  8. long longPressTime = 1000; //The long press time
  9. const char* host = "192.168.9.200"; // IP of the Node-RED instance
  10.  
  11. int buttonPin = D8;
  12.  
  13. boolean buttonActive = false;
  14. boolean longPressActive = false;
  15.  
  16. void http_request() {
  17.   WiFiClient client;
  18.   if (client.connect(host,1880))
  19.   {
  20.     Serial.println("connected to server");
  21.     client.println("GET /testing/");
  22.     client.println("Connection: close");
  23.     client.println();
  24.   }
  25. } // end void http_request
  26.  
  27. void setup() {
  28.  
  29.   pinMode(buttonPin, INPUT_PULLUP);
  30.  
  31.   Serial.begin(9600);
  32.  
  33.   delay(10);
  34.  
  35.   Serial.println();
  36.   Serial.print("Connecting to ");
  37.   Serial.println(ssid);
  38.  
  39.   WiFi.begin(ssid, password);
  40.  
  41.   while (WiFi.status() != WL_CONNECTED) {
  42.     delay(500);
  43.     Serial.print(".");
  44.   }
  45.  
  46.   Serial.println("");
  47.   Serial.println("WiFi connected");  
  48.   Serial.println("IP address: ");
  49.   Serial.println(WiFi.localIP());
  50.  
  51.   //////////////////////////
  52.   //  Code to set up OTA  //
  53.   //////////////////////////
  54.  
  55.   Serial.println();
  56.   Serial.println("Booting");
  57.   WiFi.mode(WIFI_STA);
  58.  
  59.   // Port defaults to 8266
  60.   // ArduinoOTA.setPort(8266);
  61.  
  62.   // Hostname defaults to esp8266-[ChipID]
  63.   ArduinoOTA.setHostname(OTA_hostname);
  64.  
  65.   // No authentication by default
  66.   // ArduinoOTA.setPassword(OTA_password);
  67.  
  68.   ArduinoOTA.onStart([]() {
  69.     Serial.println("Start");
  70.   });
  71.   ArduinoOTA.onEnd([]() {
  72.     Serial.println("End");
  73.   });
  74.   ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  75.     Serial.printf("Progress: %u%%\n", (progress / (total / 100)));
  76.   });
  77.   ArduinoOTA.onError([](ota_error_t error) {
  78.     Serial.printf("Error[%u]: ", error);
  79.     if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  80.     else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  81.     else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  82.     else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  83.     else if (error == OTA_END_ERROR) Serial.println("End Failed");
  84.   });
  85.   ArduinoOTA.begin();
  86.   Serial.println("Ready");
  87.   Serial.print("IP address: ");
  88.   Serial.println(WiFi.localIP());
  89.  
  90.   ///////////////////////////////
  91.   //  END: Code to set up OTA  //
  92.   ///////////////////////////////
  93.  
  94. } // End void setup
  95.  
  96. void loop() {
  97.  
  98.   ArduinoOTA.handle(); // Needs to be here for OTA
  99.  
  100.   // If button pressed
  101.   if ((digitalRead(buttonPin)==0)){
  102.     if (buttonActive == false) {
  103.  
  104.       buttonActive = true;
  105.       buttonTimer = millis();
  106.  
  107.     }
  108.     if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) {
  109.  
  110.       longPressActive = true;
  111.       Serial.println("Button held!");
  112.       http_request();
  113.  
  114.     }
  115.   } else {
  116.     if (buttonActive == true) {
  117.  
  118.       if (longPressActive == true) {
  119.  
  120.         longPressActive = false;
  121.  
  122.       } else {
  123.  
  124.         Serial.println("Button pressed!");
  125.  
  126.       }
  127.  
  128.       buttonActive = false;
  129.  
  130.     }
  131.   } // End if button pressed
  132.  
  133. } // End void loop
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top