Last Updated or created 2022-04-13
Made a generic sms sender, for check_mk monitoring + sending password of accounts.
You can send sms by filling in a form, or using a url like:
http://smsgateway.local:8080/sms/0612341234/Message%20met%20spaties
It uses a Raspberry and a sim800L module.
Remove pin from simcard fix:
sudo minicom -b 115000 -o -D /dev/serial0
AT+CPIN?
+CPIN: SIM PIN
AT+CPIN=0000
OK
AT+CLCK=”SC”,0,”0000″
OK
AT+CPIN?
+CPIN: READY
Cron
@reboot sh /home/pi/launcher.sh
Launcher
cat /home/pi/launcher.sh
#!/bin/bash
cd /home/pi
while true; do
/usr/bin/python newapi.py
done
newapi.py (uses flask)
import serial
import RPi.GPIO as GPIO
import os, time
import sys
from flask import Flask, render_template, request
app = Flask(__name__)
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField, TextAreaField
from wtforms.validators import DataRequired
from flask import render_template
app.config['SECRET_KEY'] = 'you-will-never-guess'
class FormForm(FlaskForm):
telnumber = StringField('telnumber', validators=[DataRequired()])
messagepart = TextAreaField('Text', render_kw={"rows": 5, "cols": 20})
submit = SubmitField('Send Sms')
@app.route("/sms/<number>/<message>")
def action(number, message):
num = number.encode()
mes = message.encode()
GPIO.setmode(GPIO.BOARD)
# Enable Serial Communication
port = serial.Serial("/dev/serial0", baudrate=9600, timeout=1)
# Transmitting AT Commands to the Modem
# '\r\n' indicates the Enter key
port.write('AT'+'\r\n')
rcv = port.read(10)
print rcv
time.sleep(1)
#port.write('ATE0'+'\r\n') # Disable the Echo
#rcv = port.read(10)
#print rcv
#time.sleep(1)
port.write('AT+CMGF=1'+'\r\n') # Select Message format as Text mode
rcv = port.read(10)
print rcv
time.sleep(1)
port.write('AT+CNMI=2,1,0,0,0'+'\r\n') # New SMS Message Indications
rcv = port.read(10)
print rcv
time.sleep(1)
port.write('AT+CSCS="GSM"'+'\r\n')
rcv = port.read(10)
print rcv
time.sleep(1)
# Sending a message to a particular Number
port.write('AT+CMGS="'+num+'"\r\n')
rcv = port.read(10)
print rcv
time.sleep(1)
port.write(mes+'\r\n') # Message
rcv = port.read(10)
print rcv
port.write("\x1A") # Enable to send SMS
for i in range(10):
rcv = port.read(10)
print rcv
return 'OK'
@app.route("/form")
def form():
form = FormForm()
return render_template('web.html', title='Web Sms', form=form)
@app.route('/data', methods = ['POST', 'GET'])
def data():
if request.method == 'GET':
return "The URL /data is accessed directly. Try going to '/form' to submit form"
if request.method == 'POST':
telnumber = request.form['telnumber']
messagepart = request.form['messagepart']
num = telnumber.encode()
mes = messagepart.encode()
GPIO.setmode(GPIO.BOARD)
# Enable Serial Communication
port = serial.Serial("/dev/serial0", baudrate=9600, timeout=1)
# Transmitting AT Commands to the Modem
# '\r\n' indicates the Enter key
port.write('AT'+'\r\n')
rcv = port.read(10)
print rcv
time.sleep(1)
#port.write('ATE0'+'\r\n') # Disable the Echo
#rcv = port.read(10)
#print rcv
#time.sleep(1)
port.write('AT+CMGF=1'+'\r\n') # Select Message format as Text mode
rcv = port.read(10)
print rcv
time.sleep(1)
port.write('AT+CNMI=2,1,0,0,0'+'\r\n') # New SMS Message Indications
rcv = port.read(10)
print rcv
time.sleep(1)
port.write('AT+CSCS="GSM"'+'\r\n')
rcv = port.read(10)
print rcv
time.sleep(1)
# Sending a message to a particular Number
port.write('AT+CMGS="'+num+'"\r\n')
rcv = port.read(10)
print rcv
time.sleep(1)
port.write(mes+'\r\n') # Message
rcv = port.read(10)
print rcv
port.write("\x1A") # Enable to send SMS
for i in range(10):
rcv = port.read(10)
print rcv
return '<a href="/form">Nog een SMS sturen</a>'
@app.route("/checkmk")
def checkmk():
# Enable Serial Communication
port = serial.Serial("/dev/serial0", baudrate=9600, timeout=1)
port.write('AT'+'\r\n')
rcv1 = port.read(30)
time.sleep(1)
port.write('AT+CPAS'+'\r\n')
rcv2 = port.read(30)
time.sleep(1)
port.write('AT+CGREG?'+'\r\n')
rcv3 = port.read(30)
time.sleep(1)
port.write('AT+CGATT?'+'\r\n')
rcv4 = port.read(30)
time.sleep(1)
port.write('AT+CSQ'+'\r\n')
rcv5 = port.read(30)
time.sleep(1)
return 'OK of niet' + rcv1 + rcv2 + rcv3 + rcv4 + rcv5
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080, debug=True)