I am an extremely armature coder and am currently working on an alert system that will send a message to a given phone number detailing there location. While I am able to upload my code to the hardware, when the button is pressed, no message is sent. I've run the program through several emulators and still can figure out the problem. The code is listed below. Thank you!!!
#include <SoftwareSerial.h>
SoftwareSerial sim808(0,1);
char phone_no[] = "xxxxxxxxxxx"; // replace with your phone no.
String data[5];
#define DEBUG true
String state,timegps,latitude,longitude;
void GPS () {
sendTabData("AT+CGNSINF",1000,DEBUG);
if (state !=0) {
Serial.println("State :"+state);
Serial.println("Time :"+timegps);
Serial.println("Latitude :"+latitude);
Serial.println("Longitude :"+longitude);
sim808.print("AT+CMGS=\"");
sim808.print(phone_no);
sim808.println("\"");
delay(300);
sim808.print("http://maps.google.com/maps?q=loc:");
sim808.print(latitude);
sim808.print(",");
sim808.print (longitude);
delay(200);
sim808.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(200);
sim808.println();
delay(20000);
sim808.flush();
} else {
Serial.println("GPS Initializing…");
}
}
void sendTabData(String command , const int timeout , boolean debug){
sim808.println(command);
long int time = millis();
int i = 0;
while((time+timeout) > millis()){
while(sim808.available()){
char c = sim808.read();
if (c != ',') {
data[i] +=c;
delay(100);
} else {
i++;
}
if (i == 5) {
delay(100);
goto exitL;
}
}
}exitL:
if (debug) {
state = data[1];
timegps = data[2];
latitude = data[3];
longitude =data[4];
}
}
String sendData (String command , const int timeout ,boolean debug){
String response = "";
sim808.println(command);
long int time = millis();
int i = 0;
while ( (time+timeout ) > millis()){
while (sim808.available()){
char c = sim808.read();
response +=c;
}
}
if (debug) {
Serial.print(response);
}
return response;
}
const int buttonPin = 2; // the number of the pushbutton pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(GPS, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void message() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// Show the state of pushbutton on serial monitor
Serial.println(buttonState);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(GPS, HIGH);
} else {
// turn LED off:
digitalWrite(GPS, LOW);
}
}
void loop () {
message;
setup;
GPS;
}
This code does not even compile. Please read the sticky post at the top of the forum to learn how to properly post your code using code tags. It prevents your "code" from being interpreted and converted to italics, etc.
blh64:
This code does not even compile. Please read the sticky post at the top of the forum to learn how to properly post your code using code tags. It prevents your "code" from being interpreted and converted to italics, etc.
Surprise! It did compile here, but I feel sad looking into it.
Why are you running software serial on the hardware serial pins (pins 0 & 1)?
How is the switch (button) wired? Is there a pulldown resistor?
delay(20000);
Calls to the delay() function make a program unresponsive.
Do you know the potential pitfalls of using the String class?
Your code would be easier to read and follow if it was indented in a standard way. Easy to do with the IDE autoformat tool (ctrl-t or Tools, Auto Format).
Why are you running software serial on the hardware serial pins (pins 0 & 1)?
How is the switch (button) wired? Is there a pulldown resistor?
delay(20000);
Calls to the delay() function make a program unresponsive.
Do you know the potential pitfalls of using the String class?
Your code would be easier to read and follow if it was indented in a standard way. Easy to do with the IDE autoformat tool (ctrl-t or Tools, Auto Format).
I'm using the Arduino Uno. I put software serial on pins 0,1 because I have the shield, sim808, connected to those pins. I do have a pull down resistor and a picture of the hardware is attached to the post. I am not aware of the pitfalls of using the string class. I am using Web editor so I don't have the Auto Format feature, sorry.
On the Uno, do not use Strings. Strings cause memory problems and unpredictable program crashes. Instead, use C-strings (zero terminated character arrays) and the associated functions in the string.h library. There are many C-string tutorials on line.