Advertisement
talofer99

arduino.ino

Mar 8th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. #include <ESP8266HTTPClient.h>
  2. #include <ESP8266WiFi.h>
  3.  
  4. String server_url = "http://192.168.43.172:8080/api/roomlog";
  5. int pir_pin = D6;
  6. unsigned long lastMovmentMillis;
  7. unsigned long timeGapForRelease = 1000 * 10;
  8.  
  9. String sendData(boolean *success);
  10.  
  11.  
  12.  
  13.  
  14. #include <FastLED.h>
  15. // How many leds in your strip?
  16. #define NUM_LEDS 24
  17. #define DATA_PIN 4
  18. // Define the array of leds
  19. CRGB leds[NUM_LEDS];
  20.  
  21. // constants won't change. They're used here to set pin numbers:
  22. const int buttonPin = 5; // the number of the pushbutton pin
  23. const int ledPin = LED_BUILTIN; // the number of the LED pin
  24.  
  25. // Variables will change:
  26. int ledState = LOW; // the current state of the output pin
  27. int buttonState = HIGH; // the current reading from the input pin
  28. int lastButtonState = HIGH; // the previous reading from the input pin
  29.  
  30. // the following variables are unsigned longs because the time, measured in
  31. // milliseconds, will quickly become a bigger number than can be stored in an int.
  32. unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
  33. unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
  34.  
  35. void setup() {
  36. Serial.begin(115200);
  37. Serial.println("System started");
  38. pinMode(buttonPin, INPUT_PULLUP);
  39. pinMode(ledPin, OUTPUT);
  40. pinMode(pir_pin, INPUT);
  41.  
  42. WiFi.begin("SSID", "PASSWORD");
  43.  
  44. Serial.print("Waiting for connection");
  45. while (WiFi.status() != WL_CONNECTED) { //Wait for the WiFI connection completion
  46. delay(500);
  47. Serial.print(".");
  48. }
  49.  
  50. Serial.println("Connected");
  51. delay(1000);
  52.  
  53. FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); // GRB ordering is assumed
  54.  
  55.  
  56. // set initial LED state
  57. SetLeds();
  58.  
  59. }
  60.  
  61. void loop() {
  62. // read the state of the switch into a local variable:
  63. int reading = digitalRead(buttonPin);
  64.  
  65. // check to see if you just pressed the button
  66. // (i.e. the input went from LOW to HIGH), and you've waited long enough
  67. // since the last press to ignore any noise:
  68.  
  69. // If the switch changed, due to noise or pressing:
  70. if (reading != lastButtonState) {
  71. // reset the debouncing timer
  72. lastDebounceTime = millis();
  73. }
  74.  
  75. if ((millis() - lastDebounceTime) > debounceDelay) {
  76. // whatever the reading is at, it's been there for longer than the debounce
  77. // delay, so take it as the actual current state:
  78.  
  79. // if the button state has changed:
  80. if (reading != buttonState) {
  81. buttonState = reading;
  82.  
  83. // only toggle the LED if the new button state is LOW
  84. if (buttonState == LOW) {
  85. Serial.println("FLIP");
  86. ledState = !ledState;
  87. lastMovmentMillis = millis();
  88. SetLeds();
  89.  
  90. }
  91. }
  92. }
  93.  
  94. // check pir
  95. if (digitalRead(pir_pin)) {
  96. lastMovmentMillis = millis();
  97. }
  98.  
  99.  
  100. // check free room mode
  101. if (ledState && lastMovmentMillis + timeGapForRelease < millis()) {
  102. ledState = !ledState;
  103. SetLeds();
  104. }
  105.  
  106.  
  107.  
  108. // save the reading. Next time through the loop, it'll be the lastButtonState:
  109. lastButtonState = reading;
  110. }
  111.  
  112. void SetLeds() {
  113. // set the LED:
  114. digitalWrite(ledPin, ledState);
  115. // set the RGB LEDS
  116. for (byte i = 0; i < NUM_LEDS; i++) {
  117. if (ledState) {
  118. leds[i] = CRGB::Red;
  119. } else {
  120. leds[i] = CRGB::Green;
  121. }
  122. delay(50);
  123. FastLED.show();
  124. } //ens for
  125. boolean isSuccess;
  126. String payLoad = sendData(&isSuccess);
  127. if (isSuccess) {
  128. Serial.println("Success !");
  129. } else {
  130. Serial.println("Fail:");
  131. Serial.println(payLoad);
  132. }
  133.  
  134. }
  135.  
  136.  
  137. String sendData(boolean *success) {
  138. HTTPClient http; //Declare object of class HTTPClient
  139.  
  140. http.begin(server_url);
  141. http.addHeader("Content-Type", "application/json");
  142.  
  143. int httpCode = http.POST("{\"state\":" + String(ledState) + "}"); //Send the request
  144. String payload = http.getString(); //Get the response payload
  145.  
  146. http.end(); //Close connection
  147.  
  148. if (httpCode == 200)
  149. *success = true;
  150. else
  151. *success = false;
  152.  
  153. return payload;
  154.  
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement