Created
November 14, 2021 13:13
-
-
Save pco2699/a1e7aae16ef1ce68633a7fb2c351e86a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "MHZ19.h" | |
#include <SoftwareSerial.h> // Remove if using HardwareSerial or non-uno compatabile device | |
#include <WiFi.h> | |
#include "Ambient.h" | |
#define BAUDRATE 9600 // Native to the sensor (do not change) | |
const int rx_pin = 16; //Serial rx pin no | |
const int tx_pin = 17; //Serial tx pin no | |
unsigned long getDataTimer = 0; | |
const char* ssid = "FS030W_P21197"; | |
const char* password = "22960953"; | |
unsigned int channelId = 40840; | |
const char* writeKey = "39fc282b7cd0853b"; | |
WiFiClient client; | |
Ambient ambient; | |
MHZ19 myMHZ19; | |
SoftwareSerial myPort; // Uno example | |
HardwareSerial mySerial(2); | |
/* | |
void wifi_lan_check() | |
WiFi Lan の監視をします。 | |
WiFi.status() で判断するよりも、ping で応答をチェックした方が、早いかも。 | |
*/ | |
void wifi_lan_check() { | |
int i; | |
if (WiFi.status() != 3) { | |
//Serial.print("wifi_lan_check() :#1 st="); | |
//Serial.println(WiFi.status()); | |
} | |
if (WiFi.status() != WL_CONNECTED) { | |
//Serial.println("WiFi disconnected"); | |
WiFi.disconnect(); | |
delay(50); | |
WiFi.begin(ssid, password); | |
i = 50; | |
// 結局は、回復しないで、wdt reset になるので、直ぐ自分で restart させた方が良いかも。 | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(250); | |
//Serial.print("."); | |
i--; | |
if (i < 0) { | |
//Serial.println("wifi_lan_check() :#2 ESP.restart()"); | |
delay(250); | |
// 自分で restart しないで、放っておくと、 wdt reset になるので、 | |
// 敢えて ESP.restart() しなくてもよいのだが、気が短い人は、どうぞ。 | |
ESP.restart(); | |
} | |
} | |
} | |
} | |
/*---------------------------------------------------------- | |
MH-Z19 CO2 sensor setup | |
----------------------------------------------------------*/ | |
void setup() | |
{ | |
WiFi.begin(ssid, password); | |
int i = 50; | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
i--; | |
if (i < 0) { | |
//Serial.println("wifi_lan_check() :#2 ESP.restart()"); | |
delay(250); | |
// 自分で restart しないで、放っておくと、 wdt reset になるので、 | |
// 敢えて ESP.restart() しなくてもよいのだが、気が短い人は、どうぞ。 | |
ESP.restart(); | |
i = 50; | |
} | |
} | |
ambient.begin(channelId, writeKey, &client); | |
myPort.begin(BAUDRATE, SWSERIAL_8N1, rx_pin, tx_pin); | |
if (!myPort) { // If the object did not initialize, then its configuration is invalid | |
while (1) { // Don't continue with invalid configuration | |
delay (1000); | |
} | |
} | |
myMHZ19.begin(myPort); | |
myMHZ19.autoCalibration(false); // Turn auto calibration ON (OFF autoCalibration(false)) | |
} | |
void loop() | |
{ | |
if (millis() - getDataTimer >= 2000) | |
{ | |
int CO2; | |
/* note: getCO2() default is command "CO2 Unlimited". This returns the correct CO2 reading even | |
if below background CO2 levels or above range (useful to validate sensor). You can use the | |
usual documented command with getCO2(false) */ | |
CO2 = myMHZ19.getCO2(); // Request CO2 (as ppm) | |
int8_t Temp; | |
Temp = myMHZ19.getTemperature(); // Request Temperature (as Celsius) | |
if (Temp != -17) { | |
ambient.set(1, Temp); // データーがint型かfloat型であれば、直接セットすることができます。 | |
} | |
if (CO2 != 0) { | |
ambient.set(2, CO2); | |
} | |
ambient.send(); | |
wifi_lan_check(); | |
getDataTimer = millis(); | |
} | |
delay(30 * 1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment