GSM_Shield33_1
/*******************************************************************************
ProjectName = WebComProject2013
SubProjectName = ACV_Monitor_GSMAlert
ProjectScoop = 230VAC Monitor, Send SMS when Power Outage.
Sketch name = ACV_Monitor_GSMAlert.ino
Targer Device = Arduino Uno R3
IDE = Arduino For VisualStudio 2012 & VisualMicroDebugUpgrade1211_11
Created June 1 2013
by Dit e-mailadres wordt beveiligd tegen spambots. JavaScript dient ingeschakeld te zijn om het te bekijken.
This code is part of the public domain
Revision History
V01.00 2013-06-01 Initial relese by Dit e-mailadres wordt beveiligd tegen spambots. JavaScript dient ingeschakeld te zijn om het te bekijken.
V01.01 2013-
********************************************************************************
1.Hardware / Circuit:
1.1Arduino Uno R3
1.2GSM/GPRS shield (SIM900)
1.3 Backup battery
********************************************************************************
2.Interfaces
2.1RS232 (USB)
2.2SMS
********************************************************************************
3.Functions
3.1Monitor 230ACV
3.2Send Alarm SMS
3.3Daily Voiscall / ring (1s)
3.4 Query GSM Saldo
3.5 Send Saldo SMS when below 1€
3.6SMS Service
3.6.1Send daily SMS
3.6.1.1Report: VAC
3.6.1.2Report: Temp
3.6.1.3Report: Light
3.6.1.4Report: Date/Time
3.7Clock
3.7.1Report Date/Time
3.7.2Sync UTP server
********************************************************************************
Help file:///C:/Program%20Files/arduino-1.0.4/reference/index.html
********************************************************************************
*/
// 1. LIBRARIEs
#include <GSM.h>
#include <MyInstuctions.h>
#include <avr/pgmspace.h>
// 2. DEFINEs and ENUMs
// 2.0 RS433 network configuration.
#define MyID "G1" // Msg= x47 x30 x40 x62 x20 x20 x20 "G0@b "
#define COM1 0x01 // Serial port 1
#define COM2 0x02 // Serial port 2
#define COM3 0x03 // Serial port 3
#define RF433 0x10 // virtual Wire RF433
#define SMS 0x20 // GSM/SMS
// 2.1 HW IO Pin definitions
#define pinBussyLed 13
#define pinPhotoSensor 1 // LDR-HW pin connected ADC0
#define pinVoltageSensor 2 // VAC-HW pin connected ADC1
#define TakeSampleInterval 5000 // 60 seconds (60 * 1000) = 60000
#define GSMInterval 60000 // 24 hours (24 * 3600 * 1000) = 86400000
// 2.2 PIN Number
#define PINNUMBER "1415"
#define GSMtoCallNbrFrancis "0473990345"
#define GSMtoCallNbrGuillaume "0490396758"
#define GSMtoCallNbrElsie "0478963778"
//A structure that kept remote Inst and Data
typedef struct{
int iRemoteID;
int iInstruction;
char strData[10];
} InstTypeDef;
// 3. VARIABLEs
// 3.1 Application Version
//const char strVersion[] = "RS433 WebServer V01.06";
char *strVersion = "ACV_Monitor_GSMAlert V01.00";
// 3.2 Static Global variables
InstTypeDef MyInst;
int errorRegister = 0;
long lastReadingTakeSample = millis() ; // UpdateIntervalLS
long lastReadingGSM = millis(); // UpdateIntervalSMS
int iInstruction = -1;
float temperature = -1000;
bool flagTxSMS = false;
bool flagRxSMS = true; // incomming SMS msg
//bool flagTemperature = false;
//bool flagLightMeasurement = false;
//bool flagVoltageMeasurement = true;
bool flagTakeSample = false;
bool flagGsmTest = false;
//
#define GSM_ON 8 // was 9
char *PinCode = PINNUMBER;
String remoteNumber = ""; // the number you will call
char Instruction ;
char SMScharbuffer[20];
// IMEI variable
String IMEI = "";
// 4. initialize the library instance
// Initialize msg packages
//MyInstuctions RxInst( MyID );
//MyInstuctions TxInst( MyID );
// 4.1 initialize the library instance
// modem verification object
GSM SIM900; // include a 'true' parameter for debug enabled
//GSMModem SIM900modem;
GSMVoiceCall SIM900voiceCall;
GSM_SMS SIM900SMS;
// Strings stored in flash mem for help function
prog_char Help_00[] PROGMEM = "<0> Command[6] = iTxID, iRxID, iInst";
prog_char Help_01[] PROGMEM = "<1> freeRam()";
prog_char Help_02[] PROGMEM = "<2> TakeSample()";
prog_char Help_03[] PROGMEM = "<5> GSM_PowerUp()";
prog_char Help_04[] PROGMEM = "<6> GSM_PowerDown";
prog_char Help_05[] PROGMEM = "<7> GSM_PIN_LogOn()";
prog_char Help_06[] PROGMEM = "<8> GSM_NetworkStatus()";
prog_char Help_07[] PROGMEM = "<9> GSM_AccessAlive()";
prog_char Help_08[] PROGMEM = "<10> GSM_voiceCallStatus()";
prog_char Help_09[] PROGMEM = "<11> GSM_Make1sVoiceCall()";
prog_char Help_10[] PROGMEM = "<12> GSM_SendSMS()";
prog_char Help_11[] PROGMEM = "<13> GSM_HangVoiceCall()";
prog_char Help_12[] PROGMEM = "------------------------";
prog_char Help_13[] PROGMEM = "<20> Enable TakeSample()";
prog_char Help_14[] PROGMEM = "<21> Enable TakeSample()";
prog_char Help_15[] PROGMEM = "<2.0> Enable TakeSample()";
prog_char Help_16[] PROGMEM = "<2.1> Enable TakeSample()";
prog_char Help_17[] PROGMEM = "------------------------";
prog_char Help_18[] PROGMEM = "|By taillieu.info 2013 |";
prog_char Help_19[] PROGMEM = "------------------------";
#define Help_tableSize 19 + 1 // +1
PROGMEM const char *Help_table[] = {
Help_00,
Help_01,
Help_02,
Help_03,
Help_04,
Help_05,
Help_06,
Help_07,
Help_08,
Help_09,
Help_10,
Help_11,
Help_12,
Help_13,
Help_14,
Help_15,
Help_16,
Help_17,
Help_18,
Help_19
};
//============================================
void setup()
{
// initialize serial communications and wait for port to open:
Serial.begin(9600); // 9600, 115200
Serial.setTimeout(100);
Serial.println("<<< Start initialization >>>");
//
pinMode( pinBussyLed, OUTPUT );
digitalWrite( pinBussyLed, HIGH );
//
Serial.print( "Free RAM = "); // Report Free Memory Stack - Heap
Serial.println( freeRam());
//
Serial.println( strVersion ); // Report Application Name & Version
//
Serial.print("LDR analog in is at ");
Serial.println(pinPhotoSensor);
pinMode(pinPhotoSensor, INPUT); // declare the LDR as an INPUT
//
Serial.print("VAC analog in is at ");
Serial.println(pinVoltageSensor);
pinMode(pinVoltageSensor, INPUT); // declare the LDR as an INPUT
//
Serial.print("My ID =");
Serial.println( MyID );
//
// start modem test (reset and check response)
// GSM_modemTest();
// start GSM (logon with PIN)
//GSM_PIN_LogOn();
//
//Help();
Serial.println("<<< Start Main Application >>>");
delay(100);
pinMode( pinBussyLed, LOW );
}
//============================================
void loop()
{
// Do this every 10 second.
if ( (millis() - lastReadingTakeSample) > TakeSampleInterval) {
if ( flagTakeSample) TakeSample();
lastReadingTakeSample = millis();
}
// Do this every 24 hours.
if ( (millis() - lastReadingGSM) > lastReadingGSM) {
if (flagGsmTest ) GSM_Make1sVoiceCall();
lastReadingGSM = millis();
}
// listen for incomming SMS instructions
if ( flagRxSMS ) {
//SMSMSG();
}
// add any incoming characters to the String:
iInstruction = listenforSerialInstruction();
switch ( iInstruction ) {
case -1: // default case no instructions
break;
case 0:
Serial.print("My ID =");
Serial.println( MyID );
Help();
break;
case 1:
Serial.print( "freeRam() = " ) ;
Serial.println( freeRam() ) ;
break;
case 2:
TakeSample();
break;
case 5:
GSM_PowerUp();
break;
case 6:
GSM_PowerDown();
break;
case 7:
GSM_PIN_LogOn();
break;
case 8:
GSM_NetworkStatus();
break;
case 9:
GSM_AccessAlive();
break;
case 10:
GSM_voiceCallStatus();
break;
case 11:
GSM_Make1sVoiceCall();
break;
case 12:
GSM_SendSMS();
break;
case 13:
GSM_HangVoiceCall();
break;
default:
Serial.print("No Case for this Instruction = ");
Serial.println( iInstruction, DEC );
Help();
}
}
//============================================
void TakeSample(){
Serial.println( "1 TakeSample");
digitalWrite( pinBussyLed, HIGH );
//Serial.print( "1.1 Free RAM=");
//Serial.print(freeRam());
//Serial.println( " bytes");
//Serial.print( "1.2 Temperature=");
//temperature = getTemp( ds );
//Serial.println(temperature);
Serial.print( "1.1 DAC Value (0-1024) = ");
Serial.println( getDACVal( pinPhotoSensor ) );
Serial.println( "1.end TakeSample\n");
delay(100);
digitalWrite( pinBussyLed, LOW );
}
void SMSMSG(){
Serial.println( "2 SMSMSGS");
digitalWrite( pinBussyLed, HIGH );
Serial.print( "1.3 LightVal=");
Serial.println( getDACVal( pinPhotoSensor ) );
Serial.println( "2.end SMSMSG");
delay(500);
digitalWrite( pinBussyLed, LOW );
}
int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
int getDACVal( uint8_t pinADC ){
return analogRead(pinADC);
}
void GSM_PowerUp()
{
Serial.println("PowerUp ");
pinMode(GSM_ON, OUTPUT);
digitalWrite(GSM_ON,LOW);
delay(1000);
digitalWrite(GSM_ON,HIGH);
delay(2000);
digitalWrite(GSM_ON,LOW);
delay(3000);
}
void GSM_PowerDown()
{
Serial.println("PowerDown SIIM900");
pinMode(GSM_ON, OUTPUT);
digitalWrite(GSM_ON,LOW);
delay(1000);
digitalWrite(GSM_ON,HIGH);
delay(2000);
digitalWrite(GSM_ON,LOW);
delay(3000);
}
/*
void GSM_modemTest(){
Serial.print("Starting modem test...");
if(SIM900modem.begin()) {
Serial.println("modem.begin() succeeded");
// get modem IMEI
Serial.print("Checking IMEI...");
IMEI = SIM900modem.getIMEI();
Serial.println("Modem's IMEI: " + IMEI);
Serial.println("Modem is functoning properly");
GSM_PIN_LogOn();
}
else {
Serial.println("ERROR, no modem answer.");
GSM_NetworkStatus();
Serial.println("Bye Bye...");
GSM_PowerDown();
while(true);
}
}
*/
void GSM_PIN_LogOn(){
Serial.print( "Start Sim900, PIN=" );
Serial.println( PinCode );
boolean notConnected = true;
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
int iRetry = 0;
while(notConnected)
{
//{ 0 ERROR, 1 IDLE, 2 CONNECTING, 3 GSM_READY, 4 GPRS_READY, 5 TRANSPARENT_CONNECTED};
GSM_AccessAlive();
switch (GSM_NetworkStatus()) {
case ERROR: //0
SIM900.begin(PinCode,true, false );
delay(500);
notConnected = true;
break;
case CONNECTING: //2
Serial.println("GSM Connecting...");
delay(500);
iRetry ++ ;
if ( iRetry > 5) GSM_PowerUp();
break;
case GSM_READY: //3
notConnected = false;
Serial.println("GSM initialized.");
break;
default:
Serial.println("Not connected");
}
}
GSM_AccessAlive();
}
int GSM_NetworkStatus(){
int NetworkStatus = SIM900.getStatus() ;
Serial.print("GSM_NetworkStatus = ");
Serial.println( NetworkStatus ); //{ ERROR, IDLE, CONNECTING, GSM_READY, GPRS_READY, TRANSPARENT_CONNECTED};
return NetworkStatus;
}
int GSM_AccessAlive(){
int AccessAlive = 1 ; //SIM900.isAccessAlive();
Serial.print("GSM isAccessAlive = ");
Serial.println(AccessAlive);
return AccessAlive;
}
int GSM_voiceCallStatus(){
int voiceCallStatus = SIM900voiceCall.getvoiceCallStatus();
Serial.print("GSM voiceCallStatus = ");
Serial.println(voiceCallStatus);
return voiceCallStatus;
}
void GSM_Make1sVoiceCall(){
Serial.println("### GSM_Make1sVoiceCall()");
remoteNumber = GSMtoCallNbrFrancis;
Serial.print(" Calling to ");
Serial.println(remoteNumber);
// Call the remote number
char charbuffer[20];
remoteNumber.toCharArray(charbuffer, 20);
// Check if the receiving end has picked up the call
Serial.print(" SIM900voiceCall = ");
Serial.println( SIM900voiceCall.voiceCall(charbuffer,5000) );
Serial.println("Call Established.");
GSM_voiceCallStatus();
// Wait for some input from the line
delay(5000);
GSM_voiceCallStatus();
// And hang up
SIM900voiceCall.hangCall();
GSM_voiceCallStatus();
Serial.println("Call Finished");
remoteNumber="";
}
void GSM_SendSMS(){
Serial.println("GSM_SendSMS()");
remoteNumber = GSMtoCallNbrFrancis;
Serial.print("Calling to : ");
Serial.println(remoteNumber);
Serial.println();
// Call the remote number
char charbuffer[20];
remoteNumber.toCharArray(charbuffer, 50); //Max 200 char per SMS
// sms text
char txtMsg[] = "Hello, I'm you Arduino messenger" ;
Serial.println("SENDING");
Serial.println("Message:");
Serial.println(txtMsg);
// send the message
if(flagTxSMS) {
SIM900SMS.beginSMS(charbuffer);
SIM900SMS.print(txtMsg);
SIM900SMS.endSMS();
}
Serial.println("\nCOMPLETE!");
}
void GSM_HangVoiceCall(){
Serial.println("GSM_HangVoiceCall()");
SIM900voiceCall.hangCall();
Serial.println("Call Finished");
}
int listenforSerialInstruction(){
String commandline ;
String RemoteID ;
String strInstruction ;
iInstruction = -1;
MyInst.iInstruction = -1 ;
int strIndex = -1 ;
if (Serial.available())
{
// an Serial Command ends with a blank \n
MyInst.iInstruction = 0;
strIndex = 0 ;
Serial.println("new Serial data");
commandline = Serial.readStringUntil( '\n' );
Serial.print("commandline = ");
Serial.println( commandline );
strIndex = commandline.indexOf( MyID );
if ( strIndex >= 0){
strIndex = strIndex + 2;
RemoteID = commandline.substring( strIndex, strIndex + 2 );
Serial.print("RemoteID = ");
Serial.println( RemoteID );
strIndex = strIndex + 2;
strInstruction = commandline.substring( strIndex, strIndex + 2 );
Serial.print(" strInstruction = ");
Serial.println( strInstruction );
iInstruction = strInstruction.toInt();
Serial.print(" iInstruction = ");
Serial.println( iInstruction, DEC );
}
}
return iInstruction;
}
// Printing Help
void Help() {
char buffer[30]; //A character array to hold the strings from the flash mem
for (int i = 0; i < Help_tableSize ; i++) {
strcpy_P(buffer, (char*)pgm_read_word(&(Help_table[i])));
Serial.println( buffer );
Serial.flush();
}
return;
}