Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FS.h>
- #include <WiFiManager.h>
- #include <ESP8266HTTPClient.h>
- #include <ArduinoJson.h>
- #include "Arduino.h"
- #include "SoftwareSerial.h"
- #include "DFRobotDFPlayerMini.h"
- #include <MD_Parola.h>
- #include <MD_MAX72xx.h>
- #include <SPI.h>
- #include "Font_Data.h"
- byte debug = 1;
- #define BUTTON_PIN D0
- #define rx D2
- #define tx D3
- #define led_r D4
- #define HARDWARE_TYPE MD_MAX72XX::FC16_HW
- #define MAX_DEVICES 16
- #define CS_PIN D8
- MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
- const char* text_to_disp0[] = {
- "Setup", //0
- "WiFi", //1
- "DFPlayer", //2
- "HTTP", //3
- "2", //4
- "", //5
- "1", //6
- "2", //7
- "3", //8
- "1 2", //9
- "1 3", //10
- "2 3", //11
- "1 2 3" //12ok
- };
- const char* text_to_disp1[] = {
- "START", //0
- "OK", //1
- "ERROR", //2
- "Connecting", //3
- "Connected", //4
- "", //5
- "1", //6
- "2", //7
- "3", //8
- "1 2", //9
- "1 3", //10
- "2 3", //11
- "1 2 3" //12ok
- };
- int text0 = 0;
- int text1 = 0;
- DynamicJsonBuffer jsonBuffer(200);
- WiFiManager wm;
- SoftwareSerial dfSerial(rx, tx); // RX, TX
- DFRobotDFPlayerMini myDFPlayer;
- void printDetail(uint8_t type, int value);
- //define your default values here, if there are different values in config.json, they are overwritten.
- char adres[80];
- //default custom static IP
- char static_ip[16] = "10.0.1.56";
- char static_gw[16] = "10.0.1.1";
- char static_sn[16] = "255.255.255.0";
- String strona;
- String reading_time;
- String reading_time_old;
- String kod;
- int str_len1;
- int str_len2;
- int str_len3;
- byte e = 0;
- byte e_old = 0;
- byte i = 0;
- byte j = 1;
- byte k = 0;
- int c = 0;
- int light = 0;
- int set = 0;
- int mp3 = 2;
- byte volumeLevel = 30; //variable for holding volume level
- boolean isPlaying = false;
- //flag for saving data
- bool shouldSaveConfig = false;
- //callback notifying us of the need to save config
- void saveConfigCallback() {
- if (debug == 1) Serial.println("Should save config");
- shouldSaveConfig = true;
- }
- void setupSpiffs() {
- //clean FS, for testing
- //SPIFFS.format();
- //read configuration from FS json
- if (debug == 1) Serial.println("mounting FS...");
- if (SPIFFS.begin()) {
- if (debug == 1) Serial.println("mounted file system");
- if (SPIFFS.exists("/config.json")) {
- //file exists, reading and loading
- if (debug == 1) Serial.println("reading config file");
- File configFile = SPIFFS.open("/config.json", "r");
- if (configFile) {
- if (debug == 1) Serial.println("opened config file");
- size_t size = configFile.size();
- // Allocate a buffer to store contents of the file.
- std::unique_ptr<char[]> buf(new char[size]);
- configFile.readBytes(buf.get(), size);
- DynamicJsonBuffer jsonBuffer;
- JsonObject& json = jsonBuffer.parseObject(buf.get());
- json.printTo(Serial);
- if (json.success()) {
- if (debug == 1) Serial.println("\nparsed json");
- strcpy(adres, json["adres"]);
- } else {
- if (debug == 1) Serial.println("failed to load json config");
- }
- }
- }
- } else {
- if (debug == 1) Serial.println("failed to mount FS");
- }
- //end read
- }
- void checkButton() {
- // check for button press
- if (digitalRead(BUTTON_PIN) == LOW) {
- // poor mans debounce/press-hold, code not ideal for production
- delay(50);
- if (digitalRead(BUTTON_PIN) == LOW) {
- if (debug == 1) Serial.println("Button Pressed");
- digitalWrite(led_r, HIGH);
- wm.autoConnect("ArduSTQC_Tablica", "password");
- // still holding button for 3000 ms, reset settings, code not ideaa for production
- delay(1000); // reset delay hold
- if (digitalRead(BUTTON_PIN) == LOW) {
- if (debug == 1) Serial.println("Button Held");
- if (debug == 1) Serial.println("Erasing Config, restarting");
- wm.resetSettings();
- delay(1000);
- //SPIFFS.format();
- ESP.restart();
- }
- }
- } else {
- digitalWrite(led_r, LOW);
- }
- }
- void disp() {
- if (myDisplay.displayAnimate()) // animates and returns true when an animation is completed
- {
- // Splats
- if (myDisplay.getZoneStatus(0)) // in sync with zone 2, so do both
- {
- if (set == 0) {
- myDisplay.setFont(1, nullptr);
- myDisplay.setFont(0, nullptr);
- myDisplay.displayZoneText(0, text_to_disp0[text0], PA_CENTER, 0, 0, PA_PRINT);
- myDisplay.displayZoneText(1, text_to_disp1[text1], PA_CENTER, 0, 0, PA_PRINT);
- }
- if (set == 1) {
- myDisplay.setFont(1, BigFontLower);
- myDisplay.setFont(0, BigFontUpper);
- if (kod == "000") {
- text0 = 5;
- text1 = 5;
- myDisplay.displayZoneText(0, text_to_disp0[text0], PA_CENTER, 0, 0, PA_PRINT);
- myDisplay.displayZoneText(1, text_to_disp1[text1], PA_CENTER, 0, 0, PA_PRINT);
- }
- if (kod == "100") {
- text0 = 6;
- text1 = 6;
- myDisplay.displayZoneText(0, text_to_disp0[text0], PA_LEFT, 0, 0, PA_PRINT);
- myDisplay.displayZoneText(1, text_to_disp1[text1], PA_LEFT, 0, 0, PA_PRINT);
- }
- if (kod == "020") {
- text0 = 7;
- text1 = 7;
- myDisplay.displayZoneText(0, text_to_disp0[text0], PA_CENTER, 0, 0, PA_PRINT);
- myDisplay.displayZoneText(1, text_to_disp1[text1], PA_CENTER, 0, 0, PA_PRINT);
- }
- if (kod == "003") {
- text0 = 8;
- text1 = 8;
- myDisplay.displayZoneText(0, text_to_disp0[text0], PA_RIGHT, 0, 0, PA_PRINT);
- myDisplay.displayZoneText(1, text_to_disp1[text1], PA_RIGHT, 0, 0, PA_PRINT);
- }
- if (kod == "120") {
- text0 = 9;
- text1 = 9;
- myDisplay.displayZoneText(0, text_to_disp0[text0], PA_LEFT, 0, 0, PA_PRINT);
- myDisplay.displayZoneText(1, text_to_disp1[text1], PA_LEFT, 0, 0, PA_PRINT);
- }
- if (kod == "103") {
- text0 = 10;
- text1 = 10;
- myDisplay.displayZoneText(0, text_to_disp0[text0], PA_CENTER, 0, 0, PA_PRINT);
- myDisplay.displayZoneText(1, text_to_disp1[text1], PA_CENTER, 0, 0, PA_PRINT);
- }
- if (kod == "023") {
- text0 = 11;
- text1 = 11;
- myDisplay.displayZoneText(0, text_to_disp0[text0], PA_RIGHT, 0, 0, PA_PRINT);
- myDisplay.displayZoneText(1, text_to_disp1[text1], PA_RIGHT, 0, 0, PA_PRINT);
- }
- if (kod == "123") {
- text0 = 12;
- text1 = 12;
- myDisplay.displayZoneText(0, text_to_disp0[text0], PA_CENTER, 0, 0, PA_PRINT);
- myDisplay.displayZoneText(1, text_to_disp1[text1], PA_CENTER, 0, 0, PA_PRINT);
- }
- }
- myDisplay.displayReset(0);
- myDisplay.displayReset(1);
- }
- }
- }
- void setup() {
- Serial.begin(115200);
- pinMode(led_r, OUTPUT);
- digitalWrite(led_r, LOW);
- pinMode(BUTTON_PIN, INPUT_PULLUP);
- myDisplay.begin(2);
- myDisplay.setZone(1, 0, 7);
- myDisplay.setZone(0, 8, 15);
- myDisplay.setInvert(false);
- myDisplay.setIntensity(1);
- text0 = 0;
- text1 = 0;
- disp();
- delay(1000);
- dfSerial.begin(9600);
- if (myDFPlayer.begin(dfSerial)) {
- if (debug == 1) Serial.println("DFPlayer Mini online.");
- myDFPlayer.volume(volumeLevel);
- text0 = 2;
- text1 = 1;
- disp();
- digitalWrite(led_r, LOW);
- delay(1000);
- /*while (true)
- {
- delay(0);
- }*/
- }
- else {
- if (debug == 1) Serial.println(F("Unable to begin:"));
- if (debug == 1) Serial.println(F("1.Please recheck the connection!"));
- text0 = 2;
- text1 = 2;
- disp();
- digitalWrite(led_r, HIGH);
- delay(1000);
- /*while (true)
- {
- delay(0);
- }*/
- }
- text0 = 1;
- text1 = 0;
- disp();
- delay(1000);
- setupSpiffs();
- //set config save notify callback
- wm.setSaveConfigCallback(saveConfigCallback);
- std::vector<const char*> menu = { "wifi", "info", "param", "sep", "restart", "erase", "exit" };
- wm.setMenu(menu);
- WiFiManagerParameter custom_adres("adres", "Adres API:", adres, 80);
- //add all your parameters here
- wm.addParameter(&custom_adres);
- wm.autoConnect("ArduSTQC_Tablica", "password");
- if (!wm.autoConnect()) {
- if (debug == 1) Serial.println("failed to connect and hit timeout");
- text0 = 1;
- text1 = 2;
- disp();
- digitalWrite(led_r, HIGH);
- delay(1000);
- digitalWrite(led_r, LOW);
- ESP.restart();
- }
- if (wm.autoConnect()) {
- if (debug == 1) Serial.println("Connected!");
- text0 = 1;
- text1 = 4;
- disp();
- delay(1000);
- }
- //read updated parameters
- strcpy(adres, custom_adres.getValue());
- //save the custom parameters to FS
- if (shouldSaveConfig) {
- if (debug == 1) Serial.println("saving config");
- DynamicJsonBuffer jsonBuffer;
- JsonObject& json = jsonBuffer.createObject();
- json["adres"] = adres;
- File configFile = SPIFFS.open("/config.json", "w");
- if (!configFile) {
- if (debug == 1) Serial.println("failed to open config file for writing");
- }
- json.prettyPrintTo(Serial);
- json.printTo(configFile);
- configFile.close();
- //end save
- shouldSaveConfig = false;
- }
- if (WiFi.status() == WL_CONNECTED) {
- if (debug == 1) Serial.println("WiFi OK");
- if (debug == 1) Serial.print("local ip: ");
- if (debug == 1) Serial.println(WiFi.localIP());
- if (debug == 1) Serial.print("API adres: ");
- if (debug == 1) Serial.println(adres);
- }
- text0 = 5;
- text1 = 5;
- disp();
- set = 1;
- }
- void loop() {
- disp();
- if (WiFi.status() == WL_CONNECTED) {
- if (light <= 4) myDisplay.setIntensity(10);
- if (light > 4) myDisplay.setIntensity(1);
- if (light == 9) light = 0;
- if (debug == 1) Serial.println("WiFi OK");
- HTTPClient http;
- http.begin(adres);
- int httpCode = http.GET();
- if (httpCode > 0) {
- if (debug == 1) Serial.printf("[HTTP] Odczytano kod: %d\n", httpCode);
- if (httpCode == 200) {
- strona = http.getString();
- JsonObject& root = jsonBuffer.parseObject(strona);
- int str_len = strona.length();
- strona = strona.substring(0, str_len);
- int str_len0 = strona.length();
- reading_time = strona.substring(0, 19);
- kod = strona.substring(20, str_len);
- if (kod == "000") {
- myDFPlayer.stop();
- }
- if (debug == 1) Serial.print("strona: ");
- if (debug == 1) Serial.println(strona);
- if (debug == 1) Serial.print("strona_lenght: ");
- if (debug == 1) Serial.print(str_len0);
- if (debug == 1) Serial.print(" reading_time: ");
- if (debug == 1) Serial.print(reading_time);
- if (debug == 1) Serial.print(" kod: ");
- if (debug == 1) Serial.println(kod);
- if (reading_time != reading_time_old) {
- j = 0;
- light = 0;
- reading_time_old = reading_time;
- }
- //alarm
- if (j == 0) {
- myDFPlayer.play(2);
- if (debug == 1) Serial.print("kod: ");
- if (debug == 1) Serial.println(kod);
- /*if (kod == "000") {
- text0 = 5;
- text1 = 5;
- myDFPlayer.stop();
- //disp();
- }/* else if (kod == "100") {
- text0 = 6;
- text1 = 6;
- disp();
- } else if (kod == "020") {
- text0 = 7;
- text1 = 7;
- disp();
- } else if (kod == "003") {
- text0 = 8;
- text1 = 8;
- disp();
- } else if (kod == "120") {
- text0 = 9;
- text1 = 9;
- disp();
- } else if (kod == "103") {
- text0 = 10;
- text1 = 10;
- disp();
- } else if (kod == "023") {
- text0 = 11;
- text1 = 11;
- disp();
- } else if (kod == "123") {
- text0 = 12;
- text1 = 12;
- disp();
- }*/
- //disp();
- j = 1;
- e_old = e;
- reading_time_old = reading_time;
- }
- } else {
- //myDisplay.print(httpCode);
- }
- } else {
- if (debug == 1) Serial.println("HTTP ERROR");
- set = 0;
- text0 = 3;
- text1 = 2;
- disp();
- digitalWrite(led_r, HIGH);
- delay(1000);
- digitalWrite(led_r, LOW);
- }
- light++;
- http.end();
- } else {
- if (debug == 1) Serial.println("WiFi ERROR");
- set = 0;
- text0 = 1;
- text1 = 2;
- disp();
- digitalWrite(led_r, HIGH);
- delay(2000);
- digitalWrite(led_r, LOW);
- i = 0;
- }
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement