Alexis Santiago Allende
Published © GPL3+

SMS alerts for arduino 101 BLE

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.

IntermediateFull instructions provided3 hours5,060
SMS alerts for arduino 101 BLE

Things used in this project

Story

Read more

Schematics

Figure 1

Code

Arduino code

Arduino
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);

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

Credits

Alexis Santiago Allende

Alexis Santiago Allende

0 projects • 75 followers
Im a person who since young feel a passion for electronics, I also like to cook pizza and travel. Now Im working on the internet of things

Comments