Stap 5: Software
De Arduino schets is zwaar bewerkt uit de voorbeelden in Adafruit Fona bibliotheek.
#include #include "Adafruit_FONA.h" #define FONA_RST 4 #define AUTHORIZED "+1*****************" // INSERT YOUR PHONE NUMBER HERE // this is a large buffer for replies char replybuffer[255]; HardwareSerial *fonaSerial = &Serial1; // Use the Serial1 on the Xadow Board Adafruit_FONA fona = Adafruit_FONA(FONA_RST); uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0); void setup() { while (!Serial); Serial.begin(115200); Serial.println(F("Initializing....(May take 3 seconds)")); fonaSerial->begin(115200); // The SIM800L module auto detects the baud rate, but works really well with 115200 if (! fona.begin(*fonaSerial)) { Serial.println(F("Couldn't find SIM800L")); while(1); } Serial.println(F("SIM800L is OK")); // Print SIM card IMEI number. char imei[15] = {0}; // MUST use a 16 character buffer for IMEI! uint8_t imeiLen = fona.getIMEI(imei); if (imeiLen > 0) { Serial.print("SIM card IMEI: "); Serial.println(imei); } // Configure APN Settings for TING - please change for your network fona.setGPRSNetworkSettings(F("wholesale")); } char fonaInBuffer[64]; void loop() { Serial.print(F("FONA> ")); while (! Serial.available() ) { if (fona.available()) { Serial.write(fona.read()); } } char phone[32] = {0}; if(fona.incomingCallNumber(phone)){ Serial.println(F("RING!")); Serial.print(F("Phone Number: ")); Serial.println(phone); if(phone == AUTHORIZED) fona.pickUp(); } char* bufPtr = fonaInBuffer; //handy buffer pointer if (fona.available()) //any data available from the SIM800L? { int slot = 0; //this will be the slot number of the SMS int charCount = 0; //Read the notification into fonaInBuffer do { *bufPtr = fona.read(); Serial.write(*bufPtr); delay(1); } while ((*bufPtr++ != '\n') && (fona.available()) && (++charCount < (sizeof(fonaInBuffer)-1))); //Add a terminal NULL to the notification string *bufPtr = 0; //Scan the notification string for an SMS received notification. // If it's an SMS message, we'll get the slot number in 'slot' if (1 == sscanf(fonaInBuffer, "+CMTI: \"SM\",%d", &slot)) { Serial.print("slot: "); Serial.println(slot); char callerIDbuffer[32]; //we'll store the SMS sender number in here // Retrieve SMS sender address/phone number. if (! fona.getSMSSender(slot, callerIDbuffer, 31)) { Serial.println("Didn't find SMS message in slot!"); } Serial.print(F("FROM: ")); Serial.println(callerIDbuffer); if(callerIDbuffer == AUTHORIZED) { //Send back an automatic response Serial.println("Sending reponse..."); if (!fona.enableGPRS(true)) Serial.println(F("Failed to turn on")); uint16_t returncode; if (!fona.getGSMLoc(&returncode, replybuffer, 250)) Serial.println(F("Failed!")); if (returncode == 0) { Serial.println(replybuffer); } else { Serial.print(F("Fail code #")); Serial.println(returncode); } if (!fona.sendSMS(callerIDbuffer, replybuffer)) { Serial.println(F("Failed")); } else { Serial.println(F("Sent!")); } if (!fona.enableGPRS(true)) Serial.println(F("Failed to turn on")); } } } }