How to create a high quality project Sony Spresense Board & Extension Board has arrived. Thanks for the product and quick delivery. Package photos are below.
Linux Ubuntu requires the user to be a member of the dialout group. Adding user to dialout groups is executed with the following command in the terminal:
$ sudo usermod -a -G dialout $USER
- Log out and log in again to use the new settings. Now you can connect the Spresense board to PC via the USB cable.
1. Install Spresense Arduino Library
- The Spresense Arduino Library is now installed.
2. Install Bootloader
3. Build your first sketch to verify installation
Step 1: Sony Spresense Board (Scheme and description)- This system is designed to provide security of house and property. Below we will see that the basis for creating this system is the Sony Spresense Board. All sensors that belong to this project are connected to the Sony Spresense board.
- Stepper motor is used to turn the camera in order to gain greater transparency. If the Sony Camera module is used, then the Sony Spresense Board should be placed on the axis of the Stepper motor so that the camera can have the right purpose. If another camera is used, the Sony Spresense Board can have a place somewhere in the house. The owner would thus have the ability to easily restart the system.
- Sensors are set up by the owners of the property. Their distance from the Sony Spresense Board has not been determined.
- The speaker is placed inside the house where alerts can be easily heard. The speaker connection is show in the fifth step.
The code that is update on the Sony Spresense Board is listed in the attachments with name sony_spresense_board.ino. Parts of code for elements are listed in other steps. All the code sections listed in the following steps are used in sony_spresense_board.ino.
- Stepper motor in this project serves to rotate security camera. It will constantly rotate the camera. The radius of movement is 60°.
- PIR sensor (IR PIR Motion Sensor Detector Module HC-SR501) serves to detect movement in front of the house. When it detects motion, a message will be sent to the Android application and turn on the lights front of home (Relay and bulb).
- Distance sensor (Ultrasonic Module HC-SR04) serves the distance sensor serves to inform the owner of the house via the Android application that someone is outside the door and how far away (meters).
- Microphone module serves to alert the owner if someone is speaking in the vicinity of his home.
- Security camera (missing) serves to send live video to android application. It can also be a Sony Camera module.
- NodeMCU serves to send data in Google Firebase. (with Android application)
When the microphone detects the sound, changes will be read and will be set as mic_data = 1. If there is no change in the sound sensor then the value will be mic_data = 0.
int mic_pin = 8, led_diode = 2;
void setup() {
Serial.begin(9600);
pinMode(mic_pin, INPUT);
pinMode(led_diode, OUTPUT);
}
void loop() {
boolean mic_data = digitalRead(mic_pin); //read MIC sensor data
if (mic_data == 0) //check activity
{
digitalWrite(led_diode, LOW); }
else
{
digitalWrite(led_diode, HIGH);
}
}
We can see that when the sound is detected, the LED will light up.
Step 3: Sound warning- I will connect the speakers to the Sony Spresense board. So I wil be able to make sound warning. Warnings will be saved to SD card as ".mp3" format. Depending on which sensor is activated, ".mp3" files will also be activated. This will be in the house.
- If PIR sensor activate, also activate "Sound1.mp3" file which reads: "Somebody is moving front of the house.".
- If Distance sensor activate, also activated "Sound2.mp3" file which reads: "Someone is in front of the door.".
- If Microphone activate, also activated "Sound3.mp3" file which reads: "Someone talks in front of the house.".
- This can be seen/heard on video for testing, which is indicated in Step 7.
- Installing mp3 player:
- Format the SD card as FAT.
- Insert the card into the Sony Spresense Board and connect board to your PC.
- Upload mp3_dec_installer.ino(Arduino IDE > Files > Examples > Audio > dsp_installer > mp3_dec_installer.ino).
- After upload, open Serial Monitor(Tools > Serial Monitor) check 115200 baud and on the question printed on the screen, select 1.
- In the same way, load mp3_enc_installer.ino.
- Insert the Sound.mp3 file to SD card.
- Now upload player.ino (File > Examples > Audio > application > player) on the Sony Spresense Board, connect headphones and restart the board to listen.
- player.ino folder is shown below:
#include <SDHCI.h>
#include <Audio.h>
SDClass theSD;
AudioClass *theAudio;
File myFile;
bool ErrEnd = false;
/**
* @brief Audio attention callback
*
* When audio internal error occurc, this function will be called back.
*/
static void audio_attention_cb(const ErrorAttentionParam *atprm)
{
puts("Attention!");
if (atprm->error_code >= AS_ATTENTION_CODE_WARNING)
{
ErrEnd = true;
}
}
/**
* @brief Setup audio player to play mp3 file
*
* Set clock mode to normal <br>
* Set output device to speaker <br>
* Set main player to decode stereo mp3. Stream sample rate is set to "auto detect" <br>
* System directory "/mnt/sd0/BIN" will be searched for MP3 decoder (MP3DEC file)
* Open "Sound.mp3" file <br>
* Set master volume to -16.0 dB
*/
void setup()
{
// start audio system
theAudio = AudioClass::getInstance();
theAudio->begin(audio_attention_cb);
puts("initialization Audio Library");
/* Set clock mode to normal */
theAudio->setRenderingClockMode(AS_CLKMODE_NORMAL);
/* Set output device to speaker with first argument.
* If you want to change the output device to I2S,
* specify "AS_SETPLAYER_OUTPUTDEVICE_I2SOUTPUT" as an argument.
* Set speaker driver mode to LineOut with second argument.
* If you want to change the speaker driver mode to other,
* specify "AS_SP_DRV_MODE_1DRIVER" or "AS_SP_DRV_MODE_2DRIVER" or "AS_SP_DRV_MODE_4DRIVER"
* as an argument.
*/
theAudio->setPlayerMode(AS_SETPLAYER_OUTPUTDEVICE_SPHP, AS_SP_DRV_MODE_LINEOUT);
/*
* Set main player to decode stereo mp3. Stream sample rate is set to "auto detect"
* Search for MP3 decoder in "/mnt/sd0/BIN" directory
*/
err_t err = theAudio->initPlayer(AudioClass::Player0, AS_CODECTYPE_MP3, "/mnt/sd0/BIN", AS_SAMPLINGRATE_AUTO, AS_CHANNEL_STEREO);
/* Verify player initialize */
if (err != AUDIOLIB_ECODE_OK)
{
printf("Player0 initialize error\n");
exit(1);
}
/* Open file placed on SD card */
myFile = theSD.open("Sound.mp3");
/* Verify file open */
if (!myFile)
{
printf("File open error\n");
exit(1);
}
printf("Open! %d\n",myFile);
/* Send first frames to be decoded */
err = theAudio->writeFrames(AudioClass::Player0, myFile);
if ((err != AUDIOLIB_ECODE_OK) && (err != AUDIOLIB_ECODE_FILEEND))
{
printf("File Read Error! =%d\n",err);
myFile.close();
exit(1);
}
puts("Play!");
/* Main volume set to -16.0 dB */
theAudio->setVolume(-160);
theAudio->startPlayer(AudioClass::Player0);
}
/**
* @brief Play stream
*
* Send new frames to decode in a loop until file ends
*/
void loop()
{
puts("loop!!");
/* Send new frames to decode in a loop until file ends */
int err = theAudio->writeFrames(AudioClass::Player0, myFile);
/* Tell when player file ends */
if (err == AUDIOLIB_ECODE_FILEEND)
{
printf("Main player File End!\n");
}
/* Show error code from player and stop */
if (err)
{
printf("Main player error code: %d\n", err);
goto stop_player;
}
if (ErrEnd)
{
printf("Error End\n");
goto stop_player;
}
/* This sleep is adjusted by the time to read the audio stream file.
Please adjust in according with the processing contents
being processed at the same time by Application.
*/
usleep(40000);
/* Don't go further and continue play */
return;
stop_player:
sleep(1);
theAudio->stopPlayer(AudioClass::Player0);
myFile.close();
exit(1);
}
Step 4: Stepper motorStepper motor in this project serves to rotate security camera. It will constantly rotate the camera. The radius of movement is 60°. Testing stepper motor is shown in the pictures below.
- IN1, IN2, IN3, IN4 are pins for programming the stepper motor. "d" is the delay that occurs between commands. "for" is used to create an angle that the motor will go in one direction. for the value for (int I = 0; i<150; i++), this is the angle of 90°. The first for the loop achieves that the engine rotates in one direction, while the other for the loop achieves the return to the starting position.
int IN1 = 3, IN2 = 5, IN3 = 6, IN4 = 9, d = 10;
void setup() {
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void loop() {
for (int i=0; i<150; i++) {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
delay(d);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
delay(d);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(d);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(d);
}
for (int i=0; i<150; i++) {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(d);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(d);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
delay(d);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
delay(d);
}
}
Step 5: PIR sensor- If the pir sensor detect motion, the value of the variable pir_data will be 1 (pir_data = 1) and the LED will turn on. If there is no detect motion the value of variable will be 0 (pir_data = 0) and the LED will turn off.
int pir_sensor_pin = 13, light_1 = 2;
void setup() {
Serial.begin(9600);
pinMode(pir_sensor_pin, INPUT);
pinMode(light_1, OUTPUT);
}
void loop() {
boolean pir_data = digitalRead(pir_sensor_pin); //read PIR sensor data
if (pir_data == 0) //check activity
{
digitalWrite(light_1, LOW); //turn off light
}
else
{
digitalWrite(light_1, HIGH); //turn on light
}
}
Step 6: Distance sensorDistance sensor (Ultrasonic Module HC-SR04) serves the distance sensor to inform the owner of the house via the Android application that someone is outside the door and how far away (meters). Sensor testing is shown in the pictures.
- In the code we see that the distance value is obtained. This distance value is printed using the Serial.print command and the printout is displayed as in the last photo. (Tools > Serial Monitor)
int trigPin = 10, echoPin = 11;
void setup() {
Serial.begin(9600);
pinMode(trigPin, INPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW); //turn off trigPin
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); //turn on trigPin
delayMicroseconds(10);
digitalWrite(trigPin, LOW); //turn off trigPn
long duration = pulseIn(echoPin, HIGH); //returns the sound wave travel time in microseconds
int distance = duration*0.034/2; //converts to metrics
Serial.print("Distance: ");
Serial.println(distance);
}
Step 7: Test without NodeMCU and Android application- The codes required to load according to the associated schema below is sony_spresense_board.ino.
Step 8: Sony Spresense Board and NodeMCU- NodeMCU uses serial pins Rx and Tx to receive data measured on the sensors. This data is sent to the NodeMCU from Sony Spresense Board. Communication is established using the connection shown in the scheme. NodeMCU connects to Internet using a WiFi network located in the surrounding area. After connecting, data is continuously sent to Google Firebase.
- The code to upload on the NodeMCU board is shown below. (Attachments -> nodemcu.ino) The code is uploaded before connecting to Sony Spresense Board.
- Serial.read is used to read data from Tx and Rx pins.
//NodeMCU code
#include <PubSubClient.h>
#include <FirebaseArduino.h>
Content guidelines <ESP8266WiFi.h>
#include <ArduinoJson.h>
#define FIREBASE_HOST "firebase_link"
#define FIREBASE_AUTH "firebase_api"
#define WIFI_SSID "wifi_name"
#define WIFI_PASSWORD "wifi_password"
void setup() {
Serial.begin(9600);
WiFi.begin (WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println ("");
Serial.println ("WiFi Connected!");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}
void loop() {
//PIR sensor data
int pir_data = Serial.read();
Firebase.setInt("pir_data", pir_data);
Serial.println(pir_data);
delay(500);
//Distance sensor data
int distance = Serial.read();
Firebase.setInt("distance", distance);
Serial.println(distance);
delay(500);
//Microphone module data
int mic_data = Serial.read();
Firebase.setInt("mic", mic_data);
Serial.println(mic);
delay(500);
}
- Part of the code used on the Sony Spresense Board to send data to NodeMCU is shown below.
- nodemcu.write is used to send data to Rx and Tx on NodeMCU. (1, 2, 3 is used only for testing)
//Sony Spresense Board code
#include<SoftwareSerial.h>
SoftwareSerial nodemcu(0, 1); // 0 -> Tx (nodemcu), 1 -> Rx (nodemcu)
void setup() {
//Serial S Begin at 9600 Baud
nodemcu.begin(9600);
}
void loop() {
//Write '123' to Serial
nodemcu.write(1);
delay(500);
nodemcu.write(2);
delay(500);
nodemcu.write(3);
delay(500);
}
Step 9: Google FirebaseGoogle Firebase serves to store data collected on the NodeMCU and Sony Spresense Board. These datas can be further used on websites, mobile applications and anywhere they can access the Internet. Sign in using your Google Account and follow the steps below.
After login follow the next steps:
- Click on "+ Add project"
- Fill information and click "Create"
- After loading, click "Develop" in left navigation bar. Then click "Database"
- Click "Create database" then check "Start in test mode" and click "Enable"
- Next to "Database" title in the drop-down menu, select "Realtime Database"
- Click on "Roles" tab. In code delete "false" and add "true".
- Back to "Data" tab. Copy link of your database and insert in Arduino code.
- Click on icon gear (left navigation bar) choose "Projects settings" and click on "Service accounts" choose "Database secrets".
- On right copy "Secret" code and insert in Arduino code.
Now, we've connected NodeMCU and Google Firebase.
Step 10: Android Studio- The application was created in the android studio. The part that connects the app from Google Firebase from where the sensor data is taken from is shown below.
dref = FirebaseDatabase.getInstance().getReference();
dref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
dist = dataSnapshot.child("distance").getValue().toString();
int dist_data = Integer.parseInt(dist);
if (dist_data == 0){
text_distance.setText("Nothing detected.");}
else {
text_distance.setText(dist +" meters away from entry!");
text_distance.setTextColor(col);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
- The part of the code that is specified is used to display distance data. Part of the code used to retrieve data and converting to int from Google firebase is below.
dist = dataSnapshot.child("distance").getValue().toString();
int dist_data = Integer.parseInt(dist);
- If-else is used to decide whether someone is outside or not.
if (dist_data == 0){
text_distance.setText("Nothing detected.");
}
else {
text_distance.setText(dist +" meters away from entry!");
text_distance.setTextColor(col);
}
- If there is no one outside, the zero is written to the base (sensor data) and then the text is showing "Nothing detected.". If someone is out there then the text is showing "(distance_value) meters away from entry!".
- Since the Sony Camera module is not connected, part of the code for receiving video is not written.
- Part of the code for linking text that is written in activity_main.xml (displayed in the application) and Google Firebase is in attachments with name MainActivity.java. Also the xml file is in attachments.
Comments