ARDUINO UNO+SIM808(GPS/GSM) SYNCHRONISATION - arduino

I started a project quite recently were I intend to combine the arduino uno and the sim808 module which has gps/gsm/gprs all together in one.
Using just the gps functionality works fine, just the gsm both receiving and sending messages also works good. but combining them for what i intend makes the entire system falls apart.
I intend to be able to have some coordinates registered let's say position A position B and position c, and the system i intend to design is put on a moving, let's say car, now anyone on either of the positions will send a message with the alphabet denoting the position to a number lets say "A TO +23*******98 , AND WOULD get a reply of the cars current time from that position.
With all parts combined the program would sometimes not even indicate receiving any message or even if it did the gps part won't even work.
Is there something I am doing wrong or is it not just feasible with what the components I am using pls help as I am very confused right now.
`#include <SoftwareSerial.h>
SoftwareSerial GPRS(7, 8); // RX, TX
String BUSNUM="A"; // THIS SIM IS TO BE INSTALLED ON BUS A
static String sendersnumber;
char* key;
char MESSAGE[280];
String latitude;
String longitude;
String Speed;
enum _parseState
{
PS_DETECT_MSG_TYPE,
PS_IGNORING_COMMAND_ECHO,
PS_READ_CMTI_STORAGE_TYPE,
PS_READ_CMTI_ID,
PS_DETECT_MSG_TYPE2,
PS_IGNORING_COMMAND_ECHO2,
PS_READ_CMGR_STATUS2,
PS_READ_CMGR_CONTENT2,
PS_READ_CMGR_DATE2,
PS_READ_CMGR_STATUS,
PS_READ_CMGR_NUMBER2,
PS_READ_CMGR_NUMBER,
PS_READ_CMGR_SOMETHING,
PS_READ_CMGR_DATE,
PS_READ_CMGR_CONTENT,
PS_READ_VIDEO_CONTENT,
PS_READ_VIDEO2_CONTENT,
PS_READ_CMGR_SOMETHING2,
PS_READ_VIDEO_CONTENT2,
};
byte state = PS_DETECT_MSG_TYPE; // KEEP TRACK OF WHAT STATE WE ARE IN RIGHT NOW
char buffer[100]; // WHAT WE ARE READING INTO
byte pos = 0; //WHAT POSITION WE ARE AT IN THAT BUFFER
int lastReceivedSMSId = 0; // LAST RECIEVED SMS ID
boolean validSender = false; //SOO NOT JUST ANYONE HAS ACESS TO SEND A COMMAND
void resetBuffer()
{
memset(buffer, 0, sizeof(buffer));
pos = 0;
}//BASICALLY TO RESET THE BUFFER
void setup()
{
GPRS.begin(9600);
Serial.begin(9600);
GPRS.println("AT+CGNSPWR=1");
delay(290);
GPRS.println("AT+CGNSURC=0");
delay(300);
GPRS.println("AT");
delay(300);
GPRS.println("AT+CMGF=1"); // INITIALIZE SMS
delay(300);
for (int i = 1; i <= 15; i++)
{
GPRS.print("AT+CMGD=");
GPRS.println(i);
delay(300);
// Not really necessary but prevents the serial monitor from dropping any input SINCE WE KNOW LIMIT TO STORE IS 15, THIS JUST DELETES IT CLEARS IT
while(GPRS.available())
Serial.write(GPRS.read());
}
delay(2000);
GPRS.println("AT+CGNSURC=1");
}
void loop()
{
while(GPRS.available()) //ONLY WHEN THERE IS SOMETHING AVAILABLE,
{
GSM_NUM1(GPRS.read());
//delay(50);
GPRS.println("AT+CGNSINF");
// delay(40);
GPSAnalyzer(GPRS.read());
sendSMS();
}
}
// END OF VOID LOOP
void GSM_NUM1(byte b)
{
buffer[pos++] = b;
if ( pos >= sizeof(buffer) )
resetBuffer(); // just to be safe
switch (state)
{
case PS_DETECT_MSG_TYPE:
{
if ( b == '\n' )
resetBuffer();
else {
if ( pos == 3 && strcmp(buffer, "AT+") == 0 ) {
state = PS_IGNORING_COMMAND_ECHO;
}
else if ( pos == 6 ) {
//Serial.print("Checking message type: ");
//Serial.println(buffer);
if ( strcmp(buffer, "+CMTI:") == 0 ) {
Serial.println("Received CMTI");
state = PS_READ_CMTI_STORAGE_TYPE;
}
else if ( strcmp(buffer, "+CMGR:") == 0 ) {
Serial.println("Received CMGR");
state = PS_READ_CMGR_STATUS;
}
resetBuffer();
}
}
}
break;
case PS_IGNORING_COMMAND_ECHO:
{
if ( b == '\n' ) {
//Serial.print("Ignoring echo: ");
//Serial.println(buffer);
state = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
case PS_READ_CMTI_STORAGE_TYPE:
{
if ( b == ',' ) {
Serial.print("SMS storage is ");
Serial.println(buffer);
state = PS_READ_CMTI_ID;
resetBuffer();
}
}
break;
case PS_READ_CMTI_ID:
{
if ( b == '\n' ) {
lastReceivedSMSId = atoi(buffer);
Serial.print("SMS id is ");
Serial.println(lastReceivedSMSId);
GPRS.print("AT+CMGR=");
GPRS.println(lastReceivedSMSId);
//delay(500); don't do this!
state = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
case PS_READ_CMGR_STATUS:
{
if ( b == ',' ) {
Serial.print("CMGR status: ");
Serial.println(buffer);
state = PS_READ_CMGR_NUMBER;
resetBuffer();
}
}
break;
case PS_READ_CMGR_NUMBER:
{
if ( b == ',' ) {
Serial.print("CMGR MSSG SENDERS Number: ");
Serial.println(buffer);
String sendersnumber=buffer;
// Uncomment these two lines to check the sender's cell number
//validSender = false;
//if ( strcmp(buffer, "\"+0123456789\",") == 0 )
validSender = true;
state = PS_READ_CMGR_SOMETHING;
resetBuffer();
}
}
break;
case PS_READ_CMGR_SOMETHING:
{
if ( b == ',' ) {
Serial.print("CMGR something A.K.A SENDER'S NAME: ");
Serial.println(buffer);
state = PS_READ_CMGR_DATE;
resetBuffer();
}
}
break;
case PS_READ_CMGR_DATE:
{
if ( b == '\n' ) {
Serial.print("CMGR date: ");
Serial.println(buffer);
state = PS_READ_CMGR_CONTENT;
resetBuffer();
}
}
break;
case PS_READ_CMGR_CONTENT:
{
if ( b == '\n' ) {
Serial.print("CMGR MESSAGE Content: ");
Serial.print(buffer);
String key=buffer;
// sendSMS();
// GPSAnalyzer();
GPRS.print("AT+CMGD=");
GPRS.println(lastReceivedSMSId);
//delay(500); don't do this!
state = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
}
}
void GPSAnalyzer(byte b)
{
buffer[pos++] = b;
if ( pos >= sizeof(buffer) )
resetBuffer();// just to be safe
switch (state)
{
case PS_DETECT_MSG_TYPE2:
{
if ( b == '\n' )
resetBuffer();
else {
if ( pos == 9 ) {
// Serial.print("Checking message type: ");
// Serial.println(buffer);
if ( strcmp(buffer, "+UGNSINF:") == 0 ) {
Serial.println("Received CGNSINF:");
state = PS_READ_CMGR_STATUS2;
}
resetBuffer();
}
}
}
break;
//CHECK
case PS_IGNORING_COMMAND_ECHO2:
{
if ( b == '\n' ) {
//Serial.print("Ignoring echo: ");
//Serial.println(buffer);
state = PS_DETECT_MSG_TYPE2;
resetBuffer();
}
}
break;
//THIS WOULD READ FROM +CGNSINF: (TO THE COMMA),
case PS_READ_CMGR_STATUS2:
{
if ( b == ',' ) {
Serial.print("RUN STATUS: ");
Serial.println(buffer);
String runstatus=buffer;
state = PS_READ_CMGR_NUMBER2;
resetBuffer();
}
}
break;
case PS_READ_CMGR_NUMBER2:
{
if ( b == ',' ) {
Serial.print("FIX STATUS : ");
Serial.println(buffer);
String fixstatus=buffer;
fixstatus.replace(","," ");
validSender = true;
state = PS_READ_CMGR_SOMETHING2;
resetBuffer();
}
}
break;
// this is DATE AND TIME i dont need this
case PS_READ_CMGR_SOMETHING2:
{
if ( b == ',' ) {
Serial.print("DATE AND TIME : ");
Serial.println(buffer);
String dateandtime=buffer;
state = PS_READ_CMGR_DATE2;
resetBuffer();
}
}
break;
case PS_READ_CMGR_DATE2:
{
if ( b == ',' ) {
Serial.print("LATITUDE: ");
Serial.println(buffer);
latitude=buffer;
latitude.replace(","," ");
state = PS_READ_CMGR_CONTENT2;
resetBuffer();
}
}
break;
case PS_READ_CMGR_CONTENT2:
{
if ( b == ',' ) {
Serial.print("LONGITUDE: ");
Serial.println(buffer);
longitude=buffer;
longitude.replace(","," ");
state = PS_READ_VIDEO_CONTENT2;
resetBuffer();
//delay(500); don't do this!
}
}
break;
case PS_READ_VIDEO_CONTENT2:
{
if ( b == ',' ) {
Serial.print("ALTITUDE: ");
Serial.println(buffer);
String Altitude=buffer;
state = PS_READ_VIDEO2_CONTENT;
resetBuffer();
//delay(500); don't do this!
}
}
break;
case PS_READ_VIDEO2_CONTENT:
{
if ( b == ',' ) {
Serial.print("SPEED(KM/HR): ");
Serial.println(buffer);
String Speed=buffer;
Speed.replace(","," ");
state =PS_DETECT_MSG_TYPE2;
resetBuffer();
}
}
break;
//use goto to put it at sms begining
}
}
void sendSMS()
{
if ( strcmp(key,"A") == 0 )
{
float lati=7.200970;
float longi=5.181782;
float Speed1 = atof(Speed.c_str());
float latituded = atof(latitude.c_str());
float longituded = atof(longitude.c_str());
float Distance = HaverSine(lati,longi,latituded,longituded);
float duration=Distance/Speed1;
const int StrLen = 10;
char * duration_new ;
double Value = duration;
(void) dtostrf (Value, StrLen, 6, duration_new);
String MESSAGE="BUS A";
MESSAGE+=duration_new ;
Serial.print("THE MESSAGE SENT IS ");
Serial.println(MESSAGE);
}
else if ( strcmp(key,"B") == 0 )
{
float lati=7.290970;
float longi=5.141782;
float Speed1 = atof(Speed.c_str());
float latituded = atof(latitude.c_str());
float longituded = atof(longitude.c_str());
float Distance = HaverSine(lati,longi,latituded,longituded);
float duration=Distance/Speed1;
const int StrLen = 10;
char * duration_new ;
double Value = duration;
(void) dtostrf (Value, StrLen, 6, duration_new);
String MESSAGE="BUS B";
MESSAGE+=duration_new ;
Serial.print("THE MESSAGE SENT IS ");
Serial.println(MESSAGE);
}
delay(300);
GPRS.print("AT+CMGF=1\r\n");
delay(100);
GPRS.println("AT+CMGS=\""+sendersnumber+"\"\r\n");
delay(100);
GPRS.println(MESSAGE);
delay(100);
GPRS.println((char)26);
delay(100);
GPRS.println();
delay(100);
}
float HaverSine(float lat1,float lon1,float lat2,float lon2)
{
String fixstatus;
float ToRad = PI / 180.0;
float R = 6371; // radius earth in Km
float dLat = (lat2-lat1) * ToRad;
float dLon = (lon2-lon1) * ToRad;
float a = sin(dLat/2) * sin(dLat/2) +
cos(lat1 * ToRad) * cos(lat2 * ToRad) *
sin(dLon/2) * sin(dLon/2);
String o= fixstatus + "8";
float c = 2 * atan2(sqrt(a), sqrt(1-a));
float d = R * c;
return d;
}
EXPLANATION After all declarations and initializations the first active part of the entire code is the for loop that just deletes all former messages from the sim card memory.it runs 15 times
for (int i = 1; i <= 15; i++) { GPRS.print("AT+CMGD="); GPRS.println(i); delay(300); while(GPRS.available()) Serial.write(GPRS.read()); }
Now, having functions GSM_NUM1(); GPSAnalyzer(); sendSMS(); Which are defined below in the code
GSM_NUM1(); GPSAnalyzer(); both work similarly they extract needed data which come in separated by commas. For the gps which comes in , in the form
+UGNSINF: ,,,,, ,,, ,,,,,,, ,,,,
lathitude, speed and any other important parameter is extracted.
For GSM_NUM1(); it gets the senders message and number into variable.
sendSMS(); this just does some calculation based on the message received and send a certain result to the number who requested it.
OBERVATIONS
When the program starts nothing happens which is good cause nothing is supposed to until something new comes in. Hence the void loop() { while(GPRS.available()) //ONLY WHEN THERE IS SOMETHING AVAILABLE, { GSM_NUM1(GPRS.read());
//delay(50); GPRS.println("AT+CGNSINF"); // delay(40);
GPSAnalyzer(GPRS.read()); sendSMS(); } Now this is were things go bad, as the first function works good, it waits gets the message and extracts the senders number and message"key" into variables but immediately after that I wantjust one return of the gps info shown below that's why I used ("AT+CGNSINF"); instead of ("AT+CGNSURC=1"); as the later gives continuous gps data such as
+UGNSINF: ,,,,, ,,, ,,,,,,, ,,,,
BUT instead of one line of the gps data to be gotten from were lathitude and some other parameters are extracted into variables "THE PROGRAM HANGS/STOPS for some unknown reason.
SUSPICIONS
Since the SIM808 possess gps/gsm on the same module. It makes it kind of tricky to separate them in the sense that.
IF I WANT THE GPS TO BE ON BUT NOT READ IN ANY DATA UNTIL A VALID REQUEST COMES IN , IN FORM OF AN SMS THEN THE NECESSARY GPS INFORMATION IS PARSED AND SOME CALCULATIONS ARE DONE TO GET A PARAMETER THAT WOULD BE SENT TO THE SAME NUMBER THAT REQUESTED IT.
PERSONNALY, I FEEL THAT GETTING THE GPS AND GSM TO WORK HAND IN HAND WITHOUT CANCELLING EACH OTHER OUT IN SOME WAY,SUCESSFULLY WOULD BE THE END OF MY PROBLEM. AND I THINK IT COMES DOWN TO THE POSITION OF THE AT COMMAND
GPRS.println("AT+CGNSURC=1"); AND/OR GPRS.println("AT+CGNSINF"); AS the former spits out the gps data every GNSS FIX continuously when called once and LATER spits it out just once per one command.
the attached picture just shows the continous nature of the output of ("AT+CGNSURC=1"); , just as ("AT+CGNSINF"); spits out just one line with +CGNSINF: at the begining instead

Please check that is not in overflow SRAM , that produces stranger things when you execute the program. This occur when you declare so much variables, you can use this library to check the use of sram.

I ran into this similar issue with trying to get GPS data from a module over serial but figured it out after painstaking hours. Essentially you need to validate every response from every command and flush the serial port before every write.
Flushing the serial port buffer before you write any command will ensure that you don't accidentally read left over characters from a previous command when you are trying to get the output of the one you just ran. Most TAs (Terminal Adapters) use EOL characters like OK which translates into "\r\nOK\r\n". If you do not read them all, they can hang around and interfere with the next command you send/ read a response from.
use GPRS.flush() to get rid of extra garbage characters in the serial buffer before sending commands.
Also, creating a generic command function to pass all AT commands and validate their output will save you a lot of sleep.
something like
int sendCommand(int fd, char* message, char* eol, char* response){
int wlen, rlen;
char buf[1024];
tcflush(fd, TCIOFLUSH); //flush the serial port to remove anything left over
wlen = write(fd, message, strlen(message)); //write command to terminal
tcdrain(fd); //delay until 1st readable character is written by modem
delay(100); //delay a little longer until all characters are written(hopefully)
rdlen = read(fd, buf, sizeof(buf) - 1); //read from serial port
if(rdlen > 0){ //if readable characters
if(strstr(buf, eol) != NULL){ //if my end of line character is present then
int i = 0; //output from TA is complete
while(buf[i] != '\0'){ //while buffer has more characters
response[i] = buf[i]; //copy string into the string I passed, char by char
}
return 1; //return 1 (i.e.true aka success)
}
}
return 0; //return 0 (i.e. false aka failure)
}
I wrote this for a communicating to a simcom5320 but I didnt use software serial so it'll be a little different for you. Use this function for all your at commands to ensure everything actually succeed like:
char commandReply[512];
if(sendCommand(myFileDescriptor, "AT+CGNSINF\r", "\r\nOK\r\n", commandReply)){
//do something with commandReply or not
} else {
//command failed
//do something else
}
Hope this helps!

Related

(Serial.available() > 0) waits for user input

I'm facing problem with user input in Arduino ide. I would like arduino to periodically check if user generate some input string (arduino checks it every 3 seconds), but even if there is no input after 3 seconds (string order="") arduino waits for user input and when I type something then it exit "checkIncomingOrder" function and wait another 3 seconds. I would be gratefull for any advice. My simple code is below:
String order = "";
int timer;
void setup() {
Serial.begin(9600);
timer = 0;
}
void loop() {
if (timer == 3)
{
checkIncomingOrder();
}
else
{
delay(1000);
Serial.println("waiting");
timer++;
}
}
void checkIncomingOrder() {
if (Serial.available() > 0) {
order = Serial.readStringUntil('\n');
}
if (order == "") {
return;
} else {
Serial.println("Order is: " + order);
order = "";
timer = 0;
}
}
Serial::Available() does not block. If there are characters available, readStringUntil() will be called, but that will not return until a newline is available - so that is where your blocking on input is likely to be occurring.
The following is perhaps a safer non-blocking solution:
void checkIncomingOrder()
{
char ch = 0 ;
while( ch != '\n' && Serial.available() > 0 )
{
Serial.readBytes( &ch, 1 ) ;
order += ch ;
}
if( ch == '\n' )
{
Serial.println("Order is: " + order) ;
order = "" ;
timer = 0 ;
}
}
It allows you to check for a line of input continuously rather then every three seconds - making the solution more responsive:
void loop()
{
checkIncomingOrder();
}
allowing in turn the timer variable to be removed.

Can not add line feed to string

I have an Arduino Leonardo and trying to use it as a serial to USB converter. On Serial1 I have a string ending on a number. This number I'm trying to get via USB to the PC. It works very fine but I need a '\n' at the end and I don't know how. When I try it in the line Keyboard.println or Keyboard.write, I get a various number of lines with the expected number in splitted.
#include <Keyboard.h>
String myEAN ="";
const int myPuffergrosse = 50;
char serialBuffer[myPuffergrosse];
void setup() {
Keyboard.begin();
Serial1.begin(9600);
delay(1000);
}
String getEAN (char *stringWithInt)
// returns a number from the string (positive numbers only!)
{
char *tail;
// skip non-digits
while ((!isdigit (*stringWithInt))&&(*stringWithInt!=0)) stringWithInt++;
return(stringWithInt);
}
void loop() {
// Puffer mit Nullbytes fuellen und dadurch loeschen
memset(serialBuffer,0,sizeof(myPuffergrosse));
if ( Serial1.available() ) {
int incount = 0;
while (Serial1.available()) {
serialBuffer[incount++] = Serial1.read();
}
serialBuffer[incount] = '\0'; // puts an end on the string
myEAN=getEAN(serialBuffer);
//Keyboard.write(0x0d); // that's a CR
//Keyboard.write(0x0a); // that's a LF
}
}
Since myEAN is a String, simply add the character...
myEAN += '\n';
Or, for a full carriage return/line feed combination:
myEAN += "\r\n";
See the doc: https://www.arduino.cc/en/Tutorial/StringAppendOperator
I suggest you use String in your getEAN function as well...
String getEAN(String s)
{
// returns the first positive integer found in the string.
int first, last;
for (first = 0; first < s.length(); ++first)
{
if ('0' <= s[first] && s[first] <= '9')
break;
}
if (first >= s.length())
return "";
// remove trailing non-numeric chars.
for (last = first + 1; last < s.length(); ++last)
{
if (s[last] < '0' || '9' < s[last])
break;
}
return s.substring(first, last - 1);
}

How to request for two different URL from Arduino one after another?

In the following code segment I am obtaining latitude, longitude, time, date and speed using GPS SKG13BL with Arduino UNO and then send it to the database via GSM module at every 5 secs. And if the speed (stored to s) returned by GPS is greater than 0, then an LED glows else will be low. After this code segment I am performing the database insertion by calling a URL. This works fine and obtains the results as I thought.
Code:
#include <NeoSWSerial.h>
//#include <SoftwareSerial.h>
#include <AltSoftSerial.h>
#include <NMEAGPS.h>
NeoSWSerial GSM(2, 3); // RX, TX: Connect TXD to RX & RXD to TX
static const int RXPin = 8, TXPin = 9; //when looking from antenna of gps , Last TTL pin is to be connected to 8 and second one to 9
AltSoftSerial gpsPort(RXPin, TXPin);
static const uint32_t GPSBaud = 9600;
NMEAGPS gps;
gps_fix fix;
uint8_t fixCount = 0;
char dt[15],tm[15],lati[10],longi[10],rno[13]="kl-05-jb-007";
//int block = 0;
enum _parseState {
PS_DETECT_MSG_TYPE,
PS_IGNORING_COMMAND_ECHO,
PS_HTTPPARA_RESPONSE,
PS_HTTPACTION_TYPE,
PS_HTTPACTION_RESULT,
PS_HTTPACTION_LENGTH,
PS_HTTPREAD_LENGTH,
PS_HTTPREAD_CONTENT
};
enum _actionState {
AS_IDLE,
AS_WAITING_FOR_RESPONSE
};
byte actionState = AS_IDLE;
unsigned long lastActionTime = 0;
int s;
byte parseState = PS_DETECT_MSG_TYPE;
char buffer[160],url[160];
byte pos = 0;
int contentLength = 0;
void resetBuffer() {
memset(buffer, 0, sizeof(buffer));
pos = 0;
}
void sendGSM(const char* msg, int waitMs = 500) {
GSM.println(msg);
while(GSM.available()) {
parseATText(GSM.read());
}
//delay(waitMs);
}
void setup()
{
GSM.begin(9600);
Serial.begin(9600);
gpsPort.begin(GPSBaud);
pinMode(13, OUTPUT); //pin 13 to be connected to positive of LED via a resistor
Serial.println(F("Hinder LOADING....."));
Serial.println(F("obtain gps and store data to database"));
Serial.println(F("Testing by : "));
Serial.println(F("Nikhil,Muthumani and Mathews"));
Serial.println();
sendGSM("AT+SAPBR=3,1,\"APN\",\"vodafone\"");
delay(500);
sendGSM("AT+SAPBR=1,1",3000);
delay(500);
sendGSM("AT+HTTPINIT");
delay(500);
sendGSM("AT+HTTPPARA=\"CID\",1");
delay(500);
}
void loop()
{
unsigned long now = millis();
while (gps.available( gpsPort )) {
fix = gps.read();
//Serial.println(F("b"));
// Once every 5 seconds...
if (++fixCount >= 5) {
//Serial.println(F("a"));
displayInfo();
if(s>0) // Change led state when speed greater than 0 or less than 0
digitalWrite(13, HIGH);
else
digitalWrite(13, LOW);
//send the next report if previous one is finished
if ( actionState == AS_IDLE ) {
sprintf(url, "AT+HTTPPARA=\"URL\",\"http://hinder.000webhostapp.com/HInsert.php?rno=%s&lat=%s&lng=%s&speed=%d&date=%s&time=%s\"",rno,lati,longi,s,dt,tm );
sendGSM(url);
// lastActionTime = now;
parseState = PS_HTTPPARA_RESPONSE; // a new state
actionState = AS_WAITING_FOR_RESPONSE;
fixCount = 0;
}
}
}
/* if ((gps.statistics.chars < 10) && (millis() > 5000)) {
Serial.println( F("No GPS detected: check wiring.") );
while(true);
block=1;
}*/
while(GSM.available()) {
//lastActionTime = now;
parseATText(GSM.read());
}
}
void displayInfo()
{
Serial.print(F("Location: "));
if (fix.valid.location) {
dtostrf( fix.latitude(),7,5,lati);
//sprintf(lati,"%d",fix.latitude(),5);
Serial.print(lati);
Serial.print( ',' );
dtostrf( fix.longitude(),8,5,longi);
//sprintf(longi,"%d",fix.longitude(),5);
Serial.print(longi);
} else {
Serial.print(F("INVALID"));
}
Serial.print(F(" Speed: "));
if (fix.valid.speed) {
s=fix.speed_kph();
Serial.print(s);
Serial.print(F(" KMPH "));
} else {
Serial.print(F("INVALID"));
}
// Shift the date/time to local time
NeoGPS::clock_t localSeconds;
NeoGPS::time_t localTime;
if (fix.valid.date && fix.valid.time) {
using namespace NeoGPS; // save a little typing below...
localSeconds = (clock_t) fix.dateTime; // convert structure to a second count
localSeconds += 5 * SECONDS_PER_HOUR + 30 * SECONDS_PER_MINUTE; // shift timezone
localTime = localSeconds; // convert back to a structure
}
Serial.print(F(" Date : "));
if (fix.valid.date) {
sprintf(dt,"%02d/%02d/20%d",fix.dateTime.date,fix.dateTime.month,fix.dateTime.year);
Serial.print(dt);
} else {
Serial.print(F("INVALID"));
}
Serial.print(F(" Time : "));
if (fix.valid.time) {
if (localTime.hours>12)localTime.hours-=12;//To convert 24 hr format to 12 hr format
sprintf(tm,"%02d:%02d:%02d",localTime.hours,localTime.minutes,localTime.seconds);
Serial.print(tm);
//block=1;
} else {
Serial.print(F("INVALID"));
}
Serial.println();
}
void parseATText(byte b) {
buffer[pos++] = b;
if ( pos >= sizeof(buffer) )
resetBuffer(); // just to be safe
/*
// Detailed debugging
Serial.println();
Serial.print("state = ");
Serial.println(state);
Serial.print("b = ");
Serial.println(b);
Serial.print("pos = ");
Serial.println(pos);
Serial.print("buffer = ");
Serial.println(buffer);*/
switch (parseState) {
case PS_HTTPPARA_RESPONSE:
{
parseState = PS_DETECT_MSG_TYPE;
sendGSM("AT+HTTPACTION=0");
/* while(GSM.available()) {
//lastActionTime = now;
parseATText(GSM.read());
}*/
}
break;
case PS_DETECT_MSG_TYPE:
{
if ( b == '\n' )
resetBuffer();
else {
if ( pos == 3 && strcmp(buffer, "AT+") == 0 ) {
parseState = PS_IGNORING_COMMAND_ECHO;
}
else if ( b == ':' ) {
//Serial.print("Checking message type: ");
//Serial.println(buffer);
if ( strcmp(buffer, "+HTTPACTION:") == 0 ) {
Serial.println("Received HTTPACTION");
parseState = PS_HTTPACTION_TYPE;
}
else if ( strcmp(buffer, "+HTTPREAD:") == 0 ) {
Serial.println("Received HTTPREAD");
parseState = PS_HTTPREAD_LENGTH;
}
resetBuffer();
}
}
}
break;
case PS_IGNORING_COMMAND_ECHO:
{
if ( b == '\n' ) {
Serial.print("Ignoring echo: ");
Serial.println(buffer);
parseState = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
case PS_HTTPACTION_TYPE:
{
if ( b == ',' ) {
Serial.print("HTTPACTION type is ");
Serial.println(buffer);
parseState = PS_HTTPACTION_RESULT;
resetBuffer();
}
}
break;
case PS_HTTPACTION_RESULT:
{
if ( b == ',' ) {
Serial.print("HTTPACTION result is ");
Serial.println(buffer);
parseState = PS_HTTPACTION_LENGTH;
resetBuffer();
}
}
break;
case PS_HTTPACTION_LENGTH:
{
if ( b == '\n' ) {
Serial.print("HTTPACTION length is ");
Serial.println(buffer);
// now request content
GSM.print("AT+HTTPREAD=0,");
GSM.println(buffer);
parseState = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
case PS_HTTPREAD_LENGTH:
{
if ( b == '\n' ) {
contentLength = atoi(buffer);
Serial.print("HTTPREAD length is ");
Serial.println(contentLength);
Serial.print("HTTPREAD content: ");
parseState = PS_HTTPREAD_CONTENT;
resetBuffer();
}
}
break;
case PS_HTTPREAD_CONTENT:
{
// for this demo I'm just showing the content bytes in the serial monitor
Serial.write(b);
contentLength--;
if ( contentLength <= 0 ) {
// all content bytes have now been read
parseState = PS_DETECT_MSG_TYPE;
resetBuffer();
Serial.print("\n\n\n");
actionState = AS_IDLE;
}
}
break;
}
}
Now I need a modification in this as, when the speed is greater than 0, along with the glowing of the LED, a variable flag (int flag=0; declared globally) is incremented. And flag is set to 0 when the speed is equal to 0. And if the flag value becomes 2 (i.e when speed is greater than 0 for two consecutive readings) I want to send latitude, longitude, date, time and speed at that instant to another database which is also done by calling a URL (url1). And after that I want to send that to the usual database by calling its URL (url).
I did a modification as follows :
if(s>0) // Change led state when speed greater than 0 or less than 0
{
digitalWrite(13, HIGH);
flag++;
}
else
{
digitalWrite(13, LOW);
flag=0;
}
if(flag==2)
{
sprintf(url1, "AT+HTTPPARA=\"URL\",\"http://speed.000webhostapp.com/HInsert.php?rno=%s&lat=%s&lng=%s&speed=%d&date=%s&time=%s\"",rno,lati,longi,s,dt,tm );
sendGSM(url1);
parseState = PS_HTTPPARA_RESPONSE; // a new state
actionState = AS_WAITING_FOR_RESPONSE;
}
This doesn't obtain result as I thought. Can anyone help me. Is it possible to have flag variable working this way in loop(). Is there a problem with my usage of flag or is it another problem.
It is bit tough to make arduino request for two URLs as we would have to handle two separate request and its responses, either the two request would merge in and wont obtain required result or the code would become large that arduino-uno wont be able to handle.
So i found an easy way for doing that, i.e to request for the second PHP page from the first PHP page (which is requested by arduino itself). The only difference between these two request was that the second one need an extra variable. I passed along with the first request so it was available for making request from first page.
I changed arduino code as follows :
First I modified the portion making the check - just after the displayinfo();, as I mentioned in the question, added a flag bit to it. So it become like this :
if(s>0) // s is speed
{
digitalWrite(13, HIGH); //if speed exceeds limit then LED is set to HIGH
flag++;
}
else
{
digitalWrite(13, LOW);
flag=0;
}
The URL for request from arduino was added with the flag variable too. (For the request for second page).
sprintf(url, "AT+HTTPPARA=\"URL\",\"http://hinder.000webhostapp.com/HInsert.php?flag=%d&rno=%s&lat=%s&lng=%s&speed=%d&date=%s&time=%s\"",flag,rno,lati,longi,s,dt,tm );
In the PHP code I added following lines :
if ( $flag == 2 )
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://masp203.000webhostapp.com/RTO.php?rno=".$rno."&date=".$date."&time=".$time."&speed=".$speed."&street=".$street."" );
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
}
This worked just the way I wanted it to be.
Hope this helps some one.

Arduino Gcode reader

I made G-code reader on Arduino, but it stops reading. It has many "break;" and I have also many while loops and switches, so I'm thinking that when I break on loop/switch, it will break all.
Another idea is that it goes some kind of loop, but I can't figure out where it loops.
Here is my code:
void Gcode(){
String yy,xx,gg;
char text[64];
int number=1;
while(number!=3){
while (Serial.available()>0) {
delay(3); //delay to allow buffer to fill
char c = Serial.read();
Serial.println(c);
switch(c){
case 'G':
//read_number()
while (Serial.available()>0) {
char k = Serial.read();
if(k==' ' || k=='\n'){
break;
}
else{
gg+=k;
}
}
switch(gg.toInt()){
case 1:
Serial.println(gg);
while (Serial.available()>0) {
c = Serial.read();
Serial.println(c);
switch(c){
case 'X':
while (Serial.available()>0) {
char k = Serial.read();
if(k==' ' || k=='\n'){
break;
}
else{
xx+=k;
}
}
char buf[xx.length()];
xx.toCharArray(buf,xx.length());
x2=atof(buf);
Serial.println(x2);
break;
case 'Y':
while (Serial.available()>0) {
char k = Serial.read();
if(k==' ' || k=='\n'){
break;
}
else{
yy+=k;
}
}
Serial.println(yy);
char buf2[yy.length()];
yy.toCharArray(buf2,yy.length());
y2=atof(buf2);
break;
case 'E':
break;
case 'F':
break;
default:
Serial.print("the end");
}
Serial.print("out of switch");
}
break;
case 2:
break;
default:
Serial.print("nothing");
}
break;
case '\n':
number=3;
break;
default:
Serial.print("default");
}
}
}
if(sizeof(yy)>0){
yy="";
xx="";
gg="";
}
Serial.print("quit");
}
When I send G1 X10.00 Y-100.00 \n It prints only:
G
1
X
10.00
out of s
one of your big problem is that your while end when thwere are nomore carachter. This mean that if your loop consume the buffer FASTER than it get written (remeber:9600 baud mean 960Byte/s, arduino even if slow but can compute 16.000.000 operation/s...).
Another big problem MAY bethe lack of ram, os your output is truncated. there is some function to check real time usage of ram, see http://playground.arduino.cc/Code/AvailableMemory, stopping to use String and even char array is a way better idea; the code is not that hard to write!
so the pc would send "G1 X10.00 Y-100.00 \n"
but at time X your aruino get "G1 X10.00", if you read really fast the buffer now (faster than 1/960 of a second, arduino is a way faster than that!)
so ideally you should change all your while condition removing serial available but instead putting the condition you use in the if with the break; sothe first while from
while (Serial.available()>0)
became
while ( (k=Serial.read()) != ' ' && k != '\n') //yes it is a bit weird like this
maybe a little better, with check that k is a valid caracter, AND timeout
unsigned long timeout_ms = 1000; //timeout after 1 seconds from NOW!
unsigned long start_ms = millis();
int k=Serial.read();
while ( k != ' ' && millis()-start_ms < timeout_ms){// because here we expect "Gx ", why are you was also using k != '\n'? removed, feel free to add it back
if (k == -1){
k=Serial.read(); //read the next char
continue; //return to the beginning of the while
}
[... do thigs...]
k=Serial.read(); //read the next char
//here you may add "start_ms = millis();" if you want to reset the timeout
}

Convert serial.read() into a usable string using Arduino

I'm using two Arduinos to sent plain text strings to each other using NewSoftSerial and an RF transceiver.
Each string is perhaps 20-30 characters in length. How do I convert Serial.read() into a string so I can do if x == "testing statements", etc.?
Unlimited string readed:
String content = "";
char character;
while(Serial.available()) {
character = Serial.read();
content.concat(character);
}
if (content != "") {
Serial.println(content);
}
From Help with Serial.Read() getting string:
char inData[20]; // Allocate some space for the string
char inChar = -1; // Where to store the character read
byte index = 0; // Index into array; where to store the character
void setup() {
Serial.begin(9600);
Serial.write("Power On");
}
char Comp(char* This) {
while (Serial.available() > 0) // Don't read unless there
// you know there is data
{
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
}
if (strcmp(inData, This) == 0) {
for (int i=0; i<19; i++) {
inData[i] = 0;
}
index = 0;
return(0);
}
else {
return(1);
}
}
void loop()
{
if (Comp("m1 on") == 0) {
Serial.write("Motor 1 -> Online\n");
}
if (Comp("m1 off") == 0) {
Serial.write("Motor 1 -> Offline\n");
}
}
You can use Serial.readString() and Serial.readStringUntil() to parse strings from Serial on the Arduino.
You can also use Serial.parseInt() to read integer values from serial.
int x;
String str;
void loop()
{
if(Serial.available() > 0)
{
str = Serial.readStringUntil('\n');
x = Serial.parseInt();
}
}
The value to send over serial would be my string\n5 and the result would be str = "my string" and x = 5
I was asking the same question myself and after some research I found something like that.
It works like a charm for me. I use it to remote control my Arduino.
// Buffer to store incoming commands from serial port
String inData;
void setup() {
Serial.begin(9600);
Serial.println("Serial conection started, waiting for instructions...");
}
void loop() {
while (Serial.available() > 0)
{
char recieved = Serial.read();
inData += recieved;
// Process message when new line character is recieved
if (recieved == '\n')
{
Serial.print("Arduino Received: ");
Serial.print(inData);
// You can put some if and else here to process the message juste like that:
if(inData == "+++\n"){ // DON'T forget to add "\n" at the end of the string.
Serial.println("OK. Press h for help.");
}
inData = ""; // Clear recieved buffer
}
}
}
This would be way easier:
char data [21];
int number_of_bytes_received;
if(Serial.available() > 0)
{
number_of_bytes_received = Serial.readBytesUntil (13,data,20); // read bytes (max. 20) from buffer, untill <CR> (13). store bytes in data. count the bytes recieved.
data[number_of_bytes_received] = 0; // add a 0 terminator to the char array
}
bool result = strcmp (data, "whatever");
// strcmp returns 0; if inputs match.
// http://en.cppreference.com/w/c/string/byte/strcmp
if (result == 0)
{
Serial.println("data matches whatever");
}
else
{
Serial.println("data does not match whatever");
}
The best and most intuitive way is to use serialEvent() callback Arduino defines along with loop() and setup().
I've built a small library a while back that handles message reception, but never had time to opensource it.
This library receives \n terminated lines that represent a command and arbitrary payload, space-separated.
You can tweak it to use your own protocol easily.
First of all, a library, SerialReciever.h:
#ifndef __SERIAL_RECEIVER_H__
#define __SERIAL_RECEIVER_H__
class IncomingCommand {
private:
static boolean hasPayload;
public:
static String command;
static String payload;
static boolean isReady;
static void reset() {
isReady = false;
hasPayload = false;
command = "";
payload = "";
}
static boolean append(char c) {
if (c == '\n') {
isReady = true;
return true;
}
if (c == ' ' && !hasPayload) {
hasPayload = true;
return false;
}
if (hasPayload)
payload += c;
else
command += c;
return false;
}
};
boolean IncomingCommand::isReady = false;
boolean IncomingCommand::hasPayload = false;
String IncomingCommand::command = false;
String IncomingCommand::payload = false;
#endif // #ifndef __SERIAL_RECEIVER_H__
To use it, in your project do this:
#include <SerialReceiver.h>
void setup() {
Serial.begin(115200);
IncomingCommand::reset();
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
if (IncomingCommand::append(inChar))
return;
}
}
To use the received commands:
void loop() {
if (!IncomingCommand::isReady) {
delay(10);
return;
}
executeCommand(IncomingCommand::command, IncomingCommand::payload); // I use registry pattern to handle commands, but you are free to do whatever suits your project better.
IncomingCommand::reset();
Here is a more robust implementation that handles abnormal input and race conditions.
It detects unusually long input values and safely discards them. For example, if the source had an error and generated input without the expected terminator; or was malicious.
It ensures the string value is always null terminated (even when buffer size is completely filled).
It waits until the complete value is captured. For example, transmission delays could cause Serial.available() to return zero before the rest of the value finishes arriving.
Does not skip values when multiple values arrive quicker than they can be processed (subject to the limitations of the serial input buffer).
Can handle values that are a prefix of another value (e.g. "abc" and "abcd" can both be read in).
It deliberately uses character arrays instead of the String type, to be more efficient and to avoid memory problems. It also avoids using the readStringUntil() function, to not timeout before the input arrives.
The original question did not say how the variable length strings are defined, but I'll assume they are terminated by a single newline character - which turns this into a line reading problem.
int read_line(char* buffer, int bufsize)
{
for (int index = 0; index < bufsize; index++) {
// Wait until characters are available
while (Serial.available() == 0) {
}
char ch = Serial.read(); // read next character
Serial.print(ch); // echo it back: useful with the serial monitor (optional)
if (ch == '\n') {
buffer[index] = 0; // end of line reached: null terminate string
return index; // success: return length of string (zero if string is empty)
}
buffer[index] = ch; // Append character to buffer
}
// Reached end of buffer, but have not seen the end-of-line yet.
// Discard the rest of the line (safer than returning a partial line).
char ch;
do {
// Wait until characters are available
while (Serial.available() == 0) {
}
ch = Serial.read(); // read next character (and discard it)
Serial.print(ch); // echo it back
} while (ch != '\n');
buffer[0] = 0; // set buffer to empty string even though it should not be used
return -1; // error: return negative one to indicate the input was too long
}
Here is an example of it being used to read commands from the serial monitor:
const int LED_PIN = 13;
const int LINE_BUFFER_SIZE = 80; // max line length is one less than this
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
Serial.print("> ");
// Read command
char line[LINE_BUFFER_SIZE];
if (read_line(line, sizeof(line)) < 0) {
Serial.println("Error: line too long");
return; // skip command processing and try again on next iteration of loop
}
// Process command
if (strcmp(line, "off") == 0) {
digitalWrite(LED_PIN, LOW);
} else if (strcmp(line, "on") == 0) {
digitalWrite(LED_PIN, HIGH);
} else if (strcmp(line, "") == 0) {
// Empty line: no command
} else {
Serial.print("Error: unknown command: \"");
Serial.print(line);
Serial.println("\" (available commands: \"off\", \"on\")");
}
}
String content = "";
char character;
if(Serial.available() >0){
//reset this variable!
content = "";
//make string from chars
while(Serial.available()>0) {
character = Serial.read();
content.concat(character);
}
//send back
Serial.print("#");
Serial.print(content);
Serial.print("#");
Serial.flush();
}
If you want to read messages from the serial port and you need to deal with every single message separately I suggest separating messages into parts using a separator like this:
String getMessage()
{
String msg=""; //the message starts empty
byte ch; // the character that you use to construct the Message
byte d='#';// the separating symbol
if(Serial.available())// checks if there is a new message;
{
while(Serial.available() && Serial.peek()!=d)// while the message did not finish
{
ch=Serial.read();// get the character
msg+=(char)ch;//add the character to the message
delay(1);//wait for the next character
}
ch=Serial.read();// pop the '#' from the buffer
if(ch==d) // id finished
return msg;
else
return "NA";
}
else
return "NA"; // return "NA" if no message;
}
This way you will get a single message every time you use the function.
Credit for this goes to magma. Great answer, but here it is using c++ style strings instead of c style strings. Some users may find that easier.
String string = "";
char ch; // Where to store the character read
void setup() {
Serial.begin(9600);
Serial.write("Power On");
}
boolean Comp(String par) {
while (Serial.available() > 0) // Don't read unless
// there you know there is data
{
ch = Serial.read(); // Read a character
string += ch; // Add it
}
if (par == string) {
string = "";
return(true);
}
else {
//dont reset string
return(false);
}
}
void loop()
{
if (Comp("m1 on")) {
Serial.write("Motor 1 -> Online\n");
}
if (Comp("m1 off")) {
Serial.write("Motor 1 -> Offline\n");
}
}
If you're using concatenate method then don't forget to trim the string if you're working with if else method.
Use string append operator on the serial.read(). It works better than string.concat()
char r;
string mystring = "";
while(serial.available()){
r = serial.read();
mystring = mystring + r;
}
After you are done saving the stream in a string(mystring, in this case), use SubString functions to extract what you are looking for.
I could get away with this:
void setup() {
Serial.begin(9600);
}
void loop() {
String message = "";
while (Serial.available())
message.concat((char) Serial.read());
if (message != "")
Serial.println(message);
}
Many great answers, here is my 2 cents with exact functionality as requested in the question.
Plus it should be a bit easier to read and debug.
Code is tested up to 128 chars of input.
Tested on Arduino uno r3 (Arduino IDE 1.6.8)
Functionality:
Turns Arduino onboard led (pin 13) on or off using serial command input.
Commands:
LED.ON
LED.OFF
Note: Remember to change baud rate based on your board speed.
// Turns Arduino onboard led (pin 13) on or off using serial command input.
// Pin 13, a LED connected on most Arduino boards.
int const LED = 13;
// Serial Input Variables
int intLoopCounter = 0;
String strSerialInput = "";
// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.
pinMode(LED, OUTPUT);
// initialize serial port
Serial.begin(250000); // CHANGE BAUD RATE based on the board speed.
// initialized
Serial.println("Initialized.");
}
// the loop routine runs over and over again forever:
void loop()
{
// Slow down a bit.
// Note: This may have to be increased for longer strings or increase the iteration in GetPossibleSerialData() function.
delay(1);
CheckAndExecuteSerialCommand();
}
void CheckAndExecuteSerialCommand()
{
//Get Data from Serial
String serialData = GetPossibleSerialData();
bool commandAccepted = false;
if (serialData.startsWith("LED.ON"))
{
commandAccepted = true;
digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
}
else if (serialData.startsWith("LED.OFF"))
{
commandAccepted = true;
digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
}
else if (serialData != "")
{
Serial.println();
Serial.println("*** Command Failed ***");
Serial.println("\t" + serialData);
Serial.println();
Serial.println();
Serial.println("*** Invalid Command ***");
Serial.println();
Serial.println("Try:");
Serial.println("\tLED.ON");
Serial.println("\tLED.OFF");
Serial.println();
}
if (commandAccepted)
{
Serial.println();
Serial.println("*** Command Executed ***");
Serial.println("\t" + serialData);
Serial.println();
}
}
String GetPossibleSerialData()
{
String retVal;
int iteration = 10; // 10 times the time it takes to do the main loop
if (strSerialInput.length() > 0)
{
// Print the retreived string after looping 10(iteration) ex times
if (intLoopCounter > strSerialInput.length() + iteration)
{
retVal = strSerialInput;
strSerialInput = "";
intLoopCounter = 0;
}
intLoopCounter++;
}
return retVal;
}
void serialEvent()
{
while (Serial.available())
{
strSerialInput.concat((char) Serial.read());
}
}
This always works for me :)
String _SerialRead = "";
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available() > 0) //Only run when there is data available
{
_SerialRead += char(Serial.read()); //Here every received char will be
//added to _SerialRead
if (_SerialRead.indexOf("S") > 0) //Checks for the letter S
{
_SerialRead = ""; //Do something then clear the string
}
}
}

Resources