2024-06-27 11:14:28 +02:00
|
|
|
import mysql.connector
|
|
|
|
from mysql.connector import errorcode
|
|
|
|
from tmp_0x09 import get_temp
|
|
|
|
|
|
|
|
|
|
|
|
# MySQL database connection setup
|
2024-06-27 12:14:02 +02:00
|
|
|
config_local = {
|
2024-06-27 11:14:28 +02:00
|
|
|
'user': 'root', # dein MySQL-Benutzername
|
|
|
|
'password': '', # dein MySQL-Passwort (standardmäßig leer bei XAMPP)
|
|
|
|
'host': '127.0.0.1',
|
|
|
|
'database': 'arduino',
|
|
|
|
'raise_on_warnings': True
|
|
|
|
}
|
|
|
|
|
2024-06-27 12:14:02 +02:00
|
|
|
config_cloud = {
|
|
|
|
'user': 'moritz', # dein MySQL-Benutzername
|
|
|
|
'password': 'T3st1234', # dein MySQL-Passwort (standardmäßig leer bei XAMPP)
|
|
|
|
'host': 'icetruck.mysql.database.azure.com',
|
|
|
|
'database': 'arduino',
|
|
|
|
'raise_on_warnings': True
|
|
|
|
}
|
|
|
|
|
|
|
|
def insert_data(temp, sensor_id, config):
|
2024-06-27 11:14:28 +02:00
|
|
|
try:
|
|
|
|
cnx = mysql.connector.connect(**config)
|
|
|
|
cursor = cnx.cursor()
|
2024-06-27 12:14:02 +02:00
|
|
|
add_data = ("INSERT INTO sensordaten "
|
2024-06-27 11:14:28 +02:00
|
|
|
"(temperatur, sensor_id) "
|
|
|
|
"VALUES (%s, %s)")
|
|
|
|
data = (temp, sensor_id)
|
|
|
|
cursor.execute(add_data, data)
|
|
|
|
cnx.commit()
|
|
|
|
cursor.close()
|
|
|
|
cnx.close()
|
2024-06-27 12:24:32 +02:00
|
|
|
print(f'Daten erfolgreich eingefügt in: {config["host"]}')
|
2024-06-27 11:14:28 +02:00
|
|
|
except mysql.connector.Error as err:
|
|
|
|
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
|
|
|
|
print("Etwas ist mit deinem Benutzernamen oder Passwort falsch.")
|
2024-06-27 12:32:41 +02:00
|
|
|
print(f'Datenbank: {config["host"]}')
|
2024-06-27 11:14:28 +02:00
|
|
|
elif err.errno == errorcode.ER_BAD_DB_ERROR:
|
|
|
|
print("Die Datenbank existiert nicht.")
|
2024-06-27 12:32:41 +02:00
|
|
|
print(f'Datenbank: {config["host"]}')
|
2024-06-27 11:14:28 +02:00
|
|
|
else:
|
|
|
|
print(err)
|
2024-06-27 12:32:41 +02:00
|
|
|
print(f'Datenbank: {config["host"]}')
|
2024-06-27 11:14:28 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2024-06-27 12:44:30 +02:00
|
|
|
last_temp_sensor_1 = -300
|
|
|
|
last_temp_sensor_2 = -300
|
|
|
|
last_temp_sensor_3 = -300
|
|
|
|
|
2024-06-27 11:14:28 +02:00
|
|
|
while True:
|
2024-06-27 12:44:30 +02:00
|
|
|
if get_temp(0x09) != last_temp_sensor_1:
|
|
|
|
insert_data(get_temp(0x09), 1, config_cloud)
|
|
|
|
insert_data(get_temp(0x09), 1, config_local)
|
|
|
|
last_temp_sensor_1 = get_temp(0x09)
|
|
|
|
else:
|
|
|
|
print("Daten Sensor 1 sind gleichgeblieben")
|
|
|
|
|
|
|
|
if get_temp(0x0a) != last_temp_sensor_2:
|
|
|
|
insert_data(get_temp(0x0a), 2, config_local)
|
|
|
|
insert_data(get_temp(0x0a), 2, config_cloud)
|
|
|
|
last_temp_sensor_2 = get_temp(0x0a)
|
|
|
|
else:
|
|
|
|
print("Daten Sensor 2 sind gleichgeblieben")
|
|
|
|
|
|
|
|
if get_temp(0x0b) != last_temp_sensor_3:
|
|
|
|
insert_data(get_temp(0x0b), 3, config_local)
|
|
|
|
insert_data(get_temp(0x0b), 3, config_cloud)
|
|
|
|
last_temp_sensor_3 = get_temp(0x0b)
|
|
|
|
else:
|
|
|
|
print("Daten Sensor 3 sind gleichgeblieben")
|