Hello
Je travaille sur une project qui va collecter des position GPS et les envoyer sur un server via HTTP/HTTPS. Comme débutant, j'ai un très bon résultat et je suis content. Je recois bien les positions GPS qui sont stocké dans une variable String.
J'ai aussi un autre code qui lui envoie les données au serveur toutes les 10 sec.
Il les envoi avec le protocole HTTP et AT+ commande
J0'ai testé ce code séparément dans un autre fichier, et il marche.
Maintenant, j'ai fusionné les code en ajoutant une fonction void sendDATA(){} dans lequel se trouve mon code qui envoit donc les commandes au serveur.
Le problème, c'est qu'au moment ou le code sendDATA() est exécuté, le terminal crash et reboot, comme si on appuzait sur la touche reboot. Si je commande le code sendDATA(), ca fonctionne. Si je l'utilise séparément, il fonctionne.
C'est un peu comme, si la mémoire était plein, ou si le terminal avait "trop à lire/faire...."
Seriez-vous la raison?
Maintenant, je me demande des bénéfice d'une librairie. par exemple, je pourrais créer un fichie SendData/SendData.h avec la fonction sendDATA() et le mettre dans le dossier library.
Es-ce que ca me serait bénéfique?
Quel est le bénéfique d'une librairie?
Que me recommanderiez-vous pour créer ma première librairie avec succès?
Quels sont les points que je dois pas manquer?
Merci pour vos lumières?
Voci le code de sendDATA()
NewSoftSerial.h
/* FUNCTIONS RELATED TO GPRS WHILE SENDING DATA TO THE SERVER*/
// HOW TO HAVE IT AS A LIBRARAY AND SEND DATA (GPS FIXES) TO A SERVER IN HTTP OT HTTPS
// getMessage is defined outside, but I can have that function here
// cell. is defined outside
// GPRS_ACTIVE, DEBUG and SENDDATA are defined outside but I can remove its
// requests (is a String) is defined outside but I should add it as para,meter in my function sendDATA(requests)
// for eating a single message we expect from the module
// prints out the next message from the module. if it's not the expected value, die
void waitFor(String s) {
String message=getMessage();
if (message != s) {
Serial.print(F("Wait, that's not what we were expecting. We wanted "));
Serial.println("\""+s+"\"");
cellOutputForever();
}
delay(100); // wait for a tiny bit before sending the next command
}
// if something goes wrong, abort and just display cell module output so we can see error messages
// this will loop forever
void cellOutputForever() {
Serial.println(F("Looping forever displaying cell module output..."));
while(1) {
if(cell.available()>0) {
Serial.print((char)cell.read());
}
}
}
// keep reading characters until we get char c
void waitForGSM(char c) {
while(1) {
if(cell.available()>0) {
if ((char)cell.read() == c) {
delay(100);
return;
}
}
}
}
// receive string such as "SOCKSTATUS: 1,1,0102,10,10,0"
// 0 is connection id. 1 is whether connected or not. 2 is status (0104 is connecting, 0102 is connected, others)
// 3 is sent bytes. 4 is acknowledged bytes. 5 is "received data counter"
// THIS FUNCTION WILL check that sent bytes == ack bytes, and return that value
// return 0 if they don't match or if amount of data is 0
int checkSocketString(String s) {
if (socketStringSlice(3,s) == 0)
return 0;
else if (socketStringSlice(3,s) == socketStringSlice(4,s))
return socketStringSlice(3,s);
else
return 0;
}
// returns the index of the nth instance of char c in String s
int nthIndexOf(int n, char c, String s) {
int index=0;
for (int i=0; i<=n; i++) {
index = s.indexOf(c,index+1);
}
return index;
}
// expects string such as "SOCKSTATUS: 1,1,0102,10,10,0"
// returns nth chunk of data, delimited by commas
int socketStringSlice(int n, String s) {
String slice = s.substring(nthIndexOf(n-1,',',s)+1,nthIndexOf(n,',',s));
char cArray[slice.length()+1];
slice.toCharArray(cArray, sizeof(cArray));
return atoi(cArray);
}
void sendDATA(){
/********************************/
/* SEND THE DATA TO THE SERVER */
/********************************/
//cell.println("AT+SBAND=6");
#ifdef GPRS_ACTIVE
#ifdef SENDDATA
//cell.println("AT+SBAND=6");
#ifdef DEBUG
freeRAM();
Serial.println(F("1. Attaching GPRS..."));
#endif
cell.println("AT+CGATT=1");
waitFor("OK");
#ifdef DEBUG
Serial.println(F("2. Setting up PDP Context..."));
#endif
cell.println("AT+CGDCONT=1,\"IP\",\""+apn+"\"");
waitFor("OK");
#ifdef DEBUG
Serial.println(F("3. Activating PDP Context..."));
#endif
cell.println("AT+CGACT=1,1");
waitFor("OK");
#ifdef DEBUG
Serial.println(F("4. Configuring TCP connection to TCP Server..."));
#endif
cell.println("AT+SDATACONF=1,\"TCP\",\""+ip+"\",80");
//cell.println("AT+SDATACONF=1,\"TCP\",\""+ip+"\","+port+"");
waitFor("OK");
#ifdef DEBUG
Serial.println(F("5. Starting TCP Connection..."));
#endif
cell.println("AT+SDATASTART=1,1");
waitFor("OK");
delay(5000); // wait for the socket to connect
// now we'll loop forever, checking the socket status and only breaking when we connect
while (1) {
#ifdef DEBUG
Serial.println(F("6. Checking socket status:"));
#endif
cell.println("AT+SDATASTATUS=1"); // we'll get back SOCKSTATUS and then OK
String sockstat = getMessage();
waitFor("OK");
if (sockstat=="+SOCKSTATUS: 1,0,0104,0,0,0") {
Serial.println(F("Not connected yet. Waiting 1 second and trying again."));
delay(1000);
}
else if (sockstat=="+SOCKSTATUS: 1,1,0102,0,0,0") {
Serial.println(F("Socket connected"));
break;
}
else {
Serial.println(F("We didn't expect that."));
cellOutputForever();
}
}
// we're now connected and can send HTTP packets!
//int packetLength = 26+host.length()+request.length()+useragent.length(); // 26 is size of the non-variable parts of the packet, see SIZE comments below
int packetLength = 26+host.length()+requests.length()+useragent.length();
#ifdef DEBUG
Serial.println(F("7. Sending HTTP packet..."));
#endif
cell.print("AT+SDATATSEND=1,"+String(packetLength)+"\r");
waitForGSM('>'); // wait for GSM module to tell us it's ready to recieve the packet
cell.print(requests+"\r\n"); // SIZE: 2
cell.print("Host: "+host+"\r\n"); // SIZE: 8
cell.print("User-Agent: "+useragent+"\r\n\r\n"); // SIZE: 16
cell.write(26); // ctrl+z character: send the packet
waitFor("OK");
// now we're going to keep checking the socket status forever
// break when the server tells us it acknowledged data
while (1) {
cell.println("AT+SDATASTATUS=1"); // we'll get back SOCKSTATUS and then OK
String s = getMessage(); // we want s to contain the SOCKSTATUS message
if (s == "+STCPD:1") // this means server sent data. cool, but we want to see SOCKSTATUS, so let's get next message
s = getMessage();
if (s == "+STCPC:1") // this means socket closed. cool, but we want to see SOCKSTATUS, so let's get next message
s = getMessage();
waitFor("OK");
if (!s.startsWith("+SOCKSTATUS")) {
Serial.println(F("Wait, this isn't the SOCKSTATUS message!"));
cellOutputForever(); // something went wrong
}
if (checkSocketString(s) == packetLength) // checks that packetLength bytes have been acknowledged by server
break; // we're good!
else {
Serial.println(F("Sent data not yet acknowledged by server, waiting 1 second and checking again."));
delay(1000);
}
}
#ifdef DEBUG
Serial.println(F(" "));
Serial.println(F("YES! DATA ARE SENT AND ACKNOWLEDGE BY SRERVER!"));
#endif
#endif // END IFDEF DSENDDATA
#endif // END IEF GPRS_ACTIVE
/***************************************/
/* **** END SENDING DATA TO THE SERVER */
/***************************************/
}