[SUITE, voir premier post]
Je sais que c'est un peu vague sans tout mon code, mais j'ai finalement décidé d'utiliser la fonction listen() et, je ne sais pas trop comment l'utiliser.
Et je souhaiterais savoir si vous pouvez-m'aider, et me faire un commentaire sur l'utition de ma méthode précédemment décrite.
En gros,
Dans mon setup, j'ai ceci.
Regarder mes commentaire en majuscule dans le code, c'est là ou se pose mon questionnement. A un moment j'écoute uart_gps.listen pour recevoir les position dédecter pour mon antenne gps, puis je relance l ecoute sur cell.listen() pour ecouter le GPRS et fermer l ecoute de uart_gps.listen.
Es-ce que ca vous semble juste? Ou que me recommanderiez-vous?
/ include the SoftwareSerial library to send serial commands to the cellular module:
#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <LiquidCrystalFast.h>
#define DEBUG
#define GPS_ACTIVE
#define GPRS_ACTIVE
#define LCDD
//#define TRALOG
#define LOG_INTERVAL 10000
#define TERMBAUD 4800
// *********** LED ********
int redGPS = 13; // red
int greenGPS = 10; //green
int firstTimeInLoop = 1;
// ********* GPRS **************
#define GPRSBAUD 9600
#define GPRSRX 2
#define GPRSTX 3
SoftwareSerial cell(GPRSRX,GPRSTX); // Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin. connect this to the GSM module
int GPRS_registered = 0;
int GPRS_AT_ready = 0;
// ************* GPS ******************
#define GPSBAUD 4800 // Don change it for EM406
TinyGPS gps;
boolean newGpsData = false;
unsigned long scheduler;;
#define GPSRX 4 // Unactive for now, I use Serial instead of uart:gps
#define GPSTX 5 // Unactive for now
SoftwareSerial uart_gps(GPSRX, GPSTX);
// LCD Dispaly
#ifdef LCDD
const int numRows = 4;
const int numCols = 16;
LiquidCrystalFast lcd(12, 11, 9, 8, 7, 6);
#endif
void setup()
{
//Initialize serial ports for communication with computer
Serial.begin(TERMBAUD);
uart_gps.begin(GPSBAUD);
cell.begin(GPRSBAUD);
/* Define LED port */
pinMode(redGPS, OUTPUT);
pinMode(greenGPS, OUTPUT);
#ifdef LCDD
lcd.begin(numCols, numRows);
lcd.blink();
lcd.setCursor(0,0);
lcd.print("*** Wellcome ***");
#endif
#ifdef DEBUG
Serial.println(F(" "));
Serial.println(F("* Starting iBip Communication... *"));
#endif
/*
Serial.println(F("Wait for module registered"));
waitTil("+SIND: 11"); // keep printing cell output til we get "+SIND: 11"
Serial.println(F("Module Registered"));
*/
delay(5000);
#ifdef LCDD
lcd.clear();
lcd.home();
#endif
blinkLed(greenGPS,2, 100);
delay(100);
blinkLed(redGPS,2, 100);
delay(100);
//blinkLed(redGPRS,2, 100);
//delay(100);
//blinkLed(blueGPS,2,100);
//delay(100);
// ICI JE LANCE L ECOUTE DES SIGNAUX GPRS
cell.listen();
}
Dans ma loop(), j'ai ceci:
void loop(){
// Uniquement quand je switch ON mon module
if(firstTimeInLoop) {
firstTimeInLoop = 0;
#ifdef GPRS_ACTIVE
#ifdef DEBUG
Serial.println(F("Wait for module is registered (+SIND: 11) and ready (+SIND: 4) ..."));
#endif
while (GPRS_registered == 0 || GPRS_AT_ready == 0) {
String ready = getMessage();
// CHECK IF CARD IS INSERTED
if(ready == "+SIND: 1"){
Serial.println(F("> SIM is inserted ..."));
#ifdef LCDD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SIM inserted (1)");
#endif
blinkLed(greenGPS, 1, 100);
delay(1000);
}
// CHECK IF SIM IS READY
if(ready == "+SIND: 10,\"SM\",1,\"FD\",1,\"LD\",1,\"MC\",1,\"RC\",1,\"ME\",1"){
Serial.println(F("> SIM is ready ..."));
#ifdef LCDD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SIM ready (10-1)");
#endif
blinkLed(greenGPS, 10, 100);
delay(1000);
}
// CHECK IF IS REGISTERED TO THE NETWORK
if(ready == "+SIND: 11"){
GPRS_registered = 1;
Serial.println(F("> Module is registered to the network ..."));
#ifdef LCDD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Module is registered to the network (11)");
#endif
blinkLed(greenGPS, 11, 100);
delay(1000);
}
// CHECK IS PATIALLY READY
if(ready == "+SIND: 3"){
Serial.println(F("> GPRS is partially ready ..."));
#ifdef LCDD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("GPRS is partially ready (3)");
#endif;
blinkLed(greenGPS, 3, 100);
delay(1000);
}
// CHECK IF READY
if(ready == "+SIND: 4"){
GPRS_AT_ready = 1;
Serial.println(F("> GPRS is ready ..."));
#ifdef LCDD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("GPRS is ready (4)");
#endif
blinkLed(greenGPS, 4, 100);
delay(1000);
}
// CHECK IF EMERGEMCY ONLY
if(ready == "+SIND: 7"){
Serial.println(F("> Emergency only ..."));
#ifdef LCDD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Emergency only (7)");
#endif
blinkLed(redGPS, 7, 100);
}
if(ready == "+SIND: 0"){
Serial.println(F("> SIM card removed"));
#ifdef LCDD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SIM vard removed (0)");
#endif
}
// CHECK IF NETWORK NOT AVAILABLE
if(ready == "+SIND: 8"){
Serial.println(F("> GPRS Network Not Available (8)"));
#ifdef LCDD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("GPRS Network not available (8)");
#endif
}
}
#endif // END ifdef GPRS_ACTIVE
}
#ifdef GPS_ACTIVE
if (millis() > scheduler){
scheduler = millis() + LOG_INTERVAL;
/* ******* END Check if there is fix checkGPS(); ************* */
// ICI JE VEUX ECOUTER MON GPS POUR VOIR S IL Y A DES POSITIONS DIPONIBLE. DONC J ECOUTE uart_gps, ET cell.listen N EST PLUS ECOUTE
uart_gps.listen();
#ifdef DEBUG
Serial.println(F("GPS gathering data..."));
#endif
// Parse GPS data for 3 second, Wait for 3sec for a fix
for (unsigned long start = millis(); millis() - start < 3000;){
while(uart_gps.available()){
//while(Serial.available()){
byte c = uart_gps.read();
//byte c = Serial.read();
// New valid NMEA data available
if(gps.encode(c)){
newGpsData = true;
}
}
}
// J AI MON RESULTAT DONC JE RELANCE L ECOUTE SUR cell CE QUI VA FERMER L ECOUTE sur uart_gps. ES_CE BIEN JUSTE
cell.listen();
/* ******* END Check if there is fix checkGPS(); ************* */
// If New valid GPS data available
if (newGpsData){
#ifdef DEBUG
Serial.println(F("New GPS fix available."));
freeRAM();
#endif
blinkLed(greenGPS, 1, 200);
// Get position and send to server (lat, long, alt...)
processGPS();
/*
#ifdef TRALOG
// Log current location into microSD card
processLog();
#endif
*/
newGpsData = false;
}else{
#ifdef DEBUG
Serial.println(F("No GPS fix available."));
freeRAM();
#endif
blinkLed(redGPS, 1, 200);
#ifdef LCDD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No fix available");
lcd.setCursor(0, 1);
lcd.print("Scanning sat. ..");
#endif
}
}
#endif
}
String getMessage() {
String s="";
while(1) {
if(cell.available()>0) {
s = s+(char)cell.read();
if (s.length()>1 && s[s.length()-2]=='\r' && s[s.length()-1]=='\n') { // if last 2 chars are \r\n
if (s==" \r\n" || s=="\r\n") { // skip these, move on
s="";
}
else { // we have a message!
#ifdef DEBUG
Serial.println(s.substring(0,s.length()-2));
#endif
#ifdef LCDD
lcd.clear();
lcd.setCursor(0,2);
lcd.print(s.substring(0,s.length()-2));
//displayLCD(s.substring(0,s.length()-2));
#endif
return s.substring(0,s.length()-2);
}
}
}
}
}
// END LOOP