I'm using software serial on my weather station. The way it's supposed to work is that you send an SMS to the device and it replies with the wind speed, etc.
At the moment, The wind speed is detected, waiting for an incoming SMS. The SMS arrives and it sends a response but the response SMS is sent over and over infinitely. I'm guessing the problem is due to the:
if (mySerial.available()>0){
As this is what I'm using to test whether the SMS has arrived and presumably, it's then always set to available.
Does anyone know how to basically reset the software serial after the message has been sent?
#include <SoftwareSerial.h>
#include <ADSWeather.h>
SoftwareSerial mySerial(9, 10);
#define ANEMOMETER_PIN 2
#define VANE_PIN 0
#define RAIN_PIN 3
#define CALC_INTERVAL 1000
unsigned long nextCalc;
unsigned long timer;
int windDir;
int windSpeed;
int rainAmmount;
ADSWeather ws1(RAIN_PIN, VANE_PIN, ANEMOMETER_PIN); //This should configure all pins correctly
void setup()
{
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(RAIN_PIN), ws1.countRain, FALLING); //ws1.countRain is the ISR for the rain gauge.
attachInterrupt(digitalPinToInterrupt(ANEMOMETER_PIN), ws1.countAnemometer, FALLING); //ws1.countAnemometer is the ISR for the anemometer.
nextCalc = millis() + CALC_INTERVAL;
mySerial.begin(9600); // Setting the baud rate of GSM Module
delay(100);
}
void loop()
{
timer = millis();
int rainAmmount;
long windSpeed;
long windDirection;
int windGust;
int sent;
ws1.update(); //Call this every cycle in your main loop to update all the sensor values
if(timer > nextCalc)
{
nextCalc = timer + CALC_INTERVAL;
rainAmmount = ws1.getRain();
windSpeed = ws1.getWindSpeed();
windDirection = ws1.getWindDirection();
windGust = ws1.getWindGust();
// windSpeed / 10 will give the interger component of the wind speed
// windSpeed % 10 will give the fractional component of the wind speed
Serial.print("Wind speed: ");
Serial.print(windSpeed / 10);
Serial.print('.');
Serial.print(windSpeed % 10);
Serial.print(" ");
Serial.print("Gusting at: ");
Serial.print(windGust / 10);
Serial.print('.');
Serial.print(windGust % 10);
Serial.println("");
}
if (Serial.available()>0)
{
RecieveMessage();
}
//If an message comes in
if (mySerial.available()>0){
// Serial.write(mySerial.read());
Serial.write("Message received");
//Send the wind speed response.
SendMessage();
//At this point the SendMessage() repeats and the message is sent over and over.
}
}
void SendMessage()
{
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
mySerial.println("AT+CMGS=\"NUMRemoved\"\r"); // Replace x with mobile number
delay(1000);
mySerial.println("I am SMS from GSM Module");// The SMS text you want to send
delay(100);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(1000);
}
void RecieveMessage()
{
mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
delay(1000);
}
void serialFlush(){
while(mySerial.available() > 0) {
char t = mySerial.read();
}
}
serialFlush()function in the sketch. use it.serialFlush();