Added fade off lamp
This commit is contained in:
@@ -13,13 +13,13 @@ platform = espressif8266
|
||||
;board = nodemcuv2
|
||||
board = esp07
|
||||
framework = arduino
|
||||
;board_build.ldscript = eagle.flash.1m.ld
|
||||
board_build.ldscript = eagle.flash.1m.ld
|
||||
upload_protocol = espota
|
||||
upload_port = 192.168.1.131
|
||||
;upload_port = 192.168.1.95
|
||||
monitor_speed = 115200
|
||||
lib_deps =
|
||||
sstaub/Ticker @ ^4.3.0
|
||||
knolleary/PubSubClient @ ^2.8
|
||||
ottowinter/ESPAsyncWebServer-esphome @ ^3.0.0
|
||||
ayushsharma82/WebSerial @ ^1.3.0
|
||||
robtillaart/RunningMedian @ ^0.3.6
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <PubSubClient.h>
|
||||
#include <SPI.h>
|
||||
|
||||
#include "RunningMedian.h"
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESPAsyncTCP.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
@@ -13,8 +15,8 @@
|
||||
#define LED_WRK (16) //Green
|
||||
#define LED_MQ (5) //Yellow
|
||||
|
||||
#define HOST_NAME "ElectroT"
|
||||
#define MAIN_TOPIC "/home/kort/"
|
||||
#define HOST_NAME "Electro"
|
||||
#define MAIN_TOPIC "home/kor/"
|
||||
|
||||
/* Hardware:
|
||||
MCP3201 Pin ---------------- ESP8266 Pin
|
||||
@@ -35,13 +37,14 @@ const char* mqtt_server = "192.168.1.111";
|
||||
const int scePinI = 15, scePinU = 4; // SCE - Chip select
|
||||
|
||||
const double vRef = 3.3;
|
||||
const int period = 125; //us
|
||||
const int period = 150; //us
|
||||
|
||||
unsigned long cRun = millis();
|
||||
double curr = 0.0;
|
||||
double volt = 0.0;
|
||||
double currT = 0.0;
|
||||
double currM = 0.0;
|
||||
float Pavg = 0.0f;
|
||||
int nSec, nSampl;
|
||||
unsigned long sumReading = 0;
|
||||
unsigned long sumReading2 = 0;
|
||||
@@ -66,6 +69,8 @@ Ticker tckrElcnt(mcp_output, period, 0, MICROS_MICROS);
|
||||
|
||||
AsyncWebServer server(80);
|
||||
|
||||
RunningMedian power5s = RunningMedian(5);
|
||||
|
||||
void connectToWifi() {
|
||||
Serial.println("Connecting to Wi-Fi...");
|
||||
WiFi.begin(ssid, password);
|
||||
@@ -88,6 +93,7 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||
digitalWrite(LED_WF, LOW);
|
||||
}
|
||||
|
||||
|
||||
void setup(){
|
||||
Serial.begin(9600);
|
||||
WiFi.mode(WIFI_STA);
|
||||
@@ -114,6 +120,8 @@ void setup(){
|
||||
nSampl = 0;
|
||||
spiBegin();
|
||||
tckrElcnt.start();
|
||||
WebSerial.begin(&server);
|
||||
server.begin();
|
||||
}
|
||||
|
||||
void loop(){
|
||||
@@ -127,11 +135,20 @@ void loop(){
|
||||
}
|
||||
mClient.loop();
|
||||
if(cRun + 999 < millis()){
|
||||
cRun = millis();
|
||||
digitalWrite(LED_WRK, LOW);
|
||||
nSampl++;
|
||||
currM += curr;
|
||||
I = sqrt(sumReading / double(nSampl)) / 39.7;
|
||||
V = sqrt(sumReading2 / double(nSampl))/ 1.78;
|
||||
V = sqrt(sumReading2 / double(nSampl))/ 1.75;
|
||||
power5s.add(V*I);
|
||||
WebSerial.println("Millis = " + String(cRun) + ", I = " + String(I) + ", V = " + String(V) + ", Samples = " + String(nSampl));
|
||||
char v[7];
|
||||
dtostrf(I, 7, 2, v);
|
||||
mClient.publish(MAIN_TOPIC"I", v);
|
||||
dtostrf(V, 7, 2, v);
|
||||
mClient.publish(MAIN_TOPIC"V", v);
|
||||
dtostrf(power5s.getAverage(), 7, 2, v);
|
||||
mClient.publish(MAIN_TOPIC"P", v);
|
||||
// Serial.printf("Millis = %lu, I = %f, V = %f, Samples = %d\n",
|
||||
// cRun, I, V, nSampl);
|
||||
// if(WiFi.isConnected()){
|
||||
@@ -147,17 +164,24 @@ void loop(){
|
||||
|
||||
if(++nSec > 59){
|
||||
nSec = 0;
|
||||
char v[7];
|
||||
// char v[7];
|
||||
// Serial.printf("Publish min, curr=%.2f, volt=%.2f\n", currA / 60.0f, voltA / 60.0f);
|
||||
// sprintf(v, "%.2f", curr);
|
||||
//mClient.publish(MAIN_TOPIC "curr", v);
|
||||
//mqttClient.publish(MAIN_TOPIC "curr", 1, false, v);
|
||||
dtostrf(currA/60.0f, 7, 2, v);
|
||||
mClient.publish(MAIN_TOPIC"I1min", v);
|
||||
dtostrf(voltA/60.0f, 7, 2, v);
|
||||
mClient.publish(MAIN_TOPIC"V1min", v);
|
||||
dtostrf((currA*voltA)/3600.0f, 7, 2, v);
|
||||
mClient.publish(MAIN_TOPIC"P1min", v);
|
||||
currA = voltA = 0.0f;
|
||||
}
|
||||
digitalWrite(LED_WRK, HIGH);
|
||||
cRun = millis();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void mcp_output(){
|
||||
uint16_t reading;
|
||||
int adcI, adcU;
|
||||
@@ -189,7 +213,6 @@ void mcp_output(){
|
||||
nSampl++;
|
||||
}
|
||||
|
||||
|
||||
void spiBegin(void)
|
||||
{
|
||||
pinMode(scePinI, OUTPUT);
|
||||
|
||||
Reference in New Issue
Block a user