Sigfox Smart Building

Sigfox Smart Building © CC BY

Using MKRFox1200 and a few sensors.

  • 1,261 views
  • 0 comments
  • 8 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)

Apps and online services

About this project

I assume for 3 floor Building, I am going listing all components which you need to make smart building.

1. HVAC System : Using LM35 sensor get data of temperature and send data to database.

2. Parking System : Using ultra sonic sensor get data of is parking space is empty or not and send that data to database.

3. Water Plant System : Using Soil moisture sensor get data and send that data to database.

4. Light System : Using LDR sensor get data and send data to database.

5. Water Tank Level : Using ultrasonic sensor, It will detect the depth of water tank and send that data to database.

6. Automatic Street Light : Using LDR, PIR and Relay, When it will detect low light and some motion than and than it will turn on light.

7. Fire Safety : Using MQ5 sensor, When it will detect fire it will automatically turn on water pump and start water lines.

8. Wind Speed : Using wind speed sensor we get data and send that data to database.

Advantage :

1. Lower Cost in electricity

2. Environment Friendly

3. Low Maintains Cost

Let's Get Started.

How to setup first time MKRFOX1200 ?

Step1 : Arduino

1. Install the Arduino IDE

2. Install the MKRFox1200 Arduino core from the "Boards Manager". You can follow this tutorial for help.

3. Now you can select the MKRFox1200 board

4. Install the following libraries (you can use the Arduino Library Manager):

Library : 
1. https://github.com/arduino-libraries/SigFox
2. https://github.com/arduino-libraries/ArduinoLowPower

5. Connect your board to your computer and flash the MRKFox1200 example named "FirstConfiguration" or below Code :

#include<SigFox.h>

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while(!Serial);
SigFox.begin();
Serial.print("ID : ");
Serial.println(SigFox.ID());
Serial.print("PAC : ");
Serial.println(SigFox.PAC());
}

void loop() {
// put your main code here, to run repeatedly:
}

6. Open the monitor terminal and write down the ID and PAC

NB: When flashing the MKR, it may be in "deep sleep" which causes a non-detection of the COM port. In order to wake it up, you need to tap twice on the "RST" button and the board will be redetected by your PC.

Step 2 : Sigfox Backend

Now that your MKRFox1200 is sending messages, we need to retrieve them and redirect them where we wish to. In order to do this, let's go to the Sigfox Backend service!

1. Go to the Sigfox Backend activation platform

2. Activate your device by selecting Arduino > Your country > Enter the ID and PAC previously written in step 1 part 5.

3. Create an account and validate

It's as simple as that, your MKRFox1200 is now linked to Sigfox Backend.

Step 3 : Check Device

Let's check if everything is going right. Go back here.

In the "DEVICE" tab click on the Id of your "Arduino Sigfox Kit" (MKRFox1200).

The "MESSAGES" tab shows the messages sent by your board.

If there is at least one message after rebooting your MKRFox1200, consider this step as a success ! ;)

If not, make sure the code is well uploaded on the MKRFox1200 and that you did not mistaken the input of the device ID and PAC. Also, make sure an antenna is plugged in correctly so it can reach a Sigfox base station.

Step 4 : thethings.iO

Now that messages are well sent over the Sigfox network, we need a platform to visualize them accordingly. In this tutorial, we are using thethings.iO Sigfox website. Of course, you may use a different platform (check out the other compatible ones with Sigfox here) but thethings.iO is a quick and easy way to start.

Here I am using only one sensor for sending data to sigfox.

Use as many as sensor and MKRFOX 1200 as wanted in our smart building. This is just prototype to How to get data and how to setup whole modules.

Now we can analyze our building on our desktop.

Automatic Street Light :

#include <ArduinoLowPower.h>
#include <SigFox.h>
#include "conversions.h"

int oneshot = true;
typedef struct __attribute__ ((packed)) sigfox_message {
uint8_t status;
int16_t data1;
uint8_t lastMessageStatus;
} SigfoxMessage;

SigfoxMessage msg;

int led = 13; //led as relay (For example use one channel relay)
int sensor = 2;
int state = LOW;
int val = 0;
int ldrPin = A0;
void setup()
{
if (oneshot == true)
{
Serial.begin(115200);
while (!Serial) {}
}
if (!SigFox.begin()) {
reboot();
}

SigFox.end();
if (oneshot == true) {
SigFox.debug();
}
pinMode(led, OUTPUT);
pinMode(sensor, INPUT);
pinMode(ldrPin, INPUT);
Serial.begin(9600);
}
void loop()
{
val = digitalRead(sensor);
int ldrStatus = analogRead(ldrPin);
if (val == HIGH && ldrStatus <= 200)
{
digitalWrite(led, HIGH);
delay(100);
if (state == LOW)
{
Serial.println("Motion detected!");
Serial.print("Its DARK, Turn on the Light");
float status1 = 1.0;
msg.data1 = convertoFloatToUInt16(status1, 2000);
state = HIGH;
}
}
else
{
digitalWrite(led, LOW);
delay(200);
if (state == HIGH)
{
Serial.println("Motion stopped!");
Serial.print("Its BRIGHT, Turn off the Light");
float status1 = 0.0;
msg.data1 = convertoFloatToUInt16(status1, 2000);
state = LOW;
}
}
SigFox.begin();

delay(100);
SigFox.status();
delay(1);
SigFox.beginPacket();
SigFox.write((uint8_t*)&msg, 12);
msg.lastMessageStatus = SigFox.endPacket();
if (oneshot == true) {
Serial.println("Status: " + String(msg.lastMessageStatus));
}
SigFox.end();
if (oneshot == true) {
while (1) {}
}
LowPower.sleep(15 * 60 * 1000);
}

void reboot()
{
NVIC_SystemReset();
while (1);
}

Fire Safety :

#include <ArduinoLowPower.h>
#include <SigFox.h>
#include "conversions.h"

int oneshot = true;

typedef struct __attribute__ ((packed)) sigfox_message {
uint8_t status;
int16_t data1;
uint8_t lastMessageStatus;
} SigfoxMessage;

SigfoxMessage msg;

int sensor=7;
int gas_value;
const int MotorInPin1 = 5;
const int MotorInPin2 = 6;

void setup()
{
if (oneshot == true) {
Serial.begin(115200);
while (!Serial) {}
}

if (!SigFox.begin()) {
reboot();
}

SigFox.end();

if (oneshot == true) {
SigFox.debug();
}

pinMode(MotorInPin1, OUTPUT);
pinMode(MotorInPin2, OUTPUT);
pinMode(sensor,INPUT);
}

void loop()
{
gas_value=digitalRead(sensor);
Serial.println(gas_value);

if(gas_value == 0) // Pump is on
{
float status1 = 1.0;
msg.data1 = convertoFloatToUInt16(status1, 2000);
analogWrite(MotorInPin1, HIGH);
analogWrite(MotorInPin2, LOW);
}

else // Pump is off
{
float status1 = 0.0;
msg.data1 = convertoFloatToUInt16(status1, 2000);
analogWrite(MotorInPin1, LOW);
analogWrite(MotorInPin2, LOW);
}

SigFox.begin();
delay(100);
SigFox.status();
delay(1);
SigFox.beginPacket();
SigFox.write((uint8_t*)&msg, 12);
msg.lastMessageStatus = SigFox.endPacket();

if (oneshot == true) {
Serial.println("Status: " + String(msg.lastMessageStatus));
}
SigFox.end();

if (oneshot == true) {
while (1) {}
}
LowPower.sleep(15 * 60 * 1000);
}

void reboot() {
NVIC_SystemReset();
while (1);
}

Code

MKRFOX1200Arduino
Here just prototype. Use as many as sensor you want. I am using only one-one sensor for getting data.
Sensor :
1. LM35
2. Ultrasonic
3. MQ5
4. DHT22
5. Soil Moisture
#include <ArduinoLowPower.h>
#include <SigFox.h>
#include <DHT.h>;
#include "conversions.h"

// Set oneshot to false to trigger continuous mode when you finisched setting up the whole flow
int oneshot = true;

#define DHTPIN 6     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

int sensorPin1 = A0; 
const int trigPin = 1;
const int echoPin = 2;
int temperature = A1;
const int trigPin1 = 3;
const int echoPin1 = 4;
int sensor=5;
int gas_value;
long duration;
int distance;
int temperatureS;
long duration1;
int distance1;
float hum;


typedef struct __attribute__ ((packed)) sigfox_message {
  uint8_t status;
  int16_t data1;
  int16_t data2;
  int16_t data3;
  int16_t data4;
  int16_t data5;
  int16_t data6;
  int16_t data7;
  uint8_t lastMessageStatus;
} SigfoxMessage;

// stub for message which will be sent
SigfoxMessage msg;

void setup() {

  if (oneshot == true) {
    // Wait for the serial
    Serial.begin(115200);
    while (!Serial) {}
  }

  if (!SigFox.begin()) {
    // Something is really wrong, try rebooting
    // Reboot is useful if we are powering the board using an unreliable power source
    // (eg. solar panels or other energy harvesting methods)
    reboot();
  }

  //Send module to standby until we need to send a message
  SigFox.end();

  if (oneshot == true) {
    // Enable debug prints and LED indication if we are testing
    SigFox.debug();
  }

  dht.begin();
  pinMode(sensorPin1,INPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT); 
  pinMode(temperature, INPUT);
  pinMode(trigPin1, OUTPUT); 
  pinMode(echoPin1, INPUT); 
  pinMode(sensor,INPUT);
 
}

void loop() {
  // Every 15 minutes, read all the sensors and send them
  // Let's try to optimize the data format
  // Only use floats as intermediate representaion, don't send them directly

  //Soil Moisture (Make it more for same another sensor)
  float sensorData1 = analogRead(sensorPin1); 
  msg.data1 = convertoFloatToUInt16(sensorData1, 200000);

  //Parking Status (Make it more for same another sensor)
  int statusP;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance= duration*0.034/2;
  Serial.print("Distance: ");
  Serial.println(distance);
  // set value
  if(distance < 10)
  {
    //Parking slot is not empty
    statusP = 1;
    msg.data2 = convertoFloatToUInt16(statusP, 2000);
  }
  else
  {
    //Parking slot is empty
    statusP = 0;
    msg.data2 = convertoFloatToUInt16(statusP, 2000);
  }

  //HVAC Monitor (Make it more for same another sensor)
  temperatureS = analogRead(temperature);
  float mv1 = ( temperatureS/1024.0)*5000;
  float cel1 = mv1/10;
  float farh1 = (cel1*9)/5 + 32;
  msg.data3 = convertoFloatToUInt16(cel1, 2000);
  msg.data4 = convertoFloatToUInt16(farh1, 2000);

  //Water level of tank (Make it more for same another sensor)
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  distance1 = duration1*0.034/2;
  Serial.print("Distance: ");
  Serial.println(distance1);
  // set value
  if(distance1 < 1000)
  {
    msg.data5 = convertoFloatToUInt16(distance1, 2000);
    Serial.println(distance1);
  }

  //Fire Detection (Make it more for same another sensor)
  gas_value=digitalRead(sensor);
  if(gas_value == 0)
  {
    //Gas detect
    msg.data6 = convertoFloatToUInt16(gas_value, 2000);
    Serial.println("Gas Leakage");
  }

  //Humidity (Make it more for same another sensor)
  hum = dht.readHumidity();
  msg.data7 = convertoFloatToUInt16(hum, 2000);

  
  // Start the module
  SigFox.begin();
  // Wait at least 30ms after first configuration (100ms before)
  delay(100);

  // Clears all pending interrupts
  SigFox.status();
  delay(1);

  SigFox.beginPacket();
  SigFox.write((uint8_t*)&msg, 12);

  msg.lastMessageStatus = SigFox.endPacket();

  if (oneshot == true) {
    Serial.println("Status: " + String(msg.lastMessageStatus));
  }

  SigFox.end();

  if (oneshot == true) {
    // spin forever, so we can test that the backend is behaving correctly
    while (1) {}
  }

  //Sleep for 15 minutes
  LowPower.sleep(15 * 60 * 1000);
}

void reboot() {
  NVIC_SystemReset();
  while (1);
}

Schematics

MKRFOX1200
Mkrfox1200 yza5twvgeu

Comments

Similar projects you might like

Smart Bird Feeder

Project showcase by Team ESIEE-Amiens Students

  • 5,784 views
  • 11 comments
  • 17 respects

Building Smart Cities with Less Sound Pollution

Project tutorial by Team Xspies

  • 3,354 views
  • 0 comments
  • 15 respects

Smart Home - Smart Rules using ARTIK Cloud & Photon

Project tutorial by Raghavendra Ponuganti

  • 4,852 views
  • 3 comments
  • 16 respects

IoT Bird Feeder with Sigfox and Tweeter

Project showcase by Gaël Porté

  • 4,245 views
  • 6 comments
  • 20 respects

The Environment Cube! Know the Land Beneath You using Sigfox

Project tutorial by Dhairya Parikh

  • 3,432 views
  • 0 comments
  • 6 respects
Add projectSign up / Login