SMS-TC35 beta plugin - sms send only

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
adampr1
Normal user
Posts: 17
Joined: 27 Jun 2016, 13:34
Location: Germany

SMS-TC35 beta plugin - sms send only

#1 Post by adampr1 » 21 Nov 2016, 11:45

Some code to send SMS over TC35 modem connected to ESPEASY
- debug on serial port must be deactivated.
This code is modified my beta PCD8544 plugin. It can only send SMS - its dirty but maybe some one can make it better :)
Important - only real ASCII character can be used.

Code: Select all

//#######################################################################################################
//#################################### Plugin 210: GSM MODEM - TC35 #################################
//#######################################################################################################

// Sample templates
//  Temp: [DHT11#Temperature]   Hum:[DHT11#humidity]
//  DS Temp:[Dallas1#Temperature#R]
//  Lux:[Lux#Lux#R]
//  Baro:[Baro#Pressure#R]

#define PLUGIN_210
#define PLUGIN_ID_210         210
#define PLUGIN_NAME_210       "GSM MODEM - TC35"
#define PLUGIN_VALUENAME1_210 "TC35"

boolean sendSMS = false;
byte ResetPin = 0;

boolean Plugin_210(byte function, struct EventStruct *event, String& string)
{
  boolean success = false;
  static byte displayTimer = 0;
 
  
  switch (function)
  {

    case PLUGIN_DEVICE_ADD:
      {
        Device[++deviceCount].Number = PLUGIN_ID_210;
        Device[deviceCount].Type = DEVICE_TYPE_SINGLE;
        Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
        Device[deviceCount].Ports = 0;
        Device[deviceCount].PullUpOption = false;
        Device[deviceCount].InverseLogicOption = false;
        Device[deviceCount].FormulaOption = false;
        Device[deviceCount].ValueCount = 0;
        Device[deviceCount].SendDataOption = false;
        Device[deviceCount].TimerOption = true;
        break;
      }

    case PLUGIN_GET_DEVICENAME:
      {
        string = F(PLUGIN_NAME_210);
        break;
      }

    case PLUGIN_GET_DEVICEVALUENAMES:
      {
        strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_210));
        break;
      }

    case PLUGIN_WEBFORM_LOAD:
      {
        char deviceTemplate[6][64];
        LoadCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemplate, sizeof(deviceTemplate));
        for (byte varNr = 0; varNr < 6; varNr++)
        {
          string += F("<TR><TD>Line ");
          string += varNr + 1;
          string += F(":<TD><input type='text' size='64' maxlength='64' name='Plugin_210_template");
          string += varNr + 1;
          string += F("' value='");
          string += deviceTemplate[varNr];
          string += F("'>");
        }

        //string += F("<TR><TD>Display button:<TD>");
        //addPinSelect(false, string, "taskdevicepin3", Settings.TaskDevicePin3[event->TaskIndex]);

        char tmpString[64];

        sprintf_P(tmpString, PSTR("<TR><TD>Display Timeout:<TD><input type='text' name='plugin_210_timer' value='%u'>"), Settings.TaskDevicePluginConfig[event->TaskIndex][2]);
        string += tmpString;

        success = true;
        break;
      }

    case PLUGIN_WEBFORM_SAVE:
      {
        String plugin3 = WebServer.arg("plugin_210_timer");
        Settings.TaskDevicePluginConfig[event->TaskIndex][2] = plugin3.toInt();

        char deviceTemplate[6][64];
        for (byte varNr = 0; varNr < 6; varNr++)
        {
          char argc[25];
          String arg = F("Plugin_210_template");
          arg += varNr + 1;
          arg.toCharArray(argc, 25);
          String tmpString = WebServer.arg(argc);
          strncpy(deviceTemplate[varNr], tmpString.c_str(), sizeof(deviceTemplate[varNr]));
        }

        Settings.TaskDeviceID[event->TaskIndex] = 1; // temp fix, needs a dummy value

        SaveCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemplate, sizeof(deviceTemplate));
        success = true;
        break;
      }

    case PLUGIN_INIT:
      {
       ResetPin = Settings.TaskDevicePin1[event->TaskIndex];
       //Plugin_210_reset_modem();
       pinMode(ResetPin, INPUT);
       digitalWrite(ResetPin, LOW);
       pinMode(ResetPin, OUTPUT);
       digitalWrite(ResetPin, LOW);
       delay (500);
       digitalWrite(ResetPin, HIGH);
       delay (3000);
       Serial.println("AT+CMGF=1");
       delay(500);
        success = true;
        break;
      }

    case PLUGIN_TEN_PER_SECOND:
      {
        if (Settings.TaskDevicePin3[event->TaskIndex] != -1)
        {
          if (!digitalRead(Settings.TaskDevicePin1[event->TaskIndex]))
          {
            displayTimer = Settings.TaskDevicePluginConfig[event->TaskIndex][2];
          }
        }
        break;
      }

    case PLUGIN_ONCE_A_SECOND:
      {
        if ( displayTimer > 0)
        {
          displayTimer--;
          //if (displayTimer == 0)
          //Plugin_210_modemOff();		// MODEM OFF
        }
        break;
      }

    case PLUGIN_READ:
      {
        char deviceTemplate[6][64];
        LoadCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemplate, sizeof(deviceTemplate));
        if (sendSMS)
        {
          Serial.begin(9600);
          for (byte x = 0; x < 6; x++)
          {
            String tmpString = deviceTemplate[x];
            if  (tmpString.startsWith("+"))
            {
              lcd->setCursor(0,3);
              lcd->print("SendSMS");
              tmpString = parseTemplate(tmpString, 14);
              int firstmark = tmpString.indexOf('^');
              int endmark = tmpString.indexOf('^',firstmark + 1);
              String phonenummer = tmpString.substring(0,firstmark);
              String smstext = tmpString.substring(firstmark+1, endmark);
              Serial.println("AT+CMGF=1");
              delay(500);
              String SMSmessage = "AT+CMGS=\"" + phonenummer + "\"\r";
              Serial.print(SMSmessage); // Send phonenummer to GSM MODEM
              delay(500);
              Serial.println(smstext);  // Send text to GSM MODEM
              Serial.print("\r");
              delay(500);
              Serial.println((char)26); // ASCII Ctrl-Z
              Serial.println();
              lcd->setCursor(7,3);
              lcd->print(" OK");
              delay(2000);
              lcd->setCursor(0,3);
              lcd->print("          ");
              }
            }
        sendSMS = false; 
        }
     success = false;
     break;
     }

    case PLUGIN_WRITE:
      {
        String tmpString  = string;
        int argIndex = tmpString.indexOf(',');
        if (argIndex)
          tmpString = tmpString.substring(0, argIndex);
        if (tmpString.equalsIgnoreCase(F("TC35")))
        {
          success = true;
          argIndex = string.lastIndexOf(',');
          tmpString = string.substring(argIndex + 1);
        }
        if (tmpString.equalsIgnoreCase(F("TC35CMD")))
        {
          success = true;
          argIndex = string.lastIndexOf(',');
          tmpString = string.substring(argIndex + 1);
          if (tmpString.equalsIgnoreCase(F("SendSMS")))
          Plugin_210_sendSMS();
          else if (tmpString.equalsIgnoreCase(F("Reset")))
          Plugin_210_reset_modem();			
        }
        break;
      }

  }
  return success;
}


void Plugin_210_reset_modem(void)
{
       pinMode(ResetPin, INPUT);
       digitalWrite(ResetPin, LOW);
       pinMode(ResetPin, OUTPUT);
       digitalWrite(ResetPin, LOW);
       delay (500);
       digitalWrite(ResetPin, HIGH);
       delay (1000);
}

void Plugin_210_sendSMS(void)
{
sendSMS = true;  
}


some screenshot will be uploaded in short time ;)

B.R.
Adam
Domoticz Beta release on OrangePI PC and BananaPI,
2x RFLink (last version),
ESPEasy, ESPEasy32 (different configurations),
MySensors (different configurations),
2x MiLight RGBW (over RFLink)
and others things :D

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 44 guests