adds significantly to hohm_phone.ino. Compiles but untested.

This commit is contained in:
Brendan Haines 2016-11-01 14:54:13 -06:00
parent f327370d79
commit ea3857b149

View File

@ -1,34 +1,47 @@
#include <avr/sleep.h> #include "avr/sleep.h"
#include "Adafruit_FONA.h" #include "Adafruit_FONA.h"
#include "Keypad.h"
////////////////////// ////////////////////////////////
///// PARAMETERS ///// ////////// PARAMETERS //////////
////////////////////// ////////////////////////////////
// Arduino will wake from sleep when WAKE_PIN is pulled low // Arduino will wake from sleep when WAKE_PIN is pulled low
#define WAKE_PIN 2 #define WAKE_PIN 2
#define BUT_ANS 7 #define BUT_ANS 7
#define BUT_END 8 #define BUT_END 8
// RX/TX are in reference to the Arduino // RX/TX are in reference to the Arduino, not SIM800
#define GSM_RX 3 #define GSM_RX 3
#define GSM_TX 4 #define GSM_TX 4
#define GSM_RST 5 #define GSM_RST 5
#define GSM_RING 6 #define GSM_RING 6
// TIMEOUT_SLEEP is the time to stay awake from last activity until sleep (milliseconds) // TIMEOUT_SLEEP is the time to stay awake from last activity until sleep (milliseconds)
#define TIMEOUT_SLEEP #define TIMEOUT_SLEEP 60000
// Comment the following line to use HW serial // Comment the following line to use HW serial
#define usb_testing_config #define usb_testing_config
////////////////////////// // Keypad pinout
///// END PARAMETERS ///// byte rowPins[4] = {9, 10, 11, 12};
////////////////////////// byte colPins[3] = {A0, A1, A2};
char replybuffer[255]; ////////////////////////////////////
uint8_t phonenumber[15]; ////////// END PARAMETERS //////////
uint8_t phonenumberlength = 0; ////////////////////////////////////
char phoneNumber[15];
uint8_t phoneNumberLength = 0;
char keys[4][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'#', '0', '*'}
};
unsigned long lastActiveTime;
#ifdef usb_testing_config #ifdef usb_testing_config
#include "SoftwareSerial.h" #include "SoftwareSerial.h"
@ -39,18 +52,21 @@ HardwareSerial *fonaSerial = &Serial;
#endif #endif
Adafruit_FONA fona = Adafruit_FONA(GSM_RST); Adafruit_FONA fona = Adafruit_FONA(GSM_RST);
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, 4, 3 );
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
void wake_from_sleep() { ///////////////////////////////
////////// FUNCTIONS //////////
///////////////////////////////
void wakeFromSleep() {
sleep_disable(); sleep_disable();
detachInterrupt(WAKE_PIN); detachInterrupt(WAKE_PIN);
} }
void go_to_sleep() { void goToSleep() {
sleep_enable(); sleep_enable();
attachInterrupt(WAKE_PIN, wake_from_sleep, LOW); attachInterrupt(WAKE_PIN, wakeFromSleep, LOW);
set_sleep_mode(SLEEP_MODE_PWR_DOWN); set_sleep_mode(SLEEP_MODE_PWR_DOWN);
cli(); cli();
sleep_bod_disable(); sleep_bod_disable();
@ -58,7 +74,7 @@ void go_to_sleep() {
sleep_cpu(); sleep_cpu();
} }
void in_call() { void inCall() {
while (1) { while (1) {
if ( !digitalRead(BUT_END) ) { // End button pressed if ( !digitalRead(BUT_END) ) { // End button pressed
if ( fona.hangUp() ) { if ( fona.hangUp() ) {
@ -69,20 +85,24 @@ void in_call() {
} }
} }
void begin_call() { void beginCall() {
fona.sendCheckReply( F("AT+STTONE=0"), F("OK") ); // End tone fona.sendCheckReply( F("AT+STTONE=0"), F("OK") ); // End dialtone
for(int i = 0; i<phonenumberlength; i++){ for (int i = 0; i < phoneNumberLength; i++) {
fona.playDTMF( phonenumber[i] ); fona.playDTMF( phoneNumber[i] ); // Play DTMF tones
if( fona.callPhone( phonenumber ) ) { if ( fona.callPhone( phoneNumber ) ) {
in_call(); inCall();
} }
for( int j = 0; j<phonenumberlength; j++) { for ( int j = 0; j < phoneNumberLength; j++) {
phonenumber[j] = 0; phoneNumber[j] = 0;
} }
phonenumberlength = 0; phoneNumberLength = 0;
} }
} }
//////////////////////////////////
///// ARDUINO CORE FUNCTIONS /////
//////////////////////////////////
void setup() { void setup() {
fonaSerial->begin(4800); fonaSerial->begin(4800);
if (! fona.begin(*fonaSerial)) while (1); //fona didn't start if (! fona.begin(*fonaSerial)) while (1); //fona didn't start
@ -93,12 +113,31 @@ void loop() {
while ( digitalRead(BUT_ANS) & !digitalRead(GSM_RING) ) delay(10); // Wait for answer button or end ring/call while ( digitalRead(BUT_ANS) & !digitalRead(GSM_RING) ) delay(10); // Wait for answer button or end ring/call
if ( !digitalRead(BUT_ANS) ) { if ( !digitalRead(BUT_ANS) ) {
if ( fona.pickUp() ) { if ( fona.pickUp() ) {
in_call(); inCall();
} }
} }
} }
fona.sendCheckReply( F("AT+STTONE=1,20,1000"), F("OK") ); // Start tone fona.sendCheckReply( F("AT+STTONE=1,20,1000"), F("OK") ); // Start tone
// Read from keypad
char key = keypad.getKey();
if ( key != NO_KEY ) {
phoneNumber[ phoneNumberLength ] = key;
phoneNumberLength++;
lastActiveTime = millis();
}
// Check for complete phone number (including +1 country code)
if ( ( phoneNumberLength = 10 & phoneNumber[0] != '1' ) || phoneNumberLength > 10 ) {
beginCall();
}
// Autoshutdown if inactive for extended period
// Typecast to long avoids "rollover" issues
if ( (long)(millis() - lastActiveTime) > TIMEOUT_SLEEP ) {
goToSleep();
}
} }