Jerico Cruz
Published © GPL3+

Raindrop Notification

This is a project based on Arduino MKR GSM 1400 that will send a text notification when it senses rain drops.

BeginnerWork in progress2 hours1,209
Raindrop Notification

Things used in this project

Hardware components

Arduino MKR GSM 1400
Arduino MKR GSM 1400
×1
SAM Labs Sunfounder Raindrop sensor
×1
Adafruit 328 battery lithium ion battery
×1
USB Li Ion Battery Charger
Adafruit USB Li Ion Battery Charger
×1
Hologram Global IoT SIM Card
Hologram Global IoT SIM Card
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Wiring

Code

mkr1400

Arduino
/****************************************
Auhtor: Jerico Cruz
*****************************************/

// 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){
      sendNotification();
    }
    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

Arduino
const String HOLOGRAM_DEVICE_KEY = "[hologram device key goes here.]";
const String HOLOGRAM_DESTINATION_NUMBER = "[Phone number to send notification to]";
const char GPRS_APN[] = "hologram";
const char GPRS_LOGIN[] = " ";
const char GPRS_PASSWORD[] = " ";
const char HOLOGRAM_PROCEDURE_NAME = 'S';

Credits

Jerico Cruz

Jerico Cruz

1 project • 6 followers
Founder, MotoIoT

Comments

Add projectSign up / Login