-
Notifications
You must be signed in to change notification settings - Fork 0
/
Huzzah.ino
315 lines (265 loc) · 9.95 KB
/
Huzzah.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//LCD imports
#include <SPI.h>
#include "LCD_Driver.h"
#include "LCD_GUI_Paint.h"
#include "LCD_Images.h"
//Connectivity imports
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "arduino_secrets.h" // Wifi and MQTT secrets
//TimeZone import
#include <ezTime.h>
//Constants
const char* ssid = SECRET_SSID;
const char* password = SECRET_PASS;
const char* mqttuser = SECRET_MQTTUSER;
const char* mqttpass = SECRET_MQTTPASS;
const char* mqtt_server = "mqtt.cetools.org";
//Plant profile
#include "PlantProfile.h"
//Internal fields
WiFiClient espClient;
PubSubClient client(espClient);
int currentMood = -1;
int pumpPin = 13;
int humidifierPin = 12;
int pumpToBeOn = -1;
int humidfierToBeOn = -1;
Timezone GB;
void setup() {
initialiseLCDScreen();
drawMoodOnScreen(1);
Serial.println("Hi");
//Connect to an SSID and print local IP address, taken from CASA plant monitoring class
Serial.print("Connecting to ");
Serial.println(SECRET_SSID);
WiFi.begin(SECRET_SSID, SECRET_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
//Initialise time settings
waitForSync();
Serial.println("UTC: " + UTC.dateTime());
GB.setLocation("Europe/London");
Serial.println("London time: " + GB.dateTime());
//Initialise MQTT connection
client.setServer(mqtt_server, 9001);
client.setCallback(callback);
pinMode(humidifierPin, OUTPUT);
pinMode(pumpPin, OUTPUT);
//drawMoodOnScreen(1);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
//When a new sensor reading is received from Arduino, it will be parsed and
//sent over MQTT, then the LCD screen is updated accordingly
if (Serial.available() > 0) {
String sensorDataReceived = "";
sensorDataReceived = Serial.readString();
if(sensorDataReceived != ""){
/************************************************************************/
/* */
/* Sensor values order: */
/* */
/* 1. dht22: air temperature */
/* 2. dht22: air humidity */
/* 3. ds18b20: soil temperature */
/* 4. HW390: soil humidity */
/* 5. Si1145: light */
/* 6. Si1145: infrared */
/* 7. Si1145: UV */
/* 8. Pump status: (0/on 1/off) => Integer */
/* 9. Humidifier status: (0/on 1/off) => Integer */
/* */
/************************************************************************/
String dht22_airTemperature = splitString(sensorDataReceived,',',0);
String dht22_airHumidity = splitString(sensorDataReceived,',',1);
String ds18b20_soilTemperature = splitString(sensorDataReceived,',',2);
String hw390_soilHumidity = splitString(sensorDataReceived,',',3);
String si1145_light = splitString(sensorDataReceived,',',4);
String si1145_infrared = splitString(sensorDataReceived,',',5);
String si1145_uv = splitString(sensorDataReceived,',',6);
String pump_status = splitString(sensorDataReceived,',',7);
String humidifier_status = splitString(sensorDataReceived,',',8);
Serial.println("------ ATMega8A said: -------");
Serial.println("dht22_airTemperature: " + dht22_airTemperature);
Serial.println("dht22_airHumidity: " + dht22_airHumidity);
Serial.println("ds18b20_soilTemperature: " + ds18b20_soilTemperature);
Serial.println("hw390_soilHumidity: " + hw390_soilHumidity);
Serial.println("si1145_light: " + si1145_light);
Serial.println("si1145_infrared: " + si1145_infrared);
Serial.println("si1145_uv: " + si1145_uv);
Serial.println("pump_status: " + pump_status);
Serial.println("humidifier_status: " + humidifier_status);
//int mood = resolveMood(soilMoistureReading.toFloat(), humidityReading.toFloat());
sendMQTT(
dht22_airTemperature,
dht22_airHumidity,
ds18b20_soilTemperature,
hw390_soilHumidity,
si1145_light,
si1145_infrared,
si1145_uv,
pump_status,
humidifier_status);
//drawMoodOnScreen(mood);
}
}
}
// Decide if the plant is happy or sad according to soil moisture and humidity,
// to be developed in the future to include other sensors
int resolveMood(float soilMoistureReading, float humidityReading){
if(soilMoistureReading > LOWER_MOISTURE_LEVEL)
{
return 0;
}
if(soilMoistureReading < LOWER_MOISTURE_LEVEL)
{
if(humidityReading< LOWER_HUMIDITY_LEVEL)
{
return 0;
}
return 1;
}
}
void sendMQTT(
String dht22_airTemperature,
String dht22_airHumidity,
String ds18b20_soilTemperature,
String hw390_soilHumidity,
String si1145_light,
String si1145_infrared,
String si1145_uv,
String pump_status,
String humidifier_status) {
if (!client.connected()) {
reconnect();
}
client.loop();
char msg[50];
dht22_airTemperature.toCharArray(msg,dht22_airTemperature.length()+1);
client.publish("student/CASA0014/plant/ucfnmyr/plantemoji/airTemperature", msg);
dht22_airHumidity.toCharArray(msg,dht22_airHumidity.length()+1);
client.publish("student/CASA0014/plant/ucfnmyr/plantemoji/airHumidity", msg);
ds18b20_soilTemperature.toCharArray(msg,ds18b20_soilTemperature.length()+1);
client.publish("student/CASA0014/plant/ucfnmyr/plantemoji/soilTemperature", msg);
hw390_soilHumidity.toCharArray(msg,hw390_soilHumidity.length()+1);
client.publish("student/CASA0014/plant/ucfnmyr/plantemoji/soilHumidity", msg);
si1145_light.toCharArray(msg,si1145_light.length()+1);
client.publish("student/CASA0014/plant/ucfnmyr/plantemoji/light", msg);
si1145_infrared.toCharArray(msg,si1145_infrared.length()+1);
client.publish("student/CASA0014/plant/ucfnmyr/plantemoji/infrared", msg);
si1145_uv.toCharArray(msg,si1145_uv.length()+1);
client.publish("student/CASA0014/plant/ucfnmyr/plantemoji/uv", msg);
pump_status.toCharArray(msg,pump_status.length()+1);
client.publish("student/CASA0014/plant/ucfnmyr/plantemoji/pumpStatus", msg);
humidifier_status.toCharArray(msg,humidifier_status.length()+1);
client.publish("student/CASA0014/plant/ucfnmyr/plantemoji/humidifierStatus", msg);
}
//Preparing & clearing LCD screen
void initialiseLCDScreen(){
Config_Init();
LCD_Init();
LCD_SetBacklight(100);
Paint_NewImage(LCD_WIDTH, LCD_HEIGHT, 0, BLACK);
Paint_Clear(BLACK);
}
//LCD screen output
void drawMoodOnScreen(int mood){
if(mood == 0 && currentMood != 0)
{
currentMood = 0;
Serial.println("Drawing sad face");
Paint_Clear(BLACK);
Paint_DrawImage(gImage_sad, 5, 5, 165, 160);
}
if(mood == 1 && currentMood != 1)
{
currentMood = 1;
Serial.println("Drawing happy face");
Paint_Clear(BLACK);
//Paint_SetRotate(ROTATE_0);
Paint_DrawImage(gImage_happy, 5, 4, 160, 160);
//Paint_DrawString_EN(5, 5, "Next humidifier schedule:",&Font16, BLACK, GREEN);
Paint_DrawImage(gImage_qrcode, 5, 160, 160, 160);
//Paint_DrawImage(gImage_watering_now, 6, 160, 160, 160);
}
}
//MQTT reconnection, taken from CASA plant monitoring class
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) { // while not (!) connected....
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "Plantemoji-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// ... and subscribe to messages on broker
client.subscribe("student/CASA0014/plant/ucfnmyr/plantemoji/humidifierControl");
client.subscribe("student/CASA0014/plant/ucfnmyr/plantemoji/pumpControl");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
//MQTT callback event, taken from CASA plant monitoring class
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
String convertedTopic = String(topic);
if(convertedTopic.indexOf("pumpControl") != -1)
{
if ((char)payload[0] == '1') {
digitalWrite(pumpPin, HIGH);
Serial.println("pump signal to high");
} else {
digitalWrite(pumpPin, LOW);
Serial.println("pump signal to low");
}
}
if(convertedTopic.indexOf("humidifierControl") != -1)
{
if ((char)payload[0] == '1') {
digitalWrite(humidifierPin, HIGH);
Serial.println("humidifier signal to high");
} else {
digitalWrite(humidifierPin, LOW);
Serial.println("humidifier signal to low");
}
}
}
// Useful string splitting function, taken from:
// https://stackoverflow.com/questions/9072320/split-string-into-string-array
String splitString(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}