first commit
This commit is contained in:
127
vgarden1.ino
Normal file
127
vgarden1.ino
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
/*
|
||||||
|
* This sketch sends a message to a TCP server
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <WiFiMulti.h>
|
||||||
|
#include <HTTPClient.h>
|
||||||
|
|
||||||
|
WiFiMulti WiFiMulti;
|
||||||
|
|
||||||
|
WiFiClient client;
|
||||||
|
|
||||||
|
HTTPClient http;
|
||||||
|
|
||||||
|
#define SERVER_IP "192.168.1.221:8086"
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
int pump_on = 1;
|
||||||
|
int pump_off = 0;
|
||||||
|
|
||||||
|
int led = 15;
|
||||||
|
int pump_con = 6;
|
||||||
|
|
||||||
|
void update_server(int count) {
|
||||||
|
Serial.print("Connecting to ");
|
||||||
|
Serial.println(SERVER_IP);
|
||||||
|
|
||||||
|
// wait for WiFi connection
|
||||||
|
if ((WiFi.status() == WL_CONNECTED)) {
|
||||||
|
|
||||||
|
Serial.print("[HTTP] begin...\n");
|
||||||
|
// configure traged server and url
|
||||||
|
http.begin(client, "http://" SERVER_IP "/write?db=garagedb"); //HTTP
|
||||||
|
http.setTimeout(5000);
|
||||||
|
http.addHeader("Content-Type", "application/json");
|
||||||
|
|
||||||
|
//Serial.print("[HTTP] POST...\n");
|
||||||
|
// start connection and send HTTP header and body
|
||||||
|
String count_string = String(count);
|
||||||
|
String start_string = "vstate,host=vgarden value=";
|
||||||
|
String post_string = String(start_string + count_string);
|
||||||
|
Serial.println(post_string);
|
||||||
|
int httpCode = http.POST(post_string.c_str());
|
||||||
|
count++;
|
||||||
|
|
||||||
|
// httpCode will be negative on error
|
||||||
|
if (httpCode > 0) {
|
||||||
|
// HTTP header has been send and Server response header has been handled
|
||||||
|
Serial.printf("[HTTP] POST... code: %d\n", httpCode);
|
||||||
|
|
||||||
|
// file found at server
|
||||||
|
if (httpCode == HTTP_CODE_OK) {
|
||||||
|
const String& payload = http.getString();
|
||||||
|
Serial.println("received payload:\n<<");
|
||||||
|
Serial.println(payload);
|
||||||
|
Serial.println(">>");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
http.end();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println("Reconnecting to WiFi...");
|
||||||
|
WiFi.disconnect();
|
||||||
|
WiFi.reconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
|
||||||
|
pinMode(led, OUTPUT);
|
||||||
|
pinMode(pump_con, OUTPUT);
|
||||||
|
|
||||||
|
digitalWrite(led,LOW);
|
||||||
|
digitalWrite(pump_con,LOW);
|
||||||
|
|
||||||
|
Serial.begin(115200);
|
||||||
|
delay(10);
|
||||||
|
|
||||||
|
// We start by connecting to a WiFi network
|
||||||
|
WiFiMulti.addAP("CHANGEME", "CHANGEME");
|
||||||
|
|
||||||
|
Serial.println();
|
||||||
|
Serial.println();
|
||||||
|
Serial.print("Waiting for WiFi... ");
|
||||||
|
|
||||||
|
while (WiFiMulti.run() != WL_CONNECTED) {
|
||||||
|
Serial.print(".");
|
||||||
|
delay(500);
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("");
|
||||||
|
Serial.println("WiFi connected");
|
||||||
|
Serial.println("IP address: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
delay(500);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
|
||||||
|
|
||||||
|
while(count < 5){
|
||||||
|
|
||||||
|
digitalWrite(led,HIGH);
|
||||||
|
digitalWrite(pump_con,HIGH);
|
||||||
|
update_server(pump_on);
|
||||||
|
Serial.println("Pump On, Sleeping 60s");
|
||||||
|
count++;
|
||||||
|
delay(60000);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count >= 5) {
|
||||||
|
Serial.println("Pump Off, Sleeping 15mins");
|
||||||
|
|
||||||
|
digitalWrite(led,LOW);
|
||||||
|
digitalWrite(pump_con,LOW);
|
||||||
|
|
||||||
|
update_server(pump_off);
|
||||||
|
count = 0;
|
||||||
|
delay(900000);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user