Advertisement
Guest User

Untitled

a guest
Jan 5th, 2024
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.16 KB | Source Code | 0 0
  1. // #define EB_NO_FOR
  2. #define EB_NO_CALLBACK
  3. // #define EB_NO_COUNTER
  4. #define EB_NO_BUFFER
  5. #define EB_DEB_TIME 50      // таймаут гашения дребезга кнопки (кнопка)
  6. #define EB_CLICK_TIME 500   // таймаут ожидания кликов (кнопка)
  7. #define EB_HOLD_TIME 600    // таймаут удержания (кнопка)
  8. #define EB_STEP_TIME 200    // таймаут импульсного удержания (кнопка)
  9. #define EB_FAST_TIME 30     // таймаут быстрого поворота (энкодер)
  10.  
  11. #include <FastLED.h>
  12. #define NUM_LEDS 25
  13. #include <EncButton.h>
  14. Button btn(0);
  15. #include <WiFiManager.h>
  16. #include "ArduinoJson.h"
  17. #include <ESP8266HTTPClient.h>
  18.  
  19. const char* API_URL = "http://alerts.com.ua/api/states";
  20. const char* API_KEY = "your_api_key";
  21.  
  22.  
  23. int brightness = 5; // start brightness
  24. int style = 0; // style nu,
  25.  
  26.  
  27.  
  28. WiFiManager wifiManager;
  29.  
  30. unsigned long previousMillis = 0; //temp value
  31. const long interval = 1000; // api interval
  32.  
  33.  
  34.  
  35. CRGB leds[NUM_LEDS];
  36.  
  37. int get_region_id(int id){
  38.   switch(id){
  39.     case 0: return 1; //vinitsa
  40.     case 1: return 7;// volyn
  41.     case 2: return 17;// dnipropetr
  42.     case 3: return 20;// donetsk
  43.     case 4: return 10;// zhytomyr
  44.     case 5: return 4; // zakarpatya
  45.     case 6: return 21; // zaporizhzhia
  46.     case 7: return 3;// ivano-frankivsk
  47.     case 8: return 11; // kyiv oblast
  48.     case 9: return 16; // kirovohrad
  49.     case 10: return 19; // luhansk
  50.     case 11: return 5; // lviv
  51.     case 12: return 24; // mykolaiv
  52.     case 13: return 0; // odesa
  53.     case 14: return 14; // poltava
  54.     case 15: return 8; // rivne
  55.     case 16: return 13; // sumy
  56.     case 17: return 6; // ternopil
  57.     case 18: return 18; //kharkiv
  58.     case 19: return 23; //kherson
  59.     case 20: return 9;//Khmelnytskyi
  60.     case 21: return 15; // Cherkasy
  61.     case 22: return 2;// chernovetska
  62.     case 23: return 12;//chernihiv
  63.     case 24: return 11;//kyiv
  64.   }
  65.   return -1;
  66. }
  67.  
  68. void check_click_on_startup(){
  69.   if (btn.read()){
  70.       WiFiManager wifiManager;
  71.       wifiManager.startConfigPortal("Wi-Fi Ukraine Map");
  72.   }
  73. }
  74.  
  75. DynamicJsonDocument get_api_request() {
  76.   HTTPClient http;
  77.   WiFiClient client;
  78.   http.begin(client, API_URL);
  79.   http.addHeader("X-API-Key", API_KEY);
  80.   int httpCode = http.GET();
  81.   DynamicJsonDocument doc(8192); // Create the JSON document outside of the if statement
  82.  
  83.   if (httpCode > 0) {
  84.     String payload = http.getString();
  85.  
  86.     DeserializationError error = deserializeJson(doc, payload);
  87.  
  88.     if (error) {
  89.       Serial.print(F("Error parsing JSON: "));
  90.       Serial.println(error.c_str());
  91.       return DynamicJsonDocument(0);
  92.     }
  93.     if (httpCode == 200) {
  94.       Serial.println("Hooray!");
  95.     }
  96.   }
  97.  
  98.   http.end();
  99.   return doc;
  100. }
  101.  
  102. void test_led(){
  103.   for(int i=0; i<NUM_LEDS;i++){
  104.     leds[i] = CRGB::White; FastLED.show();
  105.     delay(100);
  106.   }
  107. }
  108.  
  109. void setup() {
  110.  
  111.   pinMode(0, INPUT);
  112.  
  113.   Serial.begin(115200);
  114.   FastLED.addLeds<WS2812B, 2, GRB>(leds, NUM_LEDS);
  115.   FastLED.setBrightness(brightness);
  116.   test_led();
  117.   // check_click_on_startup();
  118.   wifiManager.autoConnect("Wi-Fi Ukraine Map");
  119.  
  120.  
  121. }
  122.  
  123. void disableLed(){
  124.   // fill_solid(leds, NUM_LEDS, CRGB::Black);
  125.  
  126.   for(int i=0; i<NUM_LEDS;i++){
  127.     leds[i] = CRGB::Black;
  128.     FastLED.show();
  129.   }
  130.  
  131. }
  132.  
  133. void enableLed(){
  134.   fill_solid(leds, NUM_LEDS, CRGB::White);
  135.   FastLED.show();
  136. }
  137.  
  138. int bright_step = 5;
  139. bool isOn = true;
  140. int click_num = 0;
  141. void loop() {
  142.   btn.tick();
  143.   click_num=0;
  144.   if (btn.step(1)){
  145.     brightness += bright_step;
  146.     if (brightness>255) brightness = 255;
  147.     if (brightness<0) brightness = 1;
  148.     FastLED.setBrightness(brightness);
  149.     FastLED.show();
  150.   }
  151.  
  152.  
  153.  
  154.   if (btn.releaseStep()){
  155.     bright_step = -bright_step;
  156.     FastLED.setBrightness(brightness);
  157.     FastLED.show();
  158.   }
  159.  
  160.   if (btn.click()){
  161.     click_num+=1;
  162.     while(btn.busy()){
  163.       btn.tick();
  164.       if (btn.click()){
  165.         click_num+=1;
  166.       }
  167.       delay(20);
  168.     }
  169.   }
  170.   Serial.println(click_num);
  171.   // if (click_num==3){
  172.   //   disableLed();
  173.   //   style = -1;
  174.   //   isOn = false;
  175.   //   Serial.println("Disabled! ");
  176.   // }
  177.  
  178.   if (btn.hold()){
  179.     disableLed();
  180.     style = -1;
  181.     isOn = false;
  182.     Serial.println("Disabled! ");
  183.   }
  184.  
  185.   if (click_num==2){
  186.     Serial.println("Clicked!");
  187.     if (isOn){
  188.       if (style==2){
  189.         style = 0;
  190.       }
  191.       else{
  192.         style += 1;
  193.       }
  194.     }
  195.     Serial.println(style);
  196.   }
  197.  
  198.   if (btn.hold()){
  199.     if (isOn){
  200.       style = -1;
  201.       disableLed();
  202.       isOn = !isOn;
  203.       Serial.println("LED OFF");
  204.     }
  205.     else{
  206.       style = 0;
  207.       isOn = true;
  208.       Serial.print("Waked Up");
  209.     }
  210.   }
  211.  
  212.   if (click_num>0 && !isOn){
  213.     style = 0;
  214.     isOn = true;
  215.     Serial.print("Waked Up");
  216.   }
  217.  
  218.  
  219.  
  220.   // if (btn.click()){
  221.   //   Serial.println("Clicked!");
  222.   //   if (isOn){
  223.   //     if (style==2){
  224.   //       style = 0;
  225.   //     }
  226.   //     else{
  227.   //       style += 1;
  228.   //     }
  229.   //   }
  230.   //   else{
  231.   //     style = 0;
  232.   //     isOn = true;
  233.   //     Serial.print("Waked Up");
  234.   //   }
  235.   //   Serial.println(style);
  236.   // }
  237.  
  238.  
  239.   switch (style){
  240.   case 0:{
  241.     unsigned long currentMillis = millis();
  242.     if (currentMillis - previousMillis >= interval) {
  243.       previousMillis = currentMillis;
  244.       DynamicJsonDocument data = get_api_request();
  245.  
  246.       for(int i=0; i<NUM_LEDS-1; i++){
  247.         String alert = data["states"][i]["alert"];
  248.         if (alert=="true"){
  249.           int led_id=get_region_id(i);
  250.           leds[led_id] = CRGB::Red; FastLED.show();
  251.         }
  252.         else{
  253.           int led_id=get_region_id(i);
  254.           leds[led_id] = CRGB::White; FastLED.show();
  255.         }
  256.       }
  257.       leds[22] = CRGB::Orange;
  258.       FastLED.show();
  259.     }
  260.     break;
  261.   }
  262.   case 1:{
  263.       for (int i=0; i<NUM_LEDS;i++){
  264.         leds[i] = CRGB::Blue;
  265.       }
  266.       FastLED.show();
  267.       break;
  268.     }
  269.   case 2:{
  270.     for (int i=0; i<NUM_LEDS;i++){
  271.       leds[i] = CRGB::Red;  
  272.     }
  273.     FastLED.show();
  274.     break;
  275.   }
  276.   default:{
  277.     Serial.print("There is no such style!");
  278.   }
  279.   }
  280.  
  281.  
  282.  
  283.   delay(20);
  284. }
  285.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement