Added fade off lamp
This commit is contained in:
@@ -175,6 +175,7 @@ void setup() {
|
|||||||
request->send(200, "text/plain", "Hi! I am ESP32.");
|
request->send(200, "text/plain", "Hi! I am ESP32.");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
AsyncElegantOTA.setID("BigRoom");
|
||||||
AsyncElegantOTA.begin(&server); // Start ElegantOTA
|
AsyncElegantOTA.begin(&server); // Start ElegantOTA
|
||||||
WebSerial.begin(&server);
|
WebSerial.begin(&server);
|
||||||
|
|
||||||
@@ -185,6 +186,8 @@ void loop() {
|
|||||||
static uint32_t cRun = millis();
|
static uint32_t cRun = millis();
|
||||||
static uint8_t sec = 0;
|
static uint8_t sec = 0;
|
||||||
static uint16_t prevLLevel = 0;
|
static uint16_t prevLLevel = 0;
|
||||||
|
static uint32_t levBlue = 0, levWhite = 0;
|
||||||
|
static unsigned long levRunB = 0, levRunW = 0;
|
||||||
char s[7];
|
char s[7];
|
||||||
|
|
||||||
//ArduinoOTA.handle();
|
//ArduinoOTA.handle();
|
||||||
@@ -202,31 +205,42 @@ void loop() {
|
|||||||
samples.add(LightLev);
|
samples.add(LightLev);
|
||||||
LightLev = samples.getMedian();
|
LightLev = samples.getMedian();
|
||||||
if ((LightLev < spLight) && move && !bLamp){
|
if ((LightLev < spLight) && move && !bLamp){
|
||||||
ledcWrite(PWM_BLUE,(levelBlue) * 2.55);
|
levBlue = levelBlue * 2.55;
|
||||||
|
ledcWrite(PWM_BLUE, levBlue);
|
||||||
statLamp |= 0x01;
|
statLamp |= 0x01;
|
||||||
bLamp = true;
|
bLamp = true;
|
||||||
Serial.println(F("BLamp On"));
|
Serial.println(F("BLamp On"));
|
||||||
WebSerial.println(F("Blue Lamp On"));
|
WebSerial.println(F("Blue Lamp On"));
|
||||||
}
|
}
|
||||||
else if((LightLev > (spLight + dbLight)) || (move == 0)){
|
else if((LightLev > (spLight + dbLight)) || (move == 0)){
|
||||||
ledcWrite(PWM_BLUE, 0);
|
if((levRunB + 50 <= millis() && levBlue > 0)){
|
||||||
|
levRunB = millis();
|
||||||
|
levBlue--;
|
||||||
|
}
|
||||||
|
ledcWrite(PWM_BLUE, levBlue);
|
||||||
statLamp &= 0xFE;
|
statLamp &= 0xFE;
|
||||||
bLamp = false;
|
bLamp = false;
|
||||||
}
|
}
|
||||||
if(lightWhite && !wLamp){
|
if(lightWhite && !wLamp){
|
||||||
ledcWrite(PWM_WHITE, (levelWhite) * 2.55);
|
levWhite = levelWhite * 2.55;
|
||||||
|
ledcWrite(PWM_WHITE, levWhite);
|
||||||
statLamp |= 0x02;
|
statLamp |= 0x02;
|
||||||
wLamp = true;
|
wLamp = true;
|
||||||
Serial.println(F("WLamp On"));
|
Serial.println(F("WLamp On"));
|
||||||
WebSerial.println(F("White Lamp On"));
|
WebSerial.println(F("White Lamp On"));
|
||||||
}
|
}
|
||||||
else if (!lightWhite){
|
else if (!lightWhite){
|
||||||
ledcWrite(PWM_WHITE, 0);
|
if((levRunW + 30 <= millis() && levWhite > 0)){
|
||||||
|
//WebSerial.println(levWhite);
|
||||||
|
levRunW = millis();
|
||||||
|
levWhite--;
|
||||||
|
}
|
||||||
|
ledcWrite(PWM_WHITE, levWhite);
|
||||||
statLamp &= 0xFD;
|
statLamp &= 0xFD;
|
||||||
wLamp = false;
|
wLamp = false;
|
||||||
}
|
}
|
||||||
if(statLamp > 0) digitalWrite(POWER, HIGH);
|
if(statLamp > 0) digitalWrite(POWER, HIGH);
|
||||||
else digitalWrite(POWER, LOW);
|
else if((levBlue > 0) && (levWhite > 0)) digitalWrite(POWER, LOW);
|
||||||
if((cRun + 1000) <= millis()){ //Once per second
|
if((cRun + 1000) <= millis()){ //Once per second
|
||||||
cRun = millis();
|
cRun = millis();
|
||||||
if(abs(LightLev - prevLLevel) > 100){
|
if(abs(LightLev - prevLLevel) > 100){
|
||||||
|
|||||||
@@ -13,13 +13,13 @@ platform = espressif8266
|
|||||||
;board = nodemcuv2
|
;board = nodemcuv2
|
||||||
board = esp07
|
board = esp07
|
||||||
framework = arduino
|
framework = arduino
|
||||||
;board_build.ldscript = eagle.flash.1m.ld
|
board_build.ldscript = eagle.flash.1m.ld
|
||||||
upload_protocol = espota
|
upload_protocol = espota
|
||||||
upload_port = 192.168.1.131
|
upload_port = 192.168.1.131
|
||||||
;upload_port = 192.168.1.95
|
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
lib_deps =
|
lib_deps =
|
||||||
sstaub/Ticker @ ^4.3.0
|
sstaub/Ticker @ ^4.3.0
|
||||||
knolleary/PubSubClient @ ^2.8
|
knolleary/PubSubClient @ ^2.8
|
||||||
ottowinter/ESPAsyncWebServer-esphome @ ^3.0.0
|
ottowinter/ESPAsyncWebServer-esphome @ ^3.0.0
|
||||||
ayushsharma82/WebSerial @ ^1.3.0
|
ayushsharma82/WebSerial @ ^1.3.0
|
||||||
|
robtillaart/RunningMedian @ ^0.3.6
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
|
|
||||||
|
#include "RunningMedian.h"
|
||||||
|
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESPAsyncTCP.h>
|
#include <ESPAsyncTCP.h>
|
||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
@@ -13,8 +15,8 @@
|
|||||||
#define LED_WRK (16) //Green
|
#define LED_WRK (16) //Green
|
||||||
#define LED_MQ (5) //Yellow
|
#define LED_MQ (5) //Yellow
|
||||||
|
|
||||||
#define HOST_NAME "ElectroT"
|
#define HOST_NAME "Electro"
|
||||||
#define MAIN_TOPIC "/home/kort/"
|
#define MAIN_TOPIC "home/kor/"
|
||||||
|
|
||||||
/* Hardware:
|
/* Hardware:
|
||||||
MCP3201 Pin ---------------- ESP8266 Pin
|
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 int scePinI = 15, scePinU = 4; // SCE - Chip select
|
||||||
|
|
||||||
const double vRef = 3.3;
|
const double vRef = 3.3;
|
||||||
const int period = 125; //us
|
const int period = 150; //us
|
||||||
|
|
||||||
unsigned long cRun = millis();
|
unsigned long cRun = millis();
|
||||||
double curr = 0.0;
|
double curr = 0.0;
|
||||||
double volt = 0.0;
|
double volt = 0.0;
|
||||||
double currT = 0.0;
|
double currT = 0.0;
|
||||||
double currM = 0.0;
|
double currM = 0.0;
|
||||||
|
float Pavg = 0.0f;
|
||||||
int nSec, nSampl;
|
int nSec, nSampl;
|
||||||
unsigned long sumReading = 0;
|
unsigned long sumReading = 0;
|
||||||
unsigned long sumReading2 = 0;
|
unsigned long sumReading2 = 0;
|
||||||
@@ -66,6 +69,8 @@ Ticker tckrElcnt(mcp_output, period, 0, MICROS_MICROS);
|
|||||||
|
|
||||||
AsyncWebServer server(80);
|
AsyncWebServer server(80);
|
||||||
|
|
||||||
|
RunningMedian power5s = RunningMedian(5);
|
||||||
|
|
||||||
void connectToWifi() {
|
void connectToWifi() {
|
||||||
Serial.println("Connecting to Wi-Fi...");
|
Serial.println("Connecting to Wi-Fi...");
|
||||||
WiFi.begin(ssid, password);
|
WiFi.begin(ssid, password);
|
||||||
@@ -88,6 +93,7 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
|
|||||||
digitalWrite(LED_WF, LOW);
|
digitalWrite(LED_WF, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void setup(){
|
void setup(){
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
@@ -114,6 +120,8 @@ void setup(){
|
|||||||
nSampl = 0;
|
nSampl = 0;
|
||||||
spiBegin();
|
spiBegin();
|
||||||
tckrElcnt.start();
|
tckrElcnt.start();
|
||||||
|
WebSerial.begin(&server);
|
||||||
|
server.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(){
|
void loop(){
|
||||||
@@ -127,11 +135,20 @@ void loop(){
|
|||||||
}
|
}
|
||||||
mClient.loop();
|
mClient.loop();
|
||||||
if(cRun + 999 < millis()){
|
if(cRun + 999 < millis()){
|
||||||
cRun = millis();
|
digitalWrite(LED_WRK, LOW);
|
||||||
nSampl++;
|
nSampl++;
|
||||||
currM += curr;
|
currM += curr;
|
||||||
I = sqrt(sumReading / double(nSampl)) / 39.7;
|
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",
|
// Serial.printf("Millis = %lu, I = %f, V = %f, Samples = %d\n",
|
||||||
// cRun, I, V, nSampl);
|
// cRun, I, V, nSampl);
|
||||||
// if(WiFi.isConnected()){
|
// if(WiFi.isConnected()){
|
||||||
@@ -147,17 +164,24 @@ void loop(){
|
|||||||
|
|
||||||
if(++nSec > 59){
|
if(++nSec > 59){
|
||||||
nSec = 0;
|
nSec = 0;
|
||||||
char v[7];
|
// char v[7];
|
||||||
// Serial.printf("Publish min, curr=%.2f, volt=%.2f\n", currA / 60.0f, voltA / 60.0f);
|
// Serial.printf("Publish min, curr=%.2f, volt=%.2f\n", currA / 60.0f, voltA / 60.0f);
|
||||||
// sprintf(v, "%.2f", curr);
|
// sprintf(v, "%.2f", curr);
|
||||||
//mClient.publish(MAIN_TOPIC "curr", v);
|
//mClient.publish(MAIN_TOPIC "curr", v);
|
||||||
//mqttClient.publish(MAIN_TOPIC "curr", 1, false, 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;
|
currA = voltA = 0.0f;
|
||||||
}
|
}
|
||||||
|
digitalWrite(LED_WRK, HIGH);
|
||||||
|
cRun = millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void mcp_output(){
|
void mcp_output(){
|
||||||
uint16_t reading;
|
uint16_t reading;
|
||||||
int adcI, adcU;
|
int adcI, adcU;
|
||||||
@@ -189,7 +213,6 @@ void mcp_output(){
|
|||||||
nSampl++;
|
nSampl++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void spiBegin(void)
|
void spiBegin(void)
|
||||||
{
|
{
|
||||||
pinMode(scePinI, OUTPUT);
|
pinMode(scePinI, OUTPUT);
|
||||||
|
|||||||
Reference in New Issue
Block a user