ESP -seadista audentima telefoni võrku ja toide nt akupangast ning andmed kupata Blynki. Telef.-s installi Blynk app lugemisels.
Allolevas näites on Wemos D1 külge poogitud CO2 sensor (MH-Z19B) tulemi lugemiseks Blynk terminal widget (virtual pin V51).
// Blynk(V51) <-> APN <- Wemos Mini D1 <- MH-Z19B
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// Blynk
char auth[] = "sdfsdfsdfsdfsdfsdfsd";
char ssid[] = "TelefonAP";
char pass[] = "fpassword";
uint32_t tim;
int x=0;
#include <SoftwareSerial.h>
SoftwareSerial co2Serial(12, 13); // define MH-Z19 RX TX
void setup() {
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
co2Serial.begin(9600);
delay(3*60*1000); // sensor preheating
//Serial.println("Start");
}
int readCO2UART() {
int ppm_uart = 0;
char cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
char response[9]; // for answer
//Serial.println("Sending CO2 request...");
co2Serial.write(cmd, 9); //request PPM CO2
// clear the buffer
memset(response, 0, 9);
int i = 0;
while (co2Serial.available() == 0) {
delay(1000);
i++;
}
if (co2Serial.available() > 0) {
co2Serial.readBytes(response, 9);
}
// print out the response in hexa
for (int i = 0; i < 9; i++) {
}
// checksum
char check = getCheckSum(response);
if (response[8] != check) {
Serial.println("Checksum error");
} else {
byte status = response[5];
if (status != 0) {
} else {
// ppm
ppm_uart = 256 * (int)response[2] + response[3];
//Serial.print("CO2: ");
//Serial.println(ppm_uart);
return ppm_uart;
}
}
}
byte getCheckSum(char *packet) {
byte i;
unsigned char checksum = 0;
for (i = 1; i < 8; i++) {
checksum += packet[i];
}
checksum = 0xff - checksum;
checksum += 1;
return checksum;
}
void Bly(){
if ((millis() > tim) ) {
tim = millis() + 10000;
int ppm = readCO2UART();
String z = "(" + String(x) + ") - " + String(ppm);
Blynk.virtualWrite(V51, z);
x++;
}
}
void loop() {
Blynk.run();
Bly();
}