RAPIFIRE

Blog

IoT Smart School Classroom Monitor

In this blog we cover building an IoT app for monitoring too much noise in a classroom using a simple sound sensor with the Ardino Yun and RAPIFIRE. When the noise sensor detects the sound is too high due to students misbehaving it sends a message to the the School Principal with a voice message. Then the Principal can investigate what is going on and restore order in the classroom.

For this app you will need the following items:

  • 1 Arduino Yun
  • 1 USPRO┬« LM393 Sound detection sensor module for Arduino. See here
  • 3 jumper cables (red, orange, yellow)
  • 1 red LED
  • Arduino IDE v1.6.7 installed on your computer

Okay let's get started. First we will assemble the IoT sensor and then create the Things in RAPIFIRE.

  • Connect the red wire to the VCC pin on the sound sensor and to the +5v on the Arduino Yun.

  • Connect the yellow wire to the GND pin on the sound sensor and to the GND pin on the Arduion Yun beside the +5v pin.

  • Connect the orange wire to the OUT pin on the sound sensor and to the digital pin 4 on the Arduino Yun.

  • Connect the LED long lead to digital pin 13 and LED short lead to the GND pin on the Arduino Yun.

  • Connect the USB cable to the Ardiuno Yun and to computer with the Arduino IDE is installed. If you don't have the IDE installed see here for instructions on how to do that.

There setup should look like this.

  • Start Arduino IDE and add the following code:
/* Connection Pins
Arduino    Microphone  
 GND          GND
 +5V          +5V
 D3           OUT (or D0) depends on Microphone
*/

int led = 13 ;// define LED Interface  
int mic = 4; // define D0 Sensor Interface  
int val = 0;// define numeric variables val  
int numSounds = 0;

void setup ()  
{
  Serial.begin(9600);
  Serial.println("Start setup");
  pinMode (led, OUTPUT) ;// define LED as output interface
  pinMode (mic, INPUT) ;// output interface D0 is defined sensor
  Serial.println("End setup");
}

void loop ()  
{
  val = digitalRead(mic);// digital interface will be assigned a value of pin 4 to read val
  if (val == LOW) // When the sound detection module detects a signal, LED flashes
  {
    digitalWrite (led, HIGH);
    numSounds = numSounds + 1;
    Serial.print("Sound Detected:");
    Serial.println(numSounds);
  }
  else
  {
    digitalWrite (led, LOW);
  }
}

  • Start the Serial Monitor in the IDE with menu choice Tools -> Serial Monitor.

  • Snap your fingers in front of the black speaker. The red LED light should come on. Also the built-in LED on the sound sensor board should blink. If it does not, then clap very loudly. If that does not work, then adjust the little screw on the blue trimpot. Clockwise rotation of the screw, decreases the sensitivity and anti-clock wise rotation increases it. See below.

  • Snap your fingers after the sensitivity adjustment. The Serial Monitor will show "Sound Detected: X" in the dialog.

  • Now let's create in Rapifire the thing Classroom 1 for the Product Smart School. First login here into RAPIFIRE.

  • Click on the Products menu item and add the new Product Smart School with a heartbeat of 60 seconds.

  • Click on the Things menu item and add the thing Classroom 1 for the product Smart School and user Bob the User.

  • Click in the Arduino IDE the Sketch -> Include Libaries -> Manage Libraries to show the Library Manager

  • Enter rapifire in the text field of the Library Manager. Click on the Rapifire result and press the Install button. Press the close dialog after download is completed.

  • Enter pubsub in the text field of the Library Manager. Click on the PubSubClient result and press the Install button. Press the close dialog after download is completed.

* Click File -> Examples -> MqttRapifireClient -> rapifire-mqtt-simple-publish.

  • This opens up a separate template file with an example to connect and publish an event to Rapifire.

  • Copy from the Classroom 1's Thing ID into the of YOUR-THING-ID of define THING_ID and the same for the define Thing Token in the YOUR-THING-KEY.

  • Merge the code from the example into our other noise detection code.

The code should look like this:

/* Connection Pins
Arduino    Microphone  
 GND          GND
 +5V          +5V
 D3           OUT (or D0) depends on Microphone
*/

#include <YunClient.h>
#include <PubSubClient.h>
#include <RapifireMqttClient.h>

#define THING_ID "SF5wxqkHsXQrpRRo2iCu89"
#define THING_KEY "q5THUglts78v"

YunClient client;

RapifireMqttClient rapifire(THING_ID, THING_KEY, client);

int led = 13 ;// define LED Interface  
int mic = 4; // define D0 Sensor Interface  
int val = 0;// define numeric variables val  
int numSounds = 0;

void setup ()  
{
  Serial.begin(9600);
  Serial.println("Start setup");
  pinMode (led, OUTPUT) ;// define LED as output interface
  pinMode (mic, INPUT) ;// output interface D0 is defined sensor

  Serial.println(F("Starting bridge..."));
  Bridge.begin();
  Serial.println(F("Bridge ready."));

  Serial.println("End setup");
}

void loop ()  
{


  val = digitalRead(mic);// digital interface will be assigned a value of pin 4 to read val
  if (val == LOW) // When the sound detection module detects a signal, LED flashes
  {


     if (!rapifire.connected()) {
        Serial.println(F("Connecting to RAPIFIRE.."));

        if (rapifire.connect()) {
          Serial.println(F("Connected."));
        }
    }

    digitalWrite (led, HIGH);
    numSounds = numSounds + 1;
    Serial.print("Sound Detected:");
    Serial.println(numSounds);
    rapifire.addValue("Sound Detected", numSounds);
    rapifire.publish();

  }
  else
  {
    digitalWrite (led, LOW);
  }
}
  • Press verify and upload to add the code to the Arduino.

  • Open the Serial Monitor in the IDE and make a loud noise by the speaker. The Serial monitor shows the "Sound Detected" message.

  • Click on the Raw Data on the Classroom 1 Thing. It shows the message Sound Detected in the table.

  • Now we are going to Cloud Code sending a phone voice message "There is too much noise in classroom 1." to the phone number "+48XXXYYYZZZ". Click on the Cloud Code, and select the Smart School product and add the following
"use strict";
//
// Function process(ctx, thing, event) is called for each SenML object
// sent by your thing.
//
function process(ctx, thing, events) {  
  console.log("Call the Principal", events);
  ctx.notifications.makeCall("+48XXXYYYZZZ", "There is too much noise in classroom 1.");
}
  • Make more loud noises by the speaker. The phone number will ring and the text will be played as an automated voice message. This phone number is called only once every 10 minutes using the Twilio phone service. See here for more details.

Congratulations, the school will now be monitored for trouble. Go add more classroom things and sensors as an exercise.

Author image
About Mark Garzone