October 5, 2023, 14:09
def read_temp_raw(): f = open(device_file, 'r') lines = f.readlines() f.close() return lines def read_temp(): lines = read_temp_raw() while lines[0].strip()[-3:] != 'YES': time.sleep(0.2) lines = read_temp_raw() equals_pos = lines[1].find('t=') if equals_pos != -1: temp_string = lines[1][equals_pos+2:] temp_c = float(temp_string) / 1000.0 #temp_f = temp_c * 9.0 / 5.0 + 32.0 return temp_c#, temp_f while True: temp = read_temp() if temp is not None: publish.single(pub_topic, str(temp), hostname=broker, port=1883, #auth=auth, tls={} ) break
py number = 123.456 shortened_number = round(number, 1) print(shortened_number)
py def read_temp_raw(): f = open(device_file, 'r') lines = f.readlines() f.close() return lines def read_temp(): lines = read_temp_raw() while lines[0].strip()[-3:] != 'YES': time.sleep(0.2) lines = read_temp_raw() equals_pos = lines[1].find('t=') if equals_pos != -1: temp_string = lines[1][equals_pos+2:] temp_c = float(temp_string) / 1000.0 temp_c = round(temp_c, 1) #it should be here #temp_f = temp_c * 9.0 / 5.0 + 32.0 return temp_c#, temp_f while True: temp = read_temp() if temp is not None: publish.single(pub_topic, str(temp), hostname=broker, port=1883, #auth=auth, tls={} ) break
{"topic":"sensors/kitchen","payload":8.7,"qos":0,"retain":false,"_msgid":"d16f4349d152bda7"}By the way the data should definitely be written to the Pi, because the Zero only has the SD card in it and I want to save it.
publish.single(pub_topic, str(temp),I just needed to add the date string, but didn't know how. But just found out how and like this
publish.single(pub_topic, current_time + str(temp),
version: "3.7" services: node-red: image: nodered/node-red:latest container_name: node-red environment: - TZ=Europe/Amsterdam ports: - "1880:1880" volumes: - node-red-data:/data restart: unless-stopped networks: - node-red-net volumes: node-red-data: networks: node-red-net: external: true name: node-red-neti have no database ^^ I followed this instruction: https://nodered.org/docs/getting-started/docker Can you put a database in there that easily?
db: image: mysql:8.0 command: --default-authentication-plugin=mysql_native_password restart: always environment: MYSQL_ROOT_PASSWORD: root_password ports: - "3306:3306"Ref.: https://blog.christian-schou.dk/creating-and-running-a-mysql-database-with-docker-compose/