Advertisement
Brendan_Bode

Sensing Unit Code

May 9th, 2025 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.43 KB | None | 0 0
  1. //Libraries needed to interface with sensors
  2. #include <SPI.h>
  3. #include "RF24.h"
  4. #include <Wire.h>
  5. #include <Adafruit_MLX90614.h>
  6. #include <Adafruit_CCS811.h>  
  7.  
  8. //NRF Pins
  9. #define CE_PIN 5
  10. #define CSN_PIN 6
  11. //Changing the address of gas sensor for proper I2C communication since initially it has same Address as Temperature Sensor
  12. #define CCS811_ADDR 0x5B
  13. //Fire Detector pin only one that has its own discrete pin
  14. #define FireDetector_PIN 4
  15.  
  16. RF24 radio(CE_PIN, CSN_PIN);
  17.  
  18. uint8_t address[][6] = { "1Node", "2Node" };
  19.  
  20. bool radioNumber;  //
  21. bool role = false; //
  22.  
  23. Adafruit_MLX90614 mlx = Adafruit_MLX90614();
  24. Adafruit_CCS811 ccs;
  25.  
  26. struct SensorData {   // Setting up how the data will be sent over all together
  27.   float ambientTemp;  // Measured in degrees Celsius
  28.   float objectTemp;
  29.   uint16_t CO2;       // Unused, but for future projects having it be sent too
  30.   uint16_t TVOC       // Measured in parts per billion
  31.   bool flameDetected; // Yes/No do "you", the sensor, see flame
  32. };
  33.  
  34. SensorData data;
  35.  
  36. void setup() {
  37.  
  38.   Serial.begin(115200);
  39.   Wire.begin();  
  40.   pinMode(FireDetector_PIN, INPUT);
  41.  
  42.  
  43.  if (!mlx.begin()) { // If there's an error with a sensor, it will wait until the error is fixed before it proceeds with setup
  44.  
  45.     Serial.println("Error: MLX90614.");
  46.     while (1);
  47.   }
  48.   Serial.println("Success MLX90614");
  49.  
  50.   if (!ccs.begin(CCS811_ADDR)) { // If there's an error with a sensor, it will wait until the error is fixed before it proceeds with setup
  51.  
  52.    Serial.println("Error: CCS811.");
  53.     while (1);
  54.   }
  55.   Serial.println("Success: CCS811 .");
  56.  
  57.   while (!ccs.available()) {
  58.     delay(100);
  59.   }
  60.   if (!radio.begin()) { //If NRF24L01 Transciever not being noticed, fix before proceeding
  61.     Serial.println("Radio hardware not responding!");
  62.     while (1);
  63.   }
  64.  
  65.   radio.setAutoAck(true);         //Configuring the Transciever
  66.   radio.enableDynamicPayloads();
  67.   radio.setRetries(5, 15);
  68.   radio.setPALevel(RF24_PA_LOW);
  69.   radio.setDataRate(RF24_1MBPS);
  70.   Serial.println(F("This is Radio 1")); //Clarifying which radio is being seen if user has serial monitor open.
  71.   radioNumber = 1;
  72.   radio.openWritingPipe(address[radioNumber]);
  73.   radio.openReadingPipe(1, address[!radioNumber]);
  74.   radio.stopListening();
  75.  
  76. }
  77.  
  78. void loop() {
  79.  
  80.     if (ccs.available() && !ccs.readData()) {  // If data is available, and there wasn't an error while reading it, get the data and store it in data struct
  81.       data.CO2 = ccs.geteCO2();
  82.       data.TVOC = ccs.getTVOC();
  83.     }
  84.     data.ambientTemp = mlx.readAmbientTempC();  //Store the two temperature readings
  85.     data.objectTemp = mlx.readObjectTempC();
  86.     data.flameDetected = (digitalRead(FireDetector_PIN) == LOW); // Store the state of Flame Detector
  87.  
  88.     // Reports to user what the current data is via Serial Monitor
  89.     Serial.print("Sending - Ambient: "); Serial.print(data.ambientTemp); Serial.print("C, ");
  90.     Serial.print("Object: "); Serial.print(data.objectTemp); Serial.print("C, ");
  91.     Serial.print("CO2: "); Serial.print(data.CO2); Serial.print("ppm, ");
  92.     Serial.print("TVOC: "); Serial.print(data.TVOC); Serial.print("ppb, ");
  93.     Serial.print("Flame: "); Serial.println(data.flameDetected ? "Yes" : "No");
  94.  
  95.     radio.stopListening();
  96.     bool report = radio.write(&data, sizeof(SensorData)); // Transmits the data out for a Reciever to pickup
  97.     radio.startListening();
  98.     delay(2000); //Waits 2 seconds to collect and then send new data
  99.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement