/****************************************
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;
}
Comments
Please log in or sign up to comment.