|
| 1 | +#include <SoftwareSerial.h> |
| 2 | +#include "Adafruit_FONA.h" |
| 3 | + |
| 4 | +#define FONA_TX 3 |
| 5 | +#define FONA_RX 4 |
| 6 | +#define FONA_RST 5 |
| 7 | +#define FONA_RI 2 |
| 8 | + |
| 9 | +#define MOTOR_PWM A1 |
| 10 | +#define MOTOR_1 A2 |
| 11 | +#define MOTOR_2 A3 |
| 12 | + |
| 13 | +#define LED 13 |
| 14 | + |
| 15 | +#define OPEN true |
| 16 | +#define CLOSE false |
| 17 | + |
| 18 | +#define BUSYWAIT 5000 // milliseconds |
| 19 | + |
| 20 | +// this is a large buffer for replies |
| 21 | +char replybuffer[255]; |
| 22 | + |
| 23 | +// or comment this out & use a hardware serial port like Serial1 (see below) |
| 24 | +SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX); |
| 25 | + |
| 26 | +Adafruit_FONA fona = Adafruit_FONA(FONA_RST); |
| 27 | + |
| 28 | +uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0); |
| 29 | + |
| 30 | +boolean fonainit(void) { |
| 31 | + Serial.println(F("Initializing....(May take 3 seconds)")); |
| 32 | + |
| 33 | + digitalWrite(LED, HIGH); |
| 34 | + delay(100); |
| 35 | + digitalWrite(LED, LOW); |
| 36 | + delay(100); |
| 37 | + digitalWrite(LED, HIGH); |
| 38 | + delay(100); |
| 39 | + digitalWrite(LED, LOW); |
| 40 | + delay(100); |
| 41 | + |
| 42 | + // make it slow so its easy to read! |
| 43 | + fonaSS.begin(4800); // if you're using software serial |
| 44 | + //Serial1.begin(4800); // if you're using hardware serial |
| 45 | + |
| 46 | + // See if the FONA is responding |
| 47 | + if (! fona.begin(fonaSS)) { // can also try fona.begin(Serial1) |
| 48 | + Serial.println(F("Couldn't find FONA")); |
| 49 | + return false; |
| 50 | + } |
| 51 | + Serial.println(F("FONA is OK")); |
| 52 | + return true; |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +void setup() { |
| 57 | + while (!Serial); // useful for Leonardo/Micro, remove in production! |
| 58 | + |
| 59 | + // set LED output for debugging |
| 60 | + pinMode(LED, OUTPUT); |
| 61 | + |
| 62 | + // set up motor pins, but turn them off |
| 63 | + pinMode(MOTOR_1, OUTPUT); |
| 64 | + digitalWrite(MOTOR_1, LOW); |
| 65 | + pinMode(MOTOR_2, OUTPUT); |
| 66 | + digitalWrite(MOTOR_2, LOW); |
| 67 | + pinMode(MOTOR_PWM, OUTPUT); |
| 68 | + digitalWrite(MOTOR_PWM, LOW); |
| 69 | + |
| 70 | + Serial.begin(115200); |
| 71 | + Serial.println(F("FONA basic test")); |
| 72 | + |
| 73 | + while (! fonainit()) { |
| 74 | + delay(5000); |
| 75 | + } |
| 76 | + |
| 77 | + // Print SIM card IMEI number. |
| 78 | + char imei[15] = {0}; // MUST use a 16 character buffer for IMEI! |
| 79 | + uint8_t imeiLen = fona.getIMEI(imei); |
| 80 | + if (imeiLen > 0) { |
| 81 | + Serial.print("SIM card IMEI: "); Serial.println(imei); |
| 82 | + } |
| 83 | + |
| 84 | + pinMode(FONA_RI, INPUT); |
| 85 | + digitalWrite(FONA_RI, HIGH); // turn on pullup on RI |
| 86 | + // turn on RI pin change on incoming SMS! |
| 87 | + fona.sendCheckReply(F("AT+CFGRI=1"), F("OK")); |
| 88 | +} |
| 89 | + |
| 90 | +int8_t lastsmsnum = 0; |
| 91 | + |
| 92 | +void loop() { |
| 93 | + digitalWrite(LED, HIGH); |
| 94 | + delay(100); |
| 95 | + digitalWrite(LED, LOW); |
| 96 | + |
| 97 | + while (fona.getNetworkStatus() != 1) { |
| 98 | + Serial.println("Waiting for cell connection"); |
| 99 | + delay(2000); |
| 100 | + } |
| 101 | + |
| 102 | + // this is a 'busy wait' loop, we check if the interrupt |
| 103 | + // pin went low, and after BUSYWAIT milliseconds break out to check |
| 104 | + // manually for SMS's and connection status |
| 105 | + // This would be a good place to 'sleep' |
| 106 | + for (uint16_t i=0; i<BUSYWAIT; i++) { |
| 107 | + if (! digitalRead(FONA_RI)) { |
| 108 | + // RI pin went low, SMS received? |
| 109 | + Serial.println(F("RI went low")); |
| 110 | + break; |
| 111 | + } |
| 112 | + delay(1); |
| 113 | + } |
| 114 | + |
| 115 | + int8_t smsnum = fona.getNumSMS(); |
| 116 | + if (smsnum < 0) { |
| 117 | + Serial.println(F("Could not read # SMS")); |
| 118 | + return; |
| 119 | + } else { |
| 120 | + Serial.print(smsnum); |
| 121 | + Serial.println(F(" SMS's on SIM card!")); |
| 122 | + } |
| 123 | + |
| 124 | + if (smsnum == 0) return; |
| 125 | + |
| 126 | + // there's an SMS! |
| 127 | + uint8_t n = 1; |
| 128 | + while (true) { |
| 129 | + uint16_t smslen; |
| 130 | + char sender[25]; |
| 131 | + |
| 132 | + Serial.print(F("\n\rReading SMS #")); Serial.println(n); |
| 133 | + uint8_t len = fona.readSMS(n, replybuffer, 250, &smslen); // pass in buffer and max len! |
| 134 | + // if the length is zero, its a special case where the index number is higher |
| 135 | + // so increase the max we'll look at! |
| 136 | + if (len == 0) { |
| 137 | + Serial.println(F("[empty slot]")); |
| 138 | + n++; |
| 139 | + continue; |
| 140 | + } |
| 141 | + if (! fona.getSMSSender(n, sender, sizeof(sender))) { |
| 142 | + // failed to get the sender? |
| 143 | + sender[0] = 0; |
| 144 | + } |
| 145 | + |
| 146 | + Serial.print(F("***** SMS #")); Serial.print(n); |
| 147 | + Serial.print(" ("); Serial.print(len); Serial.println(F(") bytes *****")); |
| 148 | + Serial.println(replybuffer); |
| 149 | + Serial.print(F("From: ")); Serial.println(sender); |
| 150 | + Serial.println(F("*****")); |
| 151 | + |
| 152 | + if (strcasecmp(replybuffer, "open sesame") == 0) { |
| 153 | + // open the door! |
| 154 | + digitalWrite(LED, HIGH); |
| 155 | + moveMotor(OPEN, 800); |
| 156 | + digitalWrite(LED, LOW); |
| 157 | + } |
| 158 | + if (strcasecmp(replybuffer, "close cadabra") == 0) { |
| 159 | + // close the door! |
| 160 | + digitalWrite(LED, HIGH); |
| 161 | + moveMotor(CLOSE, 800); |
| 162 | + digitalWrite(LED, LOW); |
| 163 | + } |
| 164 | + |
| 165 | + delay(3000); |
| 166 | + break; |
| 167 | + } |
| 168 | + fona.deleteSMS(n); |
| 169 | + |
| 170 | + delay(1000); |
| 171 | +} |
| 172 | + |
| 173 | + |
| 174 | +void moveMotor(boolean direction, uint16_t t) { |
| 175 | + digitalWrite(MOTOR_PWM, HIGH); // enable motor |
| 176 | + if (direction) { |
| 177 | + digitalWrite(MOTOR_1, HIGH); |
| 178 | + digitalWrite(MOTOR_2, LOW); |
| 179 | + } else { |
| 180 | + digitalWrite(MOTOR_2, HIGH); |
| 181 | + digitalWrite(MOTOR_1, LOW); |
| 182 | + } |
| 183 | + delay(t); |
| 184 | + digitalWrite(MOTOR_1, LOW); |
| 185 | + digitalWrite(MOTOR_2, LOW); |
| 186 | + |
| 187 | + digitalWrite(MOTOR_PWM, LOW); // disable motor |
| 188 | +} |
0 commit comments