/* * This sketch sends a message to a TCP server * */ #include #include #include #include "wifi.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 send_sensor_data() { Serial.print("Connecting to "); Serial.println(SERVER_IP); float temp_celsius = temperatureRead(); Serial.print(temp_celsius); Serial.println(" °C"); // 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(temp_celsius); String start_string = "temperature,host=vgarden_test 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(SSID_NAME, PASSWD); 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); send_sensor_data(); 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); } }