Skip to content

Instantly share code, notes, and snippets.

@dwblair
Last active August 29, 2015 14:25
Show Gist options
  • Save dwblair/65dbd10e8cddd0212e15 to your computer and use it in GitHub Desktop.
Save dwblair/65dbd10e8cddd0212e15 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
****************************************************/
#include "Adafruit_FONA.h"
#include <JeeLib.h>
#include <Wire.h>
#include <SPI.h>
#include <RTClib.h>
#include <RTC_DS3231.h>
#include<stdlib.h>
//sleeping stuff
ISR(WDT_vect) { Sleepy::watchdogEvent(); }
//RTC stuff
RTC_DS3231 RTC;
//led
#define led 9
//battery stuff
#define batteryAnalogMeasurePin A3
#define batteryReadCircuitSwitch 4
//FONA stuff ------------------------
#define sendto "16512524765"
#define FONA_RX 3
#define FONA_TX 5
#define FONA_RST A1
#define fonaKey A2
#define fonaPowerStatus A0
#define failCountMax 100
// 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);
//end FONA stuff ------------------------
// debugging -- only do Serial output if debuggin
#define debug 1 // 0: don't print anything out; 1: print out debugging statements
// how long to sleep between measurements
#define sleepSeconds 300
void setup() {
if (debug) Serial.begin(115200);
// begin I2C protocol (necessary for RTC, and any other I2C on board
Wire.begin();
// RTC -------------------------
initialize_RTC(); // NOTE: need to initialize I2C first -- but also for any other I2C library
// set mode for battery circuit control pin, and turn the circuit off
pinMode(batteryReadCircuitSwitch,OUTPUT);
digitalWrite(batteryReadCircuitSwitch, HIGH);
// FONA stuff ----------------------------------------------------
// set mode for FONA power status measurement pin
pinMode(fonaPowerStatus,INPUT);
//set mode for FONA power cycling key
pinMode(fonaKey, OUTPUT);
// make sure the FONA power cycling key is initially pulled 'high' -- i.e. not triggered
digitalWrite(fonaKey, HIGH); //go back to non-trigger
// end FONA stuff ---------------------------------------------------
// LED -----------------------------------------
//set the led pin mode to output
pinMode(led, OUTPUT);
}
void loop ()
{
// make measurements
// 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(batteryReadCircuitSwitch, LOW); //turn on battery measurement circuit
int batteryLevel = analogRead(batteryAnalogMeasurePin);
digitalWrite(batteryReadCircuitSwitch, HIGH);
// FONA sending stuff ----------------------
char message[141];
sprintf(message, "%ld, %d, %d",unixNow,batteryLevel, rtcTempInt);
// turn on the FONA
power_up_fona();
// initialize the FONA
int fonaStatus=initialize_fona();
if (!fonaStatus) {
if (debug) Serial.println("FONA not found");
}
int networkStatus=fona_find_network();
if ((networkStatus!=1)&&(networkStatus!=5)) {
if (debug) Serial.println("Couldn't find network in failCountMax tries. Aborting.");
}
if (((networkStatus==1)||(networkStatus==5))&&fonaStatus) { //then we're good to send a message!
if (debug) {
Serial.print(F("Send to #"));
Serial.println(sendto);
Serial.print(F("Type out one-line message (140 char): "));
Serial.println(message);
}
// send an SMS!
if (!fona.sendSMS(sendto, message)) {
if(debug) Serial.println(F("Failed"));
} else {
if (debug) Serial.println(F("Sent!"));
}
}
//turn off the module (should be on to begin with, but if all has failed, we should turn it off.
power_down_fona();
if(debug) {
Serial.println("Sleeping ...");
delay(1000); //seems to be necessary not to mess with serial output when sleeping
}
// PUT ATMEL 328p to SLEEP
go_to_sleep_seconds(sleepSeconds); //
}
// Sleep function
void go_to_sleep_seconds(int seconds) {
int LOG_INTERVAL_BASE = 1000; // 1 sec
for (int k=0;k<seconds;k++) {
Sleepy::loseSomeTime(LOG_INTERVAL_BASE); //-- will interfere with serial, so don't use when debugging
}
}
// RTC functions
int initialize_RTC() {
RTC.begin();
// check on the RTC
if (! RTC.isrunning()) {
if (debug) 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()) {
if(debug) Serial.println("RTC is older than compile time! Updating");
RTC.adjust(DateTime(__DATE__, __TIME__));
}
}
// Functions for FONA
void power_up_fona() {
int fonaPower=digitalRead(fonaPowerStatus);
if (!fonaPower) { // off, so power up
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
}
else {
// error! was already on for some reason -- do nothing
}
}
void power_down_fona() {
int fonaPower=digitalRead(fonaPowerStatus);
if (fonaPower) { // on, so power down
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
}
else {
// error! was already off for some reason -- do nothing
}
}
int initialize_fona() {
int fonaStatus;
fonaSerial->begin(4800);
if (! fona.begin(*fonaSerial)) {
// things are bad -- couldn't find FONA
fonaStatus=0;
}
else {
//things are good -- FONA OK
fonaStatus=1;
}
return fonaStatus;
}
int fona_find_network() {
uint8_t n = fona.getNetworkStatus();
int avail = fona.available();
int registerCountDelaySeconds=5;
int registerWaitTotalSeconds=0;
int failCount=0;
while ((n!=1)&&(n!=5)&&(failCount<failCountMax)) {
digitalWrite(led, HIGH); //turn on the LED
delay(20);
digitalWrite(led, LOW); //turn on the LED
delay(20);
if(debug) {
Serial.print(F("Network status "));
Serial.println(n);
}
n = fona.getNetworkStatus();
//avail = fona.available();
registerWaitTotalSeconds+=registerCountDelaySeconds;
delay(registerCountDelaySeconds*1000);
failCount=failCount+1;
}
if (debug) {
Serial.print("Network registration time=");
Serial.println(registerWaitTotalSeconds);
}
return n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment