Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Libraries needed to interface with sensors
- #include <SPI.h>
- #include "RF24.h"
- #include <Wire.h>
- #include <Adafruit_MLX90614.h>
- #include <Adafruit_CCS811.h>
- //NRF Pins
- #define CE_PIN 5
- #define CSN_PIN 6
- //Changing the address of gas sensor for proper I2C communication since initially it has same Address as Temperature Sensor
- #define CCS811_ADDR 0x5B
- //Fire Detector pin only one that has its own discrete pin
- #define FireDetector_PIN 4
- RF24 radio(CE_PIN, CSN_PIN);
- uint8_t address[][6] = { "1Node", "2Node" };
- bool radioNumber; //
- bool role = false; //
- Adafruit_MLX90614 mlx = Adafruit_MLX90614();
- Adafruit_CCS811 ccs;
- struct SensorData { // Setting up how the data will be sent over all together
- float ambientTemp; // Measured in degrees Celsius
- float objectTemp;
- uint16_t CO2; // Unused, but for future projects having it be sent too
- uint16_t TVOC // Measured in parts per billion
- bool flameDetected; // Yes/No do "you", the sensor, see flame
- };
- SensorData data;
- void setup() {
- Serial.begin(115200);
- Wire.begin();
- pinMode(FireDetector_PIN, INPUT);
- if (!mlx.begin()) { // If there's an error with a sensor, it will wait until the error is fixed before it proceeds with setup
- Serial.println("Error: MLX90614.");
- while (1);
- }
- Serial.println("Success MLX90614");
- 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
- Serial.println("Error: CCS811.");
- while (1);
- }
- Serial.println("Success: CCS811 .");
- while (!ccs.available()) {
- delay(100);
- }
- if (!radio.begin()) { //If NRF24L01 Transciever not being noticed, fix before proceeding
- Serial.println("Radio hardware not responding!");
- while (1);
- }
- radio.setAutoAck(true); //Configuring the Transciever
- radio.enableDynamicPayloads();
- radio.setRetries(5, 15);
- radio.setPALevel(RF24_PA_LOW);
- radio.setDataRate(RF24_1MBPS);
- Serial.println(F("This is Radio 1")); //Clarifying which radio is being seen if user has serial monitor open.
- radioNumber = 1;
- radio.openWritingPipe(address[radioNumber]);
- radio.openReadingPipe(1, address[!radioNumber]);
- radio.stopListening();
- }
- void loop() {
- 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
- data.CO2 = ccs.geteCO2();
- data.TVOC = ccs.getTVOC();
- }
- data.ambientTemp = mlx.readAmbientTempC(); //Store the two temperature readings
- data.objectTemp = mlx.readObjectTempC();
- data.flameDetected = (digitalRead(FireDetector_PIN) == LOW); // Store the state of Flame Detector
- // Reports to user what the current data is via Serial Monitor
- Serial.print("Sending - Ambient: "); Serial.print(data.ambientTemp); Serial.print("C, ");
- Serial.print("Object: "); Serial.print(data.objectTemp); Serial.print("C, ");
- Serial.print("CO2: "); Serial.print(data.CO2); Serial.print("ppm, ");
- Serial.print("TVOC: "); Serial.print(data.TVOC); Serial.print("ppb, ");
- Serial.print("Flame: "); Serial.println(data.flameDetected ? "Yes" : "No");
- radio.stopListening();
- bool report = radio.write(&data, sizeof(SensorData)); // Transmits the data out for a Reciever to pickup
- radio.startListening();
- delay(2000); //Waits 2 seconds to collect and then send new data
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement