Waveshare 7600X RPi automation via SMS

Hi Tim,
Your tutorials are great thank you.
I have successfully implemented the sms and phone call functions using 7600x hat as per your tutorial 4G and GPS HAT For Raspberry Pi - Waveshare SIM7600X - Tutorial Australia .
I now want to use Dave’s SMS-to-Shell on Github (GitHub - itiligent/SMS-to-Shell: Secure remote control & interactive linux shell access over SMS. (Supports TOTP auth for remote shell commands)) (that you also included a link to) to continuously monitor for incoming sms commands and then to execute my scripts.
My question is; you use the ‘power_on’ and ‘power_down’ in your sample code to send an sms or phone call. This it seems would shut down the modem and then restart it each time and thereby turn off the monitoring by Dave’s script?

I would like to keep the modem on standby to receive any incoming sms instructions and to perform a number of functions in a row (for example receive sms instruction, then check temperature of sensor and send sms reply and then wait and then phone call) Is there a way to do this with the code rather than startup and shutdown the 7600x each time?
I did try removing sections of your sample code, but am unsure how this would impact on the modem. I am not sure what best practice is with the 7600X and which parts of the code are essential.
Any advice regarding this would be greatly appreciated.
Regards
Paul

Hi @Paul264093, Welcome to the Forums!!!

From what I can tell from the sample code and Waveshare’s Product Wiki It shouldn’t matter if the HAT left turned on. Depending on the power jumper the Power con command in the software won’t work and the HAT will stay on as long as the Pi is powered.

Being able to restart the HAT in software may be helpful if it has issues or locks up.

Thanks for your reply Aaron.
I’ve done some more testing and it appears there is definitely an issue with the way the different scripts initialise the modem 7600X HAT.
When I us the sample code from Tim’s Tutorial it uses the ‘power-on’ and ‘power_down’ method. After running the demo code the SMS is sent , the script finishes, and one of the lights on the 7600X turns off. The ‘Network’ light turns off, the ‘Power’ light stays on.
As a comparison, with both lights on to start, I ran David’s test-sms.py (GitHub - itiligent/SMS-to-Shell: Secure remote control & interactive linux shell access over SMS. (Supports TOTP auth for remote shell commands)) which uses a different method to initialise the modem via serial.
“# Initialise modem connection
modem = serial.Serial(MODEM, MODEM_BAUD_RATE, timeout=1)”
David’s method will run multiple times without a problem.
However once I run Tim’s demo (which is pretty much the same as the Waveshare Wiki example) then the ‘Network light’ goes off (power light stays on),
then David’s code does not work.
It all seems to relate to how the code initialises the modem. I really just want to have the modem to stay on ready to receive sms instructions at all time, and to stay on after completing each process ready for the next.
I am pretty new to all the coding, but muddle my way through, recognising as much of how things work as possible.This has however got me stumped. Any ideas for a solution are much appreciated.

Hey @Paul264093,

You are indeed correct that the difference lies in how the HAT is initialised, but it should be relatively easy to reconfigure Tim’s script to run on a timed loop instead, or await a message to respond.

Within GPS.py it seems the get_gps_position() function is designed to only run once after power_on. My thoughts are that you could modify this to run on a loop, or be called whenever another function receives a message.

Specifically,
image

Could be modified to say (in pseudocode):

try:
    power_on
    while:
         check for message received on a timed loop:
               if message received, send response, then wait 20 seconds
               
               if no message received, wait 20 seconds

Maybe give this some thought, if you’re still a bit stumped I’d be happy to offer more help.

Thanks for the advice. I do really appreciate it.
I am however trying to achieve something a bit different.
I want to run the SMS-to-Shell Github program, continuously, which it does.
This will watch the modem for any incoming SMS messages I send. When it gets my SMS it will then execute the command… ( A preset command)
This command will run a script which has two parts; (1) checks temperature from a sensor .
(2) Then sends me the temperature in an SMS using Tim’s script (sends Temperature rather than GPS coordinates)
It all works well, except that Tim’s example script ‘powers on’ the modem which is already running and then ‘powers off’ the modem 7600x HAT ( which does not actually turn off the power, just the network light)
Then the Shell-to-SMS stops working because the 7600X network light is turned off.
The solution I think is how to modify Tim’s sample script to just; check the modem is running -then send the SMS and end script.
I am trying to modify Tim’s example, because the modem scripting is a bit complicated and I am only a cut and paste beginner coder.
I hope that explains things better. I am confident there is a way.

I think there is a little bit of confusion here.

The Power off commands are likely turning off the radio component of the 7600X chip but the board is still powered.

Hi Aaron,
You are absolutely correct. That is the issue. I think I have solved the problem however. One line of code was switched the network light off.
(The power remains on the entire time)
I traced it back to ‘power_down (power_key)’
in the second last par of waveshare sms.py demo code I was using as part of my script. When I switch that line off by #commenting it out, the network light stays on.

Thanks for you interest and input Aaron. I hope this little discovery helps anyone else try to implement sms-to-shell with the Waveshare demos.
—————————
Extract from Waveshare wiki 7600x demo
Sms.py

try:
power_on(power_key)
print(‘Sending Short Message Test:’)
SendShortMessage(phone_number,text_message)
print(‘Receive Short Message Test:\n’)
print('Please send message to phone ’ + phone_number)
ReceiveShortMessage()
#power_down(power_key)
except :
if ser != None:
ser.close()
GPIO.cleanup()
———————————————
(My apologies for confusing the Waveshare demo with Tim’s modified demo version. I have been combining a lot of sample code from different places. Sorry folks)

Hi Everyone,
I am progressing with implementing the sms-to-shell.py script to automate and get sms messages from my Pi via Waveshare 7600x hat.
I have it working, but when it sends an sms back to me it follows with another sms with a ‘modem status’ type message.
For example: I send it an SMS instruction (using a predefined Keyword shortcut in the sms-to-shell.py),
it then sends me what i asked for (the temperature of my coolroom from the sensor) “Temp Coolroom: 3.5C”
BUT then sends “+CMGS: 50 OK” as a second sms immediately after.
Why am i getting this and how can I stop it?
It is costing me the price of two sms rather than one.
Anybody got any ideas where this reply message is in the code?

I looked and tested modifying, but with no luck.

1 Like

Hi @Paul264093,

I had a quick look into it.
The second message seems to be line
sent_successfully = '+CMGS: ' in response.decode(MODEM_CHAR_ENCODING)

def send_sms_response Returns that line when a text is sent successfully but I can’t easily see where in the code it is sending it again.

Only thing I can think of is following the program through a line at a time to find which function is sending it again.

1 Like

Hi Paul,

I am the author of the sms-to-shell script, so if you have specifc issues, you can always post these to github for wider sharing.

Here’s a few spitball ideas to mull over:

+CMGS: 50 OK means that the message with ID 50 was successfully sent. Look to the message queue of the modem to see what that message ID number was saying. Is it possible that another running script sent a message to the modem directly and this 50 OK is simply the return acknowledgemt being intecepted by sms-to-shell?

The sms-to-shell script expects to be the only operator/manager of the modem’s queue. 3rd party scripts that want to share the modem may require some sort of check/bypass depending on the originator of the message, or perahps 3rd party scripts could simply create outputs to send directly to sms-to-shell (as custom commands from an assumed phone number?).

Regarding the issue of the modem not initialised. Let sms-to-shell manage the power up and initialise of the modem. Also make both scripts a systemd service and make sure the 3rd party script is dependent on sms-to-shell being up and running first with something like this in the unit file:

[Unit]
Description=Temp sensor 3rd party thingy
Requires=sms-to-shell.service
After=sms-to-shell.service multi-user.target

[Service]
ExecStart=/usr/bin/python3 /path/3rd-party-script.sh 
WorkingDirectory=/some-path
Restart=always
RestartSec=5
User=some-user

[Install]
WantedBy=multi-user.target

Cheers,

Dave

Just had another thought…

To be able to run multiple scripts that access the modem and send sms, you could filter specific sms messages sent to the modem within sms-to-shell.

First declare some kind of indentifier that all 3rdp party script messages will contain. You will have to add this to all SMS text output from other scripts

FILTER_ID = "some unique thing in an sms message"

then add this directly above “if phone_number not in ACL” in the process_sms funtion

# Skip processing if the message contains the filter identifier
        if FILTER_ID in content:
            logger.info("Message skipped due to identifier '%s' in content: %s", FILTER_ID, content)
            return

I’ve not tested, but something like this is the general idea to casue the sms-to-shell to ignore proceesing specific messages found in the modem queue whilst still managing the modem’s message queue so it does not clog up. If you do test, let me know how it goes as it could be a new improvement to sms-to-shell

Hi Dave,
Thanks for reaching our. Really do appreciate it.
Great idea to handle any conflicts in the modem, I will give it a go and let you know how I get on.
I do have other scripts (outside of sms-to-shell and use modem) that run and check temperature and current - and if out of range they send sms alert to me and then call me on the phone. (I own a small speciality food business that has a coolroom, and multiple fridges and am using it for monitoring.) However, they only use modem if there is a fault.
Please bear in mind Im a novice coder but have picked up a lot from just having a go. I launched sms-to-shell via crontab @reboot because i really struggled with systemd. I tried to install as Systemd and managed to crash my whole system - the micro sd card would not even reformat. But I made backups. I also tried to modify as Systemd user service.
I will login to Github for sms-to-shell work as i try to move forward and get it all working smoothly. If it’s ok i might ask a few questions later to get sms-to-shell working as recommended via Systemd root.
Cheers, and thanks for the support.
Paul

Hi Dave,
Hoping you have the time to assist.
(Setting up sms-to-shell as systemd.)
Some problem executing the setup .sh.

I ran the sms-to-shell-setup.sh with it’s default settings, after placing the 3 scripts ( sms-to-shell.py,sms-to-shell-setup.sh, otp-setup.py) in /home directory.
(I could see the files in the /home directory)
This did not work ( I have pasted response code below)

So I tried again running sms-to-shell-setup.sh placing the 3 scripts into / (In this location I could NOT see them in the folder graphical display)
See the code responses below. Can you advise what the issue may be?

Code run from /home directory: response:
paul@raspberrypi:~ sudo bash /home/sms-to-shell-setup.sh /home/sms-to-shell-setup.sh: line 8: ‘\r’: command not found
/home/sms-to-shell-setup.sh: line 16: '\r': command not found /home/sms-to-shell-setup.sh: line 19: ‘\r’: command not found
cp: cannot stat ‘sms-to-shell.py’'\r': No such file or directory /home/sms-to-shell-setup.sh: line 22: ‘\r’: command not found
/home/sms-to-shell-setup.sh: line 39: '\r': command not found chmod: cannot access '/lib/systemd/system/sms-to-shell'‘\r’‘.service’'\r\r': No such file or directory /home/sms-to-shell-setup.sh: line 42: ‘\r’: command not found
Unknown command verb daemon-reload
.
/home/sms-to-shell-setup.sh: line 45: '\r': command not found Invalid unit name "sms-to-shell " escaped as "sms-to-shell\x0d\x0d" (maybe you should use systemd-escape?). Failed to enable unit: Unit file sms-to-shell\x0d\x0d.service does not exist. Invalid unit name "sms-to-shell " escaped as "sms-to-shell\x0d\x0d" (maybe you should use systemd-escape?). Failed to start sms-to-shell\x0d\x0d.service: Unit sms-to-shell\x0d\x0d.service not found. /home/sms-to-shell-setup.sh: line 49: ‘\r’: command not found
Waiting for the service to start…
sleep: invalid time interval ‘5\r’
Try ‘sleep --help’ for more information.
/home/sms-to-shell-setup.sh: line 53: $‘\r’: command not found
Invalid unit name "sms-to-shell
" escaped as “sms-to-shell\x0d” (maybe you should use systemd-escape?).
/home/sms-to-shell-setup.sh: line 62: syntax error: unexpected end of file


Code run from / (top linux directory) reponse was:
paul@raspberrypi:~ sudo bash /sms-to-shell-setup.sh /sms-to-shell-setup.sh: line 8: ‘\r’: command not found
/sms-to-shell-setup.sh: line 16: '\r': command not found /sms-to-shell-setup.sh: line 19: ‘\r’: command not found
cp: cannot stat ‘sms-to-shell.py’'\r': No such file or directory /sms-to-shell-setup.sh: line 22: ‘\r’: command not found
/sms-to-shell-setup.sh: line 39: '\r': command not found chmod: cannot access '/lib/systemd/system/sms-to-shell'‘\r’‘.service’'\r\r': No such file or directory /sms-to-shell-setup.sh: line 42: ‘\r’: command not found
Unknown command verb daemon-reload
.
/sms-to-shell-setup.sh: line 45: '\r': command not found Invalid unit name "sms-to-shell " escaped as "sms-to-shell\x0d\x0d" (maybe you should use systemd-escape?). Failed to enable unit: Unit file sms-to-shell\x0d\x0d.service does not exist. Invalid unit name "sms-to-shell " escaped as "sms-to-shell\x0d\x0d" (maybe you should use systemd-escape?). Failed to start sms-to-shell\x0d\x0d.service: Unit sms-to-shell\x0d\x0d.service not found. /sms-to-shell-setup.sh: line 49: ‘\r’: command not found
Waiting for the service to start…
sleep: invalid time interval ‘5\r’
Try ‘sleep --help’ for more information.
/sms-to-shell-setup.sh: line 53: $‘\r’: command not found
Invalid unit name "sms-to-shell
" escaped as “sms-to-shell\x0d” (maybe you should use systemd-escape?).
/sms-to-shell-setup.sh: line 62: syntax error: unexpected end of file

I greatly appreciate any assistance.

Cheers
Paul

HI paul

I don’t think I can be of much if any assistance as know nothing about what you are doing.

One thing stands out though. The common denominator here looks to be this “\r” whatever that is. You might have to find out what it is and do something about it. Whatever it is the program seems to be upset when it cannot be found.
Cheers Bob

The (\r) in the output strongly suggests that when cutting and pasting the script you have likely appended Windows formatted line feeds. You will only get gobbledegook when this happens.

FYI you can’t cut and paste directly from many windows apps like Notepad or some browsers because Linux reads windows line feeds differently. Cut and paste the RAW versions of the script from github directly into a text editor like Notepad++… or just clone the repo directly to your system (git clone https://github.com/itiligent/SMS-to-Shell) to ensure the script is copied correctly. Follow the readme instructions from there.

1 Like

Hi Dave, Paul

Very true
I have often not been able to compile an Arduino script for what seemed to be an extremely devious reason. After a bit of hair pulling I deleted the offending bit and typed it in manually. This works every time. This happens even when copy and paste an Arduino script. Something happens, don’t quite know what. But I now know the fix.
Cheers Bob

No worries @Robert93820

Here’s how to avoid your pain…

Paste your code into Notepad++ and select all, then select:

Edit | EOL conversion | Unix (LF)

Alternately, if you are feeling brave…
From linux windows line feeds can be fixed at command line too:

sed 's/\r//' broken.script > fixed.script will remove all the \r in the file

cheers

2 Likes

Thanks Dave,
I followed your advice and successfully installed sms-to-shell as systemd. The sms-to-shell-setup.sh loaded everything into the correct place in literally seconds.
Obviously a ‘rookie’ error on my part, picking up an error when moving code.
Greatly appreciate your advice.
Paul

1 Like

Hi Robert
Thanks for your help identifying the issue in the code.
Much appreciated
Paul

I’m glad you got it going Paul… All suggestions welcome regarding improvement of the scripts.

1 Like