-
Notifications
You must be signed in to change notification settings - Fork 1
/
irrigation.ino
103 lines (76 loc) · 2.59 KB
/
irrigation.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
#include <ESP8266WiFi.h>
#include <EEPROM.h>
#include <WiFiUdp.h>
#include <coap-simple.h>
#include <DHT.h>
#define ESP_SSID "Zvirata"
#define ESP_PASS // Fill me
#define IOT_GATEWAY "192.168.0.203"
const int PORT = 5683;
const char PATH[] = "measurement";
#define SECOND 1e6
// Fun fact this pin is also needed for deep sleep wakeup
// So do not use the LED
// const int LED_PIN = 16;
const int RELAY_PIN = 15;
const int DHT_PIN = 0;
const int MOISTURE_PIN = A0;
const int BOOT_NUM_ADDR = 0;
DHT dht(DHT_PIN, DHT11);
WiFiUDP Udp;
Coap coap(Udp);
char buffer[512];
const char VERSION[] = "1.0.0";
uint32_t bootNumber;
void setup()
{
Serial.begin(74880);
Serial.print("Connecting...");
WiFi.begin(ESP_SSID, ESP_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.printf("IP address: %s\n", WiFi.localIP().toString().c_str());
// This port is used for sending. Not sure if this is needed.
Udp.begin(1337);
// rand is used by coap so we need to seed it
srand(RANDOM_REG32);
EEPROM.begin(4);
EEPROM.get(BOOT_NUM_ADDR, bootNumber);
bootNumber++;
EEPROM.put(BOOT_NUM_ADDR, bootNumber);
EEPROM.end();
Serial.printf("Started with version %s and boot number %u\n", VERSION, bootNumber);
}
// the loop function runs over and over again forever
void loop()
{
Serial.println("Running");
// DHT11 - Temp & Humidity
dht.begin();
int readData = dht.read();
float t = dht.readTemperature();
float h = dht.readHumidity();
Serial.printf("Temp: %.1f\nHum: %.1f\n", t, h);
// Moisture
int m = analogRead(MOISTURE_PIN);
Serial.printf("Moisture: %d\n", m);
// Serialize JSON
// Simple CoAP supports buffers of max length 128. That's including headers and options
sprintf(buffer, "{\"ver\":\"%s\",\"bootId\":%u,\"temperature\":%.1f,\"humidity\":%.1f,\"moisture\":%d}", VERSION, bootNumber, t, h, m);
Serial.printf("Buffer length: %d\nBuffer content: %s\n", strlen(buffer), buffer);
IPAddress ip;
ip.fromString(IOT_GATEWAY);
Serial.printf("Sending to IP %s\n", ip.toString().c_str());
Serial.printf("Sending coap message\n");
coap.send(ip, PORT, PATH, COAP_NONCON, COAP_POST, (const uint8_t *)"", 0, (const uint8_t *)buffer, strlen(buffer));
Serial.printf("Coap message sent\n");
// Milliseconds - wait one second
delay(1e3);
delay(60 * 1e3);
Serial.println("Finished delaying. All network operations should be done. Going to deep sleep.");
ESP.deepSleep(5 * 60 * SECOND);
}