Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #define EB_NO_FOR
- #define EB_NO_CALLBACK
- // #define EB_NO_COUNTER
- #define EB_NO_BUFFER
- #define EB_DEB_TIME 50 // таймаут гашения дребезга кнопки (кнопка)
- #define EB_CLICK_TIME 500 // таймаут ожидания кликов (кнопка)
- #define EB_HOLD_TIME 600 // таймаут удержания (кнопка)
- #define EB_STEP_TIME 200 // таймаут импульсного удержания (кнопка)
- #define EB_FAST_TIME 30 // таймаут быстрого поворота (энкодер)
- #include <FastLED.h>
- #define NUM_LEDS 25
- #include <EncButton.h>
- Button btn(0);
- #include <WiFiManager.h>
- #include "ArduinoJson.h"
- #include <ESP8266HTTPClient.h>
- const char* API_URL = "http://alerts.com.ua/api/states";
- const char* API_KEY = "your_api_key";
- int brightness = 5; // start brightness
- int style = 0; // style nu,
- WiFiManager wifiManager;
- unsigned long previousMillis = 0; //temp value
- const long interval = 1000; // api interval
- CRGB leds[NUM_LEDS];
- int get_region_id(int id){
- switch(id){
- case 0: return 1; //vinitsa
- case 1: return 7;// volyn
- case 2: return 17;// dnipropetr
- case 3: return 20;// donetsk
- case 4: return 10;// zhytomyr
- case 5: return 4; // zakarpatya
- case 6: return 21; // zaporizhzhia
- case 7: return 3;// ivano-frankivsk
- case 8: return 11; // kyiv oblast
- case 9: return 16; // kirovohrad
- case 10: return 19; // luhansk
- case 11: return 5; // lviv
- case 12: return 24; // mykolaiv
- case 13: return 0; // odesa
- case 14: return 14; // poltava
- case 15: return 8; // rivne
- case 16: return 13; // sumy
- case 17: return 6; // ternopil
- case 18: return 18; //kharkiv
- case 19: return 23; //kherson
- case 20: return 9;//Khmelnytskyi
- case 21: return 15; // Cherkasy
- case 22: return 2;// chernovetska
- case 23: return 12;//chernihiv
- case 24: return 11;//kyiv
- }
- return -1;
- }
- void check_click_on_startup(){
- if (btn.read()){
- WiFiManager wifiManager;
- wifiManager.startConfigPortal("Wi-Fi Ukraine Map");
- }
- }
- DynamicJsonDocument get_api_request() {
- HTTPClient http;
- WiFiClient client;
- http.begin(client, API_URL);
- http.addHeader("X-API-Key", API_KEY);
- int httpCode = http.GET();
- DynamicJsonDocument doc(8192); // Create the JSON document outside of the if statement
- if (httpCode > 0) {
- String payload = http.getString();
- DeserializationError error = deserializeJson(doc, payload);
- if (error) {
- Serial.print(F("Error parsing JSON: "));
- Serial.println(error.c_str());
- return DynamicJsonDocument(0);
- }
- if (httpCode == 200) {
- Serial.println("Hooray!");
- }
- }
- http.end();
- return doc;
- }
- void test_led(){
- for(int i=0; i<NUM_LEDS;i++){
- leds[i] = CRGB::White; FastLED.show();
- delay(100);
- }
- }
- void setup() {
- pinMode(0, INPUT);
- Serial.begin(115200);
- FastLED.addLeds<WS2812B, 2, GRB>(leds, NUM_LEDS);
- FastLED.setBrightness(brightness);
- test_led();
- // check_click_on_startup();
- wifiManager.autoConnect("Wi-Fi Ukraine Map");
- }
- void disableLed(){
- // fill_solid(leds, NUM_LEDS, CRGB::Black);
- for(int i=0; i<NUM_LEDS;i++){
- leds[i] = CRGB::Black;
- FastLED.show();
- }
- }
- void enableLed(){
- fill_solid(leds, NUM_LEDS, CRGB::White);
- FastLED.show();
- }
- int bright_step = 5;
- bool isOn = true;
- int click_num = 0;
- void loop() {
- btn.tick();
- click_num=0;
- if (btn.step(1)){
- brightness += bright_step;
- if (brightness>255) brightness = 255;
- if (brightness<0) brightness = 1;
- FastLED.setBrightness(brightness);
- FastLED.show();
- }
- if (btn.releaseStep()){
- bright_step = -bright_step;
- FastLED.setBrightness(brightness);
- FastLED.show();
- }
- if (btn.click()){
- click_num+=1;
- while(btn.busy()){
- btn.tick();
- if (btn.click()){
- click_num+=1;
- }
- delay(20);
- }
- }
- Serial.println(click_num);
- // if (click_num==3){
- // disableLed();
- // style = -1;
- // isOn = false;
- // Serial.println("Disabled! ");
- // }
- if (btn.hold()){
- disableLed();
- style = -1;
- isOn = false;
- Serial.println("Disabled! ");
- }
- if (click_num==2){
- Serial.println("Clicked!");
- if (isOn){
- if (style==2){
- style = 0;
- }
- else{
- style += 1;
- }
- }
- Serial.println(style);
- }
- if (btn.hold()){
- if (isOn){
- style = -1;
- disableLed();
- isOn = !isOn;
- Serial.println("LED OFF");
- }
- else{
- style = 0;
- isOn = true;
- Serial.print("Waked Up");
- }
- }
- if (click_num>0 && !isOn){
- style = 0;
- isOn = true;
- Serial.print("Waked Up");
- }
- // if (btn.click()){
- // Serial.println("Clicked!");
- // if (isOn){
- // if (style==2){
- // style = 0;
- // }
- // else{
- // style += 1;
- // }
- // }
- // else{
- // style = 0;
- // isOn = true;
- // Serial.print("Waked Up");
- // }
- // Serial.println(style);
- // }
- switch (style){
- case 0:{
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- DynamicJsonDocument data = get_api_request();
- for(int i=0; i<NUM_LEDS-1; i++){
- String alert = data["states"][i]["alert"];
- if (alert=="true"){
- int led_id=get_region_id(i);
- leds[led_id] = CRGB::Red; FastLED.show();
- }
- else{
- int led_id=get_region_id(i);
- leds[led_id] = CRGB::White; FastLED.show();
- }
- }
- leds[22] = CRGB::Orange;
- FastLED.show();
- }
- break;
- }
- case 1:{
- for (int i=0; i<NUM_LEDS;i++){
- leds[i] = CRGB::Blue;
- }
- FastLED.show();
- break;
- }
- case 2:{
- for (int i=0; i<NUM_LEDS;i++){
- leds[i] = CRGB::Red;
- }
- FastLED.show();
- break;
- }
- default:{
- Serial.print("There is no such style!");
- }
- }
- delay(20);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement