Skip to content

Instantly share code, notes, and snippets.

@dwblair
Last active August 29, 2015 14:25
Show Gist options
  • Save dwblair/436fd42ce9c006c8d33d to your computer and use it in GitHub Desktop.
Save dwblair/436fd42ce9c006c8d33d to your computer and use it in GitHub Desktop.
/***************************************************
This is an example for our Adafruit FONA Cellular Module
Designed specifically to work with the Adafruit FONA
----> http://www.adafruit.com/products/1946
----> http://www.adafruit.com/products/1963
----> http://www.adafruit.com/products/2468
----> http://www.adafruit.com/products/2542
These cellular modules use TTL Serial to communicate, 2 pins are
required to interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
/*
THIS CODE IS STILL IN PROGRESS!
Open up the serial console on the Arduino at 115200 baud to interact with FONA
Note that if you need to set a GPRS APN, username, and password scroll down to
the commented section below at the end of the setup() function.
*/
#include "Adafruit_FONA.h"
#include <JeeLib.h>
#include <Wire.h>
#include <SPI.h>
#include <RTClib.h>
#include <RTC_DS3231.h>
#include<stdlib.h>
//sleeping
ISR(WDT_vect) { Sleepy::watchdogEvent(); }
#define LOG_INTERVAL_BASE 1000 // in millisec -- 60000 (60 sec) max
#define LOG_INTERVAL_REPEAT 60 // number of times to repeat BASE
//RTC
RTC_DS3231 RTC;
#define FONA_RX 3
#define FONA_TX 5
#define FONA_RST A1
#define led 9
#define fonaKey A2
#define BATTERYPIN A3
#define batteryRead 4
#define powerStatus A0
#define failCountMax 100
// this is a large buffer for replies
char replybuffer[255];
int fonaPower; //checking to see whether the FONA module has power or not
// This is to handle the absence of software serial on platforms
// like the Arduino Due. Modify this code if you are using different
// hardware serial port, or if you are using a non-avr platform
// that supports software serial.
#ifdef __AVR__
#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;
#else
HardwareSerial *fonaSerial = &Serial1;
#endif
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
void setup() {
//while (!Serial);
Serial.begin(115200);
//RTC STUFF
// for i2c for RTC
Wire.begin();
RTC.begin();
// check on the RTC
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
DateTime now = RTC.now();
DateTime compiled = DateTime(__DATE__, __TIME__);
if (now.unixtime() < compiled.unixtime()) {
Serial.println("RTC is older than compile time! Updating");
RTC.adjust(DateTime(__DATE__, __TIME__));
}
//pinMode(BATTERYPIN,INPUT);
pinMode(batteryRead,OUTPUT);
pinMode(powerStatus,INPUT);
digitalWrite(batteryRead, HIGH);
pinMode(fonaKey, OUTPUT);
digitalWrite(fonaKey, HIGH); //go back to non-trigger
pinMode(led, OUTPUT);
//digitalWrite(fonaKey, HIGH);
}
void loop ()
{
//if the fona is off, turn it on
fonaPower=digitalRead(powerStatus);
Serial.print("Starting loop. fonaPower=");
Serial.println(fonaPower);
if (!fonaPower) { // this should mean it's off. should do digital read here instead.
//turn on the module
Serial.println("Turning on module.");
digitalWrite(led, HIGH); //turn on the LED
digitalWrite(fonaKey, HIGH); //go back to non-trigger
digitalWrite(fonaKey, LOW); //turn on the SMS subcircuit
delay(2000); //so now it's on
digitalWrite(fonaKey, HIGH); //go back to non-trigger
digitalWrite(led, LOW); //turn on the LED
}
else {
Serial.println("module on. no need to touch it.");
}
Serial.println(F("FONA basic test"));
Serial.println(F("Initializing....(May take 3 seconds)"));
int fonaStatus=0;
fonaSerial->begin(4800);
if (! fona.begin(*fonaSerial)) {
Serial.println(F("Couldn't find FONA"));
fonaStatus=0;
}
else {
Serial.println(F("FONA is OK"));
fonaStatus=1;
}
if (fonaStatus==1) {
Serial.println(F("FONA 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);
}
*/
//delay(5000);
uint8_t n = fona.getNetworkStatus();
int avail = fona.available();
int registerCountDelaySeconds=5;
int registerWaitTotalSeconds=0;
int failCount=0;
while ((n!=1)&&(failCount<failCountMax)) {
//Serial.print("powerStatus=");
// Serial.println(fonaPower);
digitalWrite(led, HIGH); //turn on the LED
delay(50);
digitalWrite(led, LOW); //turn on the LED
Serial.print(F("Network status "));
Serial.println(n);
n = fona.getNetworkStatus();
//avail = fona.available();
registerWaitTotalSeconds+=registerCountDelaySeconds;
delay(registerCountDelaySeconds*1000);
failCount=failCount+1;
}
if (n==1) {
// make measurements & send
Serial.println("Connected to network!");
// Onboard temp from the RTC
float rtcTemp = RTC.getTempAsFloat();
int rtcTempInt = (int) (rtcTemp*100);
//get the time
DateTime now = RTC.now();
long unixNow = now.unixtime();
digitalWrite(batteryRead, LOW); //turn on battery measurement circuit
int batteryLevel = analogRead(BATTERYPIN);
digitalWrite(batteryRead, HIGH);
// send an SMS!
char sendto[]="16512524765";
//char message[] = "test";
char message[141];
sprintf(message, "%ld, %d, %d, %d",unixNow, registerWaitTotalSeconds,batteryLevel, rtcTempInt);
//char message[]="happy birthday adeline!";
Serial.print(F("Send to #"));
Serial.println(sendto);
Serial.print(F("Type out one-line message (140 char): "));
Serial.println(message);
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("Sent!"));
}
}
}
else {
Serial.println("FONA not found");
}
//turn off the module (should be on to begin with, but if all has failed, we should turn it off.
fonaPower=digitalRead(powerStatus);
Serial.print("fonaPower=");
Serial.println(fonaPower);
if (fonaPower) {// then turn it off
Serial.println("Fona on ... turning it off.");
digitalWrite(led, HIGH); //turn on the LED
digitalWrite(fonaKey, HIGH); //go back to non-trigger
digitalWrite(fonaKey, LOW); //turn on the SMS subcircuit
delay(2000); //so now it's off
digitalWrite(fonaKey, HIGH); //go back to non-trigger
digitalWrite(led, LOW); //turn off the LED
}
else {
Serial.println("Fona off already.");
}
Serial.println("Sleeping ...");
delay(1000);
for (int k=0;k<LOG_INTERVAL_REPEAT;k++) {
Sleepy::loseSomeTime(LOG_INTERVAL_BASE); //-- will interfere with serial, so don't use when debugging
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment