Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Wire.h>
- #include <ESP8266WiFi.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #include "DHT.h"
- #define DHTTYPE DHT11
- uint8_t DHTPin = D6;
- uint8_t DHTPowerPin = D5;
- DHT dht(DHTPin, DHTTYPE);
- float Temperature;
- float Humidity;
- #define LENG 31 //0x42 + 31 bytes equal to 32 bytes
- unsigned char buf[LENG];
- int PM01Value=0; //define PM1.0 value of the air detector module
- int PM2_5Value=0; //define PM2.5 value of the air detector module
- int PM10Value=0; //define PM10 value of the air detector module
- unsigned long lastMillis;
- String apiKey = "XXXXX";
- // replace with your routers SSID
- const char* ssid = "XXXXX";
- // replace with your routers password
- const char* password = "XXXXX";
- const char* server = "api.thingspeak.com";
- WiFiClient client;
- Adafruit_SSD1306 display(-1);
- void setup()
- {
- //initialize with the I2C addr 0x3C
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
- Serial.begin(9600);
- delay(100);
- Serial.swap();
- delay(100);
- pinMode(DHTPin, INPUT);
- pinMode(DHTPowerPin, OUTPUT);
- digitalWrite(DHTPowerPin, HIGH);
- delay(100);
- dht.begin();
- // Display Text
- display.clearDisplay();
- display.setTextSize(2);
- display.setTextColor(WHITE);
- display.println("PM2.5 Data Logger");
- display.println("(Public)");
- display.setTextSize(1);
- display.println("");
- display.println("by Mark Schramm");
- display.display();
- WiFi.begin(ssid, password);
- delay(2000);
- }
- void loop() {
- Temperature = dht.readTemperature(); // Gets the values of the temperature
- //Temperature= round(Temperature*10)/10;
- //delay(250);
- Humidity = dht.readHumidity(); // Gets the values of the humidity
- //Humidity= round(Humidity);
- //delay(250);
- if(Serial.find(0x42)){ //start to read when detect 0x42
- Serial.readBytes(buf,LENG);
- if(buf[0] == 0x4d){
- if(checkValue(buf,LENG)){
- PM01Value=transmitPM01(buf); //count PM1.0 value of the air detector module
- PM2_5Value=transmitPM2_5(buf);//count PM2.5 value of the air detector module
- PM10Value=transmitPM10(buf); //count PM10 value of the air detector module
- }
- }
- }
- // Display Text
- display.clearDisplay();
- display.setTextSize(3);
- display.setTextColor(WHITE);
- display.setCursor(10,5);
- display.print(String(PM2_5Value));
- display.setTextSize(1);
- display.setCursor(93,20);
- display.print("PM2.5");
- display.setTextSize(1);
- display.drawLine( 0, 32, 128, 32, WHITE);
- display.setCursor(10,46);
- display.setTextSize(2);
- display.print(Temperature,1);
- display.print("C ");
- display.print(Humidity,0);
- display.print("%");
- if (WiFi.status() != WL_CONNECTED) {
- //display.setCursor(120,0);
- //display.setTextSize(1);
- //display.print(".");
- }else{
- display.setCursor(120,0);
- display.setTextSize(1);
- display.print("@");
- }
- if (millis() - lastMillis >= 1*60*1000UL)
- {
- lastMillis = millis(); //get ready for the next iteration
- if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
- {
- String postStr = apiKey;
- postStr +="&field1=";
- postStr += String(PM01Value);
- postStr +="&field2=";
- postStr += String(PM2_5Value);
- postStr +="&field3=";
- postStr += String(PM10Value);
- postStr +="&field4=";
- postStr += String(Temperature);
- postStr +="&field5=";
- postStr += String(Humidity);
- postStr += "\r\n\r\n";
- client.print("POST /update HTTP/1.1\n");
- client.print("Host: api.thingspeak.com\n");
- client.print("Connection: close\n");
- client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
- client.print("Content-Type: application/x-www-form-urlencoded\n");
- client.print("Content-Length: ");
- client.print(postStr.length());
- client.print("\n\n");
- client.print(postStr);
- display.setCursor(110,0);
- display.setTextSize(1);
- display.print("!");
- }
- client.stop();
- }
- display.display();
- while(Serial.available() > 0) {
- char t = Serial.read();
- }
- delay(5000);
- }
- char checkValue(unsigned char *thebuf, char leng)
- {
- char receiveflag=0;
- int receiveSum=0;
- for(int i=0; i<(leng-2); i++){
- receiveSum=receiveSum+thebuf[i];
- }
- receiveSum=receiveSum + 0x42;
- if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1])) //check the serial data
- {
- receiveSum = 0;
- receiveflag = 1;
- }
- return receiveflag;
- }
- int transmitPM01(unsigned char *thebuf)
- {
- int PM01Val;
- PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
- return PM01Val;
- }
- //transmit PM Value to PC
- int transmitPM2_5(unsigned char *thebuf)
- {
- int PM2_5Val;
- PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module
- return PM2_5Val;
- }
- //transmit PM Value to PC
- int transmitPM10(unsigned char *thebuf)
- {
- int PM10Val;
- PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module
- return PM10Val;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement