Created
August 6, 2017 14:14
-
-
Save nightcoffee/ab879beb470a96dbf39de2fd73a43a55 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 <SoftwareSerial.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WiFiMulti.h> | |
#include <ESP8266HTTPClient.h> | |
#define OLED_RESET LED_BUILTIN | |
SoftwareSerial pmSerial(D7, D8); | |
SoftwareSerial co2Serial(D5, D6); | |
Adafruit_SSD1306 display(OLED_RESET); | |
ESP8266WiFiMulti WiFiMulti; | |
static unsigned char ucRxBuf[50]; | |
static unsigned char ucRxCnt = 0; | |
static unsigned char ucCO2RxBuf[50]; | |
static unsigned char ucCO2RxCnt = 0; | |
static unsigned int pm25 = 0; | |
static unsigned int pm10 = 0; | |
static unsigned int temp = 0; | |
static unsigned int rh = 0; | |
static unsigned int hcho = 0; | |
static unsigned int co2 = 0; | |
static unsigned char uploadCnt = 0; | |
static bool isWifiConnected = false; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
pmSerial.begin(9600); | |
co2Serial.begin(9600); | |
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); | |
display.display(); | |
WiFiMulti.addAP("WIFINAME", "WIFIPASSWORD"); | |
} | |
void readPM(unsigned char ucData) { | |
byte i = 0; | |
unsigned short sum = 0; | |
if(ucRxCnt == 0 && ucData != 0x42) | |
{ | |
ucRxCnt = 0; | |
return; | |
} | |
if(ucRxCnt == 1 && ucData != 0x4d) | |
{ | |
ucRxCnt = 0; | |
return; | |
} | |
if(ucRxCnt == 2 && ucData != 0x00) | |
{ | |
ucRxCnt = 0; | |
return; | |
} | |
if(ucRxCnt == 3 && ucData != 0x24) | |
{ | |
ucRxCnt = 0; | |
return; | |
} | |
ucRxBuf[ucRxCnt++] = ucData; | |
if(ucRxCnt > 39) { | |
for(i=0;i<38;i++) | |
{ | |
sum+= ucRxBuf[i]; | |
} | |
if(sum == ucRxBuf[38] * 256 + ucRxBuf[39]) { | |
pm25 = ucRxBuf[12] * 256 + ucRxBuf[13]; | |
hcho = ucRxBuf[28] * 256 + ucRxBuf[29]; | |
temp = ucRxBuf[30] * 256 + ucRxBuf[31]; | |
rh = ucRxBuf[32] * 256 + ucRxBuf[33]; | |
} | |
ucRxCnt = 0; | |
} | |
} | |
void readCO2(unsigned char ucData) { | |
ucCO2RxBuf[ucCO2RxCnt++] = ucData; | |
if(ucCO2RxBuf[0] != 0x42 && ucCO2RxBuf[1] != 0x4D) { | |
ucCO2RxCnt = 0; | |
} | |
if(ucCO2RxCnt > 11) { | |
co2 = ucCO2RxBuf[4] * 256 + ucCO2RxBuf[5]; | |
ucCO2RxCnt = 0; | |
} | |
} | |
void displayInfo() { | |
display.setTextSize(1); | |
display.setTextColor(WHITE); | |
display.setCursor(0,0); | |
display.clearDisplay(); | |
display.print("PM 2.5: "); | |
display.print(pm25); | |
display.println(); | |
display.print("Temp: "); | |
display.print(temp / 10.0); | |
display.println(); | |
display.print("Rh: "); | |
display.print(rh / 10.0); | |
display.println(); | |
display.print("Hcho: "); | |
display.print(hcho / 1000.0); | |
display.println(); | |
display.print("CO2: "); | |
display.print(co2); | |
display.println(); | |
display.println(); | |
if(isWifiConnected) { | |
display.print("Connected"); | |
} else { | |
display.print("Upload failed"); | |
} | |
display.display(); | |
} | |
void uploadData() { | |
char data[200]; | |
uploadCnt++; | |
if(uploadCnt > 60) { | |
uploadCnt = 0; | |
if((WiFiMulti.run() == WL_CONNECTED)) { | |
HTTPClient http; | |
http.begin("http://www.lewei50.com/api/V1/Gateway/UpdateSensors/02"); //HTTP | |
http.addHeader("userkey","HERE YOUR KEY"); | |
sprintf(data,"[{'Name': 'co2', 'Value':'%d'},{'Name': 'hcho', 'Value':'%d'},{'Name': 'pm25', 'Value':'%d'},{'Name': 'rh', 'Value':'%d'},{'Name': 'temp', 'Value':'%d'}]", co2, hcho, pm25, rh, temp); | |
http.POST(data); | |
http.end(); | |
isWifiConnected = true; | |
} else { | |
isWifiConnected = false; | |
} | |
} | |
} | |
void loop() { | |
pmSerial.enableRx(true); | |
co2Serial.enableRx(false); | |
delay(500); | |
digitalWrite(LED_BUILTIN, HIGH); | |
while(pmSerial.available() > 0) { | |
readPM(pmSerial.read()); | |
} | |
pmSerial.enableRx(false); | |
co2Serial.enableRx(true); | |
byte request[] = {0x42, 0x4d, 0xe3, 0x00, 0x00, 0x01, 0x72}; | |
co2Serial.write(request, 7), | |
delay(500); | |
while(co2Serial.available() > 0) { | |
readCO2(co2Serial.read()); | |
} | |
uploadData(); | |
displayInfo(); | |
digitalWrite(LED_BUILTIN, LOW); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment