Send SMS with USB GSM modem when alarm triggered

Still no way to make it work on hassio?

Visit this it contains a list of dongles compatible with Asus, they should also be compatible with Windows and most Linux distros. It has multiple choices for Australia

1 Like

After fixing the stdin/stdout issue in the script, I don’t see why the script wont work with hassio, please give it a try and let us know.
Remember to execute this on the hassio shell before:

apk add ppp

Sorry, I missed that!

May I ask you some questions?

Should I use add-on like “ssh server” to install “chat”?

Where should I store that script? In www?

Is really all config I need is “notify:…” (and secret.yaml) and should not I write somewhere in configuration that I use that script in that folder?

And should not I write in which port I have usb gsm?

If you look at my secret sample above, I have encoded the location of the script (and the phone number) there.

In my case I put the script in the Hass config folder.

For hassio this folder is /config

I will suggest to put it there.

I encoded the serial port inside the script it self.

Does anyone knows how to pass a “secret” to the command line? I don’t, that’s why I hard coded it in the script.

1 Like

did not get it actually
you put the script at config and used "/home/homeassistant/.homeassistant/send_sms.sh" but it is incorrect location, isn’t it?

I get this

Command failed: /www/send_sms.sh +7900000000

And the script is in www

also I tried to copy your code. Changed the number and serial port. Got same issue

Then your secret should be:

notify_sms_person1: "/www/send_sms.sh +1NNNNNNNNNN"
notify_sms_person2: "/www/send_sms.sh +1NNNNNNNNNN"

Just out of curiosity, why would you want to put a bash script at the root of your www server?
Sounds like a security issue down the road.

I am just a noob. Selected www just because I used to store there custom files for lovelace.

Ok, Now it is again in config folder and I should write this:

notify_sms_person1: "/local/send_sms.sh +7NNNNNNNNNN"
notify_sms_person2: "/local/send_sms.sh +7NNNNNNNNNN"

Should I write here actual number, or I write here nnnnnn and actual number is somewhere else?

Yes, replace the entire first parameter with your phone number starting with the plus sign plus your country code plus your number (no dashes or parenthesis)

1 Like

If someone could wrap this up into a add-on, many HA nobs like me would be delighted.

I am hoping for, one day, where I have a USB-GSM dongle attached to my Pi, and if internet goes down the dongle takes over until internet is up again. That would be a security system to trust.

Bad practice, www is for the world.
NNNN stands for the actuall number
To run a script you have to whitelist the containing folder


@Tomahawk
Maybe look for an ups where you put on your pi+router
(My internet is only getting down when there is no power)

For the last four months had lighting taking out my internet twice. One time my internet service provider used one week to get internet back. Being able to use GSM as backup, or as a main communication device for a cabin up in the mountain where cable internet is not available, would make HA complete as a security device.

Whilst this work is all great, there are many variables to consider… especially considering the type of dongle/bearer etc…

I would push for a Gammu Addon instead… https://github.com/pajikos/sms-gammu-gateway … use the right tools for the right job and don’t try to re-invent the wheel… or do, but then don’t complain about breaking changes down the line… but as i started off by saying… well done on the hard work and getting this working…

The problem with Gammu is that it does not uses an async approach to talk to the modem, it locks
for every single call to the modem.

The goals will be to use the async serial interface for python to make sure we don’t use the global python lock which if used in Home Assistant will block.

I do agree, however, maybe we should create another python async friendly library to abstract SMS commands and then have a hass specific extension on top of that.

Today I completed my migration to hassio.
However, I ran into a problem in how Alpine recognizes the Hauwei USB stick, it refuses to install the usb serial driver for it.
In other Linux distros you are supposed to run usb_switch, however running this in hass.io give me the following error:

core-ssh:~# usb_modeswitch -v 12d1 -p 14fe -M 55534243123456780000000000000011062000000101000100000000000000
Error: Failed to initialize libusb. LIBUSB_ERROR_OTHER (-99)

I will follow up with a new issue on hassio

I have created a fix for hassio here

This is a version of a python script compatible with hassio:

import serial
import sys
import getopt
import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

DEST=sys.argv[1]
#MD="/dev/ttyUSB2"
MD="COM10"
if (len(sys.argv) > 2):
    message=sys.argv[2]
else:
    message = ''
    for line in sys.stdin:
        message+=line

logging.debug('Sending message '+message+' to '+DEST)

logging.debug('Opening serial port '+MD)
ser = serial.Serial(MD)  # open serial port
ser.write(b'\x1a') # Flush any previuos attempts to send message
ser.flush()

logging.debug('Sending AT+CMGF command')

line = b''
ser.write(b'AT+CMGF=1\r')
while line!=b'OK\r\n':
  line = ser.readline()
  logging.debug('Response '+str(line))

ser.flush()

logging.debug('Sending AT+CMGS command')
line = b''
phoneAsAscii = bytes(DEST, 'ascii')
ser.write(b'AT+CMGS="'+phoneAsAscii+b'"\r')
while line!=b'> \r\n':
  line = ser.readline()
  logging.debug('Response '+str(line))

ser.flush()
logging.debug('Sending message')
messageAsAscii = bytes(message, 'ascii')
ser.write(messageAsAscii + b'\x1a')
line = b''
while line!=b'OK\r\n':
  line = ser.readline()
  logging.debug('Response '+str(line))

ser.close() 

Another good reason to have a native SMS integration.

1 Like

Hey Oscar,

I have just updated the Hassos and see that your PR was merged. It would be great to understand how this will work for:

  • Internet failover
  • SMS Send/Receive (hopefully with the possibility of easily itegrating sensors from the MT’s

Let me know if you need any help testing…

Thanks for all your hard work…

My python script needs some work, so I switched back to a bash shell:

/config/send_sms.sh
#!/bin/bash

DEST=$1
MD="/dev/ttyUSB2"

if [ -z "$2" ]
then
read message
else
message="$2"
fi

echo "Sending $message to $DEST"

apk add ppp

stty -F $MD 9600 min 100 time 2 -hupcl brkint ignpar -opost -onlcr -isig -icanon -echo

chat TIMEOUT 10 "" "AT+CMGF=1" "OK" > $MD < $MD
chat TIMEOUT 10 "" "AT+CMGS=\"$DEST\"" "OK" > $MD < $MD
chat TIMEOUT 10 "" "$message^Z" "OK" > $MD < $MD

Also, if you are using a RPI4, I found that you need a udev rule in the config USB stick, for me this config was:

udev\10-gsm-modem.rules
ACTION=="add" \
, ATTRS{idVendor}=="12d1" \
, ATTRS{idProduct}=="14fe" \
, RUN+="/sbin/usb_modeswitch -X -v 12d1 -p 14fe"

You get the vendor id and product id from the output of lsusb

I have changed my mind and reusing gammu gateway in a docker style from @pajikos makes more sense than trying to reimplement all SMS handling code.

I have created a new custom integration for home assistant here