56 lines
1.3 KiB
C++
56 lines
1.3 KiB
C++
#include <Arduino.h>
|
|
#include "WiFi.h"
|
|
#include "src/ESP_influxdb/ESPinfluxdb.h"
|
|
#include "src/EmonLib/EmonLib.h"
|
|
#include "config.h"
|
|
|
|
#define Apin 32
|
|
#define Vpin 33
|
|
|
|
EnergyMonitor emon1; // Create an instance
|
|
|
|
void ConnectToWiFi(){
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.begin(SSID, WiFiPassword);
|
|
Serial.print("Connecting to "); Serial.println(SSID);
|
|
|
|
uint8_t i = 0;
|
|
while (WiFi.status() != WL_CONNECTED)
|
|
{
|
|
Serial.print('.');
|
|
delay(500);
|
|
|
|
if ((++i % 16) == 0)
|
|
{
|
|
Serial.println(F(" still trying to connect"));
|
|
}
|
|
}
|
|
Serial.print(F("Connected. My IP address is: "));
|
|
Serial.println(WiFi.localIP());
|
|
}
|
|
|
|
Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT);
|
|
|
|
void ConnectToInflux(){
|
|
while (influxdb.opendb(DATABASE)!=DB_SUCCESS) {
|
|
Serial.println("Opend database failed");
|
|
delay(10000);
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
ConnectToWiFi();
|
|
ConnectToInflux();
|
|
analogReadResolution(ADC_BITS);
|
|
emon1.voltage(Vpin, 234.26, 1.7); // Voltage: input pin, calibration, phase_shift
|
|
emon1.current(Apin, 111.1); // Current: input pin, calibration.
|
|
}
|
|
|
|
void loop() {
|
|
emon1.calcVI(20,2000); // Calculate all. No.of half wavelengths (crossings), time-out
|
|
emon1.serialprint(); // Print out all variables (realpower, apparent power, Vrms, Irms, power factor)
|
|
|
|
//influx to be implemented...
|
|
}
|