242 lines
5.8 KiB
C++
242 lines
5.8 KiB
C++
/*
|
|
* This sketch sends a message to a TCP server
|
|
*
|
|
*/
|
|
|
|
#include <WiFi.h>
|
|
#include <WiFiMulti.h>
|
|
#include <HTTPClient.h>
|
|
|
|
#include "ZZ.h"
|
|
#include "VSC.h" // Victron Solar Controller
|
|
|
|
#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;
|
|
|
|
int delay_ms = 500;
|
|
|
|
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());
|
|
|
|
// 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(String sensor_name, float sensor_value) {
|
|
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(sensor_value);
|
|
String start_string = sensor_name + ",host=vgarden_test value=";
|
|
String post_string = String(start_string + count_string);
|
|
Serial.println(post_string);
|
|
int httpCode = http.POST(post_string.c_str());
|
|
|
|
// 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 get_victron_data(){
|
|
float battV = 0;
|
|
float battA = 0;
|
|
float kWh = 0;
|
|
float PV_W = 0;
|
|
float loadA = 0;
|
|
|
|
pBLEScan->start(scan_secs, false);
|
|
delay(delay_ms);
|
|
|
|
decryptAesCtr(VERBOSE);
|
|
|
|
battV = parseBattVolts(); // -327.68 -> 327.66 V
|
|
Serial.print("battV: "); Serial.println(battV);
|
|
if (battV > 0.0 && battV < 20.0) {
|
|
send_sensor_data("battery_v", battV);
|
|
}
|
|
|
|
battA = parseBattAmps(); // -3276.8 -> 3276.6 A
|
|
Serial.print("battA: "); Serial.println(battA);
|
|
if (battA > -2000.0 && battA < 2000.0) {
|
|
send_sensor_data("battery_a", battA);
|
|
}
|
|
|
|
kWh = parseKWHtoday(); // 0 -> 655.34 kWh
|
|
Serial.print("kWh: "); Serial.println(kWh);
|
|
if (kWh < 10.0) {
|
|
send_sensor_data("yield_today", kWh);
|
|
}
|
|
|
|
PV_W = parsePVpower(); // 0 -> 65534 W
|
|
Serial.print("PV: "); Serial.println(PV_W);
|
|
if (PV_W < 100.0) {
|
|
send_sensor_data("panel_p", PV_W);
|
|
}
|
|
|
|
loadA = parseLoadAmps(); // 0 -> 51.0 A
|
|
Serial.print("loadA: "); Serial.println(loadA);
|
|
if (loadA < 25.0) {
|
|
send_sensor_data("load_p", loadA);
|
|
}
|
|
}
|
|
|
|
void get_temp_data(){
|
|
float temp_celsius = temperatureRead();
|
|
Serial.print(temp_celsius);
|
|
Serial.println(" °C");
|
|
|
|
send_sensor_data("temperature", temp_celsius);
|
|
}
|
|
|
|
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);
|
|
|
|
Serial.println("* init BLE ...\n");
|
|
BLEDevice::init("");
|
|
Serial.println("* setup scan ...\n");
|
|
pBLEScan->setAdvertisedDeviceCallbacks(new AdDataCallback());
|
|
pBLEScan->setActiveScan(true); // uses more power, but get results faster
|
|
}
|
|
|
|
void loop() {
|
|
|
|
if (count < 1){
|
|
Serial.println("Pump On");
|
|
digitalWrite(led,HIGH);
|
|
digitalWrite(pump_con,HIGH);
|
|
update_server(pump_on);
|
|
}
|
|
else {
|
|
Serial.println("Pump Off");
|
|
digitalWrite(led,LOW);
|
|
digitalWrite(pump_con,LOW);
|
|
update_server(pump_off);
|
|
}
|
|
|
|
get_temp_data();
|
|
|
|
get_victron_data();
|
|
|
|
if (count >= 9){
|
|
count = 0;
|
|
}
|
|
else {
|
|
count++;
|
|
}
|
|
|
|
delay(60000); // Sleep 1 minute
|
|
|
|
}
|