206 lines
5.5 KiB
C++
206 lines
5.5 KiB
C++
#include "ESP8266WiFi.h"
|
|
#include <PubSubClient.h>
|
|
#include "RemoteDebug.h"
|
|
#include <ArduinoOTA.h>
|
|
#include <WiFiUdp.h>
|
|
#include "myconfig.h"
|
|
WiFiClient espClient;
|
|
PubSubClient client(espClient);
|
|
#define HOST_NAME "remotedebug"
|
|
RemoteDebug Debug;
|
|
|
|
|
|
long lastMsg = 0;
|
|
char msg[50];
|
|
int value = 0;
|
|
|
|
const int sensor=A0;
|
|
const int referenceval=190;
|
|
int maxValue;
|
|
int minValue=255;
|
|
unsigned long count;
|
|
unsigned long resetcount;
|
|
const int diff=50;
|
|
int high=0;
|
|
|
|
void setup_wifi() {
|
|
// Connect to WiFi
|
|
WiFi.setSleepMode(WIFI_NONE_SLEEP);
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.begin(ssid, password);
|
|
while (WiFi.status() != WL_CONNECTED)
|
|
{
|
|
delay(500);
|
|
Serial.print("*");
|
|
}
|
|
|
|
Serial.println("");
|
|
Serial.println("WiFi connection Successful");
|
|
Serial.print("The IP Address of ESP8266 Module is: ");
|
|
Serial.print(WiFi.localIP());// Print the IP address
|
|
}
|
|
|
|
void setup(void)
|
|
{
|
|
pinMode(BUILTIN_LED, OUTPUT);
|
|
Serial.begin(115200);
|
|
setup_wifi();
|
|
client.setServer(mqttbroker,mqttport);
|
|
client.setCallback(callback);
|
|
|
|
// Port defaults to 8266
|
|
// ArduinoOTA.setPort(8266);
|
|
|
|
// Hostname defaults to esp8266-[ChipID]
|
|
// ArduinoOTA.setHostname("myesp8266");
|
|
|
|
// No authentication by default
|
|
// ArduinoOTA.setPassword("admin");
|
|
|
|
// Password can be set with it's md5 value as well
|
|
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
|
|
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
|
|
|
|
|
|
ArduinoOTA.onStart([]() {
|
|
String type;
|
|
if (ArduinoOTA.getCommand() == U_FLASH)
|
|
type = "sketch";
|
|
else // U_SPIFFS
|
|
type = "filesystem";
|
|
|
|
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
|
|
Serial.println("Start updating " + type);
|
|
});
|
|
ArduinoOTA.onEnd([]() {
|
|
Serial.println("\nEnd");
|
|
});
|
|
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
|
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
|
|
});
|
|
ArduinoOTA.onError([](ota_error_t error) {
|
|
Serial.printf("Error[%u]: ", error);
|
|
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
|
|
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
|
|
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
|
|
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
|
|
else if (error == OTA_END_ERROR) Serial.println("End Failed");
|
|
});
|
|
ArduinoOTA.begin();
|
|
Serial.println("Ready");
|
|
Serial.print("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void callback(char* topic, byte* payload, unsigned int length) {
|
|
Serial.print("Message arrived [");
|
|
Serial.print(topic);
|
|
Serial.print("] ");
|
|
for (int i = 0; i < length; i++) {
|
|
Serial.print((char)payload[i]);
|
|
}
|
|
Serial.println();
|
|
|
|
// Switch on the LED if an 1 was received as first character
|
|
if ((char)payload[0] == '1') {
|
|
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
|
|
// but actually the LED is on; this is because
|
|
// it is active low on the ESP-01)
|
|
} else {
|
|
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
|
|
}
|
|
|
|
}
|
|
|
|
void reconnect() {
|
|
// Loop until we're reconnected
|
|
while (!client.connected()) {
|
|
Serial.print("Attempting MQTT connection...");
|
|
// Create a random client ID
|
|
String clientId = "ESP8266Client-power";
|
|
//clientId += String(random(0xffff), HEX);
|
|
// Attempt to connect
|
|
if (client.connect(clientId.c_str(), "mqtt", "3dgRMrAXzj2AWVH2")) {
|
|
Serial.println("connected");
|
|
// Once connected, publish an announcement...
|
|
//client.publish("outTopic", "hello world");
|
|
// ... and resubscribe
|
|
//client.subscribe("inTopic");
|
|
} else {
|
|
Serial.print("failed, rc=");
|
|
Serial.print(client.state());
|
|
Serial.println(" try again in 5 seconds");
|
|
// Wait 5 seconds before retrying
|
|
delay(1000);
|
|
}
|
|
}
|
|
}
|
|
|
|
void getsensorvalue(int sensorvalue) {
|
|
//int sensorvalue=analogRead(sensor);
|
|
|
|
//Serial.println(analogRead(sensor));
|
|
if ( sensorvalue > maxValue ) {
|
|
maxValue=sensorvalue;
|
|
}
|
|
if ( sensorvalue < minValue ) {
|
|
minValue=sensorvalue;
|
|
}
|
|
|
|
if ( sensorvalue >= referenceval && high == 0) {
|
|
count++;
|
|
resetcount++;
|
|
Serial.println("HIGH - Min: "+ String(minValue) +", current: "+ String(sensorvalue) + ",Max: "+ String(maxValue) + ", Counter: " + count );
|
|
high=1;
|
|
}
|
|
else if (sensorvalue <= referenceval - diff && high == 1) {
|
|
Serial.println("LOW - Min: "+ String(minValue) +", current: "+ String(sensorvalue) + ",Max: "+ String(maxValue) + ", Counter: " + count );
|
|
high=0;
|
|
}
|
|
}
|
|
|
|
|
|
void loop()
|
|
{
|
|
ArduinoOTA.handle();
|
|
|
|
|
|
|
|
//long now = millis();
|
|
//if (now - lastMsg > 4000) {
|
|
// lastMsg = now;
|
|
// ++value;
|
|
// snprintf (msg, 50, "hello world #%ld", value);
|
|
// Serial.print("Publish message: ");
|
|
// Serial.println(msg);
|
|
|
|
//}
|
|
|
|
getsensorvalue(analogRead(sensor));
|
|
//Serial.println("LOW - Min: "+ String(minValue) +", current: "+ String(sensorvalue) + ",Max: "+ String(maxValue) + ", Counter: " + count );
|
|
long now = millis();
|
|
if (now - lastMsg > 60000 ) {
|
|
if (WiFi.status() != WL_CONNECTED){
|
|
WiFi.reconnect();
|
|
}
|
|
if (!client.connected()) {
|
|
reconnect();
|
|
}
|
|
//float kwh=0;
|
|
//kwh=count/100;
|
|
lastMsg = now;
|
|
snprintf (msg, 50, "{\"pulse\": %ld, \"kwh\": %.2f }", count,float(resetcount)/float(100));
|
|
resetcount=0;
|
|
client.publish("hass/sensor/sensor_powermeter/state", msg);
|
|
|
|
//delay(20); //wait for data to be published.
|
|
}
|
|
|
|
client.loop();
|
|
// EMPTY
|
|
}
|