From 3f1aade9568ba498b7acf219d6105b04c3a96b1f Mon Sep 17 00:00:00 2001 From: smallsolar Date: Mon, 26 May 2025 22:50:57 +0100 Subject: [PATCH] first commit --- README.md | 0 vgarden1.ino | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 README.md create mode 100644 vgarden1.ino diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/vgarden1.ino b/vgarden1.ino new file mode 100644 index 0000000..4c56531 --- /dev/null +++ b/vgarden1.ino @@ -0,0 +1,127 @@ +/* + * This sketch sends a message to a TCP server + * + */ + +#include +#include +#include + +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); + + } +}