I'm using SIM900 module + arduino uno + gsmlib.org library.
I'd like to send a string to endpoint on my API , and it worked correctly for the first time when I using inet.httpPOST
function in setup()
, but... in loop()
, when I call that function again, it doesn't work. On serial monitor I get:
ATT: ERROR
RIC:
ERROR
DB:STARTING NEW CONNECTION
ATT: SHUT OK
RIC:
SHUT OK
DB:SHUTTED OK
ATT: OK
RIC:
OK
DB:APN OK
ATT: OK
RIC:
OK
DB:CONNECTION OK
ATT: ERROR
RIC:
xxx.xxx.xxx.xxx
DB:ASSIGNED AN IP
status=ATTACHED
http://api.com.br/mystring
ATT: OK
RIC:
OK
DB:RECVD CMD
ATT: CONNECT OK
RIC:
OK
ATT: OK
RIC:
CONNECT OK
DB:OK TCP
ATT: >
RIC:
>
DB:>
ATT: SEND OK
RIC:
SEND OK
DB:SENT
http://api.com.br/mystring
ATT: OK
RIC:
xxx.xxx.xxx.xxx
ERROR
ALREADY CONNECT
DB:NOT CONN
ATT: OK
RIC:
ERROR
ALREADY CONNECT
DB:NOT CONN
ATT: OK
RIC:
ERROR
ALREADY CONNECT
DB:NOT CONN
How can I send data to my API continuously on loop() Could someone help me?
Following is my code (SOLVED):
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#include <inetGSM.h>
InetGSM inet;
TinyGPSPlus gps;
SoftwareSerial serialGps(11, 10);
/**
setup
*/
void setup() {
// Serial connection.
Serial.begin(19200);
Serial.println(F("Starting App Arduino"));
// Turn on GPRS
powerUpGprs();
// Start configuration of shield SIM900 with baudrate and connect to internet.
if (gsm.begin(9600)) {
Serial.println(F("\nstatus=READY"));
// Start configuration of shield GPS NEO-6M with baudrate.
serialGps.begin(9600);
// Mensure and show memory usage
Serial.println(F("\nMemory usage"));
Serial.print(freeRam());
}
/**
loop
*/
void loop() {
serialGps.listen();
while (serialGps.available() > 0) {
if (gps.encode(serialGps.read())) {
// Get GPS position and send To API
gsm.listen();
// if sim900 started, connect to internet
Serial.print(F("\r\n"));
Serial.print(F(" CONNECT TO INTERNET \r\n"));
// GPRS attach, put in order APN, username and password.
if (inet.attachGPRS("zap.com.br", "zap", "zap"))
Serial.println("status=ATTACHED");
else Serial.println("status=ERROR");
delay(1000);
// Read IP address.
gsm.SimpleWriteln("AT+CIFSR");
Serial.print(F("\r\n\r\n"));
Serial.print(F(" GET GEOLOCATION & SEND TO API \r\n"));
// get geolocation
String geo;
geo.concat("lat=" + String(gps.location.lat(), 6));
geo.concat("&lng=" + String(gps.location.lng(), 6));
geo.concat("&spd=" + String(gps.speed.kmph()));
geo.concat("&gps.uuid=1");
// send2Api
Serial.println(geo.c_str());
Serial.print(F("\n"));
gsm.SimpleWriteln("AT+CIFSR");
inet.httpPOST("api.com.br", 80, "/geolocations", geo.c_str(), "", 100);
}
}
}
/**
mensure memory usage
*/
int freeRam() {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
/**
Power up
*/
void powerUpGprs() {
pinMode(9, OUTPUT);
delay(100);
digitalWrite(9, HIGH);
Serial.println(F("\nPower Up SIM900!"));
}
Thank You!!!