Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Project: Ethernet Controller Board
- // date : 15/Oct/2019
- // Main MCU: Mega 2560 pro mini
- /* Hardware Connected
- // W5500 Module Ethernet PHY
- // Eight Relay
- 6 light
- 2 Fan
- // two digital input
- // 4 analog input
- Progrem Development
- 1) Rack controller firmware implement:
- Timer as real time clock for plants light system
- Network Time Protocol (NTP) Client
- scheduling
- 1) Network Time Protocol (NTP) Client DONE
- 2) Timer as real time clock for plants light system scheduling. DONE but Test
- 3) througnt MQTT do board setting
- Date: 21/11/2019
- By : Stanley Seow
- Fixed :
- - mqtt incoming parse error buffers
- Added/ changes :
- - Change subscribed topic to /bg/fc/racks/cmds
- - Change published topic to /bg/fc/racks/status
- - Change events topic to /bg/fc/events
- */
- #include <ArduinoJson.h>
- #include <SPI.h>
- #include <Ethernet.h>
- #include <PubSubClient.h>
- #include <EEPROM.h>
- #include <avr/wdt.h>
- #include <EthernetUdp.h>
- // ---------------------------- Function prototype
- char DIP_scan();
- void sendEvent(String type, byte level, String status,String msg);
- void publishStatus();
- void SerialAvailable();
- void Serial2Available();
- void ethernetConn();
- void sendNTPpacket(const char * address);
- // ---------------------------- Board Info
- #define version "V6"
- #define S1 "L1"
- #define S2 "L2"
- #define S3 "L3"
- #define S4 "L4"
- #define S5 "L5"
- #define S6 "L6"
- #define F1 "F1"
- #define F2 "F2"
- // ---------------------------- EEPROM Address
- #define EP_S1 1
- #define EP_S2 2
- #define EP_S3 3
- #define EP_S4 4
- #define EP_S5 5
- #define EP_S6 6
- #define EP_S7 7
- #define EP_S8 8
- #define EP_KeepAlive 21
- #define FirstBoot 20
- // ---------------------------- Rack's name
- #define R0 "mixture"
- #define R1 "rack01"
- #define R2 "rack02"
- #define R3 "rack03"
- #define R4 "rack04"
- #define R5 "rack05"
- #define R6 "rack06"
- #define R7 "rack07"
- #define R8 "rack08"
- #define R9 "rack09"
- #define R10 "rack10"
- #define R11 "rack11"
- #define R12 "rack12"
- #define R13 "rack13"
- #define R14 "rack14"
- #define R15 "rack15"
- String boardName[17] = {R0,R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15};
- // ---------------------------- GPIO In
- #define ADC_1 A0
- #define ADC_2 A1
- #define ADC_3 A2
- #define ADC_4 A3
- #define MAG_1 2
- #define MAG_2 3
- #define DIP_1 9
- #define DIP_2 8
- #define DIP_3 7
- #define DIP_4 6
- #define IN1 31
- #define IN2 29
- #define IN3 27
- #define IN4 25
- #define IN5 23
- #define IN6 21
- #define IN7 19
- // ---------------------------- GPIO Out
- #define Relay_1 45
- #define Relay_2 46
- #define Relay_3 44
- #define Relay_4 42
- #define Relay_5 41
- #define Relay_6 40
- #define Relay_7 39
- #define Relay_8 47
- #define RS485_TX 16
- #define RS485_RX 17
- #define LED_1 4 // States
- #define LED_2 5 // Ethernet
- // ---------------------------- Reconnect
- unsigned char reconnectTask;
- unsigned short reconnectDelay;
- unsigned char reconnectRetry;
- // ---------------------------- Data type
- union {
- uint8_t Byte;
- struct {
- uint8_t r1 : 1; // bit position 0
- uint8_t r2 : 1; // bit positions 1..2
- uint8_t r3 : 1; // bit positions 3..5
- uint8_t r4 : 1; // bit positions 6..7
- uint8_t r5 : 1; // bit positions 6..7
- uint8_t r6 : 1; // bit positions 6..7
- uint8_t r7 : 1; // bit positions 6..7
- uint8_t r8 : 1; // bit positions 6..7
- // total # of bits just needs to add up to the uint8_t size
- }bit;
- } DIP_Struct;
- long prevmillis;
- long oneSTask;
- bool blinkLED;
- // char buffer[200];
- bool bootup;
- char RelayArray[] = {0, Relay_1, Relay_2 ,Relay_3 ,Relay_4 ,Relay_5 ,Relay_6 ,Relay_7 ,Relay_8};
- char RelayStates[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
- char DIP_Temp;
- char RelayRunNB = 1;
- unsigned long upTime_min;
- unsigned char hour;
- unsigned char min;
- unsigned int oneMin;
- unsigned char oneHour;
- bool oneHourTask_start;
- unsigned char keepAliveCout;
- bool masterModeU0;
- bool masterModeMQTT;
- // ---------------------------- UDP NTP -------------------------------------------//
- unsigned char Udp_Task;
- unsigned long Udp_Taskdelay;
- unsigned char Udp_TimeSet_hour;
- // ----------------------------- Ethernet -------------------------------------------//
- // byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xEE };
- // IPAddress ip(192, 168, 0, 150);
- IPAddress server(192, 168, 0, 10);
- unsigned long lastTx = 0;
- EthernetClient ethClient;
- PubSubClient client(ethClient);
- char publishData[255];
- char subscribeData[64];
- #define ETH_ADD_LAST_VALUE 200
- // --------------------------------- UDP
- unsigned int localPort = 8888; // local port to listen for UDP packets
- const char timeServer[] = "192.168.0.250"; // time.nist.gov NTP server
- const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
- byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
- // A UDP instance to let us send and receive packets over UDP
- EthernetUDP Udp;
- // ============================= Callback ========================================== //
- void callback(char* topic, char* payload, unsigned int length) {
- // int i;
- char inBuffer[101];
- for (int i = 0; i < length; i++) {
- inBuffer[i] = (char)payload[i];
- inBuffer[i + 1] = '\0'; // Terminate the string
- }
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- Serial.println(inBuffer);
- StaticJsonBuffer<100> jsonBuffer2;
- JsonObject& root = jsonBuffer2.parseObject(inBuffer);
- if (!root.success()) {
- Serial.println("{\"Error\":\"parseObject_callback\"}");
- Serial.println();
- return;
- }
- if(root.containsKey("STATUS")) {
- publishStatus();
- } else if(root.containsKey("test")) {
- Serial.println("Got test from MQTT");
- } else if(root.containsKey("keepalive") && root["keepalive"] < 0){
- keepAliveCout = root["keepalive"];
- EEPROM.write(EP_KeepAlive, keepAliveCout);
- }
- else if(root["master"] == 1){
- // root.remove("master");
- root.printTo(publishData);
- Serial2.println(publishData);
- masterModeMQTT = 1;
- return;
- }
- if ( root.containsKey("name") && root["name"] == boardName[DIP_Temp] ) {
- Serial.println("Matching name");
- Serial.println(boardName[DIP_Temp]);
- if(root.containsKey(F2) && root[F2] < 2){
- if(root[F2] != RelayStates[8]){
- RelayRunNB = 8;
- EEPROM.write(EP_S8, root[F2]);
- RelayStates[8] = root[F2];
- }
- }
- if(root.containsKey(F1) && root[F1] < 2){
- if(root[F1] != RelayStates[7]){
- RelayRunNB = 7;
- EEPROM.write(EP_S7, root[F1]);
- RelayStates[7] = root[F1];
- }
- }
- if(root.containsKey(S6) && root[S6] < 2){
- if(root[S6] != RelayStates[6]){
- RelayRunNB = 6;
- EEPROM.write(EP_S6, root[S6]);
- RelayStates[6] = root[S6];
- }
- }
- if(root.containsKey(S5) && root[S5] < 2){
- if(root[S5] != RelayStates[5]){
- RelayRunNB = 5;
- EEPROM.write(EP_S5, root[S5]);
- RelayStates[5] = root[S5];
- }
- }
- if(root.containsKey(S4) && root[S4] < 2){
- if(root[S4] != RelayStates[4]){
- RelayRunNB = 4;
- EEPROM.write(EP_S4, root[S4]);
- RelayStates[4] = root[S4];
- }
- }
- if(root.containsKey(S3) && root[S3] < 2){
- if(root[S3] != RelayStates[3]){
- RelayRunNB = 3;
- EEPROM.write(EP_S3, root[S3]);
- RelayStates[3] = root[S3];
- }
- }
- if(root.containsKey(S2) && root[S2] < 2){
- if(root[S2] != RelayStates[2]){
- RelayRunNB = 2;
- EEPROM.write(EP_S2, root[S2]);
- RelayStates[2] = root[S2];
- }
- }
- if(root.containsKey(S1) && root[S1] < 2){
- if(root[S1] != RelayStates[1]){
- RelayRunNB = 1;
- EEPROM.write(EP_S1, root[S1]);
- RelayStates[1] = root[S1];
- }
- }
- } // end of matching json name
- if (root["RESET"] == 1) asm volatile (" jmp 0");
- // {
- // client.publish("/boomgrow/foodcomputer3/events","{\"ERROR\":\"CMD NO FOND\"}");
- // Serial.println("CMD NO FOND");
- // }
- }
- // ============================= Reconnect ========================================== //
- void reconnect() {
- digitalWrite(LED_2, 0);
- Serial.print("Attempting MQTT connection...");
- char tempString[64];
- boardName[DIP_Temp].toCharArray(tempString,7);
- if (client.connect(tempString)) {
- Serial.println("connected");
- sendEvent("MQTT", 3, "MQTT=1", "connected to MQTT");
- // resubscribe
- sprintf(subscribeData,"/bg/fc/racks/cmds", tempString);
- Serial.print("subscribe: ");
- Serial.println(subscribeData);
- client.subscribe(subscribeData);
- digitalWrite(LED_2, 1);
- reconnectTask = 0;
- } else {
- Serial.print("failed, rc=");
- Serial.println(client.state());
- reconnectTask = 1;
- reconnectDelay = 1000;
- }
- }
- // ============================= Setup ========================================== //
- void setup()
- {
- unsigned char i;
- // ---------------------------- Serial
- Serial.begin(57600);
- Serial2.begin(9600);
- // ---------------------------- Watch dog timer
- wdt_enable(WDTO_8S);
- // ---------------------------- GPIO Config
- pinMode(MAG_1, INPUT);
- pinMode(MAG_2, INPUT);
- pinMode(DIP_1, INPUT_PULLUP);
- pinMode(DIP_2, INPUT_PULLUP);
- pinMode(DIP_3, INPUT_PULLUP);
- pinMode(DIP_4, INPUT_PULLUP);
- pinMode(IN1, INPUT_PULLUP);
- pinMode(IN2, INPUT_PULLUP);
- pinMode(IN3, INPUT_PULLUP);
- pinMode(IN4, INPUT_PULLUP);
- pinMode(IN5, INPUT_PULLUP);
- pinMode(IN6, INPUT_PULLUP);
- pinMode(IN7, INPUT_PULLUP);
- pinMode(ADC_1, INPUT);
- pinMode(ADC_2, INPUT);
- pinMode(ADC_3, INPUT);
- pinMode(ADC_4, INPUT);
- pinMode(Relay_1, OUTPUT);
- pinMode(Relay_2, OUTPUT);
- pinMode(Relay_3, OUTPUT);
- pinMode(Relay_4, OUTPUT);
- pinMode(Relay_5, OUTPUT);
- pinMode(Relay_6, OUTPUT);
- pinMode(Relay_7, OUTPUT);
- pinMode(Relay_8, OUTPUT);
- pinMode(LED_1, OUTPUT);
- pinMode(LED_2, OUTPUT);
- // ---------------------------- Timer
- //set timer1 interrupt at 1Hz
- TCCR1A = 0;// set entire TCCR1A register to 0
- TCCR1B = 0;// same for TCCR1B
- TCNT1 = 0;//initialize counter value to 0
- // set compare match register for 1hz increments
- OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
- // turn on CTC mode
- TCCR1B |= (1 << WGM12);
- // Set CS12 and CS10 bits for 1024 prescaler
- TCCR1B |= (1 << CS12) | (1 << CS10);
- // enable timer compare interrupt
- TIMSK1 |= (1 << OCIE1A);
- // ---------------------------- Ethernet Server
- DIP_Temp = DIP_scan();
- Ethernet.init(53); // You can use Ethernet.init(pin) to configure the CS pin
- ethernetConn();
- Udp.begin(localPort);
- // ---------------------------- MQTT
- client.setServer(server, 1883);
- client.setCallback(callback);
- // ---------------------------- Data
- // RelayStates.Byte = 255;
- if (EEPROM.read(EP_KeepAlive) == 255) EEPROM.write(EP_KeepAlive,59);
- keepAliveCout = EEPROM.read(EP_KeepAlive);
- if (EEPROM.read(FirstBoot) > 1)
- {
- EEPROM.write(FirstBoot,1);
- for (i = EP_S1; i < 9; i++) EEPROM.write(i,0);
- for (i = EP_S1; i < 9; i++) RelayStates[i] = 0;
- }else for (i = EP_S1; i < 9; i++) RelayStates[i] = EEPROM.read(i);
- // Serial.print("FirstBoot ");
- // Serial.println(EEPROM.read(FirstBoot)); // FistBoot = 255
- if (!client.connected()) reconnect();
- // Udp.begin(localPort);
- sendEvent("power-up", 3, "POWERUP=1", "power up/rebooted");
- reconnectRetry = 0;
- upTime_min = 0;
- oneMin = 0;
- oneHour = 0;
- oneHourTask_start = 0;
- masterModeMQTT = 0;
- masterModeU0 = 0;
- }
- ISR(TIMER1_COMPA_vect){ //timer1 interrupt 1Hz to increament Uptime
- oneMin++;
- if (oneMin > 59)
- {
- min++;
- upTime_min++;
- oneHour++;
- oneMin = 0;
- }
- if (oneHour > keepAliveCout)
- {
- oneHour = 0;
- oneHourTask_start = 1;
- }
- if (min > 59){
- min = 0;
- hour++;
- if (hour > 23) hour = 0;
- }
- }
- // ============================= Loop ========================================== //
- void loop()
- {
- long currtmillis = millis();
- if (currtmillis - prevmillis > 1) {
- prevmillis = currtmillis;
- // if (!client.connected()) {
- switch (reconnectTask)
- {
- case 0:
- if (!client.connected()) {
- reconnect();
- reconnectDelay = 2000;
- reconnectRetry = 0;
- reconnectTask = 1;
- }
- break;
- case 1:
- if (reconnectDelay > 0) reconnectDelay--;
- else {
- reconnectTask = 0;
- reconnectRetry += 1;
- }
- if (reconnectRetry > 4) reconnectTask = 2;
- break;
- case 2:
- // ---------------------------- MQTT
- client.setServer(server, 1883);
- client.setCallback(callback);
- // ---------------------------- Ethernet Server
- DIP_Temp = DIP_scan();
- ethernetConn();
- reconnectRetry = 0;
- reconnectTask = 0;
- break;
- }
- // }else reconnectTask = 0;
- ++oneSTask;
- if(oneSTask > 300){
- oneSTask=0;
- blinkLED = !blinkLED;
- digitalWrite(LED_1, blinkLED);
- digitalWrite(RelayArray[RelayRunNB], RelayStates[RelayRunNB]);
- RelayRunNB++;
- if(RelayRunNB > 8) RelayRunNB = 1;
- if (oneHourTask_start)
- {
- oneHourTask_start = 0;
- sendEvent("keep-alive", 4, "KEEPALIVE=1", "keep alive every 1H");
- }
- // ----------------------------------------------- NTP task
- switch (Udp_Task)
- {
- case 0:
- sendNTPpacket(timeServer); // send an NTP packet to a time server
- Udp_Task++;
- break;
- case 1:
- if (Udp.parsePacket()) {
- // We've received a packet, read the data from it
- Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
- // the timestamp starts at byte 40 of the received packet and is four bytes,
- // or two words, long. First, extract the two words:
- unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
- unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
- // combine the four bytes (two words) into a long integer
- // this is NTP time (seconds since Jan 1 1900):
- unsigned long secsSince1900 = highWord << 16 | lowWord;
- // Serial.print("Seconds since Jan 1 1900 = ");
- // Serial.println(secsSince1900);
- // // now convert NTP time into everyday time:
- // Serial.print("Unix time = ");
- // // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
- const unsigned long seventyYears = 2208988800UL;
- // // subtract seventy years:
- unsigned long epoch = secsSince1900 - seventyYears;
- // // print Unix time:
- // Serial.println(epoch);
- // print the hour, minute and second:
- Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
- hour = (epoch % 86400L) / 3600 + 8;
- Serial.print(hour); // print the hour (86400 equals secs per day)
- Serial.print(':');
- if (((epoch % 3600) / 60) < 10) {
- // In the first 10 minutes of each hour, we'll want a leading '0'
- Serial.print('0');
- }
- min = (epoch % 3600) / 60;
- Serial.print(min); // print the minute (3600 equals secs per minute)
- Serial.print(':');
- if ((epoch % 60) < 10) {
- // In the first 10 seconds of each minute, we'll want a leading '0'
- Serial.print('0');
- }
- Serial.println(epoch % 60); // print the second
- Udp_Task++;
- Udp_Taskdelay = upTime_min;
- }
- break;
- case 2:
- if(hour = 11 && Udp_Taskdelay != 1){
- Udp_Taskdelay = 1;
- Ethernet.maintain();
- Udp_Task = 0;
- }
- if (hour == 12)
- {
- // Udp_Task = 0;
- Udp_Taskdelay = 0;
- }
- // if (Udp_Taskdelay > 0) Udp_Taskdelay--;
- // else
- // {
- // }
- break;
- default:
- break;
- }
- }
- }
- client.loop();
- if (Serial.available()) {
- SerialAvailable();
- }
- if (Serial2.available()) {
- Serial2Available();
- }
- wdt_reset();
- }
- // ============================= Serial ========================================== //
- void SerialAvailable(){
- char Rx0[100];
- StaticJsonBuffer<100> jsonBuffer;
- Serial.readBytesUntil('\n', Rx0, 100);
- JsonObject& root = jsonBuffer.parseObject(Rx0);
- if (!root.success()) {
- Serial.println("{\"Error\":\"parseObject\"}");
- Serial.println(Rx0);
- return;
- }
- else if(root["master"] == 1){
- root.printTo(publishData);
- Serial2.println(publishData);
- masterModeU0 = 1;
- }
- else if(root["test"] == 1){
- Serial.println("{\"recei\":\"test = 1\"}");
- }
- else if(root["name"] == boardName[DIP_Temp]){
- if(root.containsKey("hi")){
- root["name"] = boardName[DIP_Temp];
- root["hi"] = "say hi to master";
- root.printTo(publishData);
- Serial.println(publishData);
- }
- }
- if (root["RESET"] == 1) asm volatile (" jmp 0");
- }
- // ============================= Serial 2 ========================================== //
- void Serial2Available(){
- char Rx0[100];
- StaticJsonBuffer<100> jsonBuffer;
- Serial2.readBytesUntil('\n', Rx0, 100);
- JsonObject& root = jsonBuffer.parseObject(Rx0);
- if (!root.success()) {
- // Serial2.println("{\"Error\":\"parseObject\"}");
- // Serial2.println(Rx0);
- masterModeU0 = 0;
- masterModeMQTT = 0;
- return;
- }
- else if (masterModeU0){
- root.printTo(publishData);
- Serial.println(publishData);
- masterModeU0 = 0;
- }
- else if (masterModeMQTT){
- root.printTo(publishData);
- client.publish("/bg/fc/events",publishData);
- masterModeMQTT = 0;
- }
- else if(root["test"] == 1){
- Serial2.println("{\"recei\":\"test = 1\"}");
- }
- else if(root["name"] == boardName[DIP_Temp]){
- if(root.containsKey("hi")){
- root["name"] = boardName[DIP_Temp];
- root["hi"] = "say hi to master";
- root.printTo(publishData);
- Serial2.println(publishData);
- }
- if(root.containsKey(F2) && root[F2] < 2){
- if(root[F2] != RelayStates[8]){
- RelayRunNB = 8;
- EEPROM.write(EP_S8, root[F2]);
- RelayStates[8] = root[F2];
- }
- }
- if(root.containsKey(F1) && root[F1] < 2){
- if(root[F1] != RelayStates[7]){
- RelayRunNB = 7;
- EEPROM.write(EP_S7, root[F1]);
- RelayStates[7] = root[F1];
- }
- }
- if(root.containsKey(S6) && root[S6] < 2){
- if(root[S6] != RelayStates[6]){
- RelayRunNB = 6;
- EEPROM.write(EP_S6, root[S6]);
- RelayStates[6] = root[S6];
- }
- }
- if(root.containsKey(S5) && root[S5] < 2){
- if(root[S5] != RelayStates[5]){
- RelayRunNB = 5;
- EEPROM.write(EP_S5, root[S5]);
- RelayStates[5] = root[S5];
- }
- }
- if(root.containsKey(S4) && root[S4] < 2){
- if(root[S4] != RelayStates[4]){
- RelayRunNB = 4;
- EEPROM.write(EP_S4, root[S4]);
- RelayStates[4] = root[S4];
- }
- }
- if(root.containsKey(S3) && root[S3] < 2){
- if(root[S3] != RelayStates[3]){
- RelayRunNB = 3;
- EEPROM.write(EP_S3, root[S3]);
- RelayStates[3] = root[S3];
- }
- }
- if(root.containsKey(S2) && root[S2] < 2){
- if(root[S2] != RelayStates[2]){
- RelayRunNB = 2;
- EEPROM.write(EP_S2, root[S2]);
- RelayStates[2] = root[S2];
- }
- }
- if(root.containsKey(S1) && root[S1] < 2){
- if(root[S1] != RelayStates[1]){
- RelayRunNB = 1;
- EEPROM.write(EP_S1, root[S1]);
- RelayStates[1] = root[S1];
- }
- }
- if (root["RESET"] == 1) asm volatile (" jmp 0");
- }
- }
- // ============================= Ethernet ========================================== //
- void ethernetConn(){
- char i;
- IPAddress ip(192, 168, 0, DIP_Temp + ETH_ADD_LAST_VALUE);
- byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, DIP_Temp };
- Ethernet.begin(mac, ip);
- // if (Ethernet.begin(mac, ip) == 0) {
- // Serial.println("Failed to configure Ethernet using DHCP");
- // // Check for Ethernet hardware present
- // if (Ethernet.hardwareStatus() == EthernetNoHardware) {
- // Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
- // } else if (Ethernet.linkStatus() == LinkOFF)
- // Serial.println("Ethernet cable is not connected.");
- // }else{
- delay(100);
- Serial.print("Ethernet IP: ");
- Serial.print(ip);
- Serial.print("\nMAC: ");
- for(i = 0;i<6;i++){
- Serial.print(mac[i]);
- Serial.print(":");
- }
- Serial.println();
- // }
- }
- // ============================= DIP scan ========================================== //
- char DIP_scan(){
- DIP_Struct.bit.r1 = !digitalRead(DIP_1);
- DIP_Struct.bit.r2 = !digitalRead(DIP_2);
- DIP_Struct.bit.r3 = !digitalRead(DIP_3);
- DIP_Struct.bit.r4 = !digitalRead(DIP_4);
- return DIP_Struct.Byte;
- // Serial.print("Dip: ");
- // Serial.println(DIP_Struct.Byte);
- }
- // ============================= send Even ========================================== //
- void sendEvent(String type, byte level, String status,String msg)
- {
- // JSON declarations
- StaticJsonBuffer<200> jsonBuffer;
- JsonObject& root = jsonBuffer.createObject();
- root["name"] = boardName[DIP_Temp];
- root["events"] = 1;
- root["uptime"] = upTime_min;
- root["level"] = level;
- root["type"] = type;
- root["status"] = status;
- root["msg"] = msg;
- root.printTo(publishData);
- client.publish("/bg/fc/events",publishData);
- Serial.println(publishData);
- }
- void publishStatus(){
- StaticJsonBuffer<200> jsonBuffer;
- JsonObject& root = jsonBuffer.createObject();
- root["name"] = boardName[DIP_Temp];
- // root["events"] = 1;
- root["version"] = version;
- root["uptime"] = upTime_min;
- root["hour"] = hour;
- root["minute"] = min;
- root["level"] = 4;
- root["type"] = "status";
- root[S1] = ( RelayStates[1] ? "true":"false" ) ;
- root[S2] = ( RelayStates[2] ? "true":"false" ) ;
- root[S3] = ( RelayStates[3] ? "true":"false" ) ;
- root[S4] = ( RelayStates[4] ? "true":"false" ) ;
- root[S5] = ( RelayStates[5] ? "true":"false" ) ;
- root[S6] = ( RelayStates[6] ? "true":"false" ) ;
- root[F1] = ( RelayStates[7] ? "true":"false" ) ;
- root[F2] = ( RelayStates[8] ? "true":"false" ) ;
- root.printTo(publishData);
- client.publish("/bg/fc/racks/status",publishData);
- Serial.println(publishData);
- }
- // send an NTP request to the time server at the given address
- void sendNTPpacket(const char * address) {
- // set all bytes in the buffer to 0
- memset(packetBuffer, 0, NTP_PACKET_SIZE);
- // Initialize values needed to form NTP request
- // (see URL above for details on the packets)
- packetBuffer[0] = 0b11100011; // LI, Version, Mode
- packetBuffer[1] = 0; // Stratum, or type of clock
- packetBuffer[2] = 6; // Polling Interval
- packetBuffer[3] = 0xEC; // Peer Clock Precision
- // 8 bytes of zero for Root Delay & Root Dispersion
- packetBuffer[12] = 49;
- packetBuffer[13] = 0x4E;
- packetBuffer[14] = 49;
- packetBuffer[15] = 52;
- // all NTP fields have been given values, now
- // you can send a packet requesting a timestamp:
- Udp.beginPacket(address, 123); // NTP requests are to port 123
- Udp.write(packetBuffer, NTP_PACKET_SIZE);
- Udp.endPacket();
- }
- /*
- {"test":1}
- {"master":1,"name":"rack02","hi":1}
- // {S1:0,S2:0,S3:0,S4:0,S5:0,S6:0,F1:0,F2:0}
- // {"S1":0,"S2":0,"S3":0,"S4":0,"S5":0,"S6":0,"S7":0,"S8":0}
- {"master":1,"name":"rack06","S1":1,"S3":1}
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement