|
|
@@ -1,19 +1,18 @@
|
|
|
+#include <Wire.h>
|
|
|
+#include "DFRobot_INA219.h"
|
|
|
#include <ESP8266WiFi.h>
|
|
|
#include <ArduinoMqttClient.h>
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
|
#include "secrets.h"
|
|
|
|
|
|
-
|
|
|
+DFRobot_INA219_IIC ina219(&Wire, INA219_I2C_ADDRESS1);
|
|
|
WiFiClient wifiClient;
|
|
|
MqttClient mqttClient(wifiClient);
|
|
|
|
|
|
+float ina219Reading_mA = 1000;
|
|
|
+float extMeterReading_mA = 1000;
|
|
|
|
|
|
-//pin 0 red led
|
|
|
-//pin 2 blue led
|
|
|
-//#include <secrets>;
|
|
|
-
|
|
|
-// constants won't change:
|
|
|
const long interval = 10000; // interval at which to read voltage and report
|
|
|
unsigned long previousMillis = 0; // will store last time LED was updated
|
|
|
|
|
|
@@ -25,16 +24,14 @@ int sensorValue = 0;
|
|
|
char json[200];
|
|
|
int myMessage;
|
|
|
int myPin;
|
|
|
+
|
|
|
void setup()
|
|
|
{
|
|
|
Serial.begin(115200);
|
|
|
Serial.println();
|
|
|
|
|
|
- pinMode(0, OUTPUT);
|
|
|
- digitalWrite(0, HIGH);
|
|
|
-
|
|
|
- pinMode(4, OUTPUT);
|
|
|
- digitalWrite(4, LOW);
|
|
|
+ pinMode(15, OUTPUT);
|
|
|
+ digitalWrite(15, LOW);
|
|
|
pinMode(12, OUTPUT);
|
|
|
digitalWrite(12, LOW);
|
|
|
pinMode(14, OUTPUT);
|
|
|
@@ -66,13 +63,20 @@ void setup()
|
|
|
|
|
|
|
|
|
|
|
|
- // subscribe to a topic
|
|
|
- mqttClient.subscribe("test1");
|
|
|
- mqttClient.subscribe("test2");
|
|
|
- mqttClient.subscribe("test3");
|
|
|
-
|
|
|
+ // subscribe to a topic
|
|
|
+ mqttClient.subscribe("Generator1");
|
|
|
+ mqttClient.subscribe("Generator2");
|
|
|
+ mqttClient.subscribe("Generator3");
|
|
|
|
|
|
|
|
|
+ //Initialize the sensor
|
|
|
+ while(ina219.begin() != true) {
|
|
|
+ Serial.println("INA219 begin faild");
|
|
|
+ delay(2000);
|
|
|
+ }
|
|
|
+ //Linear calibration
|
|
|
+ ina219.linearCalibrate(/*The measured current before calibration*/ina219Reading_mA, /*The current measured by other current testers*/extMeterReading_mA);
|
|
|
+ Serial.println();
|
|
|
|
|
|
}
|
|
|
|
|
|
@@ -87,13 +91,13 @@ void loop() {
|
|
|
Serial.println(" bytes:");
|
|
|
|
|
|
myPin=0;
|
|
|
- if(mqttClient.messageTopic()=="test1"){
|
|
|
- myPin=4;
|
|
|
+ if(mqttClient.messageTopic()=="Generator1"){
|
|
|
+ myPin=15;
|
|
|
}
|
|
|
- if(mqttClient.messageTopic()=="test2"){
|
|
|
+ if(mqttClient.messageTopic()=="Generator2"){
|
|
|
myPin=12;
|
|
|
}
|
|
|
- if(mqttClient.messageTopic()=="test3"){
|
|
|
+ if(mqttClient.messageTopic()=="Generator3"){
|
|
|
myPin=14;
|
|
|
}
|
|
|
|
|
|
@@ -124,13 +128,14 @@ void loop() {
|
|
|
unsigned long currentMillis = millis();
|
|
|
if (currentMillis - previousMillis >= interval) {
|
|
|
previousMillis = currentMillis;
|
|
|
- sensorValue = analogRead(analogInPin);
|
|
|
- Serial.print("sensor = ");
|
|
|
- Serial.print(sensorValue);
|
|
|
- Serial.println("");
|
|
|
+
|
|
|
+ Serial.print("BusVoltage: ");
|
|
|
+ Serial.print(ina219.getBusVoltage_V(), 2);
|
|
|
+ Serial.println("V");
|
|
|
|
|
|
mqttClient.beginMessage("houseVoltage");
|
|
|
- mqttClient.print(sensorValue);
|
|
|
+ //mqttClient.print(sensorValue);
|
|
|
+ mqttClient.print((ina219.getBusVoltage_V()/.493), 2);
|
|
|
mqttClient.endMessage();
|
|
|
}
|
|
|
|