these changes were sitting on my disk from years ago

This commit is contained in:
Brendan Haines 2022-11-12 22:06:14 -07:00
parent 815f392e9e
commit b47267400e
2 changed files with 259 additions and 259 deletions

View File

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

View File

@ -1,10 +1,10 @@
# Hohm-Phone # Hohm-Phone
## Hardware Connections ## Hardware Connections
-->|-- Indicates diode -->|-- Indicates diode
WAKE_PIN -->|-- GSM_RING --- RI(Fona) WAKE_PIN -->|-- GSM_RING --- RI(Fona)
WAKE_PIN -->|-- BUT_ANS ---- Button --- GND WAKE_PIN -->|-- BUT_ANS ---- Button --- GND
WAKE_PIN -->|-- BUT_END ---- Button --- GND WAKE_PIN -->|-- BUT_END ---- Button --- GND
BUT_ANS and BUT_END have pull up resistors BUT_ANS and BUT_END have pull up resistors