Skip to content

Instantly share code, notes, and snippets.

@man-ito-ba
Last active December 13, 2016 10:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save man-ito-ba/8a063146f7aafa771c24bf4ab9213cd2 to your computer and use it in GitHub Desktop.
SMS_App
#include <GSM.h>
GSM gsmAccess;
GSM_SMS sms;
#define CodePIN ""
// Keep sender number
char Sender_Number[20];
char Admin_Number[] = "+33XXXXXXXXX";
bool SMS_LED;
bool SMS_PreviousLED;
bool SMS_WrongInstruction;
bool SMS_WrongNumber;
// Millis
const int Sec = 1000;
unsigned long MillisPrevious = 0;
unsigned long MillisInterval = 10000;
unsigned long MillisActual;
// ======== SMS Contents
char SystemeID[] = "\n-GSM Arduino-";
// Startup message
char Title_Activate[] = "\nSystem Activated ";
char Text_Connected[] = "GSM Network accessed.";
// User Instructions
char Title_Instructions[] = "Instructions:";
char Text_UserInstructions[] = "\n0 : Rappel des instructions ;\n1 : Allumer/Eteindre la LED ;\n2 : Obtenir l'Status de la LED ;\n3 : Verifier le Time d'utilisation de l'Arduino";
// LED status
char Title_LED[] = "\nLED > ";
char Text_SmsLED[] = "SMS Instruction > ";
char Text_ButtonLED[] = "button pushed > ";
char Text_LedStatus[] = "Status control > ";
char Text_LEDOff[] = "OFF";
char Text_LEDOn[] = "ON";
// Usage time
char Title_UsageTime[] = "Arduino has been awake for ";
char Text_UsageTime[] = " in total.";
// Warning messages
char Title_Warning[] = "Warning: ";
char Text_InstructionWarning[] = "incorrect instruction. ";
char Text_CorrectInstruction[] = "Send '0' to get valid instructions list.";
char Text_WarningIntruder[] = "Someone not registered to use this Arduino attempted to send it an instruction. Here is his number: ";
// No text variable, just in case I need one
char Text_Null[] = "";
// ==== Devices
// ======== LED & variables
const int LED = 12;
bool LED_Status;
// ======== Button & variables
const int Button = 4;
bool Button_Status;
bool Button_StatusPrevious;
long Button_Time = 0;
long Button_Debounce = 200;
int Potentiometre; // Variable servant à compter (simu à voir plus bas)
// ****************************************************************************
// * Setup *
// ****************************************************************************
void setup()
{
pinMode(Button, INPUT_PULLUP);
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
SMS_LED, SMS_PreviousLED,
Potentiometre,
Button_Status,
Button_StatusPrevious,
LED_Status,
MillisActual,
MillisInterval,
SMS_WrongNumber,
SMS_WrongInstruction
= 0;
// ======= Serial monitor
Serial.begin(9600);
while (!Serial) {
;
}
// ======= Connecting to network
ConnectionToNetwork();
delay(1000);
// ======= Programm launching
Serial.println(F("**************************"));
Serial.println(F("** SMS in/out Programm **"));
Serial.println(F("**************************"));
digitalWrite(LED, LOW); // LED Off to show that setup is done
SendSMS(Title_Activate, Text_Connected, Text_Null); // Message sent when Arduino is up and running
DeleteSMS(); // At statup, erasing any SMS stored while Arduino was aslept
}
// ****************************************************************************
// * LOOP *
// ****************************************************************************
void loop()
{
ReceiveSMS(); // Check if any instruction has been received
InstructionButton(); // Check if button has been pushed or not
LED_On_Off(); // Turn LED on or off if needed
delay(10);
}
// ****************************************************************************
// * Functions *
// ****************************************************************************
void ConnectionToNetwork(){
bool NoConnection = true; // Bool for connection status
while (NoConnection) { // GSM shield initialization
Serial.println(F("[Looking for network]"));
delay(500);
if (gsmAccess.begin(CodePIN) == GSM_READY) {
delay(500);
NoConnection = false;
} else {
Serial.println(F("[No connection]"));
delay(1000);
}
}
}
// ****************************************************************************
// * Reception *
// ****************************************************************************
void ReceiveSMS(){
if(sms.available()){
SenderNumber();
WrongNumber();
WrongInstruction();
InstructionSMS();
if(SMS_WrongInstruction == 1) UnknownInstruction();
DeleteSMS();
}
}
void SenderNumber(){
Serial.print(F("\n[SMS from "));
sms.remoteNumber(Sender_Number, 20);
Serial.print(Sender_Number);
Serial.println(F("]"));
}
void WrongNumber(){
for(int i = 0; i < 12; i++){
if(Sender_Number[i] == Admin_Number[i]);
else{
// if send number != admin numner, warning sent to admin number
SendSMS(Title_Warning, Text_WarningIntruder, Sender_Number);
SMS_WrongNumber = true;
return;
}
}
}
void WrongInstruction(){
if(SMS_WrongNumber == 1) return; // if sender number != admin number, this function is not executed
if(sms.peek() < 48 || sms.peek() > 51){
SMS_WrongInstruction = true;
}
else{
SMS_WrongInstruction = false;
}
}
void InstructionSMS(){
if(SMS_WrongNumber == 1) return; // if sender number != admin number, this function is not executed
if(SMS_WrongInstruction == 1) return; // if instruction was wrong, this function is not executed
int Action_Choice = sms.parseInt();
if(Action_Choice == 0){
SendSMS(Title_Instructions,
Text_UserInstructions,
Text_Null);
} else if(Action_Choice == 1){
LED_SMS_On_Off();
} else if(Action_Choice == 2){ // 2 : État de la LED
LedStatusControl();
} else if(Action_Choice == 3){ // 3 : Time d'utilisation
ArduinoUsageTime();
} else {
UnknownInstruction();
}
}
void UnknownInstruction(){
SendSMS(Title_Warning, Text_InstructionWarning, Text_CorrectInstruction);
}
void DeleteSMS(){
int MessageQtt = 0;
Serial.print(F("["));
do{
sms.flush();
MessageQtt++;
} while (sms.available() > 0);
Serial.print(MessageQtt);
Serial.print(F(" message(s)"));
Serial.println(F(" have been deleted]\n"));
}
// ****************************************************************************
// * LED *
// ****************************************************************************
// setting the LED status on and off using push button
void InstructionButton(){
Button_Status = digitalRead(Button);
if(Button_Status == LOW && Button_StatusPrevious == HIGH && millis() - Button_Time > Button_Debounce){
if(LED_Status == HIGH){
LED_Status = LOW;
delay(100);
SendSMS(Title_LED, Text_ButtonLED, Text_LEDOff);
}
else {
LED_Status = HIGH;
delay(100);
SendSMS(Title_LED, Text_ButtonLED, Text_LEDOn);
}
}
}
// setting the LED status on and off using SMS
void LED_SMS_On_Off(){
if(LED_Status == HIGH){
LED_Status = LOW;
delay(100);
SendSMS(Title_LED, Text_SmsLED, Text_LEDOff);
}
else {
LED_Status = HIGH;
delay(100);
SendSMS(Title_LED, Text_SmsLED, Text_LEDOn);
}
}
// turning the LED on and off depending on its status
void LED_On_Off(){
digitalWrite(LED, LED_Status);
Button_StatusPrevious = Button_Status;
}
// telling the admin what the LED status is
void LedStatusControl(){
switch (LED_Status) {
case 0:
SendSMS(Title_LED, Text_LedStatus, Text_LEDOff);
break;
case 1:
SendSMS(Title_LED, Text_LedStatus, Text_LEDOn);
break;
}
}
// ****************************************************************************
// * Checking the arduino time usage *
// ****************************************************************************
// how long has the Arduino been up and running?
void ArduinoUsageTime(){
char *Usage_Time;
Usage_Time = Time_To_String(millis()/1000);
Serial.println(Usage_Time);
SendSMS(Title_UsageTime, Usage_Time, Text_UsageTime);
}
char * Time_To_String(unsigned long Time_In_Seconds){
static char Time_Total[12];
long Hou = Time_In_Seconds / 3600;
Time_In_Seconds = Time_In_Seconds % 3600;
int Min = Time_In_Seconds / 60;
int Sec = Time_In_Seconds % 60;
sprintf(Time_Total, "%02ldh%02dmin%02ds", Hou, Min, Sec);
return Time_Total;
}
// ****************************************************************************
// * SendSMS *
// ****************************************************************************
void SendSMS(char Theme[], char Message1[], char Message2[]){
sms.beginSMS(Admin_Number);
sms.print(SystemeID);
sms.print(Theme);
sms.print(Message1);
sms.print(Message2);
sms.endSMS();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment