Ciao,
ho cercato di ridurre le variabili globali, ma invano. Ho deciso di provare a realizzare quanto in oggetto tramite lo sketch con gli AT command, che utilizza una quantità di memoria minore.
Invece di provare lo sketch completo, ho deciso di iniziare con quello che alla pressione di un pulsante, fa una chiamata voce ad un numero telefonico:
int8_t answer;
int onModulePin = 2;
int button = 12;
char aux_str[30];
char phone_number[]="123456789"; // ********* is the number to call
void setup(){
pinMode(onModulePin, OUTPUT);
pinMode(button, INPUT);
digitalWrite(button, HIGH);
Serial.begin(115200);
Serial.println("Starting...");
power_on();
delay(3000);
// sets the PIN code
//sendATcommand("AT+CPIN=****", "OK", 2000);
//delay(3000);
Serial.println("Connecting to the network...");
while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) ||
sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );
sprintf(aux_str, "ATD%s;", phone_number);
sendATcommand(aux_str, "OK", 10000);
// press the button for hang the call
while(digitalRead(button)==1);
Serial.println("ATH"); // disconnects the existing call
}
void loop(){
}
void power_on(){
uint8_t answer=0;
// checks if the module is started
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
// power on pulse
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);
// waits for an answer from the module
while(answer == 0){ // Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
}
}
}
int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string
delay(100);
while( Serial.available() > 0) Serial.read(); // Clean the input buffer
Serial.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do{
if(Serial.available() != 0){
// if there are data in the UART input buffer, reads it and checks for the asnwer
response[x] = Serial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
}while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
Considerate che tale sketch è presente nel sito di cooking-hacks ed adatto per lo shield fornito da loro. Io, invece utilizzo, lo shield acquistato da futura shop Servizio di incisione e taglio laser - FuturaShop. Caricando tale sketch in arduino nel monitor seriale visualizzo "starting" e poi stampa sempre AT, AT....
DOMANDA N°1: non capisco bene cosa fa la funzione "power on".
DOMANDA N°2: il problema sopra esposto credo derivi dal fatto che la board non riesce a comunicare con lo shield. Negli sketch usati prima era presente la libreria "software serial". Devo usarla anche in questo caso?