Secure gateway with sms receiver: stability?



  • So I was trying to see if I could cram both a secure gateway and an SMS receiving option into an Arduino Nano.

    By secure I mean using the awesome MY_SIGNING_SIMPLE_PASSWD option.

    First I used the Fona library, but even after some fiddling that used too much memory (108%).

    Then I explored hand-making a minimal SMS receiver, and managed to get it down to 89% of memory use. But that gives a warning that there is very little memory left.

    • is that a big stability problem? Or does MySensors reserve the memory it needs?
    • Using the antenna version of NRF24 and a 800L GSM module... might that be too much power draw for the arduino? Is this another foresee-able stability issue?
    • below is my code. I am a pretty bad programmer. Can you spot any optimisation chances?
    /**
    * The MySensors Arduino library handles the wireless radio link and protocol
    * between your home built sensors/actuators and HA controller of choice.
    * The sensors forms a self healing radio network with optional repeaters. Each
    * repeater and gateway builds a routing tables in EEPROM which keeps track of the
    * network topology allowing messages to be routed to nodes.
    *
    * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
    * Copyright (C) 2013-2015 Sensnology AB
    * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
    *
    * Documentation: http://www.mysensors.org
    * Support Forum: http://forum.mysensors.org
    *
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License
    * version 2 as published by the Free Software Foundation.
    *
    *******************************
    *
    * DESCRIPTION
    * The ArduinoGateway prints data received from sensors on the serial link.
    * The gateway accepts input on seral which will be sent out on radio network.
    *
    * The GW code is designed for Arduino Nano 328p / 16MHz
    *
    */
    #include <SoftwareSerial.h>
    SoftwareSerial mySerial(8, 7);
    
    // Enable MySensors debug prints to serial monitor
    //#define MY_DEBUG
    
    // Easy to use security, yay!
    //#define MY_SIGNING_SIMPLE_PASSWD   "putyourpasswordhere"
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    
    // Set LOW transmit power level as default, if you have an amplified NRF-module and
    // power your radio separately with a good regulator you can turn up PA level.
    #define MY_RF24_PA_LEVEL RF24_PA_LOW
    
    // Enable serial gateway
    #define MY_GATEWAY_SERIAL
    
    #include <MySensors.h>
    
    #define RELAY_PIN 3  // Arduino Digital I/O pin number
    
    
    // THIS CAN BE CHANGED
    #define SWITCH_CHILD_ID 1                 // set switch ID number on this node.
    #define SMS_CHILD_ID 2                    // set sms ID number on this node.
    char sms[25] = "";                        // holds the text messages that get sent to the gateway. Maximum length for Domoticz is 25 characters.
    
    
    // THIS SHOULD NOT BE CHANGED
    MyMessage txtmsg(SMS_CHILD_ID,V_TEXT);    // Sets up the message format that we'l be sending to the MySensors gateway later. The first part is the ID of the specific sensor module on this node. The second part tells the gateway what kind of data to expect.
    String msg = String("");
    bool flag = 0;
    
    void before()
    {
        pinMode(RELAY_PIN, OUTPUT);
        digitalWrite(RELAY_PIN, HIGH); // start with internet enabled.
    }
    
    void setup()
    {
      delay(2000); 
      Serial.begin(115200);
      mySerial.begin(9600);
      mySerial.write("\r");
      delay(3000);
      //mySerial.write("AT\r\n"); // say hi to the phone.
      //delay(1000);
      mySerial.write("AT+CMGF=1\r\n"); // set phone to sms mode.
      delay(2000);
      mySerial.write("AT+CNMI=2,2,0,0,0\r\n"); // listen to incoming messages.
      //mySerial.write("AT+CMGR=1\r\n");
      //delay(1000);
    
    }
    
    void presentation()
    {
      sendSketchInfo("Privacy gateway", "1.0");
      present(SWITCH_CHILD_ID, S_BINARY);
      present(SMS_CHILD_ID, S_INFO);
    }
    
    void loop()
    {
    /*
    if( (millis() % 4000) == 0 ) { // test function. for easy testing, also comment out the sms-delete line further down.
      Serial.println("_____");
      while (mySerial.available())
      mySerial.read();
      msg = "";
      mySerial.write("AT+CMGR=1\r\n"); // get first SMS on the phone
      delay(1);
    }
    */
      if(mySerial.available()>0){
        char SerialInByte;
        SerialInByte = (unsigned char)mySerial.read();
        if( SerialInByte == 13 ){
          processline(); // if a \n has been received, process the line.
        }
        if( SerialInByte == 10 ){ // throw away \r
        }
        else {
          msg += String(SerialInByte);
        }
      }
    }
    
    void receive(const MyMessage &message)
    {
      if (message.type==V_STATUS) {
          digitalWrite(RELAY_PIN, message.getBool());
      }
    }
    
    void processline() {
      // for debugging:
      //Serial.println("");
      //Serial.print( "Line: [" );
      //Serial.print( msg );
      //Serial.println( "]" );
    
      if( flag == 1 ){
        flag = 0;
        msg.toCharArray(sms,25);
        send(txtmsg.set(sms));
        mySerial.write("at+cmgda=\"del all\"\n\r"); // delete all received sms messages.
    
      }
      if(msg.indexOf("+3123456789") != -1){ // does the message come from the coupled phone?
        flag = 1; // if so, the next line will be the sms content itself.
        //Serial.println( "found phonenumber" );
      }
      //else{
      //  Serial.println( "no phonenumber found" );
      //}
      msg = "";
    }
    

    // updated the code 2x. Down to 87%.



  • Thoughts:

    • shortening the sms length to 5 characters brings it down to 87%.
    • Could I avoid using the string? I read it's good practice. Maybe put data into a character array straight away. Then again, using the string allows for the quick check if the mobile phone number is present.

  • Mod

    Have you considered moving to a more powerful hw?


  • Contest Winner

    The warning is just that, a warning. It is not a guarantee for the sketch being stable, nor is it a guarantee for it to not be. Easiest is to try and see if it seem to survive for some time (as in more than 15 minutes) with both receiving and sending messages.


  • Mod

    @alowhum regarding power requirements: the nrf24 uses maximum 14mA current. Not sure about the amplified version, but probably about 100-200mA. The 800L uses 2000mA. Yes, that's three zeros. So if your power supply is rated for >2A you should be fine. Running the nrf24 at the same time as 800L adds a very small amount.

    Theses things can easily be fetched from the respective datasheets.



  • Thanks for the insight.

    @mfalkvidd: currently the power for all of this stuff is coming from the 5v pin on a nano. If the 800L takes 2000ma I'm amazed the nano still works 😄

    I think the version I have has an internal condensator to boost its power.

    @gohan: I have concidered using more powerful hardware like the STM32, but I try to use chips that are "just good enough".


  • Mod

    @alowhum the 2A are most likely to be peak current and since they are coming directly from the USB port, the mcu is bypassed so you should be fine.


  • Mod

    @gohan I don't think so. Most usb chips can provide a maximum of 500mA.


  • Mod

    The 5v rail should be on its own... I have to check on my nano


  • Mod

    @gohan unless the usb chip negotiates more than 500mA with the host computer, it is not allowed to drain more than 500mA. It's in the usb spec. This avoids burning your motherboard in case it is not built to deliver more. Without this negotiation protocol, expensive repairs would have to be made when newer devices are connected to older computers.


  • Mod

    On old computers the USB chip in the motherboard will just shut down if more than 0.5A are drawn, on modern ones there are charging USB ports that can supply 1 or 2A and others that just supply the 0.5A even if you connect a power bank that can draw 2A but will just slowly charge at half amp



  • @gohan I suspect @mfalkvidd is correct as I understand it. Certainly there are USB protocols and specific devices which permit negotiation of different constraints including voltage, but this is highly unlikely in this instance....


  • Mod

    @zboblamont actually, it seems we are both correct. According to Maxim Integrated (which should be a reputable source) :

    In USB 2.0, it is not strictly legal to draw power without enumerating, although much of present-day hardware does just that, and in violation of the spec.


  • Mod

    @mfalkvidd open energy for all USB devices 😁
    I haven't looked at any specs, I just reported what I have seen in 18 years working with computers 😉


  • Contest Winner

    @gohan the USB spec require proper device descriptors and enumeration to permit more than 100mA current draw. To go above 500mA you also need to indicate charging capabilities and have termination resistors with certain values. But this all of course require the host to enforce this, which some chips don't. So it might be possible to violate,which is why older pc:s might so this (in some cases at the expense of the motherboard).
    But for safety reasons anyone developing USB drivers should make sure to follow the spec properly, since hosts cannot be "trusted" to just cap the current if you draw too much.
    I have developed such drivers when I was working for Sony Mobile. Was some time ago now, but the spec have probably not changed significantly in recent years in that regard.


  • Mod

    To get back on topic, the schottky diode between VUSB and the Nano 5V rail is a MBR0520 which has a maximum average current of 0.5A but can handle a non-repetitive peak surge current of 5.5A.

    The FT232R usb chip is not capable of negotiating more than 500mA, so drawing more than 500mA will be in violation of the usb spec.

    So if you have an original Nano, or a clone with equal or better specs (not likely), it might be able to handle the 800L. But I would not bet on it. All gsm modems I have tried have been very flaky with less than 2A supply.



  • So I created a gateway with the 800L.. and as predicted it used too much power. I've now put it on a separate node, and seems to work fine.


 

285
Online

7.6k
Users

8.5k
Topics

91.3k
Posts