Fona 808 sms code

I am using Flora micro controller and Fona 808 GSM/GPS module. I need to send a text message when push button is pressed. I tried altering the fona test coding in Adafruit fona library in Arduino IDE.

But in altered code the message is sent many times without the button being pressed. Please help me out with the problem, as I am new to coding.

#include "Adafruit_FONA.h"

#define FONA_RX 10
#define FONA_TX 9
#define FONA_RST 6


const int buttonPin = 12;
int buttonState = 0;

#include <SoftwareSerial.h>

SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;

Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);

uint8_t type;

void setup() {
  while (!Serial);

  Serial.begin(115200);
  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);
  }
  pinMode(buttonPin, INPUT);
  // Print module 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("Module IMEI: "); 
    Serial.println(imei);
  }
}

void loop() {
  Serial.print(F("FONA> "));
    char sendto[21]="70########", message[141]="Hiii";
        flushSerial(); 
        buttonState = digitalRead(buttonPin);
        if (buttonState == HIGH) {
        fona.sendSMS(sendto, message);
         Serial.println(F("Sent!"));
        }
        else {
         Serial.println(F("Failed"));
        }
   flushSerial();
}
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;
}

Hey Pushkala,

The first thing I'm seeing is the imei buffer is set to 15 instead of 16 (which is what it should be).

I also don understand why the message is set to 141, that's very high.

As I'm much more of a hardware guy I would recommend testing your circuit, some buttons have two ins and two outs, if you're using both of the ins or outs then you'll just get a constant signal to the arduino to send.

But in altered code the message is sent many times without the button being pressed

Maybe the input is floating.
You should really only send a message when the switch becomes pressed not while it is pressed.

Is the button connecting the input pin to supply or to ground when its pressed? For this code it looks like it should be connected to supply to activate.

It is good practice to use a 10K pull down resistor between the input pin and ground. This helps prevent interference from messing with the input signal.

To simplify the circuit you may want to consider changing over the logic so the input pin is set high with the internal pull-up resistor and the switch is connected to ground instead of 5V. all you would need to do is change HIGH for LOW in the main loop and initialise the pullup resistor in the setup loop by adding

digitalWrite(buttonPin, HIGH);

As a side note, i would also consider adding a loop where the code waiting for the input pin to return high after a button press to avoid switch bounce (which may cause multiple triggers). Here is an example of code I used where i have a pushbutton grounding my input pin and once the button is released the code then moves on to the next step in the code...

int pos = 0; // variable to store the servo position
int x = 0;
int t = 0;
int y = 0;
int button = 5;

void setup() {
myservo1.attach(9); // attaches the servo1 on pin 9 to the servo object
myservo2.attach(10); // attaches the servo2 on pin 10 to the servo object
pinMode(button, INPUT);
** digitalWrite(button, HIGH)**;
}

void loop()

{
myservo1.write(22); // inital position of servo1
myservo2.write(22); // inital position of servo2

if (digitalRead(button) == LOW)
** { **
** x = 0; // Wait for trigger to return high for a solid 50ms**
** while(x < 50) // (necessary to avoid switch bounce**
** {**
** if (digitalRead(button) == HIGH)**
** x++;**
** else**
** x = 0;**
** delay(1);**
** }**

I hope this is of some help.