This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
firmware-http.ino
142 lines (124 loc) · 4.97 KB
/
firmware-http.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* @briskhome/irrigation <lib/irrigation/index.js>
* └ firmware/firmware-http.ino
*
* Скетч для работы контроллера полива по протоколу HTTP.
*
* @author Егор Зайцев <ezaitsev@briskhome.com>
* @version 0.2.0
*/
#include <SPI.h>
#include <NewPing.h>
#include <OneWire.h>
#include <Ethernet2.h>
#include <TroykaDHT11.h>
#include <DallasTemperature.h>
#define TANK_VALVE 5
#define TANK_TRIGGER 1
#define TANK_ECHO 2
#define TANK_DISTANCE 500
#define TANK_TEMPERATURE 0
#define GRDN_VALVE 6
#define GRDN_MOISTURE 6 // A4
#define GRHS_VALVE 7
#define GRHS_MOISTURE 5 // A5
#define GRHS_DHT 3
#define MAX_DISTANCE 250
#define MIN_DISTANCE 50
byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x6E, 0x7A };
String request;
DHT11 dht(GRHS_DHT);
NewPing sonar(TANK_TRIGGER, TANK_ECHO, TANK_DISTANCE);
OneWire oneWire(TANK_TEMPERATURE);
IPAddress ip(192, 168, 10, 100);
EthernetServer server(80);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
pinMode(TANK_VALVE, OUTPUT);
pinMode(GRDN_VALVE, OUTPUT);
pinMode(GRHS_VALVE, OUTPUT);
Ethernet.begin(mac);
sensors.begin();
server.begin();
dht.begin();
Serial.print("IP address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
EthernetClient client = server.available();
if (client) {
Serial.println("Client connected");
boolean currentLineIsBlank = true;
while (client.connected()) {
while (client.available()) {
char c = client.read();
if (c == '\n' && currentLineIsBlank) {
while (client.available())
{
request += char((client.read()));
}
Serial.println(request);
if (request != "") {
if (request.indexOf("{\"name\":\"tank\",\"status\":true}") >= 0) {
digitalWrite(TANK_VALVE, HIGH);
} else if (request.indexOf("{\"name\":\"tank\",\"status\":false}") >= 0) {
digitalWrite(TANK_VALVE, LOW);
} else if (request.indexOf("{\"name\":\"garden\",\"status\":true}") >= 0) {
digitalWrite(GRDN_VALVE, HIGH);
} else if (request.indexOf("{\"name\":\"garden\",\"status\":false}") >= 0) {
digitalWrite(GRDN_VALVE, LOW);
} else if (request.indexOf("{\"name\":\"greenhouse\",\"status\":true}") >= 0) {
digitalWrite(GRHS_VALVE, HIGH);
} else if (request.indexOf("{\"name\":\"greenhouse\",\"status\":false}") >= 0) {
digitalWrite(GRHS_VALVE, LOW);
}
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");
client.println();
client.println("{\"status\": {\"code\": 200, \"message\": \"OK\"}}");
client.println();
} else {
String greenhouseTemperature;
String greenhouseHumidity;
String greenhouseMoisture = String(analogRead(5));
int ping = sonar.ping();
String tankLevel = String(ping / US_ROUNDTRIP_CM);
String tankTemperature = String(sensors.getTempCByIndex(0));
if (dht.read() == DHT_OK) {
greenhouseTemperature = String(dht.getTemperatureC());
greenhouseHumidity = String(dht.getHumidity());
} else {
greenhouseTemperature = String(dht.read());
greenhouseHumidity = String(dht.read());
}
String gardenMoisture = String(analogRead(4));
String tankValve = (digitalRead(TANK_VALVE) == HIGH) ? "true" : "false";
String gardenValve = (digitalRead(GRDN_VALVE) == HIGH) ? "true" : "false";
String greenhouseValve = (digitalRead(GRHS_VALVE) == HIGH) ? "true" : "false";
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");
client.println();
client.print("{\"status\": {\"code\": 200, \"message\": \"OK\"}, \"data\": [");
client.print("{\"name\": \"tank\", \"status\": " + tankValve + ", \"sensors\": {\"temperature\": " + tankTemperature + ", \"level\": " + tankLevel + "}},");
client.print("{\"name\": \"garden\", \"status\": " + gardenValve + ", \"sensors\": {\"moisture\": " + gardenMoisture + "}},");
client.print("{\"name\": \"greenhouse\", \"status\": " + greenhouseValve + ", \"sensors\": {\"temperature\": " + greenhouseTemperature + ", \"humidity\": " + greenhouseHumidity + ", \"moisture\": " + greenhouseMoisture + "}}");
client.print("]}");
// client.println();
}
client.stop();
} else if (c == '\n') {
currentLineIsBlank = true;
} else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
Serial.println("Client disconnected");
request = "";
}
}