Advertisement
Narayan

testing

Oct 24th, 2022 (edited)
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.88 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <Wire.h>
  3.  
  4. #include <ESP8266WiFi.h>
  5. #include <PubSubClient.h>
  6.  
  7. #include "SparkFunCCS811.h"
  8.  
  9. #define GOT_HERE()  do {Serial.print('['); Serial.print(__LINE__); Serial.println(']'); Serial.flush();} while(0)
  10. #define pSDA 2
  11. #define pSCL 14
  12. #define DATA_MAX_IDX 50
  13. #define MLX60914_I2C_ADDR 0x5a
  14. #define CSS811_I2C_ADDR 0x5b
  15. #define ROW 5
  16. #define COL 2
  17.  
  18. // Global objects for sensors, WiFi and MQTT
  19. CCS811 myCCS811(CSS811_I2C_ADDR);
  20. WiFiClient espClient;
  21. PubSubClient client(espClient);
  22.  
  23. // Variables to save values from sensors
  24. int dtVOC[DATA_MAX_IDX] = {0};
  25. int dCO2[DATA_MAX_IDX] = {0};
  26. int i = 0;
  27.  
  28. // Varaibles for MQTT messages
  29. char mqtt_msg[ROW][COL] = {0};
  30.  
  31. // Access credentials for WiFi connection
  32. const char* ssid = "xxxxxxxxxxx";
  33. const char* password = "xxxxxxxxxxxxxx";
  34. const char* mqtt_server = "192.168.1.157";
  35.  
  36.  
  37. // Connection to WiFi network
  38. void setup_wifi() {
  39.  
  40.   delay(10);
  41.   // We start by connecting to a WiFi network
  42.   Serial.println();
  43.   Serial.print("Connecting to ");
  44.   Serial.println(ssid);
  45.  
  46.   WiFi.mode(WIFI_STA);
  47.   WiFi.begin(ssid, password);
  48.  
  49.   while (WiFi.status() != WL_CONNECTED) {
  50.     delay(500);
  51.     Serial.print(".");
  52.   }
  53.  
  54.   Serial.println("");
  55.   Serial.println("WiFi connected");
  56.   Serial.println("IP address: ");
  57.   Serial.println(WiFi.localIP());
  58. }
  59.  
  60. // MQTT function
  61. void callback(char* topic, byte* payload, unsigned int length) {
  62.   Serial.print("Message arrived [");
  63.   Serial.print(topic);
  64.   Serial.print("] ");
  65.   for (uint8_t i = 0; i < length; i++) {
  66.     Serial.print((char)payload[i]);
  67.   }
  68.   Serial.println();
  69.  
  70.   // Switch on the LED if an 1 was received as first character
  71.   if ((char)payload[0] == '1') {
  72.     digitalWrite(LED_BUILTIN, LOW);   // Turn the LED on (Note that LOW is the voltage level
  73.     // but actually the LED is on; this is because
  74.     // it is active low on the ESP-01)
  75.   } else {
  76.     digitalWrite(LED_BUILTIN, HIGH);  // Turn the LED off by making the voltage HIGH
  77.   }
  78. }
  79.  
  80. // MQTT function
  81. void reconnect() {
  82.   // Loop until we're reconnected
  83.   while (!client.connected()) {
  84.     Serial.print("Attempting MQTT connection...");
  85.     // Create a random client ID
  86.     String clientId = "ESP8266Client-";
  87.     clientId += String(random(0xffff), HEX);
  88.     // Attempt to connect
  89.     if (client.connect(clientId.c_str())) {
  90.       Serial.println("connected");
  91.       // Once connected, publish an announcement...
  92.       client.publish("CCS811/test", "Hello from pubsubclient");
  93.       // ... and resubscribe
  94.       client.subscribe("CCS811/test");
  95.     } else {
  96.       Serial.print("failed, rc=");
  97.       Serial.print(client.state());
  98.       Serial.println(" try again in 5 seconds");
  99.       // Wait 5 seconds before retrying
  100.       delay(5000);
  101.     }
  102.   }
  103. }
  104.  
  105. void setup(){
  106.   randomSeed(micros());
  107.   Serial.begin(115200);
  108.   Wire.begin(pSDA, pSCL);
  109.   setup_wifi();
  110.   client.setServer(mqtt_server, 58999);
  111.   client.setCallback(callback);
  112.  
  113.   pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
  114.  
  115.   //This begins the CCS811 sensor and prints error status of .beginWithStatus()
  116.   CCS811Core::CCS811_Status_e returnCode = myCCS811.beginWithStatus();
  117.   Serial.print("CCS811 begin exited with: ");
  118.   //Pass the error code to a function to print the results
  119.   Serial.println(myCCS811.statusString(returnCode));
  120.  
  121.   //This sets the mode to 10 second reads, and prints returned error status.
  122.   returnCode = myCCS811.setDriveMode(2);
  123.   Serial.print("Mode request exited with: ");
  124.   Serial.println(myCCS811.statusString(returnCode));
  125. }
  126.  
  127. void loop(){
  128.  
  129.   client.setKeepAlive(90);
  130.   if (!client.connected()) {
  131.     reconnect();
  132.   }
  133.   client.loop();
  134.  
  135.   if( myCCS811.dataAvailable() ){
  136.     myCCS811.readAlgorithmResults(); //Calling this function updates the global tVOC and CO2 variables
  137.     dCO2[i] = myCCS811.getCO2();
  138.     dtVOC[i] = myCCS811.getTVOC();
  139.     Serial.print("Iteration: ");
  140.     Serial.println(i);
  141.     Serial.print("CO2[");
  142.     Serial.print(dCO2[i]);
  143.     Serial.print("] -- tVOC[");
  144.     Serial.print(dtVOC[i]);
  145.     Serial.println("]");
  146.  
  147.     if( !(i % 5) && i){
  148.       // Construct the message to send over , here
  149.       for(int j = 0; j < ROW; j++){
  150.         for(int k = 0; k < COL; k++){
  151.           Serial.print("j: ");
  152.           Serial.print(j);
  153.           Serial.print(" - dCO2: ");
  154.           Serial.print(dCO2[j]);
  155.           Serial.print(" - dtVOC: ");
  156.           Serial.println(dtVOC[j]);
  157.           mqtt_msg[j][0] = 5; //dCO2[j];
  158.           mqtt_msg[j][1] = 10; //dtVOC[j];
  159.           Serial.print("CO2 [");
  160.           Serial.print(mqtt_msg[j][0]);
  161.           Serial.print("] -- tVOC [");
  162.           Serial.print(mqtt_msg[j][1]);
  163.           Serial.println("]");
  164.         }
  165.       }
  166.       // Reset the variables and start over
  167.       memset(dCO2, 0, sizeof(dCO2));
  168.       memset(dtVOC, 0, sizeof(dtVOC));
  169.       i = 0;
  170.     }
  171.     i++;
  172.   }
  173.   delay(2000);
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement