yotanutmaliwan

การเชื่อมต่ออุปกรณ์เข้ากับ NETPIE สำหรับ CCS811

Jun 28th, 2018
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. /* connect to Gas Sensor */
  2. #include "Adafruit_CCS811.h"
  3. Adafruit_CCS811 ccs;
  4.  
  5. /* connect to wifi */
  6. #include <ESP8266WiFi.h>
  7. const char* ssid = "NSTDA-Project-7013"; // ssid wifi
  8. const char* password = "1q2w3e4r"; // password wifi
  9. WiFiClient client;
  10.  
  11. /* connect to netpie */
  12. #include <MicroGear.h>
  13. #define APPID "SmartOfficeAt418B" // APPLICATION (APPID) etc. KMUTNBM2
  14. #define KEY "9LnnyoiXdorYtNn" // device key แต่ในเว็บ netpie ใช้ session key
  15. #define SECRET "ZhGtoPWqueqmtDDrTNRutKcMT" // *secret device key
  16. #define FEEDID "418BSensors" // feed name
  17. #define ALIAS "Air_Quality_Sensor"
  18. MicroGear microgear(client);
  19.  
  20. /* Variable */
  21. int timer = 0;
  22. char str[32];
  23.  
  24. void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) {
  25. Serial.print("Incoming message --> ");
  26. msg[msglen] = '\0';
  27. Serial.println((char *)msg);
  28. }
  29.  
  30. void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) {
  31. Serial.println("Connected to NETPIE...");
  32. microgear.setAlias(ALIAS);
  33. }
  34.  
  35. void setup() {
  36. Serial.begin(115200);
  37.  
  38. // Initialize Gas Sensor
  39. if (!ccs.begin()) {
  40. Serial.println("Failed to start sensor! Please check your wiring.");
  41. while (1);
  42. }
  43.  
  44. //calibrate temperature sensor
  45. while (!ccs.available());
  46. float temp = ccs.calculateTemperature();
  47. ccs.setTempOffset(temp - 25.0);
  48.  
  49. // Initialize microgear
  50. microgear.on(MESSAGE, onMsghandler);
  51. microgear.on(CONNECTED, onConnected); //optional
  52. microgear.on(ERROR, onConnected);
  53. microgear.on(INFO, onConnected);
  54.  
  55. //Wait for WiFi Establishement
  56. Serial.println("Starting...");
  57. if (WiFi.begin(ssid, password)) {
  58. while (WiFi.status() != WL_CONNECTED) {
  59. delay(500);
  60. Serial.print(".");
  61. }
  62. }
  63.  
  64. Serial.println("WiFi connected");
  65. Serial.println("IP address: ");
  66. Serial.println(WiFi.localIP());
  67.  
  68. microgear.init(KEY, SECRET, ALIAS);
  69. microgear.connect(APPID);
  70.  
  71. }
  72.  
  73. void loop() {
  74. if (microgear.connected()) {
  75. Serial.print("*");
  76. microgear.loop();
  77. if (timer >= 60000) {
  78.  
  79. int CO2;
  80. int TVOC;
  81. float temp;
  82.  
  83. if (ccs.available()) {
  84. temp = ccs.calculateTemperature();
  85. if (!ccs.readData()) {
  86. Serial.println("CO2: ");
  87. Serial.print(ccs.geteCO2());
  88. CO2 = ccs.geteCO2();
  89. Serial.print("ppm, TVOC: ");
  90. Serial.print(ccs.getTVOC());
  91. TVOC = ccs.getTVOC();
  92. Serial.print("ppb, Temp: ");
  93. Serial.println(temp);
  94. }
  95. else {
  96. Serial.println("ERROR!");
  97. while (1);
  98. }
  99. }
  100.  
  101.  
  102.  
  103. //Don't You DARE USE white spaces in the String
  104. String data = "{\"Room_CO2\":";
  105. data += CO2 ;
  106. data += ",\"Room_TEMP\":";
  107. data += temp ;
  108. data += ",\"Room_TVOC\":";
  109. data += TVOC ;
  110. data += "}";
  111.  
  112. sprintf(str, "%d,%.2f,%d", CO2, temp, TVOC);
  113. Serial.println(str);
  114. microgear.publish("/roomquality", str);
  115.  
  116. Serial.println((char*) data.c_str());
  117.  
  118.  
  119. Serial.print("Sending --> ");
  120. microgear.writeFeed(FEEDID, data); //YOUR FEED ID, API KEY
  121.  
  122. timer = 0;
  123. }
  124. else timer += 200;
  125. } else {
  126. Serial.println("connection lost, reconnect...");
  127. if (timer >= 5000) {
  128. microgear.connect(APPID);
  129. timer = 0;
  130. }
  131. else timer += 200;
  132. }
  133. delay(200);
  134. }
Advertisement
Add Comment
Please, Sign In to add comment