#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <Adafruit_ADS1X15.h>
#include <Wire.h>
//ネットワーク
const char* ssid = "SSID";
const char* password = "password";
// 静的IPアドレス(XXを好きなものに変える。DHCPでもOK)
IPAddress ip(192, 168, 10, XX);
IPAddress gateway(192,168, 10, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress DNS(192, 168, 10, 1);
Adafruit_ADS1115 ads;
const float FACTOR = 30; //30A/1V
const float multiplier = 0.03125F * 1.27;
float ch01_current,ch23_current;
long loop_time = millis();
void connectWifi(){
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.config(ip, gateway, subnet, DNS);
delay(100);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
ESP.wdtFeed();
Serial.println("");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
Serial.println("");
Serial.println("Initializing...");
//I2C初期化
Wire.begin(0,2);
//wifi接続
connectWifi();
//ADS1115の初期化
ads.setGain(GAIN_FOUR);
if (!ads.begin()) {
Serial.println("Failed to initialize ADS.");
ESP.restart();
}
}
void getRMS(){
float voltage,voltage2;
float corriente,corriente2;
float sum = 0,sum2 = 0;
long timepo = millis();
int counter = 0;
while(millis() - timepo < 5000){
//WDT Reset回避
yield();
//ch0-1計測
voltage = ads.readADC_Differential_0_1() * multiplier;
corriente = voltage * FACTOR;
corriente /= 1000.0;
sum += sq(corriente);
//ch2-3計測
voltage2 = ads.readADC_Differential_2_3() * multiplier;
corriente2 = voltage2 * FACTOR;
corriente2 /= 1000.0;
sum2 += sq(corriente2);
counter = counter + 1;
}
corriente = sqrt(sum / counter);
corriente2 = sqrt(sum2 / counter);
ch01_current = corriente;
ch23_current = corriente2;
}
void printMeasure(String prefix, float value, String postfix){
Serial.print(prefix);
Serial.print(value,3);
Serial.println(postfix);
}
// 電力データ送信 php経由 ---------------------------------------------------
bool sendpower(String url) {
if(WiFi.status() != WL_CONNECTED) {
Serial.println("Wifi is disable");
ESP.restart();
while(1){
delay(100);
}
}
WiFiClient client;
HTTPClient http;
http.begin(client,url);
http.setTimeout(5000);
int httpCode = http.GET();
Serial.print("httpCode:");
Serial.println(httpCode);
char res = http.getString().charAt(0);
http.end();
if(httpCode==200){
Serial.println("Send OK");
Serial.println(res);
Serial.println("---end");
return true;
}else{
Serial.println("Send Failed");
return false;
}
}
void loop() {
wdt_enable(WDTO_8S);
getRMS();
float ch01_power = ch01_current * 100;
float ch23_power = ch23_current * 100;
long pass_time = millis() - loop_time;
loop_time = millis();
float ch01_watthour = (ch01_power * pass_time) / 3600000;
float ch23_watthour = (ch23_power * pass_time) / 3600000;
printMeasure("CH01_POWER: ",ch01_power,"W");
printMeasure("CH01_WHour: ",ch01_watthour,"Wh");
printMeasure("CH23_POWER: ",ch23_power,"W");
printMeasure("CH23_WHour: ",ch23_watthour,"Wh");
printMeasure("TIME: ",pass_time,"mSec");
char ch01_power_st[20];
char ch23_power_st[20];
char ch01_watthour_st[20];
char ch23_watthour_st[20];
dtostrf(ch01_power, 0, 6,ch01_power_st);
dtostrf(ch23_power, 0, 6,ch23_power_st);
dtostrf(ch01_watthour, 0, 6,ch01_watthour_st);
dtostrf(ch23_watthour, 0, 6,ch23_watthour_st);
// 192.168.10.21の81ポート set_power.phpへGETリクエスト
char url[250];
sprintf(url, "http://192.168.10.20:81/set_power.php?ch01_power=%s&ch23_power=%s&ch01_watthour=%s&ch23_watthour=%s",ch01_power_st,ch23_power_st,ch01_watthour_st,ch23_watthour_st);
Serial.println(url);
sendpower(url);
wdt_reset();
wdt_disable();
}