Farm-Data-Relay-System/examples/Controller_examples/Irrigation/Irrigation.ino

180 lines
4.2 KiB
Arduino
Raw Normal View History

2022-09-02 00:49:32 +00:00
// FARM DATA RELAY SYSTEM
//
// Irrigation Controller
//
2022-11-10 00:54:51 +00:00
// Developed by Timm Bogner (timmbogner@gmail.com) in Urbana, Illinois, USA.
2022-09-02 00:49:32 +00:00
//
//
#include "fdrs_node_config.h"
2022-09-02 00:49:32 +00:00
#include <fdrs_node.h>
#define CONTROL_1 101 //Address for controller 1
#define CONTROL_2 102 //Address for controller 2
#define CONTROL_3 103 //Address for controller 3
#define CONTROL_4 104 //Address for controller 4
2022-09-02 00:49:32 +00:00
2022-11-29 13:14:26 +00:00
#define COIL_1 4 //Coil Pin 1
#define COIL_2 5 //Coil Pin 2
#define COIL_3 13 //Coil Pin 3
#define COIL_4 14 //Coil Pin 4
2022-09-02 00:49:32 +00:00
int status_1 = 0;
int status_2 = 0;
int status_3 = 0;
int status_4 = 0;
2023-03-26 16:12:18 +00:00
bool isData = false;
2023-01-11 20:54:00 +00:00
bool newStatus = false;
2022-09-02 00:49:32 +00:00
void fdrs_recv_cb(DataReading theData) {
2022-11-09 01:04:10 +00:00
DBG(String(theData.id));
2023-01-11 20:54:00 +00:00
switch (theData.t) {
case 0: // Incoming command is to SET a value
2023-03-26 16:41:46 +00:00
2023-01-11 20:54:00 +00:00
switch (theData.id) {
case CONTROL_1:
status_1 = (int)theData.d;
2023-03-26 16:12:18 +00:00
isData = true;
2023-01-11 20:54:00 +00:00
break;
case CONTROL_2:
status_2 = (int)theData.d;
2023-03-26 16:12:18 +00:00
isData = true;
2023-01-11 20:54:00 +00:00
break;
case CONTROL_3:
status_3 = (int)theData.d;
2023-03-26 16:12:18 +00:00
isData = true;
2023-01-11 20:54:00 +00:00
break;
case CONTROL_4:
status_4 = (int)theData.d;
2023-03-26 16:12:18 +00:00
isData = true;
2023-01-11 20:54:00 +00:00
break;
}
2022-09-02 00:49:32 +00:00
break;
2023-01-11 20:54:00 +00:00
case 1: // Incoming command is to GET a value
2023-03-26 16:41:46 +00:00
switch (theData.id) {
2023-01-11 20:54:00 +00:00
case CONTROL_1:
if (digitalRead(COIL_1) == HIGH) {
loadFDRS(1, STATUS_T, CONTROL_1);
} else {
loadFDRS(0, STATUS_T, CONTROL_1);
}
break;
case CONTROL_2:
if (digitalRead(COIL_2) == HIGH) {
loadFDRS(1, STATUS_T, CONTROL_2);
} else {
loadFDRS(0, STATUS_T, CONTROL_2);
}
break;
case CONTROL_3:
if (digitalRead(COIL_3) == HIGH) {
loadFDRS(1, STATUS_T, CONTROL_3);
} else {
loadFDRS(0, STATUS_T, CONTROL_3);
}
break;
case CONTROL_4:
if (digitalRead(COIL_4) == HIGH) {
loadFDRS(1, STATUS_T, CONTROL_4);
} else {
loadFDRS(0, STATUS_T, CONTROL_4);
}
break;
}
newStatus = true;
2022-09-02 00:49:32 +00:00
break;
}
}
2023-01-11 20:54:00 +00:00
void checkCoils() { // Sends back a status report for each coil pin.
if (digitalRead(COIL_1) == HIGH) {
loadFDRS(1, STATUS_T, CONTROL_1);
} else {
loadFDRS(0, STATUS_T, CONTROL_1);
}
if (digitalRead(COIL_2) == HIGH) {
loadFDRS(1, STATUS_T, CONTROL_2);
} else {
loadFDRS(0, STATUS_T, CONTROL_2);
}
if (digitalRead(COIL_3) == HIGH) {
loadFDRS(1, STATUS_T, CONTROL_3);
} else {
loadFDRS(0, STATUS_T, CONTROL_3);
}
if (digitalRead(COIL_4) == HIGH) {
loadFDRS(1, STATUS_T, CONTROL_4);
} else {
loadFDRS(0, STATUS_T, CONTROL_4);
}
if (sendFDRS()) {
DBG("Packet received by gateway");
} else {
DBG("Unable to communicate with gateway!");
}
}
2023-01-11 21:02:05 +00:00
void updateCoils() { //These are set up for relay module which are active-LOW. Swap 'HIGH'and 'LOW' in this function to use the inverse.
2022-09-02 00:49:32 +00:00
if (status_1) {
digitalWrite(COIL_1, LOW);
2022-11-09 01:04:10 +00:00
} else {
digitalWrite(COIL_1, HIGH);
2022-09-02 00:49:32 +00:00
}
if (status_2) {
digitalWrite(COIL_2, LOW);
2022-11-09 01:04:10 +00:00
} else {
digitalWrite(COIL_2, HIGH);
2022-09-02 00:49:32 +00:00
}
if (status_3) {
digitalWrite(COIL_3, LOW);
2022-11-09 01:04:10 +00:00
} else {
digitalWrite(COIL_3, HIGH);
2022-09-02 00:49:32 +00:00
}
if (status_4) {
digitalWrite(COIL_4, LOW);
2022-11-09 01:04:10 +00:00
} else {
digitalWrite(COIL_4, HIGH);
2022-09-02 00:49:32 +00:00
}
}
void setup() {
beginFDRS();
2023-03-26 16:12:18 +00:00
pingFDRS(1000);
2022-09-02 00:49:32 +00:00
if (addFDRS(1000, fdrs_recv_cb)) {
subscribeFDRS(CONTROL_1);
subscribeFDRS(CONTROL_2);
subscribeFDRS(CONTROL_3);
subscribeFDRS(CONTROL_4);
} else {
DBG("Not Connected");
}
pinMode(COIL_1, OUTPUT);
2022-11-09 01:04:10 +00:00
digitalWrite(COIL_1, HIGH);
2022-09-02 00:49:32 +00:00
pinMode(COIL_2, OUTPUT);
2022-11-09 01:04:10 +00:00
digitalWrite(COIL_2, HIGH);
2022-09-02 00:49:32 +00:00
pinMode(COIL_3, OUTPUT);
2022-11-09 01:04:10 +00:00
digitalWrite(COIL_3, HIGH);
2022-09-02 00:49:32 +00:00
pinMode(COIL_4, OUTPUT);
2022-11-09 01:04:10 +00:00
digitalWrite(COIL_4, HIGH);
2022-09-02 00:49:32 +00:00
DBG("FARM DATA RELAY SYSTEM :: Irrigation Module");
}
void loop() {
2023-03-27 12:44:19 +00:00
loopFDRS();
2023-03-26 16:12:18 +00:00
if (isData) {
isData = false;
2022-09-02 00:49:32 +00:00
updateCoils();
checkCoils();
2022-09-02 00:49:32 +00:00
}
2023-01-11 20:54:00 +00:00
if (newStatus) {
newStatus = false;
if (sendFDRS()) {
DBG("Packet received by gateway");
} else {
DBG("Unable to communicate with gateway!");
}
}
2022-09-02 00:49:32 +00:00
}