Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Wire.h>
  3. #include <ESP8266WiFi.h>
  4. #include <Adafruit_GFX.h>
  5. #include <Adafruit_SSD1306.h>
  6. #include "DHT.h"
  7.  
  8. #define DHTTYPE DHT11
  9. uint8_t DHTPin = D6;
  10. uint8_t DHTPowerPin = D5;
  11.  
  12. DHT dht(DHTPin, DHTTYPE);
  13.  
  14. float Temperature;
  15. float Humidity;
  16.  
  17. #define LENG 31 //0x42 + 31 bytes equal to 32 bytes
  18. unsigned char buf[LENG];
  19.  
  20. int PM01Value=0; //define PM1.0 value of the air detector module
  21. int PM2_5Value=0; //define PM2.5 value of the air detector module
  22. int PM10Value=0; //define PM10 value of the air detector module
  23.  
  24. unsigned long lastMillis;
  25.  
  26.  
  27. String apiKey = "XXXXX";
  28.  
  29. // replace with your routers SSID
  30. const char* ssid = "XXXXX";
  31. // replace with your routers password
  32. const char* password = "XXXXX";
  33.  
  34. const char* server = "api.thingspeak.com";
  35. WiFiClient client;
  36.  
  37.  
  38. Adafruit_SSD1306 display(-1);
  39.  
  40. void setup()
  41. {
  42. //initialize with the I2C addr 0x3C
  43. display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  44. Serial.begin(9600);
  45. delay(100);
  46.  
  47. Serial.swap();
  48. delay(100);
  49. pinMode(DHTPin, INPUT);
  50. pinMode(DHTPowerPin, OUTPUT);
  51. digitalWrite(DHTPowerPin, HIGH);
  52.  
  53. delay(100);
  54. dht.begin();
  55.  
  56. // Display Text
  57. display.clearDisplay();
  58. display.setTextSize(2);
  59. display.setTextColor(WHITE);
  60.  
  61. display.println("PM2.5 Data Logger");
  62. display.println("(Public)");
  63.  
  64. display.setTextSize(1);
  65. display.println("");
  66. display.println("by Mark Schramm");
  67. display.display();
  68.  
  69. WiFi.begin(ssid, password);
  70.  
  71. delay(2000);
  72.  
  73.  
  74.  
  75.  
  76. }
  77.  
  78. void loop() {
  79.  
  80.  
  81. Temperature = dht.readTemperature(); // Gets the values of the temperature
  82. //Temperature= round(Temperature*10)/10;
  83. //delay(250);
  84. Humidity = dht.readHumidity(); // Gets the values of the humidity
  85. //Humidity= round(Humidity);
  86. //delay(250);
  87.  
  88.  
  89. if(Serial.find(0x42)){ //start to read when detect 0x42
  90. Serial.readBytes(buf,LENG);
  91.  
  92. if(buf[0] == 0x4d){
  93. if(checkValue(buf,LENG)){
  94. PM01Value=transmitPM01(buf); //count PM1.0 value of the air detector module
  95. PM2_5Value=transmitPM2_5(buf);//count PM2.5 value of the air detector module
  96. PM10Value=transmitPM10(buf); //count PM10 value of the air detector module
  97. }
  98. }
  99. }
  100.  
  101.  
  102. // Display Text
  103. display.clearDisplay();
  104. display.setTextSize(3);
  105. display.setTextColor(WHITE);
  106. display.setCursor(10,5);
  107. display.print(String(PM2_5Value));
  108. display.setTextSize(1);
  109. display.setCursor(93,20);
  110. display.print("PM2.5");
  111. display.setTextSize(1);
  112.  
  113. display.drawLine( 0, 32, 128, 32, WHITE);
  114. display.setCursor(10,46);
  115. display.setTextSize(2);
  116. display.print(Temperature,1);
  117. display.print("C ");
  118. display.print(Humidity,0);
  119. display.print("%");
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. if (WiFi.status() != WL_CONNECTED) {
  127.  
  128. //display.setCursor(120,0);
  129. //display.setTextSize(1);
  130. //display.print(".");
  131.  
  132. }else{
  133. display.setCursor(120,0);
  134. display.setTextSize(1);
  135. display.print("@");
  136.  
  137.  
  138. }
  139.  
  140.  
  141.  
  142. if (millis() - lastMillis >= 1*60*1000UL)
  143. {
  144. lastMillis = millis(); //get ready for the next iteration
  145.  
  146.  
  147. if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
  148. {
  149. String postStr = apiKey;
  150. postStr +="&field1=";
  151. postStr += String(PM01Value);
  152. postStr +="&field2=";
  153. postStr += String(PM2_5Value);
  154. postStr +="&field3=";
  155. postStr += String(PM10Value);
  156. postStr +="&field4=";
  157. postStr += String(Temperature);
  158. postStr +="&field5=";
  159. postStr += String(Humidity);
  160. postStr += "\r\n\r\n";
  161.  
  162. client.print("POST /update HTTP/1.1\n");
  163. client.print("Host: api.thingspeak.com\n");
  164. client.print("Connection: close\n");
  165. client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
  166. client.print("Content-Type: application/x-www-form-urlencoded\n");
  167. client.print("Content-Length: ");
  168. client.print(postStr.length());
  169. client.print("\n\n");
  170. client.print(postStr);
  171.  
  172. display.setCursor(110,0);
  173. display.setTextSize(1);
  174. display.print("!");
  175.  
  176. }
  177. client.stop();
  178.  
  179.  
  180. }
  181.  
  182. display.display();
  183.  
  184. while(Serial.available() > 0) {
  185. char t = Serial.read();
  186. }
  187.  
  188.  
  189. delay(5000);
  190.  
  191.  
  192.  
  193.  
  194. }
  195.  
  196. char checkValue(unsigned char *thebuf, char leng)
  197. {
  198. char receiveflag=0;
  199. int receiveSum=0;
  200.  
  201. for(int i=0; i<(leng-2); i++){
  202. receiveSum=receiveSum+thebuf[i];
  203. }
  204. receiveSum=receiveSum + 0x42;
  205.  
  206. if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1])) //check the serial data
  207. {
  208. receiveSum = 0;
  209. receiveflag = 1;
  210. }
  211. return receiveflag;
  212. }
  213.  
  214. int transmitPM01(unsigned char *thebuf)
  215. {
  216. int PM01Val;
  217. PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
  218. return PM01Val;
  219. }
  220. //transmit PM Value to PC
  221. int transmitPM2_5(unsigned char *thebuf)
  222. {
  223. int PM2_5Val;
  224. PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module
  225. return PM2_5Val;
  226. }
  227. //transmit PM Value to PC
  228. int transmitPM10(unsigned char *thebuf)
  229. {
  230. int PM10Val;
  231. PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module
  232. return PM10Val;
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement