Advertisement
Krejzi_Dark

tablica

Jul 25th, 2023 (edited)
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.80 KB | Source Code | 0 0
  1. #include <FS.h>
  2. #include <WiFiManager.h>
  3. #include <ESP8266HTTPClient.h>
  4. #include <ArduinoJson.h>
  5. #include "Arduino.h"
  6. #include "SoftwareSerial.h"
  7. #include "DFRobotDFPlayerMini.h"
  8. #include <MD_Parola.h>
  9. #include <MD_MAX72xx.h>
  10. #include <SPI.h>
  11. #include "Font_Data.h"
  12.  
  13. byte debug = 1;
  14.  
  15. #define BUTTON_PIN D0
  16.  
  17. #define rx D2
  18. #define tx D3
  19.  
  20. #define led_r D4
  21.  
  22. #define HARDWARE_TYPE MD_MAX72XX::FC16_HW
  23. #define MAX_DEVICES 16
  24. #define CS_PIN D8
  25. MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
  26. const char* text_to_disp0[] = {
  27. "Setup", //0
  28. "WiFi", //1
  29. "DFPlayer", //2
  30. "HTTP", //3
  31. "2", //4
  32. "", //5
  33. "1", //6
  34. "2", //7
  35. "3", //8
  36. "1 2", //9
  37. "1 3", //10
  38. "2 3", //11
  39. "1 2 3" //12ok
  40. };
  41. const char* text_to_disp1[] = {
  42. "START", //0
  43. "OK", //1
  44. "ERROR", //2
  45. "Connecting", //3
  46. "Connected", //4
  47. "", //5
  48. "1", //6
  49. "2", //7
  50. "3", //8
  51. "1 2", //9
  52. "1 3", //10
  53. "2 3", //11
  54. "1 2 3" //12ok
  55. };
  56.  
  57. int text0 = 0;
  58. int text1 = 0;
  59.  
  60. DynamicJsonBuffer jsonBuffer(200);
  61. WiFiManager wm;
  62.  
  63. SoftwareSerial dfSerial(rx, tx); // RX, TX
  64. DFRobotDFPlayerMini myDFPlayer;
  65. void printDetail(uint8_t type, int value);
  66.  
  67. //define your default values here, if there are different values in config.json, they are overwritten.
  68. char adres[80];
  69.  
  70. //default custom static IP
  71. char static_ip[16] = "10.0.1.56";
  72. char static_gw[16] = "10.0.1.1";
  73. char static_sn[16] = "255.255.255.0";
  74.  
  75. String strona;
  76. String reading_time;
  77. String reading_time_old;
  78. String kod;
  79.  
  80. int str_len1;
  81. int str_len2;
  82. int str_len3;
  83.  
  84. byte e = 0;
  85. byte e_old = 0;
  86. byte i = 0;
  87. byte j = 1;
  88. byte k = 0;
  89. int c = 0;
  90. int light = 0;
  91.  
  92. int set = 0;
  93.  
  94. int mp3 = 2;
  95.  
  96. byte volumeLevel = 30; //variable for holding volume level
  97.  
  98. boolean isPlaying = false;
  99.  
  100. //flag for saving data
  101. bool shouldSaveConfig = false;
  102.  
  103. //callback notifying us of the need to save config
  104. void saveConfigCallback() {
  105. if (debug == 1) Serial.println("Should save config");
  106. shouldSaveConfig = true;
  107. }
  108.  
  109. void setupSpiffs() {
  110. //clean FS, for testing
  111. //SPIFFS.format();
  112.  
  113. //read configuration from FS json
  114. if (debug == 1) Serial.println("mounting FS...");
  115.  
  116. if (SPIFFS.begin()) {
  117. if (debug == 1) Serial.println("mounted file system");
  118. if (SPIFFS.exists("/config.json")) {
  119. //file exists, reading and loading
  120. if (debug == 1) Serial.println("reading config file");
  121. File configFile = SPIFFS.open("/config.json", "r");
  122. if (configFile) {
  123. if (debug == 1) Serial.println("opened config file");
  124. size_t size = configFile.size();
  125. // Allocate a buffer to store contents of the file.
  126. std::unique_ptr<char[]> buf(new char[size]);
  127.  
  128. configFile.readBytes(buf.get(), size);
  129. DynamicJsonBuffer jsonBuffer;
  130. JsonObject& json = jsonBuffer.parseObject(buf.get());
  131. json.printTo(Serial);
  132. if (json.success()) {
  133. if (debug == 1) Serial.println("\nparsed json");
  134.  
  135. strcpy(adres, json["adres"]);
  136.  
  137. } else {
  138. if (debug == 1) Serial.println("failed to load json config");
  139. }
  140. }
  141. }
  142. } else {
  143. if (debug == 1) Serial.println("failed to mount FS");
  144. }
  145. //end read
  146. }
  147.  
  148. void checkButton() {
  149. // check for button press
  150. if (digitalRead(BUTTON_PIN) == LOW) {
  151. // poor mans debounce/press-hold, code not ideal for production
  152. delay(50);
  153. if (digitalRead(BUTTON_PIN) == LOW) {
  154. if (debug == 1) Serial.println("Button Pressed");
  155. digitalWrite(led_r, HIGH);
  156.  
  157. wm.autoConnect("ArduSTQC_Tablica", "password");
  158. // still holding button for 3000 ms, reset settings, code not ideaa for production
  159. delay(1000); // reset delay hold
  160. if (digitalRead(BUTTON_PIN) == LOW) {
  161. if (debug == 1) Serial.println("Button Held");
  162. if (debug == 1) Serial.println("Erasing Config, restarting");
  163. wm.resetSettings();
  164. delay(1000);
  165. //SPIFFS.format();
  166. ESP.restart();
  167. }
  168. }
  169. } else {
  170. digitalWrite(led_r, LOW);
  171. }
  172. }
  173.  
  174. void disp() {
  175. if (myDisplay.displayAnimate()) // animates and returns true when an animation is completed
  176. {
  177. // Splats
  178. if (myDisplay.getZoneStatus(0)) // in sync with zone 2, so do both
  179. {
  180. if (set == 0) {
  181. myDisplay.setFont(1, nullptr);
  182. myDisplay.setFont(0, nullptr);
  183. myDisplay.displayZoneText(0, text_to_disp0[text0], PA_CENTER, 0, 0, PA_PRINT);
  184. myDisplay.displayZoneText(1, text_to_disp1[text1], PA_CENTER, 0, 0, PA_PRINT);
  185. }
  186. if (set == 1) {
  187. myDisplay.setFont(1, BigFontLower);
  188. myDisplay.setFont(0, BigFontUpper);
  189.  
  190. if (kod == "000") {
  191. text0 = 5;
  192. text1 = 5;
  193. myDisplay.displayZoneText(0, text_to_disp0[text0], PA_CENTER, 0, 0, PA_PRINT);
  194. myDisplay.displayZoneText(1, text_to_disp1[text1], PA_CENTER, 0, 0, PA_PRINT);
  195. }
  196. if (kod == "100") {
  197. text0 = 6;
  198. text1 = 6;
  199. myDisplay.displayZoneText(0, text_to_disp0[text0], PA_LEFT, 0, 0, PA_PRINT);
  200. myDisplay.displayZoneText(1, text_to_disp1[text1], PA_LEFT, 0, 0, PA_PRINT);
  201. }
  202. if (kod == "020") {
  203. text0 = 7;
  204. text1 = 7;
  205. myDisplay.displayZoneText(0, text_to_disp0[text0], PA_CENTER, 0, 0, PA_PRINT);
  206. myDisplay.displayZoneText(1, text_to_disp1[text1], PA_CENTER, 0, 0, PA_PRINT);
  207. }
  208. if (kod == "003") {
  209. text0 = 8;
  210. text1 = 8;
  211. myDisplay.displayZoneText(0, text_to_disp0[text0], PA_RIGHT, 0, 0, PA_PRINT);
  212. myDisplay.displayZoneText(1, text_to_disp1[text1], PA_RIGHT, 0, 0, PA_PRINT);
  213. }
  214. if (kod == "120") {
  215. text0 = 9;
  216. text1 = 9;
  217. myDisplay.displayZoneText(0, text_to_disp0[text0], PA_LEFT, 0, 0, PA_PRINT);
  218. myDisplay.displayZoneText(1, text_to_disp1[text1], PA_LEFT, 0, 0, PA_PRINT);
  219. }
  220. if (kod == "103") {
  221. text0 = 10;
  222. text1 = 10;
  223. myDisplay.displayZoneText(0, text_to_disp0[text0], PA_CENTER, 0, 0, PA_PRINT);
  224. myDisplay.displayZoneText(1, text_to_disp1[text1], PA_CENTER, 0, 0, PA_PRINT);
  225. }
  226. if (kod == "023") {
  227. text0 = 11;
  228. text1 = 11;
  229. myDisplay.displayZoneText(0, text_to_disp0[text0], PA_RIGHT, 0, 0, PA_PRINT);
  230. myDisplay.displayZoneText(1, text_to_disp1[text1], PA_RIGHT, 0, 0, PA_PRINT);
  231. }
  232. if (kod == "123") {
  233. text0 = 12;
  234. text1 = 12;
  235. myDisplay.displayZoneText(0, text_to_disp0[text0], PA_CENTER, 0, 0, PA_PRINT);
  236. myDisplay.displayZoneText(1, text_to_disp1[text1], PA_CENTER, 0, 0, PA_PRINT);
  237. }
  238. }
  239.  
  240. myDisplay.displayReset(0);
  241. myDisplay.displayReset(1);
  242. }
  243. }
  244. }
  245.  
  246. void setup() {
  247. Serial.begin(115200);
  248. pinMode(led_r, OUTPUT);
  249. digitalWrite(led_r, LOW);
  250. pinMode(BUTTON_PIN, INPUT_PULLUP);
  251.  
  252. myDisplay.begin(2);
  253. myDisplay.setZone(1, 0, 7);
  254. myDisplay.setZone(0, 8, 15);
  255.  
  256. myDisplay.setInvert(false);
  257. myDisplay.setIntensity(1);
  258.  
  259. text0 = 0;
  260. text1 = 0;
  261. disp();
  262. delay(1000);
  263.  
  264. dfSerial.begin(9600);
  265. if (myDFPlayer.begin(dfSerial)) {
  266. if (debug == 1) Serial.println("DFPlayer Mini online.");
  267. myDFPlayer.volume(volumeLevel);
  268. text0 = 2;
  269. text1 = 1;
  270. disp();
  271. digitalWrite(led_r, LOW);
  272. delay(1000);
  273. /*while (true)
  274. {
  275. delay(0);
  276. }*/
  277. }
  278.  
  279. else {
  280. if (debug == 1) Serial.println(F("Unable to begin:"));
  281. if (debug == 1) Serial.println(F("1.Please recheck the connection!"));
  282. text0 = 2;
  283. text1 = 2;
  284. disp();
  285. digitalWrite(led_r, HIGH);
  286. delay(1000);
  287. /*while (true)
  288. {
  289. delay(0);
  290. }*/
  291. }
  292.  
  293. text0 = 1;
  294. text1 = 0;
  295. disp();
  296. delay(1000);
  297.  
  298. setupSpiffs();
  299.  
  300. //set config save notify callback
  301. wm.setSaveConfigCallback(saveConfigCallback);
  302.  
  303. std::vector<const char*> menu = { "wifi", "info", "param", "sep", "restart", "erase", "exit" };
  304. wm.setMenu(menu);
  305.  
  306. WiFiManagerParameter custom_adres("adres", "Adres API:", adres, 80);
  307.  
  308. //add all your parameters here
  309. wm.addParameter(&custom_adres);
  310.  
  311. wm.autoConnect("ArduSTQC_Tablica", "password");
  312.  
  313. if (!wm.autoConnect()) {
  314. if (debug == 1) Serial.println("failed to connect and hit timeout");
  315. text0 = 1;
  316. text1 = 2;
  317. disp();
  318. digitalWrite(led_r, HIGH);
  319. delay(1000);
  320. digitalWrite(led_r, LOW);
  321. ESP.restart();
  322. }
  323.  
  324. if (wm.autoConnect()) {
  325. if (debug == 1) Serial.println("Connected!");
  326. text0 = 1;
  327. text1 = 4;
  328. disp();
  329. delay(1000);
  330. }
  331.  
  332. //read updated parameters
  333. strcpy(adres, custom_adres.getValue());
  334.  
  335. //save the custom parameters to FS
  336. if (shouldSaveConfig) {
  337. if (debug == 1) Serial.println("saving config");
  338. DynamicJsonBuffer jsonBuffer;
  339. JsonObject& json = jsonBuffer.createObject();
  340. json["adres"] = adres;
  341.  
  342. File configFile = SPIFFS.open("/config.json", "w");
  343. if (!configFile) {
  344. if (debug == 1) Serial.println("failed to open config file for writing");
  345. }
  346.  
  347. json.prettyPrintTo(Serial);
  348. json.printTo(configFile);
  349. configFile.close();
  350. //end save
  351. shouldSaveConfig = false;
  352. }
  353.  
  354. if (WiFi.status() == WL_CONNECTED) {
  355. if (debug == 1) Serial.println("WiFi OK");
  356.  
  357. if (debug == 1) Serial.print("local ip: ");
  358. if (debug == 1) Serial.println(WiFi.localIP());
  359. if (debug == 1) Serial.print("API adres: ");
  360. if (debug == 1) Serial.println(adres);
  361. }
  362. text0 = 5;
  363. text1 = 5;
  364. disp();
  365. set = 1;
  366. }
  367.  
  368. void loop() {
  369. disp();
  370. if (WiFi.status() == WL_CONNECTED) {
  371. if (light <= 4) myDisplay.setIntensity(10);
  372. if (light > 4) myDisplay.setIntensity(1);
  373. if (light == 9) light = 0;
  374.  
  375. if (debug == 1) Serial.println("WiFi OK");
  376.  
  377. HTTPClient http;
  378. http.begin(adres);
  379.  
  380. int httpCode = http.GET();
  381. if (httpCode > 0) {
  382. if (debug == 1) Serial.printf("[HTTP] Odczytano kod: %d\n", httpCode);
  383.  
  384. if (httpCode == 200) {
  385. strona = http.getString();
  386.  
  387. JsonObject& root = jsonBuffer.parseObject(strona);
  388.  
  389. int str_len = strona.length();
  390. strona = strona.substring(0, str_len);
  391. int str_len0 = strona.length();
  392.  
  393. reading_time = strona.substring(0, 19);
  394. kod = strona.substring(20, str_len);
  395.  
  396. if (kod == "000") {
  397. myDFPlayer.stop();
  398. }
  399.  
  400. if (debug == 1) Serial.print("strona: ");
  401. if (debug == 1) Serial.println(strona);
  402. if (debug == 1) Serial.print("strona_lenght: ");
  403. if (debug == 1) Serial.print(str_len0);
  404. if (debug == 1) Serial.print(" reading_time: ");
  405. if (debug == 1) Serial.print(reading_time);
  406. if (debug == 1) Serial.print(" kod: ");
  407. if (debug == 1) Serial.println(kod);
  408.  
  409. if (reading_time != reading_time_old) {
  410. j = 0;
  411. light = 0;
  412. reading_time_old = reading_time;
  413. }
  414.  
  415. //alarm
  416. if (j == 0) {
  417. myDFPlayer.play(2);
  418. if (debug == 1) Serial.print("kod: ");
  419. if (debug == 1) Serial.println(kod);
  420.  
  421. /*if (kod == "000") {
  422. text0 = 5;
  423. text1 = 5;
  424. myDFPlayer.stop();
  425. //disp();
  426. }/* else if (kod == "100") {
  427. text0 = 6;
  428. text1 = 6;
  429. disp();
  430. } else if (kod == "020") {
  431. text0 = 7;
  432. text1 = 7;
  433. disp();
  434. } else if (kod == "003") {
  435. text0 = 8;
  436. text1 = 8;
  437. disp();
  438. } else if (kod == "120") {
  439. text0 = 9;
  440. text1 = 9;
  441. disp();
  442. } else if (kod == "103") {
  443. text0 = 10;
  444. text1 = 10;
  445. disp();
  446. } else if (kod == "023") {
  447. text0 = 11;
  448. text1 = 11;
  449. disp();
  450. } else if (kod == "123") {
  451. text0 = 12;
  452. text1 = 12;
  453. disp();
  454. }*/
  455. //disp();
  456. j = 1;
  457. e_old = e;
  458. reading_time_old = reading_time;
  459. }
  460. } else {
  461. //myDisplay.print(httpCode);
  462. }
  463. } else {
  464. if (debug == 1) Serial.println("HTTP ERROR");
  465. set = 0;
  466. text0 = 3;
  467. text1 = 2;
  468. disp();
  469. digitalWrite(led_r, HIGH);
  470. delay(1000);
  471. digitalWrite(led_r, LOW);
  472. }
  473. light++;
  474. http.end();
  475. } else {
  476. if (debug == 1) Serial.println("WiFi ERROR");
  477. set = 0;
  478. text0 = 1;
  479. text1 = 2;
  480. disp();
  481. digitalWrite(led_r, HIGH);
  482. delay(2000);
  483. digitalWrite(led_r, LOW);
  484. i = 0;
  485. }
  486.  
  487. delay(100);
  488. }
  489.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement