talofer99 icon

arduino.ino

talofer99 | PRO | 03/08/20 08:12:27 PM UTC | 0 ⭐ | 396 👁️ | Never ⏰ | []
text |

4.03 KB

|

None

|

0 👍

/

0 👎

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
 String server_url = "http://192.168.43.172:8080/api/roomlog";
int pir_pin = D6;
unsigned long lastMovmentMillis;
unsigned long timeGapForRelease = 1000 * 10;
 String sendData(boolean *success);
    #include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 24
#define DATA_PIN 4
// Define the array of leds
CRGB leds[NUM_LEDS];
 // constants won't change. They're used here to set pin numbers:
const int buttonPin = 5;    // the number of the pushbutton pin
const int ledPin = LED_BUILTIN;      // the number of the LED pin
 // Variables will change:
int ledState = LOW;         // the current state of the output pin
int buttonState = HIGH;             // the current reading from the input pin
int lastButtonState = HIGH;   // the previous reading from the input pin
 // the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
 void setup() {
  Serial.begin(115200);
  Serial.println("System started");
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  pinMode(pir_pin, INPUT);
   WiFi.begin("SSID", "PASSWORD");
   Serial.print("Waiting for connection");
  while (WiFi.status() != WL_CONNECTED) {  //Wait for the WiFI connection completion
    delay(500);
    Serial.print(".");
  }
   Serial.println("Connected");
  delay(1000);
   FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);  // GRB ordering is assumed
    // set initial LED state
  SetLeds();
 }
 void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);
   // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:
   // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
   if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:
     // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;
       // only toggle the LED if the new button state is LOW
      if (buttonState == LOW) {
        Serial.println("FLIP");
        ledState = !ledState;
        lastMovmentMillis = millis();
        SetLeds();
       }
    }
  }
   // check pir
  if (digitalRead(pir_pin)) {
    lastMovmentMillis = millis();
  }
    // check free room mode
  if (ledState &&  lastMovmentMillis +  timeGapForRelease < millis()) {
    ledState = !ledState;
    SetLeds();
  }
     // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
}
 void SetLeds() {
  // set the LED:
  digitalWrite(ledPin, ledState);
  // set the RGB LEDS
  for (byte i = 0; i < NUM_LEDS; i++) {
    if (ledState) {
      leds[i] = CRGB::Red;
    } else {
      leds[i] = CRGB::Green;
    }
    delay(50);
    FastLED.show();
  } //ens for
  boolean isSuccess;
  String payLoad = sendData(&isSuccess);
  if (isSuccess) {
    Serial.println("Success !");
  } else {
    Serial.println("Fail:");
    Serial.println(payLoad);
  }
 }
  String sendData(boolean *success) {
  HTTPClient http;    //Declare object of class HTTPClient
   http.begin(server_url);
  http.addHeader("Content-Type", "application/json");
   int httpCode = http.POST("{\"state\":" + String(ledState) + "}"); //Send the request
  String payload = http.getString();                  //Get the response payload
   http.end();  //Close connection
   if (httpCode == 200)
    *success = true;
  else
    *success = false;
   return payload;
  }

Comments