New Electro, Midroom autocalibration co2
This commit is contained in:
@@ -10,8 +10,8 @@ TimerHandle_t wifiReconnectTimer;
|
||||
|
||||
//const char* ssid = "wf-home";
|
||||
//const char* password = "0ndthnrf";
|
||||
const char* ssid = "mt-24";
|
||||
const char* password = "727479-o";
|
||||
const char* ssid = "wf-home";
|
||||
const char* password = "0ndthnrf";
|
||||
const char* mqtt_server = "192.168.1.111";
|
||||
|
||||
|
||||
|
||||
@@ -15,9 +15,11 @@ board = esp07
|
||||
framework = arduino
|
||||
;board_build.ldscript = eagle.flash.1m.ld
|
||||
upload_protocol = espota
|
||||
upload_port = 192.168.1.136
|
||||
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
|
||||
|
||||
@@ -4,9 +4,14 @@
|
||||
#include <PubSubClient.h>
|
||||
#include <SPI.h>
|
||||
|
||||
#define LED_WF (2) //Blue
|
||||
#define LED_WRK (3) //Green
|
||||
#define LED_MQ (4) //Red
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESPAsyncTCP.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <WebSerial.h>
|
||||
|
||||
#define LED_WF (0) //Red
|
||||
#define LED_WRK (16) //Green
|
||||
#define LED_MQ (5) //Yellow
|
||||
|
||||
#define HOST_NAME "ElectroT"
|
||||
#define MAIN_TOPIC "/home/kort/"
|
||||
@@ -25,18 +30,23 @@
|
||||
|
||||
const char* ssid = "wf-home";
|
||||
const char* password = "0ndthnrf";
|
||||
const char* mqtt_server = "192.168.1.250";
|
||||
const char* mqtt_server = "192.168.1.111";
|
||||
|
||||
const int scePin = 15; // SCE - Chip select
|
||||
const int scePinI = 15, scePinU = 4; // SCE - Chip select
|
||||
|
||||
const double vRef = 3.3;
|
||||
const int period = 1000; //us
|
||||
const int period = 125; //us
|
||||
|
||||
unsigned long cRun = millis();
|
||||
double curr = 0.0;
|
||||
double volt = 0.0;
|
||||
double currT = 0.0;
|
||||
double currM = 0.0;
|
||||
int nSec, nSampl;
|
||||
unsigned long sumReading = 0;
|
||||
unsigned long sumReading2 = 0;
|
||||
long sumAvg = 0, sumAvg2 = 0;
|
||||
int maxV = 0, minV = 0;
|
||||
|
||||
void connectToWifi();
|
||||
void connectToMqtt();
|
||||
@@ -52,7 +62,9 @@ WiFiEventHandler wifiDisconnectHandler;
|
||||
void spiBegin();
|
||||
void mcp_output();
|
||||
|
||||
Ticker tckrElcnt(mcp_output, period, 0, MILLIS);
|
||||
Ticker tckrElcnt(mcp_output, period, 0, MICROS_MICROS);
|
||||
|
||||
AsyncWebServer server(80);
|
||||
|
||||
void connectToWifi() {
|
||||
Serial.println("Connecting to Wi-Fi...");
|
||||
@@ -68,7 +80,6 @@ void onWifiConnect(const WiFiEventStationModeGotIP& event) {
|
||||
Serial.print("IP: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
Serial.println("Connecting to MQTT...");
|
||||
//reconnect();
|
||||
digitalWrite(LED_WF, HIGH);
|
||||
}
|
||||
|
||||
@@ -77,8 +88,6 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||
digitalWrite(LED_WF, LOW);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void setup(){
|
||||
Serial.begin(9600);
|
||||
WiFi.mode(WIFI_STA);
|
||||
@@ -95,9 +104,6 @@ void setup(){
|
||||
pinMode(LED_WF, OUTPUT);
|
||||
pinMode(LED_MQ, OUTPUT);
|
||||
pinMode(LED_WRK, OUTPUT);
|
||||
pinMode(scePin, OUTPUT);
|
||||
digitalWrite(scePin, HIGH);
|
||||
//spiBegin();
|
||||
digitalWrite(LED_WF, LOW);
|
||||
digitalWrite(LED_MQ, LOW);
|
||||
digitalWrite(LED_WRK, LOW);
|
||||
@@ -106,78 +112,95 @@ void setup(){
|
||||
nSec = 0;
|
||||
curr = -1;
|
||||
nSampl = 0;
|
||||
//tckrElcnt.start();
|
||||
spiBegin();
|
||||
tckrElcnt.start();
|
||||
}
|
||||
|
||||
void loop(){
|
||||
static bool led_wrk = false;
|
||||
//tckrElcnt.update();
|
||||
static float currA = 0.0f, voltA = 0.0f;
|
||||
double I, V;
|
||||
tckrElcnt.update();
|
||||
ArduinoOTA.handle();
|
||||
if(!mClient.connected()){
|
||||
digitalWrite(LED_MQ, LOW);
|
||||
reconnect();
|
||||
}
|
||||
mClient.loop();
|
||||
if(cRun + 999 < millis()){
|
||||
cRun = millis();
|
||||
mcp_output();
|
||||
nSampl++;
|
||||
currM += curr;
|
||||
if(WiFi.isConnected()){
|
||||
Serial.printf("Sec: %d\t%.2f\n", nSec, curr);
|
||||
// Serial.print("\t");
|
||||
// Serial.println(curr * 233);
|
||||
led_wrk = !led_wrk;
|
||||
digitalWrite(LED_WRK, led_wrk);
|
||||
}
|
||||
I = sqrt(sumReading / double(nSampl)) / 39.7;
|
||||
V = sqrt(sumReading2 / double(nSampl))/ 1.78;
|
||||
// Serial.printf("Millis = %lu, I = %f, V = %f, Samples = %d\n",
|
||||
// cRun, I, V, nSampl);
|
||||
// if(WiFi.isConnected()){
|
||||
// Serial.printf("Sec: %d\tI = %.2f\tU = %.2f\tsamls=%d\tIAvg=%d\n", nSec, I, V, nSampl, sumAvg2/nSampl);
|
||||
// // Serial.print("\t");
|
||||
// // Serial.println(curr * 233);
|
||||
// }
|
||||
|
||||
nSampl = 0;
|
||||
sumReading = sumReading2 = sumAvg = sumAvg2 = 0;
|
||||
currA += I;
|
||||
voltA += V;
|
||||
|
||||
if(++nSec > 59){
|
||||
nSec = 0;
|
||||
currM = currM / 60.0;
|
||||
char v[7];
|
||||
Serial.printf("Publish min, curr=%.2f\n", currM);
|
||||
sprintf(v, "%.2f", curr);
|
||||
mClient.publish(MAIN_TOPIC "curr", v);
|
||||
// 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);
|
||||
currM = 0.0;
|
||||
currA = voltA = 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void mcp_output(){
|
||||
double currTI = 0.0;
|
||||
int sampl = 0;
|
||||
int out = 0;
|
||||
unsigned long start = micros();
|
||||
//while((start + 20000) >= micros()){
|
||||
SPI.begin();
|
||||
SPI.setDataMode(SPI_MODE0);
|
||||
SPI.setBitOrder(MSBFIRST);
|
||||
SPI.setFrequency(800000);
|
||||
digitalWrite(scePin, LOW);
|
||||
out = SPI.transfer16(0x0000);
|
||||
digitalWrite(scePin, HIGH);
|
||||
//yield();
|
||||
//delayMicroseconds(5);
|
||||
out = out << 3; //Must shift out the 3 MSB's (trash bits)
|
||||
out = out >> 4;
|
||||
currTI += out;// * (vRef / 4096); // Using VRef
|
||||
sampl++;
|
||||
//}
|
||||
Serial.printf("Time=%d, Out = %d, Samples=%d\n", int(micros() - start), out, sampl);
|
||||
//curr = 30.0 * sqrt(currTI / sampl);
|
||||
curr = currTI / sampl;
|
||||
uint16_t reading;
|
||||
int adcI, adcU;
|
||||
|
||||
digitalWrite(scePinI, LOW);
|
||||
reading = SPI.transfer16(0x0000);
|
||||
digitalWrite(scePinI, HIGH);
|
||||
reading = reading << 3; //Must shift out the 3 MSB's (trash bits)
|
||||
reading = reading >> 4;
|
||||
adcI = reading - 2044;
|
||||
//Serial.println(reading);
|
||||
if(maxV < adcI) maxV = adcI;
|
||||
if(minV > adcI) minV = adcI;
|
||||
sumReading += adcI * adcI;
|
||||
sumAvg += adcI;
|
||||
|
||||
digitalWrite(scePinU, LOW);
|
||||
reading = SPI.transfer16(0x0000);
|
||||
digitalWrite(scePinU, HIGH);
|
||||
reading = reading << 3; //Must shift out the 3 MSB's (trash bits)
|
||||
reading = reading >> 4;
|
||||
adcU = reading - 3120;
|
||||
//Serial.println(reading);
|
||||
if(maxV < adcI) maxV = adcU;
|
||||
if(minV > adcI) minV = adcU;
|
||||
sumReading2 += adcU * adcU;
|
||||
sumAvg2 += adcI;
|
||||
|
||||
nSampl++;
|
||||
}
|
||||
|
||||
|
||||
void spiBegin(void)
|
||||
{
|
||||
pinMode(scePin, OUTPUT);
|
||||
pinMode(scePinI, OUTPUT);
|
||||
pinMode(scePinU, OUTPUT);
|
||||
digitalWrite(scePinI, HIGH);
|
||||
digitalWrite(scePinU, HIGH);
|
||||
|
||||
SPI.begin();
|
||||
SPI.setDataMode(SPI_MODE0);
|
||||
SPI.setBitOrder(MSBFIRST);
|
||||
SPI.setFrequency(800000);
|
||||
digitalWrite(scePin, HIGH);
|
||||
SPI.setFrequency(1200000);
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
@@ -188,6 +211,7 @@ void reconnect() {
|
||||
// Attempt to connect
|
||||
if (mClient.connect(HOST_NAME)) {
|
||||
Serial.println("connected");
|
||||
digitalWrite(LED_MQ, HIGH);
|
||||
// Once connected, publish an announcement...
|
||||
} else {
|
||||
Serial.print("failed, rc=");
|
||||
|
||||
@@ -105,6 +105,7 @@ void setup() {
|
||||
}
|
||||
//Serial1.println();
|
||||
mhz19.begin(Serial);
|
||||
mhz19.autoCalibration();
|
||||
Wire.begin();
|
||||
bool status = bme.begin(BME280_ADDRESS_ALTERNATE);
|
||||
if (!status) {
|
||||
@@ -382,18 +383,58 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
|
||||
EEPROM.put(4, mvDelaySet);
|
||||
EEPROM.commit();
|
||||
}
|
||||
if(strcmp(topic, TOPIC"/mode") == 0){
|
||||
if(strcmp(topic, TOPIC"/ac/mode") == 0){
|
||||
char m = payload[0];
|
||||
switch (m)
|
||||
{
|
||||
case 'A':
|
||||
ac.setMode(kToshibaAcAuto);
|
||||
break;
|
||||
|
||||
case 'C':
|
||||
ac.setMode(kToshibaAcCool);
|
||||
break;
|
||||
case 'D':
|
||||
ac.setMode(kToshibaAcDry);
|
||||
break;
|
||||
case 'F':
|
||||
ac.setMode(kToshibaAcFan);
|
||||
break;
|
||||
case 'H':
|
||||
ac.setMode(kToshibaAcHeat);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
mvDelaySet = atoi(payload);
|
||||
ac.send();
|
||||
}
|
||||
if(strcmp(topic, TOPIC"/ac/temp") == 0){
|
||||
int setTemp = atoi(payload);
|
||||
ac.setTemp(setTemp);
|
||||
ac.send();
|
||||
}
|
||||
if(strcmp(topic, TOPIC"/ac/state") == 0){
|
||||
char m = payload[0];
|
||||
if(m == '1') ac.on();
|
||||
else ac.off();
|
||||
ac.send();
|
||||
}
|
||||
if(strcmp(topic, TOPIC"/ac/eco") == 0){
|
||||
char m = payload[0];
|
||||
if(m == '1'){
|
||||
ac.setEcono(true);
|
||||
mqttClient.publish(TOPIC"/ac/turbo", 1, false, "0");
|
||||
}
|
||||
else ac.setEcono(false);
|
||||
ac.send();
|
||||
}
|
||||
if(strcmp(topic, TOPIC"/ac/turbo") == 0){
|
||||
char m = payload[0];
|
||||
if(m == '1'){
|
||||
ac.setTurbo(true);
|
||||
mqttClient.publish(TOPIC"/ac/eco", 1, false, "0");
|
||||
}
|
||||
else ac.setTurbo(false);
|
||||
ac.send();
|
||||
}
|
||||
b_led.start();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user