PIR alarm sensor

PIR alarm sensor

This project details a basic PIR sensor to be used in a cloud alarm system.

Material

Hardware

  • Arduino Uno
  • hellothing NB-IoT shield
  • Arduino PIR sensor

Steps process

Step 1: 

Assemble the hardware

Connect the shield to the Arduino.
The PIR sensor works with 5V. Wire the PIR sensor to the shield by connecting the pin on the right(RED) to 5V, the pin on the left(BLACK) to GND and the pin in the middle(YELLOW) to pin 2 of the Arduino.

Step 2: 

Add sketch definitions

Add the code below to the top of the Arduino sketch.

Include the hellothing_BG96_NBIoT library.
Define the constant variables.
Add the function prototypes for the interrupt service, the sensor reading and the connection with the platform.
Create an instance of the communication layer.
Define all the global variables.

#include "hellothing_BG96_NBIoT.h" #define INPUT_SIZE 50 #define DEBUG_BAUD_RATE 19200 #define PIR 2 bool sensorValue(void); bool sendMessage(bool data_message); void pirISR(void); NBIoT *BG96_NBIoT = new NBIoT(NB_IOT); char input[INPUT_SIZE + 1]; bool triggered = false; uint8_t i; uint32_t time = 0; uint32_t sleep = 300000;

Step 3: 

Setup the pins

The code is added to the setup() function of the sketch.

The code below sets up all of the pins in the right configuration.
The analog reference is set to the external reference for the temperature sensor.
The modem pins is set to enable the power and serial communication.
The PIR pin(2) is set up as an input and an interrupt is attached to the pin to detect when the sensor picks up movement.

analogReference(EXTERNAL); pinMode(MDM_PWR_EN, OUTPUT); pinMode(MDM_RX, INPUT); pinMode(MDM_TX, OUTPUT); pinMode(PIR, INPUT); attachInterrupt(digitalPinToInterrupt(PIR), pirISR, RISING);

Step 4: 

Setup the modem

The code is added to the setup() function of the sketch.

Start the debug serial port on the defined baud rate.
Power up the modem.
Initialise the modem and specify the network parameters.
Get the device IMEI, IMSI and ICCID and send it to the platform.

Serial.begin(DEBUG_BAUD_RATE); Serial.println(F("BG96 NB-IoT PIR example!")); BG96_NBIoT->modemPowerUp(); BG96_NBIoT->modemInit(); BG96_NBIoT->setAPN("nbiot.vodacom.za"); BG96_NBIoT->setOperator("65501"); BG96_NBIoT->setExtConfig(LTE_B8); delay(3000); strcpy(BG96_NBIoT->imei, BG96_NBIoT->getIMEI()); strcpy(BG96_NBIoT->imsi, BG96_NBIoT->getIMSI()); strcpy(BG96_NBIoT->iccid, BG96_NBIoT->getICCID()); sendMessage(false);

Step 5: 

Add sensorValue function

The function reads the temperature and the state of the PIR sensor and sends it to the platform.

bool sensorValue(void) { char result[8]; dtostrf(BG96_NBIoT->getTemp(), 6, 2, result); BG96_NBIoT->JsonDoc["temp"] = result; dtostrf(digitalRead(PIR), 1, 0, result); BG96_NBIoT->JsonDoc["pir"] = result; serializeJson(BG96_NBIoT->JsonDoc, input); BG96_NBIoT->JsonDoc.clear(); return BG96_NBIoT->sendData(input); }

Step 6: 

Add sendMessage function

The function creates the TCP/IP connection to the platform.
It sends either the device IDs or the signal and sensor details.
After sending the data the connection is closed.

bool sendMessage(bool data_message) { if (BG96_NBIoT->getNetworkAttach()) { for (i = 0; i < 3; i++) { if (BG96_NBIoT->openConnection("thingcola.hellothing.com", "30001")) { BG96_NBIoT->JsonDoc["id"] = BG96_NBIoT->imei; serializeJson(BG96_NBIoT->JsonDoc, input); BG96_NBIoT->JsonDoc.clear(); BG96_NBIoT->sendData(input); if (data_message) { BG96_NBIoT->sendSignalDetails(); sensorValue(); } else { BG96_NBIoT->sendDeviceID(); } break; } } BG96_NBIoT->closeConnection(); } }

Step 7: 

Add interrupt service

The interrupt service sets the variable to true whenever the PIR detects movement.

void pirISR(void) { triggered = true; }

Step 8: 

Add the main loop

The main loop is idle until the timer expires or the interrupt is triggered.
The timer resets whenever data is send to the platform.

void loop() { if (millis() - time > sleep) { time = millis(); sendMessage(true); } else if (triggered) { time = millis(); triggered = false; sendMessage(true); } digitalWrite(LED1, !digitalRead(PIR)); }

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top