Ciao a tutti,
dovrei far eseguire un comando in loop ad arduino per accendere e spegnere dei leds ma solo nel caso in cui venga mandato un certo tipo di messaggio;
mi spiego meglio: per ora sono riuscito ad accendere e spegnere un led, ora mi piacerebbe attivare o deattivare una funzione (sempre tramite sms).
qualcuno può aiutarmi?
Hai fornito pochi dettagli.
Quale Arduino hai? Quale shield per ricevere sms usi?
Ciao,
Ho un arduino 2009 e la shield della sagecom;
il codice che ho utilizzato per accendere o spegnere un led tramite sms è questo:
#include <SoftwareSerial.h>
int onModulePin = 2; // the pin to switch on the module (without press on button)
int count = 0;
// EN: String buffer for the GPRS shield message
String msg = String("");
// EN: Set to 1 when the next GPRS shield message will contains the SMS message
int SmsContentFlag = 0;
//control pins of relay.
int luce=13;
void testModule(){
Serial.flush();
}
void switchModule(){
digitalWrite(onModulePin,HIGH);
delay(2000);
digitalWrite(onModulePin,LOW);
}
void setup(){
pinMode(led, OUTPUT);
pinMode(onModulePin, OUTPUT);
pinMode( 13, OUTPUT );
digitalWrite( 13, LOW );
Serial.begin(19200); // the GPRS baud rate
switchModule(); // switch the module ON
for (int i=0;i<2;i++){
delay(8000);
}
Serial.println("AT+CMGF=1"); // set the SMS mode to text
delay(200);
}
void loop(){
char SerialInByte;
if(Serial.available())
{
SerialInByte = (unsigned char)Serial.read();
delay(5);
// -------------------------------------------------------------------
// EN: Program also listen to the GPRS shield message.
// -------------------------------------------------------------------
// EN: If the message ends with <CR> then process the message
if( SerialInByte == 13 ){
// EN: Store the char into the message buffer
ProcessGprsMsg();
}
if( SerialInByte == 10 ){
// EN: Skip Line feed
}
else {
// EN: store the current character in the message string buffer
msg += String(SerialInByte);
}
}}
void ProcessSms( String sms ){
if( sms.indexOf("on") >= 0 ){
digitalWrite( luce, HIGH );
}
if( sms.indexOf("off") >= 0 ){
digitalWrite( luce, LOW );
}
}
// EN: Request Text Mode for SMS messaging
void GprsTextModeSMS(){
Serial.println( "AT+CMGF=1" );
}
void GprsReadSmsStore( String SmsStorePos ){
Serial.print( "AT+CMGR=" );
Serial.println( SmsStorePos );
}
// EN: Clear the GPRS shield message buffer
void ClearGprsMsg(){
msg = "";
}
// EN: interpret the GPRS shield message and act appropiately
void ProcessGprsMsg() {
if( msg.indexOf( "Call Ready" ) >= 0 ){
// Serial.println( "*** GPRS Shield registered on Mobile Network ***" );
GprsTextModeSMS();
}
// EN: unsolicited message received when getting a SMS message
if( msg.indexOf( "+CMTI" ) >= 0 ){
// Serial.println( "*** SMS Received ***" );
// EN: Look for the coma in the full message (+CMTI: "SM",6)
// In the sample, the SMS is stored at position 6
int iPos = msg.indexOf( "," );
String SmsStorePos = msg.substring( iPos+1 );
// Serial.print( "SMS stored at " );
// Serial.println( SmsStorePos );
// EN: Ask to read the SMS store
GprsReadSmsStore( SmsStorePos );
}
// EN: SMS store readed through UART (result of GprsReadSmsStore request)
if( msg.indexOf( "+CMGR:" ) >= 0 ){
// EN: Next message will contains the BODY of SMS
SmsContentFlag = 1;
// EN: Following lines are essentiel to not clear the flag!
ClearGprsMsg();
return;
}
// EN: +CMGR message just before indicate that the following GRPS Shield message
// (this message) will contains the SMS body
if( SmsContentFlag == 1 ){
// Serial.println( "*** SMS MESSAGE CONTENT ***" );
// Serial.println( msg );
// Serial.println( "*** END OF SMS MESSAGE ***" );
ProcessSms( msg );
}
ClearGprsMsg();
// EN: Always clear the flag
SmsContentFlag = 0;
}
Ora questo funziona, come faccio per far attivare o disattivare una funzione che mandi in loop l'accensione e lo spegnimento random di due led?
Se ho capito quel che vuoi fare:
All'interno del controllo del messaggio tu hai:
if( sms.indexOf("on") >= 0 )
{ digitalWrite( luce, HIGH );
}
Ovvero agisci direttamente sul led.
Se invece qui lavori con una variabile globale di stato esempio: unsigned int Stato=0;
if( sms.indexOf("on") >= 0 )
{ Stato=1;
}
[code]if( sms.indexOf("off") >= 0 )
{ Stato=0;
}
Nella loop(), dove hai la ProcessGprsMsg
void loop(){
... tuo codice
if( SerialInByte == 13 )
{ ProcessGprsMsg();
if(Stato==1) digitalWrite( luce, HIGH );
if(Stato==0) digitalWrite( luce, LOW );
..... if stato==3 e puoi avere altri stati Stato=0 se off, Stato=1 se ricevi on, Stato=3 se ti inventi altri comandi
}
}
[/code]
ok fin qua ci sono grazie mille.
ho messo "stato" ed ho capito come usarlo, l'unica cosa che non capisco è come mai quando mando l'sms i leds lampeggiano random (come dovrebbero) ma solo per alcuni secondi e non in loop come vorrei
Ora ho fatto due funzioni randomeyes () e sleep ()
#include <SoftwareSerial.h>
int ledPin = 13;
int ledPin2 = 12;
long randOn = 0;
long randOff = 0;
//int led = 13;
int onModulePin = 2; // the pin to switch on the module (without press on button)
int count = 0;
// EN: String buffer for the GPRS shield message
String msg = String("");
// EN: Set to 1 when the next GPRS shield message will contains the SMS message
int SmsContentFlag = 0;
//control pins of relay.
//int relay_a=13;
unsigned int Stato=0;
void testModule(){
Serial.flush();
}
void switchModule(){
digitalWrite(onModulePin,HIGH);
delay(2000);
digitalWrite(onModulePin,LOW);
}
void setup(){
randomSeed (analogRead (0));
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(onModulePin, OUTPUT);
Serial.begin(19200); // the GPRS baud rate
switchModule(); // switch the module ON
for (int i=0;i<2;i++){
delay(8000);
}
Serial.println("AT+CMGF=1"); // set the SMS mode to text
delay(200);
}
void randomeyes(){
randOn = random (200, 1000);
randOff = random (150, 250);
digitalWrite(ledPin, HIGH);
delay(randOn);
digitalWrite(ledPin, LOW);
delay(randOff);
digitalWrite(ledPin2, HIGH);
delay(randOn);
digitalWrite(ledPin2, LOW);
delay(randOff);
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, HIGH);
delay(randOn);
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
delay(randOff);
}
void sleep (){
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
}
void loop(){
char SerialInByte;
if(Serial.available())
{
SerialInByte = (unsigned char)Serial.read();
delay(5);
// -------------------------------------------------------------------
// EN: Program also listen to the GPRS shield message.
// -------------------------------------------------------------------
// EN: If the message ends with <CR> then process the message
if( SerialInByte == 13 ){
// EN: Store the char into the message buffer
ProcessGprsMsg();
do {randomeyes();
}while (Stato==1);
if(Stato==0) sleep();}
if( SerialInByte == 10 ){
// EN: Skip Line feed
}
else {
// EN: store the current character in the message string buffer
msg += String(SerialInByte);
}
}}
void ProcessSms( String sms ){
if( sms.indexOf("on") == 1 ){
Stato=1;
count++;
}
if( sms.indexOf("off") >= 0 ){
Stato=0;
count++;
}
if (count == 3){
Serial.println( "AT+CMGD=1,4"); //cancella tutto dopo 6 messaggi
}
}
// EN: Request Text Mode for SMS messaging
void GprsTextModeSMS(){
Serial.println( "AT+CMGF=1" );
}
void GprsReadSmsStore( String SmsStorePos ){
Serial.print( "AT+CMGR=" );
Serial.println( SmsStorePos );
}
// EN: Clear the GPRS shield message buffer
void ClearGprsMsg(){
msg = "";
}
// EN: interpret the GPRS shield message and act appropiately
void ProcessGprsMsg() {
if( msg.indexOf( "Call Ready" ) >= 0 ){
// Serial.println( "*** GPRS Shield registered on Mobile Network ***" );
GprsTextModeSMS();
}
// EN: unsolicited message received when getting a SMS message
if( msg.indexOf( "+CMTI" ) >= 0 ){
// Serial.println( "*** SMS Received ***" );
// EN: Look for the coma in the full message (+CMTI: "SM",6)
// In the sample, the SMS is stored at position 6
int iPos = msg.indexOf( "," );
String SmsStorePos = msg.substring( iPos+1 );
// Serial.print( "SMS stored at " );
// Serial.println( SmsStorePos );
// EN: Ask to read the SMS store
GprsReadSmsStore( SmsStorePos );
}
// EN: SMS store readed through UART (result of GprsReadSmsStore request)
if( msg.indexOf( "+CMGR:" ) >= 0 ){
// EN: Next message will contains the BODY of SMS
SmsContentFlag = 1;
// EN: Following lines are essentiel to not clear the flag!
ClearGprsMsg();
return;
}
// EN: +CMGR message just before indicate that the following GRPS Shield message
// (this message) will contains the SMS body
if( SmsContentFlag == 1 ){
// Serial.println( "*** SMS MESSAGE CONTENT ***" );
// Serial.println( msg );
// Serial.println( "*** END OF SMS MESSAGE ***" );
ProcessSms( msg );
}
ClearGprsMsg();
// EN: Always clear the flag
SmsContentFlag = 0;
}
quando mando l'sms con scritto "on" le luci vanno ma solo per pochi secondi e poi si fermano,
invece dovrebbero andare in loop fino a che non mando l'sms con scritto "off"..
non riesco a capire dove ho sbagliato
ho modificato nel void loop() alcuni parametri in questo modo:
if(Serial.available())
{
SerialInByte = (unsigned char)Serial.read();
delay(5);
// -------------------------------------------------------------------
// EN: Program also listen to the GPRS shield message.
// -------------------------------------------------------------------
// EN: If the message ends with <CR> then process the message
if( SerialInByte == 13 ){
// EN: Store the char into the message buffer
ProcessGprsMsg();
}
if( SerialInByte == 10 ){
// EN: Skip Line feed
}
else {
// EN: store the current character in the message string buffer
msg += String(SerialInByte);
}
}
do{
randomeyes();
} while ( Stato==1 );
if( Stato==0 ){
sleep();
}
}
ora quando mando l'sms per accenderlo funziona e i led si accendono e si spengono in loop
non riesco però a fermarli con l'sms di spegnimento :0
do{
randomeyes();
} while ( Stato==1 );
cicla sempre fintantoché la variabile stato vale uno. Quindi il codice rimarrà sempre intrappolato in quel do while e ne puoi uscire se dentro la funzione randomyas() imposti la variabile di stato globale a 0.
Quando il codice è intrappolato in quel do while, i dati che arrivano dalla seriale non vengono più processati.
Trova il modo di fare ripetere all'infinito (il loop() è un buon posto) la sequenze delle istruzioni:
Legge da seriale
interpreta il dato
imposta la variabile di stato
randomled()
dentro randomled:
if stato == 1 {
fai il lampeggio random
} else {
spegni tutti i led
}
Grazie MauroTec, quindi se ho capito bene dovrei trovare il modo di mettere queste istruzioni:
void ProcessSms( String sms ){
if( sms.indexOf("on") >= 0 ){
Stato=1;
}
if( sms.indexOf("off") >= 0 ){
Stato=0;
}
}
// EN: Request Text Mode for SMS messaging
void GprsTextModeSMS(){
Serial.println( "AT+CMGF=1" );
}
void GprsReadSmsStore( String SmsStorePos ){
Serial.print( "AT+CMGR=" );
Serial.println( SmsStorePos );
}
// EN: Clear the GPRS shield message buffer
void ClearGprsMsg(){
msg = "";
}
// EN: interpret the GPRS shield message and act appropiately
void ProcessGprsMsg() {
if( msg.indexOf( "Call Ready" ) >= 0 ){
// Serial.println( "*** GPRS Shield registered on Mobile Network ***" );
GprsTextModeSMS();
}
// EN: unsolicited message received when getting a SMS message
if( msg.indexOf( "+CMTI" ) >= 0 ){
// Serial.println( "*** SMS Received ***" );
// EN: Look for the coma in the full message (+CMTI: "SM",6)
// In the sample, the SMS is stored at position 6
int iPos = msg.indexOf( "," );
String SmsStorePos = msg.substring( iPos+1 );
// Serial.print( "SMS stored at " );
// Serial.println( SmsStorePos );
// EN: Ask to read the SMS store
GprsReadSmsStore( SmsStorePos );
}
// EN: SMS store readed through UART (result of GprsReadSmsStore request)
if( msg.indexOf( "+CMGR:" ) >= 0 ){
// EN: Next message will contains the BODY of SMS
SmsContentFlag = 1;
// EN: Following lines are essentiel to not clear the flag!
ClearGprsMsg();
return;
}
// EN: +CMGR message just before indicate that the following GRPS Shield message
// (this message) will contains the SMS body
if( SmsContentFlag == 1 ){
// Serial.println( "*** SMS MESSAGE CONTENT ***" );
// Serial.println( msg );
// Serial.println( "*** END OF SMS MESSAGE ***" );
ProcessSms( msg );
}
ClearGprsMsg();
// EN: Always clear the flag
SmsContentFlag = 0;
}
nell void loop()?? come faccio??
Nessuno può aiutarmi?? =( =( =(
Su questo forum non si "affittano" programmatori per scrivere i programmi al proprio posto
Ti sono stati dati dei suggerimenti, sta a te metterli in pratica.
Non sto cercando dei programmatori in affitto, tutt'altro caro Leo72
mi sembra anche che da solo ho fatto molti progressi seguendo alcune istruzioni fornitemi un questo forum, se chiedo aiuto è semplicemente perché magari le istruzioni date non sono così chiare o almeno
non per me.
Nessuno può aiutarmi?? smiley-cry smiley-cry smiley-cry
Mio personale parere, tu sei partito con un programma troppo complesso che ti porta in confusione, porta in confusione anche me, quindi riparti da qualcosa di semplice scritta di tuo pugno, testane il funzionamento superato il quale inserisci una modifica e rifai il test e via così fino a raggiungere il risultato cercato.
Insomma devi imparare le basi di logica e un po di programmazione spicciola e questo lo si fa sperimentando codice di piccole dimensioni e apportando le modifiche per poi testarne il comportamento.
Il problema è il do while che hai inserito e ti ho spiegato che la funzione loop e già un ciclo infinito.
Ciao.
_tuft:
Non sto cercando dei programmatori in affitto, tutt'altro caro Leo72
Caro _tuft, evitiamo il sarcasmo per favore.
Alle volte non è facile leggere il proprio di codice, figurarsi quello degli altri. Se poi il programma è anche lungo ed usa cose particolari come nel tuo caso, non puoi pensare che la gente si metta lì a studiare il codice ed a risolvere i problemi al posto tuo, non avendo per giunta neanche il tuo stesso hardware per fare i test.