I'm trying to monitor the sensor values (range 0-1000) connected to Arduino UNO's 12th-pin. If the value stays below say 500, then it should SEND SMS. I tested the sensor and GSM shield seperately and both of them are working fine. But my program is not giving the sensor reading in the serial monitor nor sending the SMS. I'm pretty sure it got stuck at sendRequest() part of the code.
// the setup routine runs once when you press reset:
#include "Adafruit_FONA.h"
#define FONA_TX 2
#define FONA_RX 3
#define FONA_RST 4
// this is a large buffer for replies
//char replybuffer[255];
char buffer[50];
char r[15];
// We default to using software serial. If you want to use hardware serial
// (because softserial isnt supported) comment out the following three lines
// and uncomment the HardwareSerial line
#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX,FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;
SoftwareSerial Sensor_Serial(12,13);
// Hardware serial is also possible!
// HardwareSerial *fonaSerial = &Serial1;
// Use this for FONA 800 and 808s
//Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
// Use this one for 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 readCO[] = {0xFF, 0X01, 0X86, 0X00, 0X00, 0X70};
byte response[] = {0,0,0,0,0,0};
//multiplier for value. default is 1.
int valMultiplier = 1;
void setup() {
while (!Serial);
Serial.begin(9600);
Sensor_Serial.begin(9600); //Opens the virtual serial port with a baud of 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 is 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()
{
sendRequest(readCO);
float valCO = getValue(response);
static unsigned long startTime = 0;
if (valCO < 500)
dtostrf(valCO, 3, 2, r); // I don't know how to covert the unsigned long variable to string and I know //this part does the float to string conversion
sprintf(buffer, "%s High Level", r);
Serial.print("CO = ");
Serial.println(valCO);
if (millis() - startTime > 1UL*60UL*1000UL) { // Has it been 1 minute since the value was below 500?
startTime = millis();
fona.sendSMS("0XXXXXXX", buffer);
}
}
void sendRequest(byte packet[])
{
while(!Sensor_Serial.available()) // If this part works the entire code works I think!!
//keep sending request until we start to get a response
{
//Sensor_Serial.write(readCO,9);
delay(50);
}
int timeout=0; //set a timeoute counter
while(Sensor_Serial.available() < 9 ) //Wait to get a 9 byte response
{
timeout++;
if(timeout > 10) //if it takes to long there was probably an error
{
while(Sensor_Serial.available()) //flush whatever we have
Sensor_Serial.read();
break; //exit and try again
}
delay(50);
}
for (int i=0; i < 9; i++)
{
response[i] = Sensor_Serial.read();
}
}
unsigned long getValue(byte packet[])
{
int high = packet[2]; //high byte for value is 3rd byte in packet in the packet
int low = packet[3]; //low byte for value is 4th byte in the packet
unsigned long val = high*256 + low; //Combine high byte and low byte with this formula to get value
return val* valMultiplier;
}
OUTPUT: . . . . FONA is OK Found FONA 3G (European) ---> AT+GSN <--- 8XXXXXXXXXXXXXX Module IMEI: 8XXXXXXXXXXXX
I guess this part of the is waiting for like ever and not returning any response
while(!Sensor_Serial.available()) //keep sending request until we start to get a response
{
Sensor_Serial.write(readCO,9);
delay(50);
}
Any help guys?