Skocz do zawartości

Sterowanie przekaźnikiem za pomocą GSM


MarJanPol

Pomocna odpowiedź

Witam, czy mogę prosić o podpowiedź jak zmodyfikować poniższy kod, żeby konkretny sms np o treści ALARM powodował załączenie się przekaźnika na danym pinie? Chodzi mi o to jak te pojedyncze znaki w zmiennej char c wykorzystać w ifie do porównania z jakimś konkretnym wyeazem - zapewne po wyrażeniu (c = sms.read()). Jakoś nie potrafię sam wymyśleć...

// include the GSM library
#include <GSM.h>

// PIN Number for the SIM
#define PINNUMBER ""

// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;

// Array to hold the number a SMS is retreived from
char senderNumber[20];

void setup() {
 // initialize serial communications and wait for port to open:
 Serial.begin(9600);
 while (!Serial) {
   ; // wait for serial port to connect. Needed for native USB port only
 }

 Serial.println("SMS Messages Receiver");

 // connection state
 boolean notConnected = true;

 // Start GSM connection
 while (notConnected) {
   if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
     notConnected = false;
   } else {
     Serial.println("Not connected");
     delay(1000);
   }
 }

 Serial.println("GSM initialized");
 Serial.println("Waiting for messages");
}

void loop() {
 char c;

 // If there are any SMSs available()
 if (sms.available()) {
   Serial.println("Message received from:");

   // Get remote number
   sms.remoteNumber(senderNumber, 20);
   Serial.println(senderNumber);

   // An example of message disposal
   // Any messages starting with # should be discarded
   if (sms.peek() == '#') {
     Serial.println("Discarded SMS");
     sms.flush();
   }

   // Read message bytes and print them
   while (c = sms.read()) {
     Serial.print(c);
   }

   Serial.println("\nEND OF MESSAGE");

   // Delete message from modem memory
   sms.flush();
   Serial.println("MESSAGE DELETED");
 }

 delay(1000);

}
Link do komentarza
Share on other sites

Zarejestruj się lub zaloguj, aby ukryć tę reklamę.
Zarejestruj się lub zaloguj, aby ukryć tę reklamę.

jlcpcb.jpg

jlcpcb.jpg

Produkcja i montaż PCB - wybierz sprawdzone PCBWay!
   • Darmowe płytki dla studentów i projektów non-profit
   • Tylko 5$ za 10 prototypów PCB w 24 godziny
   • Usługa projektowania PCB na zlecenie
   • Montaż PCB od 30$ + bezpłatna dostawa i szablony
   • Darmowe narzędzie do podglądu plików Gerber
Zobacz również » Film z fabryki PCBWay

a czy zadziała coś takiego? - tzn czy pod zmienną wiadomosc podstawi się odebrany sms?

String wiadomosc = "";

wiadomosc = sms.readStringUntil();

If (wiadomosc == ALARM)

...

a może wystarczy wiadomosc = sms.read(); ?

Sorki, że tak pytam, ale nie mogę sam sprawdzić przez kilka najbliższych dni, bo jestem w delegacji...

Link do komentarza
Share on other sites

Nie mogłem ogarnąć tego Stringa więc próbuję z tablicą, ale też nie wychodzi. Co robię źle?

W skrócie - dodałem :

char message [199];

int i;

for (i = 0; i <200; i = i + 1)

message=sms.read();

Serial.println(message);

ale wiadomość się nie wyświetla 🙁 help!

Cały kod po zmianie:

// include the GSM library

#include

// PIN Number for the SIM

#define PINNUMBER ""

// initialize the library instances

GSM gsmAccess;

GSM_SMS sms;

// Array to hold the number a SMS is retreived from

char senderNumber[20];

char message [199];

int i;

void setup() {

// initialize serial communications and wait for port to open:

Serial.begin(9600);

while (!Serial) {

; // wait for serial port to connect. Needed for native USB port only

}

Serial.println("SMS Messages Receiver");

// connection state

boolean notConnected = true;

// Start GSM connection

while (notConnected) {

if (gsmAccess.begin(PINNUMBER) == GSM_READY) {

notConnected = false;

} else {

Serial.println("Not connected");

delay(1000);

}

}

Serial.println("GSM initialized");

Serial.println("Waiting for messages");

}

void loop() {

char c;

// If there are any SMSs available()

if (sms.available()) {

Serial.println("Message received from:");

// Get remote number

sms.remoteNumber(senderNumber, 20);

Serial.println(senderNumber);

// An example of message disposal

// Any messages starting with # should be discarded

if (sms.peek() == '#') {

Serial.println("Discarded SMS");

sms.flush();

}

// Read message bytes and print them

for (i = 0; i <200; i = i + 1)

message=sms.read();

Serial.println(message);

Serial.println("\nEND OF MESSAGE");

// Delete message from modem memory

sms.flush();

Serial.println("MESSAGE DELETED");

}

delay(1000);

}

Link do komentarza
Share on other sites

Witam, prawie mi się udało, problem jest taki, że działa tylko 2 razy po wgraniu programu do Arduino - to znaczy jednym sms mogą włączyć diodę a drugim wyłączyć. Ale wygaszenie diody i kolejne wysłanie smsa o treści "on" już nie powoduje załączenia się diody. Co robię nie tak?

#include <GSM.h>

// PIN Number
#define PINNUMBER ""

// initialize the library instance
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;

char remoteNumber[20];  // Holds the emitting number
char smsData[80]; //tablica do przechowania smsa
byte smsIndex = 0; //Then, you create an index into the array
char c;

void setup() 
{
// initialize serial communications
Serial.begin(9600); 

Serial.println("SMS Messages Receiver");

// connection state
boolean notConnected = true;

// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while(notConnected)
{
  if(gsmAccess.begin(PINNUMBER)==GSM_READY)
    notConnected = false;
  else
  {
    Serial.println("Not connected");
    delay(1000);
  }
}

Serial.println("GSM initialized");
Serial.println("Waiting for messages");

pinMode(13,OUTPUT);
digitalWrite (13, LOW); 

}

void loop() 
{
// If there are any SMSs available()  
if (sms.available())
{
  Serial.println("Message received from:");

  // Get remote number
  sms.remoteNumber(remoteNumber, 20);
  Serial.println(remoteNumber);

  // This is just an example of message disposal    
  // Messages starting with # should be discarded
  if(sms.peek()=='#')
  {
    Serial.println("Discarded SMS");
    sms.flush();
  }

  // Read message bytes and print them. Read and (effectively) throw away all but the last character from the message.
  while(c=sms.read())
  {
  smsData[smsIndex++] = c;}
       smsIndex = 0;
  //-Serial.println("\nEND OF MESSAGE");
  // delete message from modem memory
  sms.flush();
  Serial.println("MESSAGE DELETED");
if (strcmp(smsData, "on") == 0)
{Serial.println("OKAY");
digitalWrite (13, HIGH); 
}
else
{Serial.println("NO");
digitalWrite (13, LOW);
}
delay(1000);

}}
Link do komentarza
Share on other sites

Dołącz do dyskusji, napisz odpowiedź!

Jeśli masz już konto to zaloguj się teraz, aby opublikować wiadomość jako Ty. Możesz też napisać teraz i zarejestrować się później.
Uwaga: wgrywanie zdjęć i załączników dostępne jest po zalogowaniu!

Anonim
Dołącz do dyskusji! Kliknij i zacznij pisać...

×   Wklejony jako tekst z formatowaniem.   Przywróć formatowanie

  Dozwolonych jest tylko 75 emoji.

×   Twój link będzie automatycznie osadzony.   Wyświetlać jako link

×   Twoja poprzednia zawartość została przywrócona.   Wyczyść edytor

×   Nie możesz wkleić zdjęć bezpośrednio. Prześlij lub wstaw obrazy z adresu URL.

×
×
  • Utwórz nowe...

Ważne informacje

Ta strona używa ciasteczek (cookies), dzięki którym może działać lepiej. Więcej na ten temat znajdziesz w Polityce Prywatności.