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?
-
If I was you I would add a few more print statements to your code to prove that's where it is stopping. It might be worth checking if pins 12 and 13 are reserved by the shields for something.Code Gorilla– Code Gorilla2017年08月08日 07:16:15 +00:00Commented Aug 8, 2017 at 7:16
-
Yes inside the while loop it printed a statement. I mean after Sensor_Serial.write(readCO,9); Serial.print("CO ppm = "); OUTPUT: I can see the CO ppm= and I have tested myself pin 12 and 13 they don't seem reserved by the GSM as I got the sensor output. But the code doesn't seem to work with the UART and SMS together...LD_959– LD_9592017年08月09日 06:21:33 +00:00Commented Aug 9, 2017 at 6:21
1 Answer 1
You have multiple software serial ports and are expecting them to work like real ones.
They don't.
You can only have one of them "active" at a time (that is, ready to receive data). You need to switch between them with Sensor_Serial.listen()
and fonaSerial->listen()
.
In general it is bad to have more than one SoftwareSerial (and personally I think even one is bad) since they are very heavyweight and block all other operations while they are either sending or receiving data (which means your listening one cannot operate at all while your non-listening one happens to be sending).
If you have a requirement for more than one serial port you should really be using a board that has more than one serial port.
-
In that case, to not use software serial ports I tried one of the four UART's on Arduino Mega for sensor. Can please tell me how do I connect my sensor to the RX0 and TX0. As I have tried assigning sensor pins as: SoftwareSerial Sensor_Serial(0,1); But that didn't make any difference. Please, any fix for this line of code as it is stopping the functioning of sensor's part - while(!Sensor_Serial.available()) as nothing works from this part on.LD_959– LD_9592017年08月09日 06:27:53 +00:00Commented Aug 9, 2017 at 6:27
-
1If you are using a real serial port then you do not try and map it to SoftwareSerial. I believe T/Rx0 are called
Serial
, T\Rx1 is calledSerial1
etc.Code Gorilla– Code Gorilla2017年08月09日 07:29:44 +00:00Commented Aug 9, 2017 at 7:29 -
@LD_959 Firstly: do not use pins 0/1 - they are used by your existing
Serial
connection. Secondly: you must not useSoftwareSerial
on pins you expect to be using as a real UART. That is just plain daft. Why use hardware serial pins as GPIO and then slap SoftwareSerial over the top? All UART pins haveSerialX
objects assigned to them. TX0/RX0 isSerial
, TX1/RX1 isSerial1
etc.Majenko– Majenko2017年08月09日 11:58:08 +00:00Commented Aug 9, 2017 at 11:58 -
Using Serial1 for the sensor worked perfectly. Thank you guys...!!LD_959– LD_9592017年08月10日 00:09:22 +00:00Commented Aug 10, 2017 at 0:09
-
I have 3 sensors connected to other UART's of Arduino.. can you please tell me how do I print the output sensor readings in this format: CO1= val1 CO2=val2 CO3=val3LD_959– LD_9592017年08月10日 02:21:58 +00:00Commented Aug 10, 2017 at 2:21
Explore related questions
See similar questions with these tags.