Project tutorial
SMS alerts for arduino 101 BLE

SMS alerts for arduino 101 BLE © GPL3+

This project is about how to connect arduino 101 with raspberry pi zero w by BLE, and make a program in Node js that sends an SMS alert.

  • 2,526 views
  • 0 comments
  • 10 respects

Components and supplies

Apps and online services

About this project

My idea:

I am currently working with the arduino 101 and raspberry pi zero w boards for a project in a hackster contest, every day I learn more things and I think this information and project will serve the people to start doing iot projects.

Material:

My raspberry pi zero w, a usb-micro usb cable, and a portable battery for mobile.

Arduino 101 board, breadboard, Grove LCD RGB, Grove Shield V3, USB A to , wires and other portable battery for mobile.

Here I leave the tutorial that I realized on how to send data of the arduino 101 to raspberry pi zero w by bluetooth of low energy. Using node js to perform the communication. I'm leaving from here.

https://www.hackster.io/alexis-santiago-allende/arduino-101-connects-with-raspberry-pi-zero-w-63adc0

Arduino code:

I added the libraries to use the LCD screen of the grove kit, as well as the variables to manipulate the color of the LCD screen background. Also add the variable contain to be able to control what is shown on the LCD screen.

#include <Wire.h> 
#include "rgb_lcd.h" 
rgb_lcd lcd; 
const int colorR = 198; 
const int colorG = 78; 
const int colorB = 25; 
long previousMillis = 0;  // last time the battery level was checked, in ms 
boolean conter=LOW; 

Below is the code you use to initialize the LCD grove and also to start it in the selected color.

lcd.begin(16, 2); 
 lcd.setRGB(colorR, colorG, colorB); 

Here is the code to send the data to the raspberry pi zero w. The temperature is in the center and also the code to display the current temperature on the LCD. There is an if function that says that when the temperature is equal or greater than 32 degrees, then the SMS is sent to our cell phone, and this activity is shown by the LCD

void updateSensor(){ 
 int sensorValue = analogRead(A0); 
 float sensorLevel = sensorValue*(3.3/1023); 
 int temp=(sensorLevel-0.5)*100; 
 switchCharacteristic.setValue(temp); 
  lcd.setCursor(0, 1); 
  lcd.print(temp); 
  if(temp>=32 && conter==LOW){ 
  lcd.setCursor(0, 0); 
  lcd.print("Sending SMS"); 
  conter=HIGH; 
   } 
   else if(temp<=30 && conter==HIGH){ 
     lcd.setCursor(0, 0); 
     lcd.print("Temperatura:"); 
     conter=LOW; 
     } 
 } 

Node js:

To use the service and to make calls or text messages, you can register and request a trial account. The service is called Twilio, they are quite accesible and they put many test codes in different programming languages like node js, python, c #, java, etc. I will only use node js in my example.

Install twilio with NPM package

$sudo npm install twilio

After registering, you will get the auth token and the Sid account which are very important because with them you can access the service. Below is how I create an object with the twilio service and then I make two other variables with the auth token and the account Sid

 const twilio = require('twilio') 
 var accountSid = 'AC4c3a664e0475a08a4e0fdbd016555a70';
 var authToken = '22ee6e5fe596967997a2d1a57d6d73eb';  

Then I create a message object where I keep the following data of the message that I will send.

 const sendMessage = () => {  
 phone.messages.create({    
 to: "+526462378678",    
 from: "+12818266123 ",    
 body: 'Here is very hot with: '+temperatura,   }) } 

The code to send the data is the following, it is similar to the one of my previous example:

  ledCharacteristic.read(function(error, data) {             // data is a buffer             console.log('Temperature now is: ' + data.readUInt8(0)); 
       if (data.readUInt8(0)>=32 && contador===0) {  temperatura=data.readUInt8(0);                     sendMessage();  contador=1;  } 
       else if(data.readUInt8(0)<=30 && contador==1){ 
       contador=0;   }  });   }, 1000);   }); 

I print in console the value of the current temperature after the device that emits the data has been connected. Then enter a function that depends on the value of the temperature: if the temperature is higher than 32 degrees, the command to send an SMS to the telephone will be issued. The system restarts when the current temperature is equal to 28 degrees Celsius or less.

Final

When the arduino 101 is connected to the raspberry pi zero w, on the console, the current temperature value is displayed in degrees Celsius.

In parallel the value of the temperature can be observed in the LCD screen connected to the Grove Shield Arduino

When the temperature reaches 32 degrees or more, it changes the message on the screen to "sending message" which is when raspberry pi sends the order that an SMS is sent to my cell phone.

It appears after a few seconds, the notification of a new SMS and I am ready to read it.

Code

Arduino codeArduino
Temperature sensor and Grove LCD
#include <CurieBLE.h>

BLEPeripheral blePeripheral;  // BLE Peripheral Device (the board you're programming)
BLEService ledService("19b10000-e8f2-537e4f6c-d104768a1214"); // BLE LED Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEIntCharacteristic switchCharacteristic("19b10001-e8f2-537e4f6c-d104768a1214",  // standard 16-bit characteristic UUID
    BLERead | BLENotify); 

#include <Wire.h>
#include "rgb_lcd.h"

rgb_lcd lcd;

const int colorR = 198;
const int colorG = 78;
const int colorB = 25;
long previousMillis = 0;  // last time the battery level was checked, in ms
boolean conter=LOW;

void setup() {
  Serial.begin(9600);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.setRGB(colorR, colorG, colorB);
  // Print a message to the LCD.
  lcd.print("Temperatura:");

  // set advertised local name and service UUID:
  blePeripheral.setLocalName("LED");
  blePeripheral.setAdvertisedServiceUuid(ledService.uuid());

  // add service and characteristic:
  blePeripheral.addAttribute(ledService);
  blePeripheral.addAttribute(switchCharacteristic);

  // set the initial value for the characeristic:
  switchCharacteristic.setValue(0);

  // begin advertising BLE service:
  blePeripheral.begin();

  Serial.println("BLE LED Peripheral");
}

void loop() {
  // listen for BLE peripherals to connect:
  BLECentral central = blePeripheral.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
         long currentMillis = millis();
      // if 200ms have passed, check the battery level:
      if (currentMillis - previousMillis >= 1000) {
        previousMillis = currentMillis;
        updateSensor();
      }

     
      }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

void updateSensor(){

  int sensorValue = analogRead(A0);
  float sensorLevel = sensorValue*(3.3/1023);
  int temp=(sensorLevel-0.5)*100;
  switchCharacteristic.setValue(temp);
   lcd.setCursor(0, 1);
   lcd.print(temp);
   if(temp>=32 && conter==LOW){
   lcd.setCursor(0, 0);
   lcd.print("Sending SMS");
   conter=HIGH;
    }
    else if(temp<=30 && conter==HIGH){
      lcd.setCursor(0, 0);
      lcd.print("Temperatura:");
      conter=LOW;
      }
  }
Node js code JavaScript
SMS alert and bluetooth low energy connection
var noble = require('noble');
// Search only for the Service UUID of the device (remove dashes)
var serviceUuids = ['19b10000e8f2537e4f6cd104768a1214'];
// Search only for the led charateristic
var characteristicUuids = ['19b10001e8f2537e4f6cd104768a1214'];

const twilio = require('twilio')
const debounce = require('debounce')
 
var accountSid = 'AC4c3a664e0475a08a4e0fdbd016555a70'; 
var authToken = '22ee6e5fe596967997a2d1a57d6d73eb'; 

const phone = new twilio(accountSid, authToken);
var temperatura = 0;
var contador= 0;

const sendMessage = () => {
  phone.messages.create({
    to: "+526462378678",
    from: "+12818266123 ",
    body: 'Here is very hot with: '+temperatura, 
  }) 
}

// start scanning when bluetooth is powered on
noble.on('stateChange', function(state) {
  if (state === 'poweredOn') {
    noble.startScanning(serviceUuids);
  } else {
    noble.stopScanning();
  }
});

// Search for BLE peripherals
noble.on('discover', function(peripheral) {
  peripheral.connect(function(error) {
    console.log('connected to peripheral: ' + peripheral.uuid);
    // Only discover the service we specified above
    peripheral.discoverServices(serviceUuids, function(error, services) {
      var service = services[0];
      console.log('discovered service');

      service.discoverCharacteristics(characteristicUuids, function(error, characteristics) {
        console.log('discovered characteristics');
        // Assign Characteristic
        var ledCharacteristic = characteristics[0];
        
        setInterval(function() {
           
            ledCharacteristic.read(function(error, data) {
            // data is a buffer
            console.log('Temperature now is: ' + data.readUInt8(0));
            if (data.readUInt8(0)>=32 && contador===0) {
                    temperatura=data.readUInt8(0);
                    sendMessage();
                    contador=1;
                  }
            else if(data.readUInt8(0)<=30 && contador==1){
                      contador=0;
                  
                   }
                });
            
        }, 1000);

      });
    });
  });
});

Schematics

Figure 1
Vegas3 lyolt2pmey

Comments

Similar projects you might like

Arduino 101 BLE App

Project in progress by Alexis Santiago Allende

  • 10,795 views
  • 26 comments
  • 51 respects

Arduino 101 Connects with Raspberry Pi Zero W

Project tutorial by Alexis Santiago Allende

  • 8,195 views
  • 0 comments
  • 17 respects

Eco-Thing #1 "Eco-Smart-Container V1.0"

Project tutorial by Alexis Santiago Allende

  • 5,305 views
  • 4 comments
  • 28 respects

Arduino 101 Home BLE System

Project tutorial by Alexis Santiago Allende

  • 2,424 views
  • 0 comments
  • 11 respects

My First Arduino 101 Car Controlled by BLE

Project tutorial by Alexis Santiago Allende

  • 1,827 views
  • 0 comments
  • 9 respects

Arduino/Genuino 101 BLE Thermometer With TMP102 and Blynk

Project tutorial by Konstantin Dimitrov

  • 7,877 views
  • 1 comment
  • 26 respects
Add projectSign up / Login