Sunspot Home

Using a Raspberry pi to send and receive SMS texts via a HiLink 3G internet modem
NB! working but not complete!

Objectives
1) Receive a text when there is new data or a system crash etc.
2) Request data by sending text "Report" to the HiLink USB dongle.

My land line internet speed is less tha 0.6 Mbits so I now use a 3G mobile internet link.

Huawei Unlocked E3372 LTE/4G 150 Mbps USB Dongle - HiLink - plugs into -
TP-Link TL-MR3420 4G N300 Wi-Fi Router
Huawei E3372 External Log Periodic yagi antenna connects to the HiLink

These give 7 to 12 Mbits /sec (at a price...)

But they also allow my Raspberry Pis to send my phone text messages via the web pages presented by the Hilink dongle at http://192.168.8.1/html/smsinbox.html
An SMS sent to the Pis can request a data report as an SMS reply.
So a simple mobile phone without internet access could also control and interrogate devices on my LAN.

Some early tests to see if this is possible.

Many thanks to this site

Send an SMS from a bash script

#!/bin/bash

# /home/graham/SMS/command.sh

# e.g. - - /home/graham/SMS/command.sh 0794xxxxx77 "test SMS from Raspberry PI"

NUMBER=$1
MESSAGE=$2

/home/graham/SMS/session.sh
/home/graham/SMS/token.sh

LENGTH=${#MESSAGE}
TIME=$(date +"%Y-%m-%d %T")
TOKEN=$(</var/www/html/ramdisk2/token.txt)

SMS="<request><Index>-1</Index><Phones><Phone>$NUMBER</Phone></Phones><Sca/><Content>$MESSAGE</Content><Length>$LENGTH</Length><Reserved>1</Reserved><Date>$TIME</Date></request>"

echo $SMS

curl -v -b /var/www/html/ramdisk2/session.txt -c /var/www/html/ramdisk2/session.txt -H "X-Requested-With: XMLHttpRequest" --data "$SMS" http://192.168.8.1/api/sms/send-sms --header "__RequestVerificationToken: $TOKEN" --header "Content-Type:text/xml"

 

- which uses - session.sh

#!/bin/bash

curl -b /var/www/html/ramdisk2/session.txt -c /var/www/html/ramdisk2/session.txt http://192.168.8.1/html/index.html > /dev/null 2>&1

 

- and token.sh

#!/bin/bash

# /home/graham/SMS/token.sh

TOKEN=$(curl -s -b /var/www/html/ramdisk2/session.txt -c /var/www/html/ramdisk2/session.txt http://192.168.8.1/html/smsinbox.html)
TOKEN=$(echo $TOKEN | cut -d'"' -f 10)

echo $TOKEN > /var/www/html/ramdisk2/token.txt

 

Save the last SMS received to xml file - read1smstofile-to-ramdisk2.sh

#!/bin/sh

# /home/graham/SMS/read1sms-to-ramdisk2.sh

RESPONSE=`curl -s -X GET http://192.168.8.1/api/webserver/SesTokInfo`
COOKIE=`echo "$RESPONSE"| grep SessionID=| cut -b 10-147`
TOKEN=`echo "$RESPONSE"| grep TokInfo| cut -b 10-41`
DATA="<request><PageIndex>1</PageIndex><ReadCount>1</ReadCount><BoxType>1</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>1</UnreadPreferred></request>"

#curl -b $COOKIE -c $COOKIE -H "X-Requested-With: XMLHttpRequest" --data "$DATA" http://192.168.8.1/api/sms/sms-list --header "__RequestVerificationToken: $TOKEN" --header "Content-Type:text/xml"
curl -b $COOKIE -c $COOKIE -H "X-Requested-With: XMLHttpRequest" --data "$DATA" http://192.168.8.1/api/sms/sms-list --header "__RequestVerificationToken: $TOKEN" --header "Content-Type:text/xml" >/var/www/html/ramdisk2/mymessage.xml

First test of Blassic basic program "get_last_message.bas" to extract the phone number, date and message text and respond with a report if the message received is "Report"

use apt-get install xsltproc to get xsltproc to extract text from xml message file
I tried to use Blassic but some SMS have many lines and it proved difficult find the end of the message.

#!/usr/sbin/blassic

'/home/graham/SMS/get_last_message.bas
count = 0
DIM message$(100)

LABEL StartAgain

PRINT "Count after StartAgain = ", count

message$ = ""

SHELL "/home/graham/SMS/read1sms-to-ramdisk2.sh"
SHELL "xsltproc /home/graham/SMS/transform.xsl /var/www/html/ramdisk2/mymessage.xml > /var/www/html/ramdisk2/message.txt"

OPEN "/var/www/html/ramdisk2/message.txt" FOR INPUT AS #1
FOR i= 1 to 100
INPUT #1,message$(i)
IF EOF (1) THEN j=(i-1): GOTO NextBit
NEXT i
CLOSE #1

Label NextBit

Phone$ = message$(7)
Received$ = message$(9)

FOR i = 11 to j
message$ = message$ + CHR$(10) + message$(i)
NEXT i

PRINT "SMS received from ", Phone$ ," at ", Received$
PRINT "j= " , j , " " , message$

PRINT "middle bit",MID$(message$, 2, 6)

IF ((MID$(message$, 2, 6) = "Report") AND (count = 0)) THEN PRINT "doing something" : GOTO SendReportFile

PAUSE 10000

count = 0 '**

GOTO StartAgain

Label SendReportFile

'if count is 1 then send a reply
' but if you come here again due to reading the same SMS again
' then count keeps rising until a "sent message" null text is seen
' then count becomes 0 again because the loop sees ** above

count = count + 1
PRINT "Count before sending = ", count

IF count > 1 THEN GOTO StartAgain

OPEN "/var/www/html/ramdisk2/i2c72temp_latest.txt" FOR INPUT AS #1
INPUT #1,T_Outside$
PRINT "T_Outside$ = ",T_Outside$
CLOSE #1
PAUSE 100
OPEN "/var/www/html/ramdisk2/4b1_tmp36_pool-air_latest.txt" FOR INPUT AS #1
INPUT #1,T_PoolAir$
PRINT "T_PoolAir$ = ",T_PoolAir$
CLOSE #1
PAUSE 100
OPEN "/var/www/html/ramdisk2/4b3_pool_latest.txt" FOR INPUT AS #1
INPUT #1,T_Pool$
PRINT "T_Pool$ = ",T_Pool$
CLOSE #1
PAUSE 100
OPEN "/var/www/html/ramdisk2/4b0_potpounds_latest.txt" FOR INPUT AS #1
INPUT #1,HivePounds$
PRINT "HivePounds$ = ",HivePounds$
CLOSE #1

sms_report$ = "T Outside " + T_Outside$ + CHR$(10) + " T Inside " + T_PoolAir$ + CHR$(10) + " T Pool " + T_Pool$ + CHR$(10) + " Hive lb " + HivePounds$

SMS$ = "/home/graham/SMS/command.sh 079xxxxx677 " + CHR$34 + sms_report$ + CHR$34

' NB! CHR$34 is " CHR$(10) is linefeed - so each value is printed on a new line on the phone SMS display

PRINT SMS$

SHELL SMS$
GOTO StartAgain

 

(The report will be saved to /var/www/html/ramdisk2/sms_report.txt by other programs)

mymessage.xml looks like

<?xml version="1.0" encoding="utf-8"?>
<response>
<Count>1</Count>
<Messages>
<Message>
<Smstat>0</Smstat>
<Index>40029</Index>
<Phone>+447941225677</Phone>
<Content>Report</Content>
<Date>2016-09-27 17:37:09</Date>
<Sca></Sca>
<SaveType>4</SaveType>
<Priority>0</Priority>
<SmsType>1</SmsType>
</Message>
</Messages>
</response>

message.txt looks like (with a better knowledge of xsltproc this could be cleaned up (but Blassic string handling is so easy!)

<?xml version="1.0"?>

1


-
+447941225677
-
2016-09-27 17:37:09
-
Report

 

transform.xsl looks like

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:template match="Message">
-
<xsl:value-of select="Phone"/>
-
<xsl:value-of select="Date"/>
-
<xsl:value-of select="Content"/>

</xsl:template>
</xsl:stylesheet>

 

 

 

 


Please email me if you want to swap notes

SUNSPOT HOME

 

 

 

 

 

apt-get install xsltproc