Create a gist now

Instantly share code, notes, and snippets.

#include <ESP8266WiFi.h>
#define DEBUG
// Wi-Fi Settings
const char* ssid = "freifunk-myk.de"; // your wireless network name (SSID)
const char* password = ""; // your Wi-Fi network password
WiFiClient client;
// ThingSpeak Settings
const int channelID = 230627;
String writeAPIKey = "1DD12C70KTO4UNP0"; // write API key for your ThingSpeak Channel
const char* server = "api.thingspeak.com";
char sds_buffer[11];
byte sds_buffer_pos_w=0;
void bufferClear() {
for(sds_buffer_pos_w=0; sds_buffer_pos_w<11; sds_buffer_pos_w++) sds_buffer[sds_buffer_pos_w] = 0x00;
sds_buffer_pos_w=0;
}
void setup() {
Serial.begin(9600);
WiFi.begin(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
#ifdef DEBUG
Serial.println(F("SDS011-Monitor Debug Console"));
#endif
bufferClear();
}
void loop() {
unsigned int pm25=0;
unsigned int pm10=0;
unsigned int devId=0;
byte checksum=0;
if(Serial.available()) {
sds_buffer[sds_buffer_pos_w] = Serial.read();
#ifdef DEBUG
Serial.print(F("RX: 0x"));
Serial.print(sds_buffer[sds_buffer_pos_w], 16);
Serial.print(F(" at position 0x"));
Serial.println(sds_buffer_pos_w, 16);
#endif
if(sds_buffer[0] != 0xAA) {
sds_buffer_pos_w=0;
#ifdef DEBUG
Serial.println(F("Invalid start byte"));
#endif
return;
}
sds_buffer_pos_w++;
if(sds_buffer_pos_w < 10) return;
if(sds_buffer[9] != 0xAB) {
//Narf...
sds_buffer_pos_w=0;
sds_buffer[0]=0x00;
#ifdef DEBUG
Serial.println(F("Invalid stop byte"));
#endif
return;
}
if(sds_buffer[1] != 0xC0) {
bufferClear();
#ifdef DEBUG
Serial.println(F("Invalid command byte"));
#endif
return;
}
pm10 = (sds_buffer[4] + (sds_buffer[5] << 8));
pm25 = (sds_buffer[2] + (sds_buffer[3] << 8));
devId = (sds_buffer[6] + (sds_buffer[7] << 8));
for(sds_buffer_pos_w=2; sds_buffer_pos_w<8; sds_buffer_pos_w++) checksum += sds_buffer[sds_buffer_pos_w];
if(checksum != sds_buffer[8]) {
//Narf…
bufferClear();
#ifdef DEBUG
Serial.print(F("Invalid CRC byte: 0x"));
Serial.print(checksum, 16);
Serial.print(F(" != 0x"));
Serial.println((byte)sds_buffer[8], 16);
#endif
return;
}
#ifdef DEBUG
Serial.print(F("PM10: "));
Serial.print(pm10);
Serial.print(F(" - PM2.5: "));
Serial.println(pm25);
#endif
HTTPSendData(pm10, pm25);
bufferClear();
}
}
bool HTTPSendData(unsigned int pm10, unsigned int pm25) {
if (client.connect(server, 80)) {
// Construct API request body
String body = "field1=";
body += String(pm10);
body += "&field2=";
body += String(pm25);
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + writeAPIKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(body.length());
client.print("\n\n");
client.print(body);
client.print("\n\n");
#ifdef DEBUG
Serial.print("Message to ");
Serial.println(server);
Serial.print("POST /update HTTP/1.1\n");
Serial.print("Host: api.thingspeak.com\n");
Serial.print("Connection: close\n");
Serial.print("X-THINGSPEAKAPIKEY: " + writeAPIKey + "\n");
Serial.print("Content-Type: application/x-www-form-urlencoded\n");
Serial.print("Content-Length: ");
Serial.print(body.length());
Serial.print("\n\n");
Serial.print(body);
Serial.print("\n\n");
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment