Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Dan Tudose
Published © GPL3+

Home Monitoring With Wireless Sensor Nodes

Monitor your home using this powerful tutorial. Only two components needed: a Raspberry Pi and a Sparrow Sensor Node Kit.

IntermediateFull instructions provided3 hours28,213
Home Monitoring With Wireless Sensor Nodes

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Sparrow Wireless Sensor Node Kit
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
Optional.
×1

Software apps and online services

DeviceHub Cloud Service - Free package
DeviceHub.net DeviceHub Cloud Service - Free package

Story

Read more

Schematics

Project Diagram

Remote sensor & PIR connection diagram

This diagram shows how to connect the PIR sensor to the Sparrow Nest programming board

Code

Python Code for DeviceHub

Python
This code runs on RaspberryPi
from devicehub import Sensor, Device, Project
from time import sleep
from random import randint
import serial

PROJECT_ID      = 'your Project ID'
DEVICE_UUID     = 'your Device UUID'
API_KEY         = 'your API Key'

SENSOR_NAMES    = ['Temperature', 'Humidity', 'Pressure', 'Light', 'IR', 'UV', 'Remote_Presence', 'Remote_Temperature']
SENSOR_VALUES   = []
SENSORS         = []

def add_sensor(type, name, dev):
        AN = Sensor(type, name)
        dev.addSensor(AN)
        return AN

def analog_input(dev, sensor, value):
        sensor.addValue(float(value))
        dev.send()
        return


ser=serial.Serial('/dev/ttyUSB0', 9600)
ser.flushInput()
ser.flushOutput()
line = ser.readline()
line = ser.readline()
print 'first', line

project = Project(PROJECT_ID, ssl_verify=False)
device = Device(project, DEVICE_UUID, API_KEY)

for s in SENSOR_NAMES:
        num = add_sensor(Sensor.ANALOG, s, device)
        SENSORS.append(num)


while True:
        try:

                SENSOR_VALUES = []
                line = ser.readline()
                print line
                print len(line)
                fields = line[:-1].split(',')
                #if( len(line) > 35 and fields != None):
                if(fields != None):
                        for f in fields:
                                SENSOR_VALUES.append(f)

                        for i in range(0, 8):
                                print SENSOR_NAMES[i], SENSOR_VALUES[i]
                                analog_input(device, SENSORS[i], SENSOR_VALUES[i])
                        print 'Data Sent OK!'

        except:
                print 'Something went wrong, retrying...'
        sleep(8.0)

CSV code for the Sparrow node

C/C++
This code runs on the Sparrow node attached to the RasperryPi
#include <Wire.h>
#include "SI1145.h"
#include <SHT2x.h>
#include <BaroSensor.h>
#include "SparrowTransfer.h"
 
//create object
SparrowTransfer ST; 
 
struct RECEIVE_DATA_STRUCTURE{
  //put your variable definitions here for the data you want to receive
  //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
  uint8_t presence;
  float temperature;
 
};
//give a name to the group of data
RECEIVE_DATA_STRUCTURE mydata;

struct SENSOR_DATA{
  //we store here sensor readings
  float temperature;
  float humidity;
  float pressure;
  float light;
  float IR;
  float UV; 
  uint8_t remote_presence; 
  float remote_temp;
};

SENSOR_DATA node;

int controlPin = 7; // control pin for all sensors on the node
int greenLedPin = 11; // green LED

Adafruit_SI1145 uv = Adafruit_SI1145();

int getSensorData(){ //reads all sensors
  node.temperature = SHT2x.GetTemperature(); //temperature in degrees C
  node.humidity = SHT2x.GetHumidity(); //humidity in RH
  node.pressure = BaroSensor.getPressure(); //pressure in mbar
  node.light = uv.readVisible(); //visible light 
  node.IR = uv.readIR(); //infrared light level
  node.UV = uv.readUV() / 100.0; //UV index

  return 1;
}

void blinkLED() //blinks the green LED
{
  digitalWrite(greenLedPin,LOW);
  delay(20);
  digitalWrite(greenLedPin,HIGH);  
}

void sendSensorData(){ //sends sensor data over the serial interface as CSV
  Serial.print(node.temperature); //temperature in degrees C
  Serial.print(",");
  Serial.print(node.humidity); //humidity in RH
  Serial.print(",");
  Serial.print(node.pressure); //pressure in mbar
  Serial.print(",");
  Serial.print(node.light); //visible light 
  Serial.print(",");
  Serial.print(node.IR); //infrared light level
  Serial.print(",");
  Serial.print(node.UV); //UV index
  Serial.print(",");
  Serial.print(node.remote_presence); // presence from the remote sensor
  Serial.print(",");
  Serial.println(node.remote_temp); // temperature from the remote sensor
}

void uvInit(){ // initializes the Si1145 light sensor
  if (! uv.begin()) { //no luck first time, try to initialize again
    Serial.println("Didn't find Si1145");
    digitalWrite(controlPin, HIGH);
    delay(100);
    digitalWrite(controlPin, LOW);
    delay(1000);
    if (! uv.begin()) while(1); //the light sensor could not be initialized
  }
}

void setup() {

  //start the library, pass in the data details  
  ST.begin(details(mydata));
  
  pinMode(controlPin, OUTPUT);  //sensor on/off control
  pinMode(greenLedPin, OUTPUT);      // this is where the red LED on the node is connected
  
  digitalWrite(controlPin, LOW); // enable all sensors
  delay(1000); //wait for things to settle
  
  Serial.begin(9600);
  
  uvInit(); // init light sensor
  
  BaroSensor.begin(); // init barometric sensor
}


void loop() {
  if(ST.receiveData()){ //check remote sensor readings
    node.remote_presence = mydata.presence;
    node.remote_temp = mydata.temperature;
  }
  
  if(getSensorData()){
    sendSensorData();
    blinkLED();
  }
  delay(1000);

}

Remote Sensor Code

C/C++
This code runs on the remote wireless sensor node.
#include "SparrowTransfer.h"
#include <Wire.h>
#include <SHT2x.h>

#define DEBUG 1

//create object
SparrowTransfer ST; 
 
struct SEND_DATA_STRUCTURE{
  //put your variable definitions here for the data you want to send
  //THIS MUST BE EXACTLY THE SAME ON THE OTHER SPARROW
  uint8_t presence;
  float temperature;
};
 
//give a name to the group of data
SEND_DATA_STRUCTURE mydata;

int redLedPin = 8;              // red LED
int greenLedPin = 11;           // green LED
int inputPin = 18;              // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int controlPin = 7;             // sensor on/off control
 
uint8_t detect(){
  int val = digitalRead(inputPin);  // read PIR input value
  if (val == HIGH) {            // check if the input is HIGH
      digitalWrite(redLedPin, LOW);  // turn LED ON
      return 1;
    }
  else {
      digitalWrite(redLedPin, HIGH); // turn LED OFF
      return 0;
    }
}

void blinkLED() //blinks the green LED
{
  digitalWrite(greenLedPin,LOW);
  delay(20);
  digitalWrite(greenLedPin,HIGH);  
}
 
void setup(){
 
  pinMode(greenLedPin, OUTPUT);      // declare green LED as output
  pinMode(redLedPin, OUTPUT);        // declare red LED as output
  pinMode(inputPin, INPUT);          // declare sensor as input
  pinMode(controlPin, OUTPUT);       // declare sensor control pin as output

  digitalWrite(controlPin, LOW);     // turn on sensors
  delay(1000);
  Wire.begin();
  
  //start the library, pass in the data details
  ST.begin(details(mydata));
  mydata.presence = 0;
  mydata.temperature = -1.23; //default value to test if something is wrong
 
  digitalWrite(greenLedPin, HIGH); //start with all LEDs off
  digitalWrite(redLedPin, HIGH); 
  
  #ifdef DEBUG //only for local debugging purposes
    Serial.begin(9600);
  #endif

}
 
void loop(){
 
  mydata.presence = detect();
  mydata.temperature = SHT2x.GetTemperature();

  #ifdef DEBUG
    Serial.println(mydata.presence);
    Serial.println(mydata.temperature);
  #endif
  
  ST.sendData();
  blinkLED();
  delay(1000);
}

Home Monitor Github

Main repo

Credits

Dan Tudose
6 projects • 34 followers

Comments