Skip to content

Instantly share code, notes, and snippets.

@dwblair
Last active August 29, 2015 14:25
Show Gist options
  • Save dwblair/cc04081b0c51b1396d8b to your computer and use it in GitHub Desktop.
Save dwblair/cc04081b0c51b1396d8b to your computer and use it in GitHub Desktop.
send values via fona
/***************************************************
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
****************************************************/
/*
THIS CODE IS STILL IN PROGRESS!
Open up the serial console on the Arduino at 115200 baud to interact with FONA
Note that if you need to set a GPRS APN, username, and password scroll down to
the commented section below at the end of the setup() function.
*/
#include "Adafruit_FONA.h"
#include <JeeLib.h>
#include <Wire.h>
#include <SPI.h>
#include <RTClib.h>
#include <RTC_DS3231.h>
#include<stdlib.h>
//sleeping
ISR(WDT_vect) { Sleepy::watchdogEvent(); }
#define LOG_INTERVAL_BASE 1000 // in millisec -- 60000 (60 sec) max
#define LOG_INTERVAL_REPEAT 60 // number of times to repeat BASE
//RTC
RTC_DS3231 RTC;
#define FONA_RX 11
#define FONA_TX 12
#define FONA_RST 13
#define led 9
#define remote 3
#define BATTERYPIN A3
#define batteryRead 4
#define powerStatus A0
#define failCountMax 100
// this is a large buffer for replies
char replybuffer[255];
// 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);
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
void setup() {
//RTC STUFF
// for i2c for RTC
Wire.begin();
RTC.begin();
// check on the RTC
if (! RTC.isrunning()) {
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()) {
Serial.println("RTC is older than compile time! Updating");
RTC.adjust(DateTime(__DATE__, __TIME__));
}
//pinMode(BATTERYPIN,INPUT);
pinMode(batteryRead,OUTPUT);
pinMode(powerStatus,INPUT);
digitalWrite(batteryRead, HIGH);
pinMode(remote, OUTPUT);
digitalWrite(remote, HIGH); //go back to non-trigger
pinMode(led, OUTPUT);
//digitalWrite(remote, HIGH);
int fonaPower;
for (int i=0;i<10;i++) {
//if the fona is off, turn it on
fonaPower=digitalRead(powerStatus);
Serial.print("Starting loop. fonaPower=");
Serial.println(fonaPower);
if (!fonaPower) { // this should mean it's off. should do digital read here instead.
//turn on the module
Serial.println("Turning on module.");
digitalWrite(led, HIGH); //turn on the LED
digitalWrite(remote, HIGH); //go back to non-trigger
digitalWrite(remote, LOW); //turn on the SMS subcircuit
delay(2000); //so now it's on
digitalWrite(remote, HIGH); //go back to non-trigger
digitalWrite(led, LOW); //turn on the LED
}
else {
Serial.println("module on. no need to touch it.");
}
//while (!Serial);
Serial.begin(115200);
Serial.println(F("FONA basic test"));
Serial.println(F("Initializing....(May take 3 seconds)"));
int fonaStatus=0;
fonaSerial->begin(4800);
if (! fona.begin(*fonaSerial)) {
Serial.println(F("Couldn't find FONA"));
fonaStatus=0;
}
else {
Serial.println(F("FONA is OK"));
fonaStatus=1;
}
if (fonaStatus==1) {
Serial.println(F("FONA is OK"));
/*
// Print SIM card IMEI number.
char imei[15] = {0}; // MUST use a 16 character buffer for IMEI!
uint8_t imeiLen = fona.getIMEI(imei);
if (imeiLen > 0) {
Serial.print("SIM card IMEI: "); Serial.println(imei);
}
*/
//delay(5000);
uint8_t n = fona.getNetworkStatus();
int avail = fona.available();
int registerCountDelaySeconds=5;
int registerWaitTotalSeconds=0;
int failCount=0;
while ((n!=1)&&(failCount<failCountMax)) {
//Serial.print("powerStatus=");
// Serial.println(fonaPower);
digitalWrite(led, HIGH); //turn on the LED
delay(50);
digitalWrite(led, LOW); //turn on the LED
Serial.print(F("Network status "));
Serial.println(n);
n = fona.getNetworkStatus();
//avail = fona.available();
registerWaitTotalSeconds+=registerCountDelaySeconds;
delay(registerCountDelaySeconds*1000);
failCount=failCount+1;
}
if (n==1) {
// make measurements & send
Serial.println("Connected to network!");
// 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(batteryRead, LOW);
int batteryLevel = analogRead(BATTERYPIN);
// send an SMS!
char sendto[]="16512524765";
//char message[] = "test";
char message[141];
sprintf(message, "%ld, %d, %d, %d",unixNow, registerWaitTotalSeconds,batteryLevel, rtcTempInt);
//char message[]="happy birthday adeline!";
flushSerial();
Serial.print(F("Send to #"));
Serial.println(sendto);
Serial.print(F("Type out one-line message (140 char): "));
Serial.println(message);
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("Sent!"));
}
}
}
else {
Serial.println("FONA not found");
}
//turn off the module (should be on to begin with, but if all has failed, we should turn it off.
fonaPower=digitalRead(powerStatus);
Serial.print("fonaPower=");
Serial.println(fonaPower);
if (fonaPower) {// then turn it off
Serial.println("Fona on ... turning it off.");
digitalWrite(led, HIGH); //turn on the LED
digitalWrite(remote, HIGH); //go back to non-trigger
digitalWrite(remote, LOW); //turn on the SMS subcircuit
delay(2000); //so now it's off
digitalWrite(remote, HIGH); //go back to non-trigger
digitalWrite(led, LOW); //turn off the LED
}
else {
Serial.println("Fona off already.");
}
//check fona power again!
/*
fonaPower=digitalRead(powerStatus);
Serial.print("fonaPower=");
Serial.println(fonaPower);
if (fonaPower) {
//blink a lot to indicate weirdness!
for (int i=0;i<10;i++) {
digitalWrite(led, HIGH);
delay(20);
digitalWrite(led,LOW);
delay(20);
}
//try turning it off again
Serial.println("Fona on ... trying to turn it off again");
digitalWrite(led, HIGH); //turn on the LED
digitalWrite(remote, HIGH); //go back to non-trigger
digitalWrite(remote, LOW); //turn on the SMS subcircuit
delay(2000); //so now it's off
digitalWrite(remote, HIGH); //go back to non-trigger
digitalWrite(led, LOW); //turn off the LED
}
*/
Serial.println("Sleeping ...");
delay(1000);
for (int k=0;k<LOG_INTERVAL_REPEAT;k++) {
Sleepy::loseSomeTime(LOG_INTERVAL_BASE); //-- will interfere with serial, so don't use when debugging
}
}
}
void printMenu(void) {
Serial.println(F("-------------------------------------"));
Serial.println(F("[?] Print this menu"));
Serial.println(F("[a] read the ADC (2.8V max)"));
Serial.println(F("[b] read the Battery V and % charged"));
Serial.println(F("[C] read the SIM CCID"));
Serial.println(F("[U] Unlock SIM with PIN code"));
Serial.println(F("[i] read RSSI"));
Serial.println(F("[n] get Network status"));
Serial.println(F("[v] set audio Volume"));
Serial.println(F("[V] get Volume"));
Serial.println(F("[H] set Headphone audio"));
Serial.println(F("[e] set External audio"));
Serial.println(F("[T] play audio Tone"));
Serial.println(F("[P] PWM/Buzzer out"));
// FM (SIM800 only)
Serial.println(F("[f] tune FM radio"));
Serial.println(F("[F] turn off FM"));
Serial.println(F("[m] set FM volume"));
Serial.println(F("[M] get FM volume"));
Serial.println(F("[q] get FM station signal level"));
// Phone
Serial.println(F("[c] make phone Call"));
Serial.println(F("[h] Hang up phone"));
Serial.println(F("[p] Pick up phone"));
// SMS
Serial.println(F("[N] Number of SMSs"));
Serial.println(F("[r] Read SMS #"));
Serial.println(F("[R] Read All SMS"));
Serial.println(F("[d] Delete SMS #"));
Serial.println(F("[s] Send SMS"));
// Time
Serial.println(F("[y] Enable network time sync"));
Serial.println(F("[Y] Enable NTP time sync (GPRS)"));
Serial.println(F("[t] Get network time"));
// GPRS
Serial.println(F("[G] Enable GPRS"));
Serial.println(F("[g] Disable GPRS"));
Serial.println(F("[l] Query GSMLOC (GPRS)"));
Serial.println(F("[w] Read webpage (GPRS)"));
Serial.println(F("[W] Post to website (GPRS)"));
// GPS
Serial.println(F("[O] Turn GPS on (SIM808)"));
Serial.println(F("[o] Turn GPS off (SIM808)"));
Serial.println(F("[x] GPS fix status (SIM808)"));
Serial.println(F("[L] Query GPS location (SIM808)"));
Serial.println(F("[E] Raw NMEA out (SIM808)"));
Serial.println(F("[S] create Serial passthru tunnel"));
Serial.println(F("-------------------------------------"));
Serial.println(F(""));
}
void loop() {
Serial.print(F("FONA> "));
while (! Serial.available() ) {
if (fona.available()) {
Serial.write(fona.read());
}
}
char command = Serial.read();
Serial.println(command);
switch (command) {
case '?': {
printMenu();
break;
}
case 'a': {
// read the ADC
uint16_t adc;
if (! fona.getADCVoltage(&adc)) {
Serial.println(F("Failed to read ADC"));
} else {
Serial.print(F("ADC = ")); Serial.print(adc); Serial.println(F(" mV"));
}
break;
}
case 'b': {
// read the battery voltage and percentage
uint16_t vbat;
if (! fona.getBattVoltage(&vbat)) {
Serial.println(F("Failed to read Batt"));
} else {
Serial.print(F("VBat = ")); Serial.print(vbat); Serial.println(F(" mV"));
}
if (! fona.getBattPercent(&vbat)) {
Serial.println(F("Failed to read Batt"));
} else {
Serial.print(F("VPct = ")); Serial.print(vbat); Serial.println(F("%"));
}
break;
}
case 'U': {
// Unlock the SIM with a PIN code
char PIN[5];
flushSerial();
Serial.println(F("Enter 4-digit PIN"));
readline(PIN, 3);
Serial.println(PIN);
Serial.print(F("Unlocking SIM card: "));
if (! fona.unlockSIM(PIN)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
break;
}
case 'C': {
// read the CCID
fona.getSIMCCID(replybuffer); // make sure replybuffer is at least 21 bytes!
Serial.print(F("SIM CCID = ")); Serial.println(replybuffer);
break;
}
case 'i': {
// read the RSSI
uint8_t n = fona.getRSSI();
int8_t r;
Serial.print(F("RSSI = ")); Serial.print(n); Serial.print(": ");
if (n == 0) r = -115;
if (n == 1) r = -111;
if (n == 31) r = -52;
if ((n >= 2) && (n <= 30)) {
r = map(n, 2, 30, -110, -54);
}
Serial.print(r); Serial.println(F(" dBm"));
break;
}
case 'n': {
// read the network/cellular status
uint8_t n = fona.getNetworkStatus();
Serial.print(F("Network status "));
Serial.print(n);
Serial.print(F(": "));
if (n == 0) Serial.println(F("Not registered"));
if (n == 1) Serial.println(F("Registered (home)"));
if (n == 2) Serial.println(F("Not registered (searching)"));
if (n == 3) Serial.println(F("Denied"));
if (n == 4) Serial.println(F("Unknown"));
if (n == 5) Serial.println(F("Registered roaming"));
break;
}
/*** Audio ***/
case 'v': {
// set volume
flushSerial();
Serial.print(F("Set Vol %"));
uint8_t vol = readnumber();
Serial.println();
if (! fona.setVolume(vol)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
break;
}
case 'V': {
uint8_t v = fona.getVolume();
Serial.print(v); Serial.println("%");
break;
}
case 'H': {
// Set Headphone output
if (! fona.setAudio(FONA_HEADSETAUDIO)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
fona.setMicVolume(FONA_HEADSETAUDIO, 15);
break;
}
case 'e': {
// Set External output
if (! fona.setAudio(FONA_EXTAUDIO)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
fona.setMicVolume(FONA_EXTAUDIO, 10);
break;
}
case 'T': {
// play tone
flushSerial();
Serial.print(F("Play tone #"));
uint8_t kittone = readnumber();
Serial.println();
// play for 1 second (1000 ms)
if (! fona.playToolkitTone(kittone, 1000)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
break;
}
/*** FM Radio ***/
case 'f': {
// get freq
flushSerial();
Serial.print(F("FM Freq (eg 1011 == 101.1 MHz): "));
uint16_t station = readnumber();
Serial.println();
// FM radio ON using headset
if (fona.FMradio(true, FONA_HEADSETAUDIO)) {
Serial.println(F("Opened"));
}
if (! fona.tuneFMradio(station)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("Tuned"));
}
break;
}
case 'F': {
// FM radio off
if (! fona.FMradio(false)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
break;
}
case 'm': {
// Set FM volume.
flushSerial();
Serial.print(F("Set FM Vol [0-6]:"));
uint8_t vol = readnumber();
Serial.println();
if (!fona.setFMVolume(vol)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
break;
}
case 'M': {
// Get FM volume.
uint8_t fmvol = fona.getFMVolume();
if (fmvol < 0) {
Serial.println(F("Failed"));
} else {
Serial.print(F("FM volume: "));
Serial.println(fmvol, DEC);
}
break;
}
case 'q': {
// Get FM station signal level (in decibels).
flushSerial();
Serial.print(F("FM Freq (eg 1011 == 101.1 MHz): "));
uint16_t station = readnumber();
Serial.println();
int8_t level = fona.getFMSignalLevel(station);
if (level < 0) {
Serial.println(F("Failed! Make sure FM radio is on (tuned to station)."));
} else {
Serial.print(F("Signal level (dB): "));
Serial.println(level, DEC);
}
break;
}
/*** PWM ***/
case 'P': {
// PWM Buzzer output @ 2KHz max
flushSerial();
Serial.print(F("PWM Freq, 0 = Off, (1-2000): "));
uint16_t freq= readnumber();
Serial.println();
if (! fona.setPWM(freq)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
break;
}
/*** Call ***/
case 'c': {
// call a phone!
char number[30];
flushSerial();
Serial.print(F("Call #"));
readline(number, 30);
Serial.println();
Serial.print(F("Calling ")); Serial.println(number);
if (!fona.callPhone(number)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("Sent!"));
}
break;
}
case 'h': {
// hang up!
if (! fona.hangUp()) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
break;
}
case 'p': {
// pick up!
if (! fona.pickUp()) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
break;
}
/*** SMS ***/
case 'N': {
// read the number of SMS's!
int8_t smsnum = fona.getNumSMS();
if (smsnum < 0) {
Serial.println(F("Could not read # SMS"));
} else {
Serial.print(smsnum);
Serial.println(F(" SMS's on SIM card!"));
}
break;
}
case 'r': {
// read an SMS
flushSerial();
Serial.print(F("Read #"));
uint8_t smsn = readnumber();
Serial.print(F("\n\rReading SMS #")); Serial.println(smsn);
// Retrieve SMS sender address/phone number.
if (! fona.getSMSSender(smsn, replybuffer, 250)) {
Serial.println("Failed!");
break;
}
Serial.print(F("FROM: ")); Serial.println(replybuffer);
// Retrieve SMS value.
uint16_t smslen;
if (! fona.readSMS(smsn, replybuffer, 250, &smslen)) { // pass in buffer and max len!
Serial.println("Failed!");
break;
}
Serial.print(F("***** SMS #")); Serial.print(smsn);
Serial.print(" ("); Serial.print(smslen); Serial.println(F(") bytes *****"));
Serial.println(replybuffer);
Serial.println(F("*****"));
break;
}
case 'R': {
// read all SMS
int8_t smsnum = fona.getNumSMS();
uint16_t smslen;
for (int8_t smsn=1; smsn<=smsnum; smsn++) {
Serial.print(F("\n\rReading SMS #")); Serial.println(smsn);
if (!fona.readSMS(smsn, replybuffer, 250, &smslen)) { // pass in buffer and max len!
Serial.println(F("Failed!"));
break;
}
// if the length is zero, its a special case where the index number is higher
// so increase the max we'll look at!
if (smslen == 0) {
Serial.println(F("[empty slot]"));
smsnum++;
continue;
}
Serial.print(F("***** SMS #")); Serial.print(smsn);
Serial.print(" ("); Serial.print(smslen); Serial.println(F(") bytes *****"));
Serial.println(replybuffer);
Serial.println(F("*****"));
}
break;
}
case 'd': {
// delete an SMS
flushSerial();
Serial.print(F("Delete #"));
uint8_t smsn = readnumber();
Serial.print(F("\n\rDeleting SMS #")); Serial.println(smsn);
if (fona.deleteSMS(smsn)) {
Serial.println(F("OK!"));
} else {
Serial.println(F("Couldn't delete"));
}
break;
}
case 's': {
// send an SMS!
char sendto[21], message[141];
flushSerial();
Serial.print(F("Send to #"));
readline(sendto, 20);
Serial.println(sendto);
Serial.print(F("Type out one-line message (140 char): "));
readline(message, 140);
Serial.println(message);
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("Sent!"));
}
break;
}
/*** Time ***/
case 'y': {
// enable network time sync
if (!fona.enableNetworkTimeSync(true))
Serial.println(F("Failed to enable"));
break;
}
case 'Y': {
// enable NTP time sync
if (!fona.enableNTPTimeSync(true, F("pool.ntp.org")))
Serial.println(F("Failed to enable"));
break;
}
case 't': {
// read the time
char buffer[23];
fona.getTime(buffer, 23); // make sure replybuffer is at least 23 bytes!
Serial.print(F("Time = ")); Serial.println(buffer);
break;
}
/*********************************** GPS (SIM808 only) */
case 'o': {
// turn GPS off
if (!fona.enableGPS(false))
Serial.println(F("Failed to turn off"));
break;
}
case 'O': {
// turn GPS on
if (!fona.enableGPS(true))
Serial.println(F("Failed to turn on"));
break;
}
case 'x': {
int8_t stat;
// check GPS fix
stat = fona.GPSstatus();
if (stat < 0)
Serial.println(F("Failed to query"));
if (stat == 0) Serial.println(F("GPS off"));
if (stat == 1) Serial.println(F("No fix"));
if (stat == 2) Serial.println(F("2D fix"));
if (stat == 3) Serial.println(F("3D fix"));
break;
}
case 'L': {
// check for GPS location
char gpsdata[80];
fona.getGPS(0, gpsdata, 80);
Serial.println(F("Reply in format: mode,longitude,latitude,altitude,utctime(yyyymmddHHMMSS),ttff,satellites,speed,course"));
Serial.println(gpsdata);
break;
}
case 'E': {
flushSerial();
Serial.print(F("GPS NMEA output sentences (0 = off, 34 = RMC+GGA, 255 = all)"));
uint8_t nmeaout = readnumber();
// turn on NMEA output
fona.enableGPSNMEA(nmeaout);
break;
}
/*********************************** GPRS */
case 'g': {
// turn GPRS off
if (!fona.enableGPRS(false))
Serial.println(F("Failed to turn off"));
break;
}
case 'G': {
// turn GPRS on
if (!fona.enableGPRS(true))
Serial.println(F("Failed to turn on"));
break;
}
case 'l': {
// check for GSMLOC (requires GPRS)
uint16_t returncode;
if (!fona.getGSMLoc(&returncode, replybuffer, 250))
Serial.println(F("Failed!"));
if (returncode == 0) {
Serial.println(replybuffer);
} else {
Serial.print(F("Fail code #")); Serial.println(returncode);
}
break;
}
case 'w': {
// read website URL
uint16_t statuscode;
int16_t length;
char url[80];
flushSerial();
Serial.println(F("NOTE: in beta! Use small webpages to read!"));
Serial.println(F("URL to read (e.g. www.adafruit.com/testwifi/index.html):"));
Serial.print(F("http://")); readline(url, 79);
Serial.println(url);
Serial.println(F("****"));
if (!fona.HTTP_GET_start(url, &statuscode, (uint16_t *)&length)) {
Serial.println("Failed!");
break;
}
while (length > 0) {
while (fona.available()) {
char c = fona.read();
// Serial.write is too slow, we'll write directly to Serial register!
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */
UDR0 = c;
#else
Serial.write(c);
#endif
length--;
if (! length) break;
}
}
Serial.println(F("\n****"));
fona.HTTP_GET_end();
break;
}
case 'W': {
// Post data to website
uint16_t statuscode;
int16_t length;
char url[80];
char data[80];
flushSerial();
Serial.println(F("NOTE: in beta! Use simple websites to post!"));
Serial.println(F("URL to post (e.g. httpbin.org/post):"));
Serial.print(F("http://")); readline(url, 79);
Serial.println(url);
Serial.println(F("Data to post (e.g. \"foo\" or \"{\"simple\":\"json\"}\"):"));
readline(data, 79);
Serial.println(data);
Serial.println(F("****"));
if (!fona.HTTP_POST_start(url, F("text/plain"), (uint8_t *) data, strlen(data), &statuscode, (uint16_t *)&length)) {
Serial.println("Failed!");
break;
}
while (length > 0) {
while (fona.available()) {
char c = fona.read();
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */
UDR0 = c;
#else
Serial.write(c);
#endif
length--;
if (! length) break;
}
}
Serial.println(F("\n****"));
fona.HTTP_POST_end();
break;
}
/*****************************************/
case 'S': {
Serial.println(F("Creating SERIAL TUBE"));
while (1) {
while (Serial.available()) {
delay(1);
fona.write(Serial.read());
}
if (fona.available()) {
Serial.write(fona.read());
}
}
break;
}
default: {
Serial.println(F("Unknown command"));
printMenu();
break;
}
}
// flush input
flushSerial();
while (fona.available()) {
Serial.write(fona.read());
}
}
void flushSerial() {
while (Serial.available())
Serial.read();
}
char readBlocking() {
while (!Serial.available());
return Serial.read();
}
uint16_t readnumber() {
uint16_t x = 0;
char c;
while (! isdigit(c = readBlocking())) {
//Serial.print(c);
}
Serial.print(c);
x = c - '0';
while (isdigit(c = readBlocking())) {
Serial.print(c);
x *= 10;
x += c - '0';
}
return x;
}
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout) {
uint16_t buffidx = 0;
boolean timeoutvalid = true;
if (timeout == 0) timeoutvalid = false;
while (true) {
if (buffidx > maxbuff) {
//Serial.println(F("SPACE"));
break;
}
while(Serial.available()) {
char c = Serial.read();
//Serial.print(c, HEX); Serial.print("#"); Serial.println(c);
if (c == '\r') continue;
if (c == 0xA) {
if (buffidx == 0) // the first 0x0A is ignored
continue;
timeout = 0; // the second 0x0A is the end of the line
timeoutvalid = true;
break;
}
buff[buffidx] = c;
buffidx++;
}
if (timeoutvalid && timeout == 0) {
//Serial.println(F("TIMEOUT"));
break;
}
delay(1);
}
buff[buffidx] = 0; // null term
return buffidx;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment