Hohm-Phone/Hohm_Phone.ino

250 lines
5.6 KiB
Arduino
Raw Normal View History

#include "avr/sleep.h"
2016-11-01 10:52:31 -06:00
#include "Adafruit_FONA.h"
#include "Keypad.h"
2016-11-01 10:52:31 -06:00
////////////////////////////////
////////// PARAMETERS //////////
////////////////////////////////
2016-11-01 10:52:31 -06:00
// Arduino will wake from sleep when WAKE_PIN is pulled low. Connect any waking buttons to WAKE_PIN
2016-11-01 10:52:31 -06:00
#define WAKE_PIN 2
2016-11-01 21:29:16 -06:00
#define BUT_ANS A3
#define BUT_END A4
2016-11-01 10:52:31 -06:00
// RX/TX are in reference to the Arduino, not SIM800
2016-11-01 10:52:31 -06:00
#define GSM_RX 3
2016-11-01 21:29:16 -06:00
#define GSM_TX 11
2016-11-03 10:31:20 -06:00
#define GSM_RST A2
2016-11-01 21:29:16 -06:00
#define GSM_RING A5
2016-11-01 10:52:31 -06:00
// TIMEOUT_SLEEP is the time to stay awake from last activity until sleep (milliseconds)
2016-11-01 21:29:16 -06:00
#define TIMEOUT_SLEEP 6000
2016-11-01 10:52:31 -06:00
2016-11-03 10:43:45 -06:00
// Charging voltage thresholds. Will turn on charging at CHG_VLO and turn off charging at CHG_VHI (millivolts)
#define CHG_VLO 3900
#define CHG_VHI 4100
#define CHG_PIN A0
// Comment the following line to use HW serial for Fona and disable debugging information
#define USB_DEBUG
2016-11-01 10:52:31 -06:00
2016-11-01 21:29:16 -06:00
// Comment the following line to disable sleeping the Arduino
//#define SLEEP
// Keypad pinout
byte rowPins[4] = {9, 4, 5, 7};
byte colPins[3] = {8, 10, 6};
2016-11-01 10:52:31 -06:00
////////////////////////////////////
////////// END PARAMETERS //////////
////////////////////////////////////
2016-11-01 21:29:16 -06:00
char phoneNumber[15] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int phoneNumberLength = 0;
char keys[4][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
2016-11-01 21:29:16 -06:00
{'*', '0', '#'}
};
unsigned long lastActiveTime;
bool dialtoneActive = false;
2016-11-01 21:29:16 -06:00
bool startDialtone = false;
#ifdef USB_DEBUG
2016-11-01 10:52:31 -06:00
#include "SoftwareSerial.h"
SoftwareSerial fonaSS = SoftwareSerial(GSM_RX, GSM_TX);
SoftwareSerial *fonaSerial = &fonaSS;
#else
HardwareSerial *fonaSerial = &Serial;
#endif
Adafruit_FONA fona = Adafruit_FONA(GSM_RST);
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, 4, 3 );
2016-11-01 10:52:31 -06:00
///////////////////////////////
////////// FUNCTIONS //////////
///////////////////////////////
2016-11-01 10:52:31 -06:00
void wakeFromSleep() {
2016-11-01 10:52:31 -06:00
sleep_disable();
detachInterrupt(WAKE_PIN);
#ifdef USB_DEBUG
Serial.println("Woke up");
#endif
2016-11-01 21:29:16 -06:00
startDialtone = true;
2016-11-01 10:52:31 -06:00
}
void goToSleep() {
#ifdef USB_DEBUG
Serial.println("Going to sleep");
#endif
2016-11-01 10:52:31 -06:00
sleep_enable();
attachInterrupt(WAKE_PIN, wakeFromSleep, LOW);
2016-11-01 10:52:31 -06:00
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
cli();
sleep_bod_disable();
sei();
sleep_cpu();
}
void inCall() {
2016-11-01 10:52:31 -06:00
while (1) {
if ( !digitalRead(BUT_END) || fona.getCallStatus() < 3 ) { // End button pressed
fona.hangUp();
#ifdef USB_DEBUG
Serial.println("Hanging up");
#endif
break;
2016-11-01 10:52:31 -06:00
}
2016-11-01 10:52:31 -06:00
// numpad stuff
}
for ( int j = 0; j < phoneNumberLength; j++) {
phoneNumber[j] = 0;
}
phoneNumberLength = 0;
2016-11-01 22:09:46 -06:00
startDialtone = true;
2016-11-01 10:52:31 -06:00
}
void beginCall() {
#ifdef USB_DEBUG
Serial.println("Starting Call");
#endif
fona.sendCheckReply( F("AT+STTONE=0"), F("OK") ); // End dialtone
dialtoneActive = false;
if ( fona.callPhone( phoneNumber ) ) {
#ifdef USB_DEBUG
Serial.println("Call Started");
#endif
inCall();
}
#ifdef USB_DEBUG
Serial.println("Call ended");
#endif
2016-11-01 10:52:31 -06:00
}
void resumeDialtone() {
fona.sendCheckReply( F("AT+STTONE=1,20,30000" ), F("OK") ); // Start dialtone
dialtoneActive = true;
}
//////////////////////////////////
///// ARDUINO CORE FUNCTIONS /////
//////////////////////////////////
2016-11-01 10:52:31 -06:00
void setup() {
2016-11-01 21:29:16 -06:00
#ifdef USB_DEBUG
Serial.begin(9600);
Serial.println("Booted. Setting up");
#endif
pinMode( WAKE_PIN, INPUT_PULLUP );
pinMode( BUT_ANS, INPUT_PULLUP );
pinMode( BUT_END, INPUT_PULLUP );
pinMode( GSM_RST, OUTPUT );
pinMode( GSM_RING, INPUT_PULLUP );
2016-11-03 10:43:45 -06:00
pinMode( CHG_PIN, OUTPUT );
digitalWrite( GSM_RST, HIGH );
2016-11-03 10:43:45 -06:00
digitalWrite( CHG_PIN, HIGH );
2016-11-01 10:52:31 -06:00
fonaSerial->begin(4800);
2016-11-01 21:29:16 -06:00
if (! fona.begin(*fonaSerial)) {
#ifdef USB_DEBUG
Serial.println("Fona failed to respond");
#endif
while (1); //fona didn't start
}
while ( !fona.setAudio(FONA_EXTAUDIO) ) {}
#ifdef USB_DEBUG
Serial.println("Setup Complete");
#endif
2016-11-01 21:29:16 -06:00
startDialtone = true;
lastActiveTime = millis();
fona.sendCheckReply( F("AT+CLVL=20"), F("OK") ); // set dialtone volume
2016-11-01 10:52:31 -06:00
}
void loop() {
if ( !digitalRead(GSM_RING) ) {
while ( digitalRead(BUT_ANS) & !digitalRead(GSM_RING) ) delay(10); // Wait for answer button or end ring/call
if ( !digitalRead(BUT_ANS) ) {
2016-11-01 22:33:13 -06:00
fona.pickUp();
delay(100);
2016-11-01 22:33:13 -06:00
inCall();
2016-11-01 10:52:31 -06:00
}
}
2016-11-01 21:29:16 -06:00
if ( startDialtone ) {
resumeDialtone();
2016-11-01 21:29:16 -06:00
startDialtone = false;
}
// Read from keypad
char key = keypad.getKey();
if ( key != NO_KEY ) {
phoneNumber[ phoneNumberLength ] = key;
2016-11-01 21:29:16 -06:00
phoneNumberLength = phoneNumberLength + 1;
2016-11-03 10:43:45 -06:00
if ( dialtoneActive ) {
fona.sendCheckReply( F("AT+STTONE=0"), F("OK") ); // End dialtone
dialtoneActive = false;
delay(50);
}
fona.playDTMF( key ); // Play DTMF tone
lastActiveTime = millis();
2016-11-01 21:29:16 -06:00
#ifdef USB_DEBUG
int i;
for (i = 0; i < 15; i = i + 1) {
Serial.print(phoneNumber[i]);
Serial.print(", ");
}
Serial.println(phoneNumberLength);
#endif
// Check for complete phone number (including +1 country code)
if ( ( phoneNumberLength == 10 & phoneNumber[0] != '1' ) || phoneNumberLength > 10 ) {
beginCall();
}
}
// Clear stored phone number on press of end button
if ( !digitalRead(BUT_END) ) {
for ( int j = 0; j < phoneNumberLength; j++) {
phoneNumber[j] = 0;
}
phoneNumberLength = 0;
#ifdef USB_DEBUG
Serial.println( phoneNumberLength );
#endif
resumeDialtone();
}
2016-11-03 10:43:45 -06:00
uint16_t vbat;
fona.getBattVoltage(&vbat);
if ( vbat < CHG_VLO ) {
digitalWrite( CHG_PIN, HIGH );
} else if ( vbat > CHG_VHI ) {
digitalWrite( CHG_PIN, LOW );
}
2016-11-01 21:29:16 -06:00
#ifdef SLEEP
// Autoshutdown if inactive for extended period
// Typecast to long avoids "rollover" issues
if ( (long)(millis() - lastActiveTime) > TIMEOUT_SLEEP ) {
goToSleep();
}
2016-11-01 21:29:16 -06:00
#endif
2016-11-01 10:52:31 -06:00
}