Seeed-Studio / GPRS_SIM900

library for GPRS shield with sim900 module.
MIT License
138 stars 96 forks source link

Toggle pin by sms #41

Closed Welsyntoffie closed 5 years ago

Welsyntoffie commented 5 years ago

Hi. I could not find a forum for this library. Can you please add example or comment how to switch a pin based on sms received? Example. Pin/password Relay1 On. (1234 Relay1 On). If pin is match then switch on relay1. This would be very helpfull.

lanselambor commented 5 years ago

@Welsyntoffie I think you change examples/GPRS_ReadSMS like below,

int sms_pin = 6;
void loop() {

    /* omitted code */

    pinMode(sms_pin, OUTPUT);
    digitalWrite(sms_pin, LOW);
}
void loop() {
   messageIndex = gprs.isSMSunread();
   if (messageIndex > 0) { //At least, there is one UNREAD SMS            
      Serial.print("Recieved Message.");
      digitalWrite(sms_pin);
      delay(200);
      digitalWrite(sms_pin);
   }
Welsyntoffie commented 5 years ago

Not exactly what I had in mind. I receive alot of advertising messages. So any sms will toggle the pin.

What I had in mind was to send "1234 Relay1 On" to the gsm shield. Then read the message, check if the password is a match, then read on, if sms text Relay1 On then sms_pin = HIGH. If sms text Relay1 Off then sms_pin = LOW.

Jonas-Meyer97 commented 5 years ago

@Welsyntoffie Something like this should help. It just how to parse a string in Arduino and has not really something to do with this library.

void setup() {
  Serial.begin(9600);

  int password = 0;
  char device[20] = {0};
  unsigned int deviceIndex = 0;

  char smsStr[] = "1254 Relay 1";

  sscanf(smsStr, "%4u %19s %u", &password, device, &deviceIndex);

  Serial.println(password);
  Serial.println(device);
  Serial.println(deviceIndex);
}

void loop() {

}
Welsyntoffie commented 5 years ago

This worked for me. Thank you... Modified the smsread example.

''' String messagestring;

void setup() {

pinmode(13, OUTPUT);

void loop() {

// added following below the last Serial print of the example

messagestring = String(message); //creates a string from an array if (messagestring.indexOf("Password") >= 0) { if (messagestring.indexOf("Relay1 On") >= 0) { digitalWrite(13, !digitalRead(13)); } } '''

This solved my mission and seems easy.