Skip to content

Commit 413454d

Browse files
authored
New weather station version with valve control
1 parent 5bb9ed2 commit 413454d

1 file changed

Lines changed: 99 additions & 47 deletions

File tree

Lines changed: 99 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,60 @@
11
#include <LowPower.h>
2+
#include <Servo.h>
23
#include <ArduinoJson.h>
34
#include <Http.h>
45

6+
#define RX_PIN 11
7+
#define TX_PIN 12
8+
#define RST_PIN 10
9+
#define MOISTURE_PIN 3
10+
#define SERVO_PIN 4
11+
512
unsigned long lastRunTime = 0;
613
unsigned long waitForRunTime = 0;
714
unsigned long millisOffset = 0;
8-
unsigned int RX_PIN = 11;
9-
unsigned int TX_PIN = 12;
10-
unsigned int RST_PIN = 10;
11-
HTTP http(9600, RX_PIN, TX_PIN, RST_PIN, true);
15+
const bool DEBUG = false;
16+
const HTTP http(9600, RX_PIN, TX_PIN, RST_PIN, DEBUG);
17+
const Servo servo;
1218

1319
/*
14-
* the setup routine runs once when you press reset:
20+
* functions
1521
*/
16-
void setup() {
17-
Serial.begin(9600);
18-
while(!Serial);
19-
Serial.println("Starting!");
20-
}
2122

22-
/*
23-
* the loop routine runs over and over again forever:
24-
*/
25-
void loop() {
26-
if (shouldTrackTimeEntry()){
27-
http.wakeUp();
28-
trackTimeEntry();
29-
}
30-
else {
31-
http.sleep();
32-
}
23+
void openValve(){
24+
servo.attach(SERVO_PIN);
25+
servo.write(14);
26+
delay(1000);
27+
servo.detach();
3328
}
3429

35-
/*
36-
* functions
37-
*/
30+
void closeValve(){
31+
servo.attach(SERVO_PIN);
32+
servo.write(90);
33+
delay(1000);
34+
servo.detach();
35+
}
36+
3837
void print(const __FlashStringHelper *message, int code = -1){
39-
if (code != -1){
40-
Serial.print(message);
41-
Serial.println(code);
38+
if (DEBUG){
39+
if (code != -1){
40+
Serial.print(message);
41+
Serial.println(code);
42+
}
43+
else {
44+
Serial.println(message);
45+
}
4246
}
43-
else {
44-
Serial.println(message);
47+
}
48+
49+
void print(const char *message, int code = -1){
50+
if (DEBUG){
51+
if (code != -1){
52+
Serial.print(message);
53+
Serial.println(code);
54+
}
55+
else {
56+
Serial.println(message);
57+
}
4558
}
4659
}
4760

@@ -55,46 +68,85 @@ void sleepEightSeconds(){
5568
millisOffset += 8000;
5669
}
5770

58-
bool shouldTrackTimeEntry(){
71+
bool shouldTrackWeatherEntry(){
5972
/*
6073
* This calculation uses the max value the unsigned long can store as key. Remember when a negative number
6174
* is assigned or the maximun is exceeded, then the module is applied to that value.
6275
*/
63-
sleepEightSeconds();
6476
unsigned long elapsedTime = currentMillis() - lastRunTime;
6577
print(F("Elapsed time: "), elapsedTime);
6678
return elapsedTime >= waitForRunTime;
6779
}
6880

69-
void trackTimeEntry(){
81+
unsigned int readMoisture() {
82+
unsigned long total = 0;
83+
for (unsigned int i=0; i<100; ++i){
84+
total += analogRead(MOISTURE_PIN);
85+
}
86+
return total/100;
87+
}
88+
89+
void manageGarden(){
7090

7191
char response[32];
7292
char body[90];
7393
Result result;
7494

75-
print(F("Cofigure bearer: "), http.configureBearer("your.apn"));
95+
print(F("Cofigure bearer: "), http.configureBearer("bearer"));
7696
result = http.connect();
7797
print(F("HTTP connect: "), result);
7898

79-
unsigned int moisture = random(1023);
80-
char voltage[6];
81-
http.readVoltage(voltage);
99+
// char voltage[6];
100+
// http.readVoltage(voltage);
82101

83-
sprintf(body, "[{\"weatherEntry\":[{\"m\": %d, \"cv\": %s}], \"n\": \"Arduino\"}]", moisture, voltage);
84-
Serial.println(body);
102+
sprintf(body, "{\"w\":{\"m\": %d}}", readMoisture());
103+
print(body);
85104

86-
result = http.post("your.api", body, response);
105+
result = http.post("your.endpoint", body, response);
87106
print(F("HTTP POST: "), result);
107+
print(F("HTTP disconnect: "), http.disconnect());
108+
88109
if (result == SUCCESS) {
89-
Serial.println(response);
110+
print(response);
90111
StaticJsonBuffer<32> jsonBuffer;
91112
JsonObject& root = jsonBuffer.parseObject(response);
92-
lastRunTime = currentMillis();
93-
waitForRunTime = root["waitForRunTime"];
94113

95-
print(F("Last run time: "), lastRunTime);
96-
print(F("Next post in: "), waitForRunTime);
114+
if (strcmp(root["action"], "open-valve") == 0){
115+
print(F("Opening valve for: "), root["value"]);
116+
openValve();
117+
delay(root["value"]);
118+
closeValve();
119+
}
120+
else {
121+
lastRunTime = currentMillis();
122+
print(F("Wait for run time: "), root["value"]);
123+
waitForRunTime = root["value"];
124+
}
97125
}
98-
99-
print(F("HTTP disconnect: "), http.disconnect());
100-
}
126+
}
127+
128+
/*
129+
* the setup routine runs once when you press reset:
130+
*/
131+
void setup() {
132+
pinMode(MOISTURE_PIN, INPUT);
133+
Serial.begin(9600);
134+
while(!Serial);
135+
print("Starting!");
136+
openValve();
137+
closeValve();
138+
}
139+
140+
/*
141+
* the loop routine runs over and over again forever:
142+
*/
143+
void loop() {
144+
if (shouldTrackWeatherEntry()){
145+
http.wakeUp();
146+
manageGarden();
147+
}
148+
else {
149+
http.sleep();
150+
sleepEightSeconds();
151+
}
152+
}

0 commit comments

Comments
 (0)