From 4610ba3792c9cea531930d2c20a6d215b9a92fa3 Mon Sep 17 00:00:00 2001
From: Joerg Lehmann
Date: Thu, 9 Nov 2017 17:10:57 +0100
Subject: [PATCH] diverse Anpassungen
---
REAME.md | 262 +++++++++++++++++++++++++++
bin/process_sms_request.py | 8 +-
install-files/etc/cron.d/gsm_poweron | 2 +-
root-bin/connect_to_internet | 2 +
root-bin/my_web_server.py | 67 ++++++-
web-root/index.tpl | 6 +
web-root/upload_config.tpl | 46 +++++
7 files changed, 386 insertions(+), 7 deletions(-)
create mode 100644 REAME.md
create mode 100644 web-root/upload_config.tpl
diff --git a/REAME.md b/REAME.md
new file mode 100644
index 0000000..925a0ab
--- /dev/null
+++ b/REAME.md
@@ -0,0 +1,262 @@
+## RASPBERRY PI ALS ZENTRALRECHNER FUER BIENENSTOCKUEBERWACHUNG
+
+# Hardware Variante A
+- Raspberry Pi Zero W
+- 3G Stick Huawei E3531
+
+
+# Hardware Variante B (Kurt Jakob)
+- Raspberry Pi 3 Model B
+- Itead Raspberry Pi GSM/GPRS Board (SIM800)
+- ModMyPi Itead GSM Board Gehäuse
+
+
+## INSTALLATION
+
+Image: 2017-09-07-raspbian-stretch-lite.zip
+
+Installation auf SD:
+# unzip -p 2017-09-07-raspbian-stretch-lite.zip |dd of=/dev/sdXXX bs=4M conv=fsync
+
+# raspi-config
+- Change User Password: meielis-...
+- Hostname: beielipi
+- locale: en_US.utf8 (auch Default)
+- locale: de_CH.utf8
+- Timezone: Europe/Zurich
+- Change Wifi Country: CH
+- Keyboard: Generic 105-key (Intl), German (Switzerland)
+
+# cat /etc/wpa_supplicant/wpa_supplicant.conf
+country=CH
+ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
+update_config=1
+
+network={
+ ssid="haerdoepfu27"
+ psk="XXXXXXXXXXXX"
+}
+
+# apt-get update
+# apt-get upgrade
+
+Software installieren:
+# apt-get install gammu gammu-smsd python-gammu
+# apt-get install python-serial
+# apt-get install python-yaml
+# apt-get install minicom
+# apt-get install ppp
+# apt-get install python-pexpect
+
+
+### Variante A
+Einrichten GSM:
+
+/etc/usb_modeswitch.d/12d1:1f01:
+# Huawei E353 (3.se)
+
+DefaultVendor=0x12d1
+DefaultProduct=0x1f01
+TargetVendor=0x12d1
+TargetProduct=0x1f01
+
+MessageContent="55534243123456780000000000000011062000000100000000000000000000"
+NoDriverLoading=1
+
+
+/root/.gammurc:
+; Configuration file generated by gammu-detect.
+; Please check The Gammu Manual for more information.
+
+[gammu]
+device = /dev/ttyUSB0
+name = Phone on USB serial port HUAWEI HUAWEI_Mobile
+connection = at
+
+[gammu1]
+device = /dev/ttyUSB1
+name = Phone on USB serial port HUAWEI HUAWEI_Mobile
+connection = at
+
+[gammu2]
+device = /dev/ttyUSB2
+name = Phone on USB serial port HUAWEI HUAWEI_Mobile
+connection = at
+
+
+Quick Tutorial Gammu:
+https://wammu.eu/docs/manual/quick/index.html
+
+Test SMS-Versand:
+gammu sendsms TEXT 0765006123 -text "Halooo von beielipi"
+
+
+
+### Variante B
+
+Wie Raspberry Zero, jedoch folgende Besonderheiten:
+
+# systemctl disable hciuart.service
+
+root@beielipi:/etc/cron.d# more gsm_poweron
+@reboot root /usr/local/bin/gsm_poweron.py
+
+
+root@beielipi:~# cat /usr/local/bin/gsm_poweron.py
+#!/usr/bin/python
+#
+# GSM-Modul anschalten, falls ein keine Antwort gibt...
+#
+import time
+import RPi.GPIO as GPIO
+import serial
+
+def PowerOn():
+ # RPi.GPIO Layout verwenden (wie Pin-Nummern)
+ GPIO.setmode(GPIO.BOARD)
+
+ # Pin 11 (GPIO 17) auf Output setzen
+ GPIO.setup(11, GPIO.OUT)
+
+ # Druecken simulieren
+ GPIO.output(11, GPIO.HIGH)
+
+ # Pause, mindestens eine Sekunde gemaess https://www.itead.cc/wiki/RPI_SIM800_GSM/GPRS_ADD-ON_V2.0
+ time.sleep(1.5)
+
+ # Weg vom Taster...
+ GPIO.output(11, GPIO.LOW)
+
+ # Cleanup
+ GPIO.cleanup()
+
+with serial.Serial('/dev/ttyAMA0', 115200, timeout=2) as ser:
+ ser.write(b'AT\n')
+ line = ser.readline()
+ line = ser.readline()
+ print "Feedback: %s" % (line)
+ if line.strip() != "OK":
+ print "Modem reagiert nicht, evtl. abgeschaltet... wir schalten es ein..."
+ PowerOn()
+ser.close() # close port
+===
+
+root@beielipi:~# cat /boot/config.txt
+...
+# Wegen GSM Modul
+dtoverlay=pi3-miniuart-bt
+#dtoverlay=pi3-disable-bt
+enable_uart=1
+===
+
+root@beielipi:~# cat /boot/cmdline.txt
+dwc_otg.lpm_enable=0 root=PARTUUID=61612258-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
+===
+
+
+root@beielipi:~# cat /root/.gammurc
+; Diese Konfigurationsdatei wurde von gammu-detect erstellt.
+; Bitte lesen Sie das Gammu-Benutzerhandbuch für weitere Informationen.
+
+[gammu]
+device = /dev/ttyAMA0
+name = Telefon an serielle USB-Schnittstelle Silicon_Labs CP2102_USB_to_UART_Bri
+dge_Controller angeschlossen
+connection = at
+pin = 8296
+
+===
+
+
+SMS senden:
+
+$ gammu-smsd-inject TEXT 0765006123 -text "Hallo Velo"
+
+
+
+root@beielipi:/tmp# cat /etc/gammu-smsdrc
+# Configuration file for Gammu SMS Daemon
+
+# Gammu library configuration, see gammurc(5)
+[gammu]
+# Please configure this!
+port = /dev/ttyAMA0
+connection = at
+# Debugging
+#logformat = textall
+logformat = errorsdate
+pin = 8296
+
+# SMSD configuration, see gammu-smsdrc(5)
+[smsd]
+service = files
+#logfile = syslog
+# Increase for debugging information
+debuglevel = 0
+PIN = 8296
+RunOnReceive = /usr/local/bin/run_on_receive.sh
+
+# Paths where messages are stored
+inboxpath = /var/spool/gammu/inbox/
+outboxpath = /var/spool/gammu/outbox/
+sentsmspath = /var/spool/gammu/sent/
+errorsmspath = /var/spool/gammu/error/
+===
+
+
+## Blabla
+
+User/Gruppe erstellen:
+# groupadd -g 2000 beieli
+# useradd -m -g beieli beieli -s /bin/bash
+# usermod -a -G gammu beieli
+# usermod -a -G dialout beieli
+
+Passwoerter anpassen:
+# passwd pi => meielis-...
+
+
+
+
+root@beielipi:~# rpi-update
+ *** Raspberry Pi firmware updater by Hexxeh, enhanced by AndrewS and Dom
+ *** Performing self-update
+ % Total % Received % Xferd Average Speed Time Time Time Current
+ Dload Upload Total Spent Left Speed
+100 13403 100 13403 0 0 25182 0 --:--:-- --:--:-- --:--:-- 25193
+ *** Relaunching after update
+ *** Raspberry Pi firmware updater by Hexxeh, enhanced by AndrewS and Dom
+ *** We're running for the first time
+ *** Backing up files (this will take a few minutes)
+ *** Backing up firmware
+ *** Backing up modules 4.9.41+
+#############################################################
+This update bumps to rpi-4.9.y linux tree
+Be aware there could be compatibility issues with some drivers
+Discussion here:
+https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=167934
+##############################################################
+ *** Downloading specific firmware revision (this will take a few minutes)
+ % Total % Received % Xferd Average Speed Time Time Time Current
+ Dload Upload Total Spent Left Speed
+100 168 0 168 0 0 25 0 --:--:-- 0:00:06 --:--:-- 35
+100 54.1M 100 54.1M 0 0 989k 0 0:00:56 0:00:56 --:--:-- 1153k
+ *** Updating firmware
+ *** Updating kernel modules
+ *** depmod 4.9.50-v7+
+ *** depmod 4.9.50+
+ *** Updating VideoCore libraries
+ *** Using HardFP libraries
+ *** Updating SDK
+ *** Running ldconfig
+ *** Storing current firmware revision
+ *** Deleting downloaded files
+ *** Syncing changes to disk
+ *** If no errors appeared, your firmware was successfully updated to a31db890da1e5135fe666909d562bdc6113c14d6
+ *** A reboot is needed to activate the new firmware
+root@beielipi:~#
+root@beielipi:~# init 6
+Connection to 192.168.0.34 closed by remote host.
+Connection to 192.168.0.34 closed.
+
+
diff --git a/bin/process_sms_request.py b/bin/process_sms_request.py
index 0611ee1..5be8909 100755
--- a/bin/process_sms_request.py
+++ b/bin/process_sms_request.py
@@ -43,8 +43,8 @@ def send_sms(phonenumbers , text):
'Number': phonenumber
}
- #os.system('echo "%s" | /usr/bin/gammu-smsd-inject TEXT %s' % (text,phonenumber))
- print("YYY Send SMS to %s, Text: %s" % (phonenumber, text))
+ os.system('echo "%s" | /usr/bin/gammu-smsd-inject TEXT %s' % (text,phonenumber))
+ #print("YYY Send SMS to %s, Text: %s" % (phonenumber, text))
def send_mail(send_from, send_to, subject, text, files=None):
os.system('/usr/bin/sudo %s/root-bin/connect_to_internet' % (APP_ROOT))
@@ -211,10 +211,10 @@ def shutdown():
os.system('/usr/bin/sudo /sbin/init 0')
def hotspot_on():
- os.system('/usr/bin/sudo %s/root-bin/hotspot on' % (APP_ROOT))
+ os.system('/usr/bin/sudo %s/root-bin/hotspot_on' % (APP_ROOT))
def hotspot_off():
- os.system('/usr/bin/sudo %s/root-bin/hotspot off' % (APP_ROOT))
+ os.system('/usr/bin/sudo %s/root-bin/hotspot_off' % (APP_ROOT))
def command_not_understood(phonenumber, message):
send_sms([phonenumber],'Befehl nicht verstanden: %s\n\nMoegliche Befehle: help, info, balance, reboot, shutdown, hotspot' % (message[:50]))
diff --git a/install-files/etc/cron.d/gsm_poweron b/install-files/etc/cron.d/gsm_poweron
index c882670..83e1d64 100644
--- a/install-files/etc/cron.d/gsm_poweron
+++ b/install-files/etc/cron.d/gsm_poweron
@@ -1 +1 @@
-@reboot root /usr/local/bin/gsm_poweron.py
+@reboot root /home/beieli/root-bin/gsm_poweron.py
diff --git a/root-bin/connect_to_internet b/root-bin/connect_to_internet
index 1ea1ad2..78a650e 100755
--- a/root-bin/connect_to_internet
+++ b/root-bin/connect_to_internet
@@ -2,5 +2,7 @@
systemctl stop gammu-smsd
pon rnet
sleep 3
+# Nameserver statisch eintragen...
+echo "nameserver 8.8.8.8" >/etc/resolv.conf
# Wir wollen sicher sein, dass die Verbindung steht...
ping -c 1 -w 30 8.8.8.8 >/dev/null
diff --git a/root-bin/my_web_server.py b/root-bin/my_web_server.py
index 1a08aff..6f3ddbf 100755
--- a/root-bin/my_web_server.py
+++ b/root-bin/my_web_server.py
@@ -1,5 +1,5 @@
#!/usr/bin/python
-from bottle import Bottle, run, template, static_file, error, redirect
+from bottle import Bottle, run, template, static_file, error, redirect, post, request
import yaml
import glob
import re
@@ -8,6 +8,7 @@ import random
import string
import os
import shutil
+import subprocess
app = Bottle()
@@ -88,6 +89,39 @@ def CreateDatafile(scale_uuid,infotime):
for line in ifile:
file.write(line)
+def GetInfoText():
+ with open('%s/bin/beielimon-config.yaml' % (APP_ROOT), 'r') as f:
+ config_file_lines = f.read()
+
+ # Wir maskieren das Passwort
+ config_file_lines = re.sub(r"mailpwd:.*\n", "mailpwd: MASKED\n", config_file_lines)
+
+ commands_output = ""
+ for comm_arr in [ ['uptime','--pretty'],
+ ['df','-h','/boot','/root'],
+ ['free','-m'],
+ ['ip','a'] ]:
+ commands_output = "%s\n# %s\n%s" % (commands_output,' '.join(comm_arr),subprocess.check_output(comm_arr))
+
+ res = """Konfigurationsfile:
+
+%s
+
+Diverse Befehle: %s
+
+""" % (config_file_lines, commands_output)
+
+ return res
+
+
+def is_valid_yaml(yaml_string):
+ res = True
+ try:
+ data_parsed = yaml.safe_load(yaml_string)
+ except yaml.YAMLError:
+ res = False
+
+ return res
@app.route('/')
def index():
@@ -95,11 +129,40 @@ def index():
data = {
'scales': hash_scales,
'infotime': time.strftime("%Y", time.localtime()),
- 'beielipi_mobile_number': config_data['beielipi_mobile_number']
+ 'beielipi_mobile_number': config_data['beielipi_mobile_number'],
+ 'infotext': GetInfoText()
}
return template('%s/web-root/index.tpl' % (APP_ROOT), data)
+@app.get('/upload_config')
+def upload_config():
+ my_message = request.GET.get('message', '').strip()
+ my_level = request.GET.get('level', '').strip()
+ data = {
+ 'message': my_message,
+ 'level': my_level
+ }
+
+ return template('%s/web-root/upload_config.tpl' % (APP_ROOT), data)
+
+@app.post('/upload_config')
+def do_upload_config():
+ config_yaml = request.forms.get('config_yaml')
+ if is_valid_yaml(config_yaml):
+ # Wir machen noch ein Backup vor dem Ueberschreiben
+ date_format_string = "+%Y%m%d-%H%M%S"
+ os.system('cp %s/bin/beielimon-config.yaml %s/config-backups/beielimon-config.yaml.$(date %s)' % (APP_ROOT,APP_ROOT,date_format_string))
+ with open('%s/bin/beielimon-config.yaml' % (APP_ROOT), 'w') as file:
+ file.write(string.replace(config_yaml,'\r',''))
+ redirect("/upload_config?message=Konfiguration%20erfolgreich%20ersetzt.%20Bitte%20BeieliPi%20rebooten%20mit%20http%3A%2F%2Fbeielipi.local%2Freboot_beielipi&level=success")
+ else:
+ redirect("/upload_config?message=Fehler%20mit%20YAML-Syntax&level=danger")
+
+@app.route('/reboot_beielipi')
+def reboot_beielipi():
+ os.system('/sbin/init 6')
+
@app.route('/static/')
def server_static(filepath):
return static_file(filepath, root='%s/web-root/static' % (APP_ROOT))
diff --git a/web-root/index.tpl b/web-root/index.tpl
index c75462d..beef990 100644
--- a/web-root/index.tpl
+++ b/web-root/index.tpl
@@ -39,6 +39,12 @@
% end
Messdaten herunterladen
Lädt Daten des aktuellen Jahres herunter.
+
+
+
+
+
+
+
+
+
+
diff --git a/web-root/upload_config.tpl b/web-root/upload_config.tpl
new file mode 100644
index 0000000..48f77da
--- /dev/null
+++ b/web-root/upload_config.tpl
@@ -0,0 +1,46 @@
+
+
+