Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.58 KB | None | 0 0
  1. // Project: Ethernet Controller Board
  2. // date : 15/Oct/2019
  3. // Main MCU: Mega 2560 pro mini
  4.  
  5. /* Hardware Connected
  6. // W5500 Module Ethernet PHY
  7. // Eight Relay
  8. 6 light
  9. 2 Fan
  10. // two digital input
  11. // 4 analog input
  12. */
  13. /* Progrem Development
  14. 1) Rack controller firmware implement:
  15.  
  16. Timer as real time clock for plants light system
  17. Network Time Protocol (NTP) Client
  18. scheduling
  19.  
  20. 1) Network Time Protocol (NTP) Client DONE
  21. 2) Timer as real time clock for plants light system scheduling. DONE but Test
  22. 3) througnt MQTT do board setting
  23. */
  24. #include <ArduinoJson.h>
  25. #include <SPI.h>
  26. #include <Ethernet.h>
  27. #include <PubSubClient.h>
  28. #include <EEPROM.h>
  29. #include <avr/wdt.h>
  30. #include <EthernetUdp.h>
  31. // ---------------------------- Function prototype
  32. char DIP_scan();
  33. void sendEvent(String type, byte level, String status,String msg);
  34. void publishStatus();
  35. void SerialAvailable();
  36. void Serial2Available();
  37. void ethernetConn();
  38. void sendNTPpacket(const char * address);
  39. // ---------------------------- Board Info
  40. #define version "V5.1"
  41. #define S1 "S1"
  42. #define S2 "S2"
  43. #define S3 "S3"
  44. #define S4 "S4"
  45. #define S5 "S5"
  46. #define S6 "S6"
  47. #define F1 "F1"
  48. #define F2 "F2"
  49. // ---------------------------- EEPROM Address
  50. #define EP_S1 1
  51. #define EP_S2 2
  52. #define EP_S3 3
  53. #define EP_S4 4
  54. #define EP_S5 5
  55. #define EP_S6 6
  56. #define EP_S7 7
  57. #define EP_S8 8
  58. #define EP_KeepAlive 21
  59. #define FirstBoot 20
  60.  
  61. // ---------------------------- Rack's name
  62. #define R0 "mixture"
  63. #define R1 "rack01"
  64. #define R2 "rack02"
  65. #define R3 "rack03"
  66. #define R4 "rack04"
  67. #define R5 "rack05"
  68. #define R6 "rack06"
  69. #define R7 "rack07"
  70. #define R8 "rack08"
  71. #define R9 "rack09"
  72. #define R10 "rack10"
  73. #define R11 "rack11"
  74. #define R12 "rack12"
  75. #define R13 "rack13"
  76. #define R14 "rack14"
  77. #define R15 "rack15"
  78. String boardName[17] = {R0,R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15};
  79. // ---------------------------- GPIO In
  80. #define ADC_1 A0
  81. #define ADC_2 A1
  82. #define ADC_3 A2
  83. #define ADC_4 A3
  84. #define MAG_1 2
  85. #define MAG_2 3
  86. #define DIP_1 9
  87. #define DIP_2 8
  88. #define DIP_3 7
  89. #define DIP_4 6
  90.  
  91. #define IN1 31
  92. #define IN2 29
  93. #define IN3 27
  94. #define IN4 25
  95. #define IN5 23
  96. #define IN6 21
  97. #define IN7 19
  98. // ---------------------------- GPIO Out
  99. #define Relay_1 45
  100. #define Relay_2 46
  101. #define Relay_3 44
  102. #define Relay_4 42
  103. #define Relay_5 41
  104. #define Relay_6 40
  105. #define Relay_7 39
  106. #define Relay_8 47
  107. #define RS485_TX 16
  108. #define RS485_RX 17
  109. #define LED_1 4 // States
  110. #define LED_2 5 // Ethernet
  111.  
  112.  
  113. // ---------------------------- Reconnect
  114. unsigned char reconnectTask;
  115. unsigned short reconnectDelay;
  116. unsigned char reconnectRetry;
  117. // ---------------------------- Data type
  118. union {
  119. uint8_t Byte;
  120. struct {
  121. uint8_t r1 : 1; // bit position 0
  122. uint8_t r2 : 1; // bit positions 1..2
  123. uint8_t r3 : 1; // bit positions 3..5
  124. uint8_t r4 : 1; // bit positions 6..7
  125. uint8_t r5 : 1; // bit positions 6..7
  126. uint8_t r6 : 1; // bit positions 6..7
  127. uint8_t r7 : 1; // bit positions 6..7
  128. uint8_t r8 : 1; // bit positions 6..7
  129. // total # of bits just needs to add up to the uint8_t size
  130. }bit;
  131. } DIP_Struct;
  132.  
  133. long prevmillis;
  134. long oneSTask;
  135. bool blinkLED;
  136. // char buffer[200];
  137. bool bootup;
  138.  
  139. char RelayArray[] = {0, Relay_1, Relay_2 ,Relay_3 ,Relay_4 ,Relay_5 ,Relay_6 ,Relay_7 ,Relay_8};
  140. char RelayStates[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
  141. char DIP_Temp;
  142.  
  143. char RelayRunNB = 1;
  144.  
  145. unsigned long upTime_min;
  146. unsigned char hour;
  147. unsigned char min;
  148. unsigned int oneMin;
  149. unsigned char oneHour;
  150. bool oneHourTask_start;
  151. unsigned char keepAliveCout;
  152.  
  153. bool masterModeU0;
  154. bool masterModeMQTT;
  155. // ---------------------------- UDP NTP -------------------------------------------//
  156. unsigned char Udp_Task;
  157. unsigned long Udp_Taskdelay;
  158. unsigned char Udp_TimeSet_hour;
  159. // ----------------------------- Ethernet -------------------------------------------//
  160. // byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xEE };
  161. // IPAddress ip(192, 168, 0, 150);
  162. IPAddress server(192, 168, 0, 10);
  163. unsigned long lastTx = 0;
  164. EthernetClient ethClient;
  165. PubSubClient client(ethClient);
  166. char publishData[255];
  167. char subscribeData[64];
  168. #define ETH_ADD_LAST_VALUE 200
  169. // --------------------------------- UDP
  170. unsigned int localPort = 8888; // local port to listen for UDP packets
  171. const char timeServer[] = "192.168.0.250"; // time.nist.gov NTP server
  172. const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
  173. byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
  174. // A UDP instance to let us send and receive packets over UDP
  175. EthernetUDP Udp;
  176.  
  177. // ============================= Callback ========================================== //
  178. void callback(char* topic, char* payload, unsigned int length) {
  179. // int i;
  180. Serial.print("Message arrived [");
  181. Serial.print(topic);
  182. Serial.print("] ");
  183. // for (int i=0;i<length;i++) {
  184. // Serial.print((char)payload[i]);
  185. // }
  186. Serial.println(payload);
  187.  
  188. StaticJsonBuffer<100> jsonBuffer2;
  189. JsonObject& root = jsonBuffer2.parseObject(payload);
  190.  
  191. if (!root.success()) {
  192. Serial.println("{\"Error\":\"parseObject_callback\"}");
  193. Serial.println();
  194. return;
  195. }
  196.  
  197. if(root.containsKey("STATUS")) publishStatus();
  198.  
  199. else if(root.containsKey("test")) Serial.println("Got test from MQTT");
  200.  
  201. else if(root.containsKey("keepalive") && root["keepalive"] < 0){
  202. keepAliveCout = root["keepalive"];
  203. EEPROM.write(EP_KeepAlive, keepAliveCout);
  204. }
  205. else if(root["master"] == 1){
  206. // root.remove("master");
  207. root.printTo(publishData);
  208. Serial2.println(publishData);
  209. masterModeMQTT = 1;
  210. return;
  211. }
  212. if(root.containsKey(F2) && root[F2] < 2){
  213. if(root[F2] != RelayStates[8]){
  214. RelayRunNB = 8;
  215. EEPROM.write(EP_S8, root[F2]);
  216. RelayStates[8] = root[F2];
  217. }
  218. }
  219. if(root.containsKey(F1) && root[F1] < 2){
  220. if(root[F1] != RelayStates[7]){
  221. RelayRunNB = 7;
  222. EEPROM.write(EP_S7, root[F1]);
  223. RelayStates[7] = root[F1];
  224. }
  225. }
  226. if(root.containsKey(S6) && root[S6] < 2){
  227. if(root[S6] != RelayStates[6]){
  228. RelayRunNB = 6;
  229. EEPROM.write(EP_S6, root[S6]);
  230. RelayStates[6] = root[S6];
  231. }
  232. }
  233. if(root.containsKey(S5) && root[S5] < 2){
  234. if(root[S5] != RelayStates[5]){
  235. RelayRunNB = 5;
  236. EEPROM.write(EP_S5, root[S5]);
  237. RelayStates[5] = root[S5];
  238. }
  239. }
  240. if(root.containsKey(S4) && root[S4] < 2){
  241. if(root[S4] != RelayStates[4]){
  242. RelayRunNB = 4;
  243. EEPROM.write(EP_S4, root[S4]);
  244. RelayStates[4] = root[S4];
  245. }
  246. }
  247. if(root.containsKey(S3) && root[S3] < 2){
  248. if(root[S3] != RelayStates[3]){
  249. RelayRunNB = 3;
  250. EEPROM.write(EP_S3, root[S3]);
  251. RelayStates[3] = root[S3];
  252. }
  253. }
  254. if(root.containsKey(S2) && root[S2] < 2){
  255. if(root[S2] != RelayStates[2]){
  256. RelayRunNB = 2;
  257. EEPROM.write(EP_S2, root[S2]);
  258. RelayStates[2] = root[S2];
  259. }
  260. }
  261. if(root.containsKey(S1) && root[S1] < 2){
  262. if(root[S1] != RelayStates[1]){
  263. RelayRunNB = 1;
  264. EEPROM.write(EP_S1, root[S1]);
  265. RelayStates[1] = root[S1];
  266. }
  267. }
  268.  
  269. if (root["RESET"] == 1) asm volatile (" jmp 0");
  270. // {
  271. // client.publish("/boomgrow/foodcomputer3/events","{\"ERROR\":\"CMD NO FOND\"}");
  272. // Serial.println("CMD NO FOND");
  273. // }
  274. }
  275.  
  276. // ============================= Reconnect ========================================== //
  277. void reconnect() {
  278. digitalWrite(LED_2, 0);
  279. Serial.print("Attempting MQTT connection...");
  280. char tempString[64];
  281. boardName[DIP_Temp].toCharArray(tempString,7);
  282. if (client.connect(tempString)) {
  283. Serial.println("connected");
  284. // client.publish("/boomgrow/foodcomputer3/events","{\"board\":\"Eth connected\"}");
  285. sendEvent("MQTT", 3, "MQTT=1", "connected to MQTT");
  286. // ... and resubscribe
  287. // sprintf(subscribeData, "/boomgrow/foodcomputer3/cmds/%s",boardName[DIP_Temp]);
  288. sprintf(subscribeData,"/boomgrow/foodcomputer3/%s/cmds", tempString);
  289. Serial.print("subscribe: ");
  290. Serial.println(subscribeData);
  291. client.subscribe(subscribeData);
  292. digitalWrite(LED_2, 1);
  293. reconnectTask = 0;
  294. } else {
  295. Serial.print("failed, rc=");
  296. Serial.println(client.state());
  297. reconnectTask = 1;
  298. reconnectDelay = 1000;
  299. }
  300. }
  301.  
  302. // ============================= Setup ========================================== //
  303. void setup()
  304. {
  305. unsigned char i;
  306. // ---------------------------- Serial
  307. Serial.begin(57600);
  308. Serial2.begin(9600);
  309. // ---------------------------- Watch dog timer
  310. wdt_enable(WDTO_8S);
  311. // ---------------------------- GPIO Config
  312. pinMode(MAG_1, INPUT);
  313. pinMode(MAG_2, INPUT);
  314. pinMode(DIP_1, INPUT_PULLUP);
  315. pinMode(DIP_2, INPUT_PULLUP);
  316. pinMode(DIP_3, INPUT_PULLUP);
  317. pinMode(DIP_4, INPUT_PULLUP);
  318.  
  319. pinMode(IN1, INPUT_PULLUP);
  320. pinMode(IN2, INPUT_PULLUP);
  321. pinMode(IN3, INPUT_PULLUP);
  322. pinMode(IN4, INPUT_PULLUP);
  323. pinMode(IN5, INPUT_PULLUP);
  324. pinMode(IN6, INPUT_PULLUP);
  325. pinMode(IN7, INPUT_PULLUP);
  326.  
  327. pinMode(ADC_1, INPUT);
  328. pinMode(ADC_2, INPUT);
  329. pinMode(ADC_3, INPUT);
  330. pinMode(ADC_4, INPUT);
  331.  
  332. pinMode(Relay_1, OUTPUT);
  333. pinMode(Relay_2, OUTPUT);
  334. pinMode(Relay_3, OUTPUT);
  335. pinMode(Relay_4, OUTPUT);
  336. pinMode(Relay_5, OUTPUT);
  337. pinMode(Relay_6, OUTPUT);
  338. pinMode(Relay_7, OUTPUT);
  339. pinMode(Relay_8, OUTPUT);
  340. pinMode(LED_1, OUTPUT);
  341. pinMode(LED_2, OUTPUT);
  342. // ---------------------------- Timer
  343. //set timer1 interrupt at 1Hz
  344. TCCR1A = 0;// set entire TCCR1A register to 0
  345. TCCR1B = 0;// same for TCCR1B
  346. TCNT1 = 0;//initialize counter value to 0
  347. // set compare match register for 1hz increments
  348. OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
  349. // turn on CTC mode
  350. TCCR1B |= (1 << WGM12);
  351. // Set CS12 and CS10 bits for 1024 prescaler
  352. TCCR1B |= (1 << CS12) | (1 << CS10);
  353. // enable timer compare interrupt
  354. TIMSK1 |= (1 << OCIE1A);
  355. // ---------------------------- Ethernet Server
  356. DIP_Temp = DIP_scan();
  357. Ethernet.init(53); // You can use Ethernet.init(pin) to configure the CS pin
  358. ethernetConn();
  359. Udp.begin(localPort);
  360. // ---------------------------- MQTT
  361. client.setServer(server, 1883);
  362. client.setCallback(callback);
  363. // ---------------------------- Data
  364. // RelayStates.Byte = 255;
  365. if (EEPROM.read(EP_KeepAlive) == 255) EEPROM.write(EP_KeepAlive,59);
  366. keepAliveCout = EEPROM.read(EP_KeepAlive);
  367. if (EEPROM.read(FirstBoot) > 1)
  368. {
  369. EEPROM.write(FirstBoot,1);
  370. for (i = EP_S1; i < 9; i++) EEPROM.write(i,0);
  371. for (i = EP_S1; i < 9; i++) RelayStates[i] = 0;
  372. }else for (i = EP_S1; i < 9; i++) RelayStates[i] = EEPROM.read(i);
  373. // Serial.print("FirstBoot ");
  374. // Serial.println(EEPROM.read(FirstBoot)); // FistBoot = 255
  375. if (!client.connected()) reconnect();
  376. // Udp.begin(localPort);
  377.  
  378. sendEvent("power-up", 3, "POWERUP=1", "power up/rebooted");
  379. reconnectRetry = 0;
  380. upTime_min = 0;
  381. oneMin = 0;
  382. oneHour = 0;
  383. oneHourTask_start = 0;
  384.  
  385. masterModeMQTT = 0;
  386. masterModeU0 = 0;
  387. }
  388. ISR(TIMER1_COMPA_vect){ //timer1 interrupt 1Hz to increament Uptime
  389. oneMin++;
  390. if (oneMin > 59)
  391. {
  392. min++;
  393. upTime_min++;
  394. oneHour++;
  395. oneMin = 0;
  396. }
  397. if (oneHour > keepAliveCout)
  398. {
  399. oneHour = 0;
  400. oneHourTask_start = 1;
  401. }
  402. if (min > 59){
  403. min = 0;
  404. hour++;
  405. if (hour > 23) hour = 0;
  406. }
  407.  
  408. }
  409. // ============================= Loop ========================================== //
  410.  
  411. void loop()
  412. {
  413. long currtmillis = millis();
  414. if (currtmillis - prevmillis > 1) {
  415. prevmillis = currtmillis;
  416.  
  417. // if (!client.connected()) {
  418. switch (reconnectTask)
  419. {
  420. case 0:
  421. if (!client.connected()) {
  422. reconnect();
  423. reconnectDelay = 2000;
  424. reconnectRetry = 0;
  425. reconnectTask = 1;
  426. }
  427. break;
  428. case 1:
  429. if (reconnectDelay > 0) reconnectDelay--;
  430. else {
  431. reconnectTask = 0;
  432. reconnectRetry += 1;
  433. }
  434. if (reconnectRetry > 4) reconnectTask = 2;
  435. break;
  436. case 2:
  437. // ---------------------------- MQTT
  438. client.setServer(server, 1883);
  439. client.setCallback(callback);
  440. // ---------------------------- Ethernet Server
  441. DIP_Temp = DIP_scan();
  442. ethernetConn();
  443. reconnectRetry = 0;
  444. reconnectTask = 0;
  445. break;
  446. }
  447. // }else reconnectTask = 0;
  448.  
  449. ++oneSTask;
  450. if(oneSTask > 300){
  451. oneSTask=0;
  452. blinkLED = !blinkLED;
  453. digitalWrite(LED_1, blinkLED);
  454.  
  455. digitalWrite(RelayArray[RelayRunNB], RelayStates[RelayRunNB]);
  456. RelayRunNB++;
  457. if(RelayRunNB > 8) RelayRunNB = 1;
  458.  
  459. if (oneHourTask_start)
  460. {
  461. oneHourTask_start = 0;
  462. sendEvent("keep-alive", 4, "KEEPALIVE=1", "keep alive every 1H");
  463. }
  464.  
  465. // ----------------------------------------------- NTP task
  466. switch (Udp_Task)
  467. {
  468. case 0:
  469. sendNTPpacket(timeServer); // send an NTP packet to a time server
  470. Udp_Task++;
  471. break;
  472. case 1:
  473. if (Udp.parsePacket()) {
  474. // We've received a packet, read the data from it
  475. Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
  476.  
  477. // the timestamp starts at byte 40 of the received packet and is four bytes,
  478. // or two words, long. First, extract the two words:
  479.  
  480. unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
  481. unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
  482. // combine the four bytes (two words) into a long integer
  483. // this is NTP time (seconds since Jan 1 1900):
  484.  
  485. unsigned long secsSince1900 = highWord << 16 | lowWord;
  486. // Serial.print("Seconds since Jan 1 1900 = ");
  487. // Serial.println(secsSince1900);
  488.  
  489. // // now convert NTP time into everyday time:
  490. // Serial.print("Unix time = ");
  491. // // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
  492. const unsigned long seventyYears = 2208988800UL;
  493. // // subtract seventy years:
  494. unsigned long epoch = secsSince1900 - seventyYears;
  495. // // print Unix time:
  496. // Serial.println(epoch);
  497.  
  498.  
  499. // print the hour, minute and second:
  500. Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
  501. hour = (epoch % 86400L) / 3600 + 8;
  502. Serial.print(hour); // print the hour (86400 equals secs per day)
  503. Serial.print(':');
  504. if (((epoch % 3600) / 60) < 10) {
  505. // In the first 10 minutes of each hour, we'll want a leading '0'
  506. Serial.print('0');
  507. }
  508. min = (epoch % 3600) / 60;
  509. Serial.print(min); // print the minute (3600 equals secs per minute)
  510. Serial.print(':');
  511. if ((epoch % 60) < 10) {
  512. // In the first 10 seconds of each minute, we'll want a leading '0'
  513. Serial.print('0');
  514. }
  515. Serial.println(epoch % 60); // print the second
  516. Udp_Task++;
  517. Udp_Taskdelay = upTime_min;
  518. }
  519. break;
  520. case 2:
  521. if(hour = 11 && Udp_Taskdelay != 1){
  522. Udp_Taskdelay = 1;
  523. Ethernet.maintain();
  524. Udp_Task = 0;
  525. }
  526. if (hour == 12)
  527. {
  528. // Udp_Task = 0;
  529. Udp_Taskdelay = 0;
  530. }
  531.  
  532. // if (Udp_Taskdelay > 0) Udp_Taskdelay--;
  533. // else
  534. // {
  535. // }
  536. break;
  537.  
  538. default:
  539. break;
  540. }
  541.  
  542.  
  543. }
  544.  
  545. }
  546.  
  547. client.loop();
  548. if (Serial.available()) {
  549. SerialAvailable();
  550. }
  551. if (Serial2.available()) {
  552. Serial2Available();
  553. }
  554.  
  555. wdt_reset();
  556. }
  557. // ============================= Serial ========================================== //
  558. void SerialAvailable(){
  559. char Rx0[100];
  560. StaticJsonBuffer<100> jsonBuffer;
  561. Serial.readBytesUntil('\n', Rx0, 100);
  562. JsonObject& root = jsonBuffer.parseObject(Rx0);
  563.  
  564. if (!root.success()) {
  565. Serial.println("{\"Error\":\"parseObject\"}");
  566. Serial.println(Rx0);
  567. return;
  568. }
  569. else if(root["master"] == 1){
  570. root.printTo(publishData);
  571. Serial2.println(publishData);
  572. masterModeU0 = 1;
  573. }
  574. else if(root["test"] == 1){
  575. Serial.println("{\"recei\":\"test = 1\"}");
  576. }
  577. else if(root["name"] == boardName[DIP_Temp]){
  578. if(root.containsKey("hi")){
  579. root["name"] = boardName[DIP_Temp];
  580. root["hi"] = "say hi to master";
  581.  
  582. root.printTo(publishData);
  583. Serial.println(publishData);
  584. }
  585. }
  586. if (root["RESET"] == 1) asm volatile (" jmp 0");
  587. }
  588. // ============================= Serial 2 ========================================== //
  589. void Serial2Available(){
  590. char Rx0[100];
  591. StaticJsonBuffer<100> jsonBuffer;
  592. Serial2.readBytesUntil('\n', Rx0, 100);
  593. JsonObject& root = jsonBuffer.parseObject(Rx0);
  594.  
  595. if (!root.success()) {
  596. // Serial2.println("{\"Error\":\"parseObject\"}");
  597. // Serial2.println(Rx0);
  598. masterModeU0 = 0;
  599. masterModeMQTT = 0;
  600. return;
  601. }
  602.  
  603. else if (masterModeU0){
  604. root.printTo(publishData);
  605. Serial.println(publishData);
  606. masterModeU0 = 0;
  607. }
  608. else if (masterModeMQTT){
  609. root.printTo(publishData);
  610. client.publish("/boomgrow/foodcomputer3/events",publishData);
  611. masterModeMQTT = 0;
  612. }
  613. else if(root["test"] == 1){
  614. Serial2.println("{\"recei\":\"test = 1\"}");
  615. }
  616.  
  617. else if(root["name"] == boardName[DIP_Temp]){
  618. if(root.containsKey("hi")){
  619. root["name"] = boardName[DIP_Temp];
  620. root["hi"] = "say hi to master";
  621. root.printTo(publishData);
  622. Serial2.println(publishData);
  623. }
  624. if(root.containsKey(F2) && root[F2] < 2){
  625. if(root[F2] != RelayStates[8]){
  626. RelayRunNB = 8;
  627. EEPROM.write(EP_S8, root[F2]);
  628. RelayStates[8] = root[F2];
  629. }
  630. }
  631. if(root.containsKey(F1) && root[F1] < 2){
  632. if(root[F1] != RelayStates[7]){
  633. RelayRunNB = 7;
  634. EEPROM.write(EP_S7, root[F1]);
  635. RelayStates[7] = root[F1];
  636. }
  637. }
  638. if(root.containsKey(S6) && root[S6] < 2){
  639. if(root[S6] != RelayStates[6]){
  640. RelayRunNB = 6;
  641. EEPROM.write(EP_S6, root[S6]);
  642. RelayStates[6] = root[S6];
  643. }
  644. }
  645. if(root.containsKey(S5) && root[S5] < 2){
  646. if(root[S5] != RelayStates[5]){
  647. RelayRunNB = 5;
  648. EEPROM.write(EP_S5, root[S5]);
  649. RelayStates[5] = root[S5];
  650. }
  651. }
  652. if(root.containsKey(S4) && root[S4] < 2){
  653. if(root[S4] != RelayStates[4]){
  654. RelayRunNB = 4;
  655. EEPROM.write(EP_S4, root[S4]);
  656. RelayStates[4] = root[S4];
  657. }
  658. }
  659. if(root.containsKey(S3) && root[S3] < 2){
  660. if(root[S3] != RelayStates[3]){
  661. RelayRunNB = 3;
  662. EEPROM.write(EP_S3, root[S3]);
  663. RelayStates[3] = root[S3];
  664. }
  665. }
  666. if(root.containsKey(S2) && root[S2] < 2){
  667. if(root[S2] != RelayStates[2]){
  668. RelayRunNB = 2;
  669. EEPROM.write(EP_S2, root[S2]);
  670. RelayStates[2] = root[S2];
  671. }
  672. }
  673. if(root.containsKey(S1) && root[S1] < 2){
  674. if(root[S1] != RelayStates[1]){
  675. RelayRunNB = 1;
  676. EEPROM.write(EP_S1, root[S1]);
  677. RelayStates[1] = root[S1];
  678. }
  679. }
  680. if (root["RESET"] == 1) asm volatile (" jmp 0");
  681. }
  682. }
  683. // ============================= Ethernet ========================================== //
  684. void ethernetConn(){
  685. char i;
  686. IPAddress ip(192, 168, 0, DIP_Temp + ETH_ADD_LAST_VALUE);
  687. byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, DIP_Temp };
  688. Ethernet.begin(mac, ip);
  689. // if (Ethernet.begin(mac, ip) == 0) {
  690. // Serial.println("Failed to configure Ethernet using DHCP");
  691. // // Check for Ethernet hardware present
  692. // if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  693. // Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
  694. // } else if (Ethernet.linkStatus() == LinkOFF)
  695. // Serial.println("Ethernet cable is not connected.");
  696. // }else{
  697. delay(100);
  698. Serial.print("Ethernet IP: ");
  699. Serial.print(ip);
  700. Serial.print("\nMAC: ");
  701. for(i = 0;i<6;i++){
  702. Serial.print(mac[i]);
  703. Serial.print(":");
  704. }
  705. Serial.println();
  706. // }
  707. }
  708. // ============================= DIP scan ========================================== //
  709. char DIP_scan(){
  710. DIP_Struct.bit.r1 = !digitalRead(DIP_1);
  711. DIP_Struct.bit.r2 = !digitalRead(DIP_2);
  712. DIP_Struct.bit.r3 = !digitalRead(DIP_3);
  713. DIP_Struct.bit.r4 = !digitalRead(DIP_4);
  714. return DIP_Struct.Byte;
  715. // Serial.print("Dip: ");
  716. // Serial.println(DIP_Struct.Byte);
  717. }
  718. // ============================= send Even ========================================== //
  719. void sendEvent(String type, byte level, String status,String msg)
  720. {
  721. // JSON declarations
  722. StaticJsonBuffer<200> jsonBuffer;
  723. JsonObject& root = jsonBuffer.createObject();
  724. root["name"] = boardName[DIP_Temp];
  725. root["events"] = 1;
  726. root["upTime"] = upTime_min;
  727. root["level"] = level;
  728. root["type"] = type;
  729. root["status"] = status;
  730. root["msg"] = msg;
  731.  
  732. root.printTo(publishData);
  733. client.publish("/boomgrow/foodcomputer3/events",publishData);
  734. Serial.println(publishData);
  735.  
  736. }
  737. void publishStatus(){
  738.  
  739. StaticJsonBuffer<200> jsonBuffer;
  740. JsonObject& root = jsonBuffer.createObject();
  741.  
  742. root["name"] = boardName[DIP_Temp];
  743. // root["events"] = 1;
  744. root["version"] = version;
  745. root["upTime"] = upTime_min;
  746. root["hour"] = hour;
  747. root["minute"] = min;
  748. root["level"] = 4;
  749. root["type"] = "status";
  750. // root["status"] = "status=1";
  751. // root["keepAliveCout"] = keepAliveCout;
  752. root[S1] = RelayStates[1];
  753. root[S2] = RelayStates[2];
  754. root[S3] = RelayStates[3];
  755. root[S4] = RelayStates[4];
  756. root[S5] = RelayStates[5];
  757. root[S6] = RelayStates[6];
  758. root[F1] = RelayStates[7];
  759. root[F2] = RelayStates[8];
  760.  
  761. root.printTo(publishData);
  762. client.publish("/boomgrow/foodcomputer3/events",publishData);
  763. Serial.println(publishData);
  764. }
  765.  
  766. // send an NTP request to the time server at the given address
  767. void sendNTPpacket(const char * address) {
  768. // set all bytes in the buffer to 0
  769. memset(packetBuffer, 0, NTP_PACKET_SIZE);
  770. // Initialize values needed to form NTP request
  771. // (see URL above for details on the packets)
  772. packetBuffer[0] = 0b11100011; // LI, Version, Mode
  773. packetBuffer[1] = 0; // Stratum, or type of clock
  774. packetBuffer[2] = 6; // Polling Interval
  775. packetBuffer[3] = 0xEC; // Peer Clock Precision
  776. // 8 bytes of zero for Root Delay & Root Dispersion
  777. packetBuffer[12] = 49;
  778. packetBuffer[13] = 0x4E;
  779. packetBuffer[14] = 49;
  780. packetBuffer[15] = 52;
  781.  
  782. // all NTP fields have been given values, now
  783. // you can send a packet requesting a timestamp:
  784. Udp.beginPacket(address, 123); // NTP requests are to port 123
  785. Udp.write(packetBuffer, NTP_PACKET_SIZE);
  786. Udp.endPacket();
  787. }
  788.  
  789. /*
  790. {"test":1}
  791. {"master":1,"name":"rack02","hi":1}
  792.  
  793.  
  794. // {S1:0,S2:0,S3:0,S4:0,S5:0,S6:0,F1:0,F2:0}
  795. // {"S1":0,"S2":0,"S3":0,"S4":0,"S5":0,"S6":0,"S7":0,"S8":0}
  796.  
  797. {"master":1,"name":"rack06","S1":1,"S3":1}
  798.  
  799. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement