c++ - How to read the last text message sent to the buffer(stored in SIM card)-Arduino -
i'm using fona 3g (sim5320e) arduino mega 2560.
i have buffer store text messages (sms) - char replybuffer [255]
. i'm sending 10-digit phone number occupying 10 bytes in replybuffer
per text message. suppose, have sent 5 text messages sim card , of them stored in replybuffer
. how can read last 10-digit number or last text message in buffer can send sms last mobile number rather 1st mobile number done currently?
// setup routine runs once when press reset: #include "adafruit_fona.h" #define fona_tx 11 #define fona_rx 12 #define fona_rst 9 // large buffer replies char replybuffer[255]; char buffer[50]; char r1[6];char r2[6]; // default using software serial. if want use hardware serial // (because softserial isnt supported) comment out following 3 lines // , uncomment hardwareserial line #include <softwareserial.h> softwareserial fonass = softwareserial(fona_tx,fona_rx); softwareserial *fonaserial = &fonass; // hardware serial possible! // hardwareserial *fonaserial = &serial1; // use fona 800 , 808s //adafruit_fona fona = adafruit_fona(fona_rst); // use 1 fona 3g adafruit_fona_3g fona = adafruit_fona_3g(fona_rst); uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0); uint8_t type; byte readco1[] = {0xff, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00,0x79}; //command packet read co (see app note) byte response1[] = {0,0,0,0,0,0,0,0,0}; //create array store response byte readco2[] = {0xff, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00,0x79}; //command packet read co (see app note) byte response2[] = {0,0,0,0,0,0,0,0,0}; //multiplier value. default 1. set 25 o2 25% int valmultiplier1 = 1;int valmultiplier2 = 1; void setup() { while (!serial); serial.begin(9600); serial1.begin(9600); //opens virtual serial port baud of 9600 serial2.begin(9600); serial.println(f("fona basic test")); serial.println(f("initializing....(may take 3 seconds)")); fonaserial->begin(4800); if (! fona.begin(*fonaserial)) { serial.println(f("couldn't find fona")); while (1); } type = fona.type(); serial.println(f("fona ok")); serial.print(f("found ")); switch (type) { case fona800l: serial.println(f("fona 800l")); break; case fona800h: serial.println(f("fona 800h")); break; case fona808_v1: serial.println(f("fona 808 (v1)")); break; case fona808_v2: serial.println(f("fona 808 (v2)")); break; case fona3g_a: serial.println(f("fona 3g (american)")); break; case fona3g_e: serial.println(f("fona 3g (european)")); break; default: serial.println(f("???")); break; } } void loop() { //get readings of sensors sendrequest1(readco1); int valco1 = getvalue1(response1); sendrequest2(readco2); int valco2 = getvalue2(response2); // timer initialization static unsigned long starttime = 0; uint16_t smslen; int16_t slot; if (! fona.readsms(slot, replybuffer, 250, &smslen)) { // pass in buffer , max len! serial.println("failed!"); } //loop function deleting first byte upon second or latest byte arrival if (valco1 < 5 && valco2 < 5 ) // fona.deletesms(0); // has been 1 minute since value below 5? if (millis() - starttime > 1ul*60ul*1000ul) { //sendalert(); // memset(replybuffer, 0, sizeof replybuffer); starttime = millis(); fona.sendsms(replybuffer, "got it"); } serial.print("co1 ppm = "); serial.print(valco1); serial.print(" co2 ppm = "); serial.println(valco2); } void sendrequest1(byte packet1[]) { while(!serial1.available()) //keep sending request until start response { serial1.write(readco1,9); delay(50); } int timeout=0; //set timeoute counter while(serial1.available() < 9 ) //wait 9 byte response { timeout++; if(timeout > 10) //if takes long there error { while(serial1.available()) //flush whatever have serial1.read(); break; //exit , try again } delay(50); } (int i=0; < 9; i++) { response1[i] = serial1.read(); } } unsigned long getvalue1(byte packet1[]) { int high1 = packet1[2]; //high byte value 3rd byte in packet in packet int low1 = packet1[3]; //low byte value 4th byte in packet unsigned long val1 = high1*256 + low1; //combine high byte , low byte formula value return val1* valmultiplier1; } void sendrequest2(byte packet2[]) { while(!serial2.available()) //keep sending request until start response { serial2.write(readco2,9); delay(50); } int timeout=0; //set timeoute counter while(serial2.available() < 9 ) //wait 9 byte response { timeout++; if(timeout > 10) //if takes long there error { while(serial2.available()) //flush whatever have serial2.read(); break; //exit , try again } delay(50); } (int i=0; < 9; i++) { response2[i] = serial2.read(); } } unsigned long getvalue2(byte packet2[]) { int high2 = packet2[2]; //high byte value 3rd byte in packet in packet int low2= packet2[3]; //low byte value 4th byte in packet unsigned long val2 = high2*256 + low2; //combine high byte , low byte formula value return val2* valmultiplier2; }
link download fona 3g library.
open fonatest program have @ different commands.
Comments
Post a Comment