adds the raspi code and rc.local
adds the raspi code and rc.local. also moving the licence and readme back that was moved by accident
This commit is contained in:
parent
3054997fe1
commit
7963ee5b24
4 changed files with 122 additions and 115 deletions
23
raspberry_pi/etc/rc.local
Normal file
23
raspberry_pi/etc/rc.local
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#!/bin/sh -e
|
||||||
|
#
|
||||||
|
# rc.local
|
||||||
|
#
|
||||||
|
# This script is executed at the end of each multiuser runlevel.
|
||||||
|
# Make sure that the script will "exit 0" on success or any other
|
||||||
|
# value on error.
|
||||||
|
#
|
||||||
|
# In order to enable or disable this script just change the execution
|
||||||
|
# bits.
|
||||||
|
#
|
||||||
|
# By default this script does nothing.
|
||||||
|
|
||||||
|
# Print the IP address
|
||||||
|
_IP=$(hostname -I) || true
|
||||||
|
if [ "$_IP" ]; then
|
||||||
|
printf "My IP address is %s\n" "$_IP"
|
||||||
|
fi
|
||||||
|
|
||||||
|
sleep 20
|
||||||
|
sudo python3 /home/pi/Schreibtisch/button.py &
|
||||||
|
|
||||||
|
exit 0
|
|
@ -1,4 +1,10 @@
|
||||||
# status bot
|
import RPi.GPIO as GPIO
|
||||||
|
import time
|
||||||
|
|
||||||
|
GPIO.setwarnings(False)
|
||||||
|
GPIO.setmode(GPIO.BOARD)
|
||||||
|
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
|
||||||
|
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
@ -7,17 +13,6 @@ import urllib.request, json
|
||||||
import datetime
|
import datetime
|
||||||
import tweepy
|
import tweepy
|
||||||
import random
|
import random
|
||||||
path = os.path.abspath(__file__)
|
|
||||||
dir_path = os.path.dirname(path)
|
|
||||||
print("dir path is", dir_path)
|
|
||||||
|
|
||||||
def internet_on(url):
|
|
||||||
try:
|
|
||||||
urllib.request.urlopen(url)
|
|
||||||
return True
|
|
||||||
except urllib.error.URLError as e:
|
|
||||||
print(e.reason)
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def textselect(min, max):
|
def textselect(min, max):
|
||||||
|
@ -28,12 +23,10 @@ def textselect(min, max):
|
||||||
else:
|
else:
|
||||||
return random.randint(min, max)
|
return random.randint(min, max)
|
||||||
|
|
||||||
def log(out):
|
|
||||||
f = open(dir_path + os.sep + "log.csv", "a")
|
|
||||||
f.write(out)
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
while True:
|
path = os.path.abspath(__file__)
|
||||||
|
dir_path = os.path.dirname(path)
|
||||||
|
print("dir path is", dir_path)
|
||||||
|
|
||||||
# starting by configuring the bot
|
# starting by configuring the bot
|
||||||
file_configure_path = dir_path + os.sep + "config.json"
|
file_configure_path = dir_path + os.sep + "config.json"
|
||||||
|
@ -53,88 +46,79 @@ while True:
|
||||||
api = tweepy.API(auth)
|
api = tweepy.API(auth)
|
||||||
|
|
||||||
|
|
||||||
if internet_on("http://twitter.com") == True:
|
state = 1
|
||||||
|
state_b4 = 1
|
||||||
# finding out the status of the space...
|
|
||||||
api_url = data_config["status_api"]["url"]
|
|
||||||
with urllib.request.urlopen(api_url) as url:
|
|
||||||
data_space = json.loads(url.read().decode())
|
|
||||||
#print(data_space["state"]["open"])
|
|
||||||
|
|
||||||
|
|
||||||
file_path = path + "space.json"
|
while True:
|
||||||
if os.path.exists(file_path) == True:
|
if GPIO.input(12) == GPIO.HIGH:
|
||||||
# Reading data back
|
#print("button was pushed:")
|
||||||
with open(file_path, 'r') as f:
|
|
||||||
data_space_b4 = json.load(f)
|
state = 1
|
||||||
#print(data_space_b4["state"]["open"])
|
|
||||||
|
|
||||||
|
|
||||||
if data_space["state"]["open"] != data_space_b4["state"]["open"]:
|
if GPIO.input(12) == GPIO.LOW:
|
||||||
print("status changed.")
|
#print("button was not pushed:")
|
||||||
|
state = 0
|
||||||
|
|
||||||
# starting by configuring the bot
|
|
||||||
file_status_path = path + "status.json"
|
|
||||||
with open(file_status_path, 'r') as f:
|
|
||||||
data_status = json.load(f)
|
|
||||||
|
|
||||||
time_now = time.time()
|
time_now = time.time()
|
||||||
|
|
||||||
# finding out wich state it change and sending out the text
|
file_status_path = dir_path + os.sep + "status.json"
|
||||||
if data_space["state"]["open"] == False:
|
#print(file_status_path)
|
||||||
print("closing at", time.time(), ",last change happend at", datetime.datetime.fromtimestamp(
|
|
||||||
int(data_space["state"]["lastchange"]) ).strftime('%Y-%m-%d %H:%M:%S'))
|
|
||||||
|
|
||||||
number = len(data_status["closing_text"])
|
|
||||||
|
if state == 1 and state_b4 == 0:
|
||||||
|
|
||||||
|
if os.path.exists(file_status_path) == True:
|
||||||
|
with open(file_status_path, 'r') as f:
|
||||||
|
data_status = json.load(f)
|
||||||
|
|
||||||
|
number = len(data_status["opening_text"])
|
||||||
number = textselect(0, number - 1)
|
number = textselect(0, number - 1)
|
||||||
|
|
||||||
text = data_status["closing_text"][number]["text"] + " [closed:" + datetime.datetime.fromtimestamp(
|
text = data_status["opening_text"][number]["text"] + " [OPEN:" + datetime.datetime.fromtimestamp(
|
||||||
int( time_now + 3600 )).strftime('%Y-%m-%d %H:%M:%S') + "]"
|
int( time_now )).strftime('%Y-%m-%d %H:%M:%S') + "]"
|
||||||
|
|
||||||
print("Text:", text)
|
print("Text:", text)
|
||||||
|
|
||||||
|
#print("opened")
|
||||||
|
#text = "#Krautspace was opened. testmode (if you think I am wrong, report to me this bug/observation!) " + datetime.datetime.fromtimestamp(
|
||||||
|
# int( time_now )).strftime('%Y-%m-%d %H:%M:%S') + ""
|
||||||
|
|
||||||
|
#print("Text:", text)
|
||||||
try:
|
try:
|
||||||
api.update_status(text)
|
api.update_status(text)
|
||||||
# same text as before cannot be posted!
|
# same text as before cannot be posted!
|
||||||
except:
|
except:
|
||||||
print(time_now, "did not tweet closing status")
|
print(time_now, "did not tweet closing status")
|
||||||
|
|
||||||
log(str(time_now) + ";" + str(data_space["state"]["lastchange"]) + ";" + str(data_space["state"]["open"])
|
if state == 0 and state_b4 == 1:
|
||||||
+ ";" + str(number) + "\n")
|
#print("closed")
|
||||||
|
|
||||||
else:
|
if os.path.exists(file_status_path) == True:
|
||||||
print("opening at", time.time(), ",last change happend at", datetime.datetime.fromtimestamp(
|
with open(file_status_path, 'r') as f:
|
||||||
int(data_space["state"]["lastchange"]) ).strftime('%Y-%m-%d %H:%M:%S'))
|
data_status = json.load(f)
|
||||||
|
|
||||||
number = len(data_status["opening_text"])
|
number = len(data_status["closing_text"])
|
||||||
number = textselect(0, number - 1)
|
number = textselect(0, number - 1)
|
||||||
|
|
||||||
text = data_status["opening_text"][number]["text"] + " [open:" + datetime.datetime.fromtimestamp(
|
text = data_status["closing_text"][number]["text"] + " [CLOSED:" + datetime.datetime.fromtimestamp(
|
||||||
int( time_now + 3600 )).strftime('%Y-%m-%d %H:%M:%S') + "]"
|
int( time_now )).strftime('%Y-%m-%d %H:%M:%S') + "]"
|
||||||
|
|
||||||
print("Text:", text)
|
print("Text:", text)
|
||||||
|
|
||||||
|
|
||||||
|
#text = "#Krautspace was closed. testmode (if you think I am wrong, report to me this bug/observation!) " + datetime.datetime.fromtimestamp(
|
||||||
|
# int( time_now )).strftime('%Y-%m-%d %H:%M:%S') + ""
|
||||||
|
|
||||||
|
#print("Text:", text)
|
||||||
try:
|
try:
|
||||||
api.update_status(text)
|
api.update_status(text)
|
||||||
|
# same text as before cannot be posted!
|
||||||
except:
|
except:
|
||||||
print(time_now, "did not tweet opening status")
|
print(time_now, "did not tweet closing status")
|
||||||
|
|
||||||
log(str(time_now) + ";" + str(data_space["state"]["lastchange"]) + ";" + str(data_space["state"]["open"])
|
state_b4 = state
|
||||||
+ ";" + str(number) + "\n")
|
|
||||||
|
|
||||||
|
time.sleep(10)
|
||||||
else:
|
|
||||||
print("status is still the same at", time.time(), datetime.datetime.fromtimestamp(
|
|
||||||
int(data_space["state"]["lastchange"]) ).strftime('%Y-%m-%d %H:%M:%S'))
|
|
||||||
|
|
||||||
else:
|
|
||||||
print("We're back, baby! #Krautspace space status is active again. @HackspaceJena")
|
|
||||||
|
|
||||||
|
|
||||||
# saving the current space status just too compare them in the next round
|
|
||||||
with open(file_path, 'w') as outfile:
|
|
||||||
json.dump(data_space, outfile, indent=4)
|
|
||||||
|
|
||||||
else:
|
|
||||||
print(time.time(), "internet is off")
|
|
||||||
|
|
||||||
# how often shall we check for status?
|
|
||||||
time.sleep(data_config["status_api"]["checking_period"])
|
|
Reference in a new issue