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.
FinalWhen 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.
Comments