I am working on a middle school project that uses the Fona, the Ultimate GPS, and a piezoelectric senor. The objective is to use the piezo to trigger the fona into sending the GPS coordinates given by the Ultimate GPS. I dont believe that the program that I am using is getting past the void setup. The only thing shown by the Serial Monitor is the fona being put into text mode. I've checked my wiring and I'm pretty sure it is correct. The wiring is in the beginning of the program if you need to see it.
//GPS
#include <TinyGPS.h>
#include <SoftwareSerial.h>
long lat,lon; // create variable for latitude and longitude objec
SoftwareSerial gpsSerial(8, 7); // create gps sensor connection
TinyGPS gps; // create gps object
/*
Hardware:
Arduino Uno
Ultimate GPS
Adafruit Fona
Piezoelectric Sensor
Lithium Battery
The piezo circuit:
* + connection of the piezo attached to analog in 0
* - connection of the piezo attached to ground
* 1-megohm resistor attached from analog in 0 to ground
The Fona Curcuit:
* Vio connection of the Fona connected to 5v
* RX connection of the Fona connected to TX
* TX connection of the Fona connected to RX
* Key connection of the Fona connected to Ground
* Ground connection of the Fona connected to Ground
* Lithium Battery
The GPS Curcuit:
* The Ultimate GPS stacked onto the Arduino
*/
// these constants won't change:
const int ledPin = 13; // led connected to digital pin 13
const int AirbagSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 4; // threshold value to decide when an accident has occured
// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
int ledState = LOW; // variable used to store the last LED status, to toggle the light
//Code to send SMS from Arduino
int timesTosend=1; //Setting the integer for "timesTosend" to 1
int count=0; //Setting the integer for "count" to 0
char phone_no[]="4045551212"; //phone number
void setup()
{
pinMode(ledPin, OUTPUT); // declare the ledPin as OUTPUT
Serial.begin(9600); //Open serial connection at baud 9600
delay(2000);
Serial.println("AT+CMGF=1"); //Set GSM to text mode
delay(2000);
gpsSerial.begin(9600); // connect gps sensor
delay(2000);
}
void loop()
{
delay(1000);
//GPS
while(gpsSerial.available()){
if(gps.encode(gpsSerial.read())){
gps.get_position(&lat,&lon); // get latitude and longitude
//SOS Trigger
sensorReading = analogRead(AirbagSensor);
if (sensorReading >= threshold)
{
while(count<timesTosend){
delay(1500);
Serial.print("AT+CMGS=\"770777777777"); //This is where you imput the emergency number
Serial.print(phone_no);
Serial.println("\"");
while(Serial.read()!='>');
{
// display position
Serial.print("Position: lat: lon: ");
Serial.print("lat: ");Serial.print(lat);Serial.print(" ");// print latitude
Serial.print("lon: ");Serial.println(lon); // print longitude
delay(500);
Serial.write(0x1A); //sends ctrl+z end of message
Serial.write(0x0D); //Carraige Return in Hex
Serial.write(0x0A); //Line Feed in Hex
delay(5000);
}
count++;
}
}
}
}
}
Last edited by robothand on Mon Nov 17, 2014 5:57 pm, edited 1 time in total.
I currently have the Ultimate GPS on software Serial using pins 7 and 8. Is there a conflict setting up a software serial for the Fona using ports 2 and 3? I tried setting up the fona with software serial on pins 2 and 3 but the program didnt work. Below is the SMS code that I was using at first but i need to convert it to software serial on pins 2 and 3. How would I do this? Thanks.
I tried to import the program that you gave me into the overall program that I plan to use for my project but it is still giving me a blank. Any help would be appreciated.
//GPS
#include <TinyGPS.h>
#include <SoftwareSerial.h>
long lat,lon; // create variable for latitude and longitude objec
SoftwareSerial fona = SoftwareSerial(2,3); //
SoftwareSerial gpsSerial(8, 7); // create gps sensor connection
TinyGPS gps; // create gps object
/*
Hardware:
Arduino Uno
Ultimate GPS
Adafruit Fona
Piezoelectric Sensor
Lithium Battery
The piezo circuit:
* + connection of the piezo attached to analog in 0
* - connection of the piezo attached to ground
* 1-megohm resistor attached from analog in 0 to ground
The Fona Curcuit:
* Vio connection of the Fona connected to 5v
* RX connection of the Fona connected to Digital 3
* TX connection of the Fona connected to Digital 2
* Key connection of the Fona connected to Ground
* Ground connection of the Fona connected to Ground
* Lithium Battery
The GPS Curcuit:
* The Ultimate GPS stacked onto the Arduino
*/
// these constants won't change:
const int ledPin = 13; // led connected to digital pin 13
const int AirbagSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 4; // threshold value to decide when an accident has occured
// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
int ledState = LOW; // variable used to store the last LED status, to toggle the light
//Code to send SMS from Arduino
int timesTosend=1; //Setting the integer for "timesTosend" to 1
int count=0; //Setting the integer for "count" to 0
char phone_no[]="+404555555555"; //phone number
void setup()
{
pinMode(ledPin, OUTPUT); // declare the ledPin as OUTPUT
fona.begin(9600); //Open serial connection at baud 9600
delay(2000);
fona.println("AT+CMGF=1"); //Set GSM to text mode
delay(2000);
gpsSerial.begin(9600); //
delay(2000);
}
void loop()
{
delay(1000);
//GPS
while(gpsSerial.available()){
if(gps.encode(gpsSerial.read())){
gps.get_position(&lat,&lon); // get latitude and longitude
//SOS Trigger
sensorReading = analogRead(AirbagSensor);
if (sensorReading >= threshold)
{
while(count<timesTosend){
delay(1500);
fona.print("AT+CMGS=\"+404555555555"); //This is where you imput the emergency number
fona.print(phone_no);
fona.println("\"");
while(Serial.read()!='>');
{
// display position
fona.print("Position: lat: lon: ");
fona.print("lat: ");Serial.print(lat);Serial.print(" ");// print latitude
fona.print("lon: ");Serial.println(lon); // print longitude
delay(500);
fona.write(0x1A); //sends ctrl+z end of message
fona.write(0x0D); //Carraige Return in Hex
fona.write(0x0A); //Line Feed in Hex
delay(5000);
}
count++;
}
}
}
}
}
//Code to send SMS from Arduino
//Using Rx and Tx pins
int timesTosend=1;
int count=0;
char phone_no[]=""; //phone number
void setup()
{
Serial.begin(9600); //Open serial connection at baud 9600
delay(2000);
Serial.println("AT+CMGF=1"); //Set GSM to text mode
delay(200);
}
void loop()
{
while(count<timesTosend){
delay(1500);
Serial.print("AT+CMGS=\"4045551212");
Serial.print(phone_no);
Serial.println("\"");
while(Serial.read()!='>');
{
Serial.print("I got it to work on the Fona Phone");
delay(500);
Serial.write(0x1A); //sends ctrl+z end of message
Serial.write(0x0D); //
Serial.write(0x0A);
delay(5000);
}
count++;
}
}
robothand wrote:Does your serial monitor work on your end when you run the code below? The program works but I see no activity on the serial monitor.
No. That program doesn't write anything to the serial monitor. You have to add Serial.print statements to it to see anything on serial monitor, like this:
Thanks so much for you help on the code that you provided. I saw from your posting that you are in Buffalo. Not sure if you live there now but if you do hang in there. Well working on the last piece of the puzzle. The sensor program works with the fona program. Now I'm trying to integrate the GPS program
So far with the project the sensor values in the program is set to true. Now I'm trying to have the fona code get the GPS coordinates when the sensor values go true. and send the sms.
Below is the fona code with the sensor values true
//GPS
long lat,lon; // create variable for latitude and longitude object
SoftwareSerial gpsSerial(8, 7); // create gps sensor connection
TinyGPS gps; // create gps object
SoftwareSerial fona = SoftwareSerial(2,3); //pin 2 is tx from FONA, pin 3 is RX from FONA
//Code to send SMS from Arduino
int timesTosend=1;
int count=0;
char phone_no[]=""; //phone number
int sensorReading = 6;
const int threshold = 4;
void setup()
{
Serial.begin(9600);
gpsSerial.begin(9600); // connect gps sensor
Serial.println(F("Fona SMS test"));
fona.begin(9600); //Open serial connection at baud 9600
delay(2000);
Serial.println(F("Set GSM to text mode"));
fona.println("AT+CMGF=1"); //Set GSM to text mode
delay(200);
}
void loop()
{
if (sensorReading >= threshold) {
while(count<timesTosend){
delay(1500);
while(gpsSerial.available()){ // check for gps data
if(gps.encode(gpsSerial.read())){ // encode gps data
gps.get_position(&lat,&lon); // get latitude and longitud
Serial.println(F("Enter phone number"));
fona.print("AT+CMGS=\"4046631873");
fona.print(phone_no);
fona.println("\"");
while(fona.read()!='>');
{
Serial.println(F("Send text"));
fona.print("lat:");
delay(500);
fona.write(0x1A); //sends ctrl+z end of message
fona.write(0x0D); //
fona.write(0x0A);
delay(5000);
}
count++;
}
}
}
}
}
The program run to the point is sets to GSM to text mode and stops. What I'm I doing wrong?
The problem is that you are trying to read the GPS only when you detect that the sensor is above the threshold, and you've got a delay of 1500ms in there. You have to remember that tinyGPS is reading the GPS report one character at a time, so you're got to be there reading available characters all the time.
I adjusted the code to always read characters from GPS:
#include <TinyGPS.h>
#include <SoftwareSerial.h>
#include <FONA.h>
//GPS
long lat,lon; // create variable for latitude and longitude object
SoftwareSerial gpsSerial(8, 7); // create gps sensor connection
TinyGPS gps; // create gps object
SoftwareSerial fona = SoftwareSerial(2,3); //pin 2 is tx from FONA, pin 3 is RX from FONA
//Code to send SMS from Arduino
int timesTosend=1;
int count=0;
char phone_no[]=""; //phone number
int sensorReading = 6;
const int threshold = 4;
bool gotPosition = false; //set to true when we have a GPS position
void setup()
{
Serial.begin(9600);
gpsSerial.begin(9600); // connect gps sensor
Serial.println(F("Fona SMS test"));
fona.begin(9600); //Open serial connection at baud 9600
delay(2000);
Serial.println(F("Set GSM to text mode"));
fona.println("AT+CMGF=1"); //Set GSM to text mode
delay(200);
}
void loop()
{
while(gpsSerial.available()){ // check for gps data
if(gps.encode(gpsSerial.read())){ // encode gps data
gps.get_position(&lat,&lon); // get latitude and longitude
gotPosition = true;
}
}
if ((gotPosition) && (sensorReading >= threshold)) {
while(count<timesTosend){
Serial.println(F("Enter phone number"));
fona.print("AT+CMGS=\"4046631873");
fona.print(phone_no);
fona.println("\"");
while(fona.read()!='>');
{
Serial.println(F("Send text"));
fona.print("lat:");
delay(500);
fona.write(0x1A); //sends ctrl+z end of message
fona.write(0x0D); //
fona.write(0x0A);
delay(5000);
}
count++;
}
}
}
Thanks so much for your help. I made some adjustment to the program and it works. I'm getting 0 for lon and lat and I'm thinking that due to the fact I'm inside a building. It's raining outside now and I'll give a try later.
Thanks but there's one more problem.
When I upload the program to the Arduino the text gets sent but the coordinates that are sent are always Latitude=0 Longitude=0. Where in the program do I need to look to fix this problem.
#include <TinyGPS.h>
#include <SoftwareSerial.h>
#include <FONA.h>
//GPS
long lat,lon; // create variable for latitude and longitude object
SoftwareSerial gpsSerial(8, 7); // create gps sensor connection
TinyGPS gps; // create gps object
SoftwareSerial fona = SoftwareSerial(2,3); //pin 2 is tx from FONA, pin 3 is RX from FONA
//Code to send SMS from Arduino
int timesTosend=1;
int count=0;
char phone_no[]=""; //phone number
int sensorReading = 10;
const int threshold = 4;
bool gotPosition = true; //set to true when we have a GPS position
void setup()
{
Serial.begin(9600);
gpsSerial.begin(9600); // connect gps sensor
Serial.println(F("Fona SMS test"));
fona.begin(9600); //Open serial connection at baud 9600
delay(2000);
Serial.println(F("Set GSM to text mode"));
fona.println("AT+CMGF=1"); //Set GSM to text mode
delay(200);
}
void loop()
{
while(gpsSerial.available()){ // check for gps data
if(gps.encode(gpsSerial.read())){ // encode gps data
gps.get_position(&lat,&lon); // get latitude and longitude
gotPosition = true;
}
}
if ((gotPosition) && (sensorReading >= threshold)) {
while(count<timesTosend){
Serial.print("lat:"); Serial.print(lat); Serial.print(" ");
Serial.print("lon:"); Serial.println(lon);
Serial.println(F("Enter phone number"));
fona.print("AT+CMGS=\"404555555555");
fona.print(phone_no);
fona.println("\"");
while(fona.read()!='>');
{
Serial.println(F("Send text"));
fona.println("Reporting an accident at the following location ");
fona.print("lat: ");fona.print(lat);fona.print(" ");// print latitude
fona.print("lon: ");fona.println(lon); // print longitude
delay(500);
fona.write(0x1A); //sends ctrl+z end of message
fona.write(0x0D); //
fona.write(0x0A);
delay(5000);
}
count++;
}
}
}