MotorCycle Rain Sensor and Notification Part 2: Putting It All Together

Welcome to the second part of our raindrop notification series. In this post, we will discuss how to put all our components/modules together.

Refer to the image below for the wiring.

raindrop.jpg
 
Analyzer MKR GSM
VCC VCC
GND GND
A0 A0
D0 Pin 7

Components

rainsensor.jpg

There are three components involved in this project. First, the raindrop sensor itself. This component is responsible for sending an electrical signal to the analyzer when it detects water.

 
analyzer.jpg

The second component is the Analyzer, which determines the threshold (the amount of water present in the sensor). As more water becomes present on the sensor, the value on A0 gets lower. Once the threshold is reached, the value on D0 for high to low.

 

The third and last component is the Arduino board, the A0 pin on the analyzer should be connected to A0 pin on the board and the D0 pin on the analyzer should be connected to a digital pin (in our example it’s connected to pin 7). The arduino board is responsible in collected the data that the analyzer is sending and determines what to do with them. In our case, when the value of D0 becomes low, we trigger a text message.

 

GSM SIM Provider

 

In order to send text messages out from the MKR GSM1400, you need a IoT SIM provider. For this project I used Hologram (https://hologram.io/). For $5.00 a pop, the first sim your register on your dashboard comes with 100MB free data. It’s perfect for testing out the feature of this SIM and the board’s capability.

 

The Code

/****************************************
Author: Jerico Cruz

This code listens to the AO and D0 pins from the rain sensor analyzer. They are connected to analog pin A0 and digital pin 7 on the arduino board respectively. 
*****************************************/

// libraries
#include <MKRGSM.h>
#include "settings.h"

// APN data
String HOLOGRAM_MESSAGE = "";

// initialize the library instance
GSMClient client;
GPRS gprs;
GSM gsmAccess;

// Hologram's Embedded API (https://hologram.io/docs/reference/cloud/embedded/) URL and port
char server[] = "cloudsocket.hologram.io";
int port = 9999;

const int analogPin=A0; //the AO of the module attach to A0
const int digitalPin=7; //D0 attach to pin7
int Astate=0; //store the value of A0
boolean Dstate=0; //store the value of D0
boolean messageSent = false;
time_t flag;

void setup() {
  pinMode(digitalPin,INPUT); //set digitalPin as INPUT
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {  
  Astate=analogRead(analogPin); //read the value of A0
  Dstate=digitalRead(digitalPin); //read the value of D0

  //Once the raindrop sensor threshold for "wet" is reached, send a notification.
  if(Dstate==LOW) 
  {    
    if(! messageSent)
      
    
    else {
      checkHoursLapsed();  
    }    
  }
  else{
    messageSent = false;
  }  
}

void checkHoursLapsed() {
    if (gsmConnect()){
        time_t diff = gsmAccess.getTime() - flag;

        if ((diff / 3600) >= 1) {
          messageSent = false;
          flag = 0;  
        }
    }
}

void sendNotification() { 
  
  if (gsmConnect()){
    if (client.connect(server, port)) {
      // Send a Message request:
      HOLOGRAM_MESSAGE = "Rain detected.";
      client.println(HOLOGRAM_PROCEDURE_NAME+HOLOGRAM_DEVICE_KEY+HOLOGRAM_DESTINATION_NUMBER+" "+HOLOGRAM_MESSAGE);
      messageSent = true;
      flag = gsmAccess.getTime();
    }
  
    client.stop();
    gsmAccess.shutdown();
  }
}

boolean gsmConnect() {
  boolean connected = false;
  while (!connected) {
    //Serial.println(gsmAccess.begin()); //Uncomment for testing
    
    if ((gsmAccess.begin() == GSM_READY) &&
        (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {

          connected = true;
          digitalWrite(LED_BUILTIN, LOW);       
    }
    else{
        digitalWrite(LED_BUILTIN, HIGH);
    } 
  }

  return connected;
}

Settings.h

const String HOLOGRAM_DEVICE_KEY = "[Get this from Hologram Dashboard]";
const String HOLOGRAM_DESTINATION_NUMBER = "[Number where you want to send the message to.]";
const char GPRS_APN[] = "hologram";
const char GPRS_LOGIN[] = " ";
const char GPRS_PASSWORD[] = " ";
const char HOLOGRAM_PROCEDURE_NAME = 'S';