SOTM
Published © CC BY-NC

Sensor Array with RedBear Duo and Particle Cloud

Use RedBear Duo to read from a sensor array and report data to the Particle Cloud.

EasyProtip1 hour70
Sensor Array with RedBear Duo and Particle Cloud

Things used in this project

Hardware components

RedBear Duo
×1
Moisture Sensor
×1
830 Reconfigurable Breadboard
×1
Jumper Wire Set
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE

Story

Read more

Schematics

Sensor Array using RedBear

Sensorarray bb sixuzlsrzd

Code

readSensorArray.ino

Arduino
Code to read the Sensor Array and send event to Particle Cloud
// ------------
// Read Moisture Sensors on RedBear and send data to Particle Cloud
//
//      Features:
//          1) Detect number of sensors connected
//          2) Use Sleepmode to go reduce power consumption
//
//      Notes:
//
//          Sensor 0 is on D0/A0, Sensor 1 is on D1/A1 and so on
//          We are not using D7 as this is used by internal LED which we are using indicator that reaading is in progress
//
//          As the code executes quite quickly and goes straight into sleep, you might find it difficult flashing new firmware.
//          Recommend to start off with sleep commented out - you will need to hit reset button to trigger new reading.
//          Sleep is commented out by default (Line 114)
//
//          Alternative is to wait for device to go to sleep, and flash firmware.
//          Then press and hold RESET + SETUP button, the let go of RESET until white or purple light comes back.
//
//      19 December 2018 
//      Author: dante@serveronthemove.com.au
//      Hardware: RedBear Duo
//      Firmware: v0.3.3 - should work with earlier versions
//
//      Sensors: 4 pin Moisture sensor with Analog output
//
//       Wiring:
//          Moisture Sensor -> RedBear Duo
//          GND             -> GND
//          VCC             -> Dx
//          DO              - Not used
//          AO              -> Ax
//
//          For example, using two sensors
//              Sensor 1 uses A0 and D0
//              Sensor 2 uses A4 and D4
//          The code will automatically detect the sensors, however it is important that Analog and Digital are aligned.
// ------------

/*
 declare variables
*/
const int numberOfSensors = 7; // how many sensor are we be scanning
const int settleIv = 2000; // how long to wait between turning sensor on and reading from sensor
const int sensorThreshold = 300; // For connected sensors the value read (when turned on) should be below threshold. Used to detect if sensor connecte
const int secondsSleep = 60; // amount of seconds RedBear Duo should go to sleep

const String seperator = ","; // seperator to be used 

String sensorName = "Patch0/Sensor"; // Sensorname to send to Particle.io Cloud
String sensorReadings = ""; // holds the sensor readings per loop

//
// Sensor Array Configuration
//
int moistureSensorSetup[7][2] = {{D0,A0},{D1,A1},{D2,A2},{D3,A3},{D4,A4},{D5,A5},{D6,A6}}; // Digital (power on/off) and Analogue (read value) couples 


// bug in Particle Cloud for firmware 0.3.3 - LED_BUILTIN is not defined, hence below workaround
// -- workaround start
#ifndef LED_BUILTIN
    #define LED_BUILTIN 7
#endif
// -- workaround end

void setup() {
    // Turn D7/LED_BULTIN on to indicate that reading/processing is taking place. Will be useful when putting setup into case to provide user feedback
    digitalWrite(LED_BUILTIN, HIGH);

    // loop through all sensors and set the PINs up
    for (int i = 0; i < numberOfSensors; i++) {
        // setup D as output to power esnor, and A as input to read the value
        pinMode(moistureSensorSetup[i][0],OUTPUT); // We are setting DX to outout
        // no need to set the ADC's as output - will be done implicit by analogRead()
        
        // test if sensor is connected - (values under the defined threshold when turned off)
        digitalWrite(moistureSensorSetup[i][0],LOW);
        
        // initialise SensorRead    
        int sensorRead = 0;

        // only if Sensor is connected
        if (analogRead(moistureSensorSetup[i][1]) < sensorThreshold) {
            // turn the sensor on, by setting digital pin to high
            digitalWrite(moistureSensorSetup[i][0],HIGH);
            
            // wait for sensor to settle
            delay(settleIv);

            // read sensor value from Analog Port
            sensorRead = analogRead(moistureSensorSetup[i][1]);

            // turn sensor off
            digitalWrite(moistureSensorSetup[i][0],LOW);
        }

        // add a seperator to list if there has been a previous value
        if (sensorReadings != "") {
            sensorReadings = sensorReadings + seperator;
        }            

        // append the sensor reading
        sensorReadings = sensorReadings + String(sensorRead);
    }
    
    // once all sensors have been read, publish sensor values to cloud
    Particle.publish(sensorName, sensorReadings, 3600, PRIVATE);

    // Turn D7/LED_BULTIN off to indicate process is done
    digitalWrite(LED_BUILTIN, LOW);

    // disable line below when developing, as otherwise it will go immediately to sleep post processing and will make
    // flashing difficult.
    //System.sleep(SLEEP_MODE_DEEP, 60);
}

// we are not doing anything in loop as waking from sleep will restart (startup executed again)
void loop() {
    // intentionally empty
}

Credits

SOTM

SOTM

4 projects • 1 follower

Comments

Add projectSign up / Login