Raspberry Pi code

CRON
#  starting every 5 minutes perform value resets and data grabs from lora devices by name - BEDRM  APPLE etc
*/5 * * * * sudo /home/pi/lora/control.bas

# just after midnight remove the lora data text file for yesterday then copy the lora graphs to the usb memory stick
00 0 * * * sudo rm /var/www/html/ramdisk/lora_data_for_plot_APPLE.txt
01 0 * * * sudo rm /var/www/html/ramdisk/lora_data_for_plot_BEDRM.txt
02 0 * * * /bin/bash -c 'd=$(date +%F); cp /var/www/html/ramdisk/lora_heltec_APPLE.jpg /media/pi/FOUR123/lora_data_for_plot/${d}_lora_APPLE.jpg'
03 0 * * * /bin/bash -c 'd=$(date +%F); cp /var/www/html/ramdisk/lora_heltec_BEDRM.jpg /media/pi/FOUR123/lora_data_for_plot/${d}_lora_BEDRM.jpg'

# reboot at 4 minutes past midnight
04 0 * * *    /sbin/reboot
@reboot sudo /home/pi/MakeRAMdisk/setup-ramfs.sh
@reboot sudo /home/pi/lora/clear_the_data_file.sh

# make bedroom beeper play alarm
16 07 * * * sudo /home/pi/lora/WIFI_GPT_lora/send_command_wifi_arg.py BEDRMb15

#!/usr/sbin/blassic

' sudo /home/pi/lora/control.bas
' use cron to run every 5 minutes
' reset the temperature and hysteresis every time and not just at a change point because a failed
' command or power off crash will leave the values at default for hours

hour$ = LEFT$(time$,2)
hour = VAL(hour$)
print hour

' set the HotBox temperatures
If (hour >= 9) AND (hour < 18)  THEN SHELL "sudo  /home/pi/lora/WIFI_GPT_lora/send_command_wifi_arg.py APPLEt2002"
If (hour >= 18) OR (hour < 9)   THEN SHELL "sudo  /home/pi/lora/WIFI_GPT_lora/send_command_wifi_arg.py APPLEt1002"
PAUSE 8000
' plot the graph
SHELL "sudo /home/pi/lora/gnuplot/lora_heltec_APPLE.sh"
PAUSE 3000


'set the Bedroom temperatures
If (hour >= 7) AND (hour < 20)  THEN SHELL "sudo  /home/pi/lora/WIFI_GPT_lora/send_command_wifi_arg.py BEDRMt1002"
If (hour >= 20) OR (hour < 7)   THEN SHELL "sudo  /home/pi/lora/WIFI_GPT_lora/send_command_wifi_arg.py BEDRMt1802"
PAUSE 8000
' plot the graph
SHELL "sudo /home/pi/lora/gnuplot/lora_heltec_BEDRM.sh"
PAUSE 3000


'read the incubator temperature (no control yet)
SHELL "sudo /home/pi/lora/WIFI_GPT_lora/send_command_wifi_arg.py INCUBt0000"
PAUSE 8000
' plot the graph
SHELL "sudo /home/pi/lora/gnuplot/lora_lilygo_INCUB.sh"

SYSTEM


Via WIFI use the python code below to tell the master ESP32/LORA node to send text to command  a LORA sensor/control node and have it reply with data. Save the data to ramdisk.
Th blassic basic script above grabs the ramdisk data and tells gnuplot to create a graph as a webpage. The Pi then displays the webpage among others on the kitchen display.


#!/usr/bin/python3
# Example:
#   just ask for data
#   sudo /home/pi/lora/WIFI_GPT_lora/send_command_wifi_arg.py APPLEr0000
#   sudo /home/pi/lora/WIFI_GPT_lora/send_command_wifi_arg.py BEDRMr0000
#   sudo /home/pi/lora/WIFI_GPT_lora/send_command_wifi_arg.py INCUBt0000
#   sudo /home/pi/lora/WIFI_GPT_lora/send_command_wifi_arg.py HONEY,t
#   change thermostat set point and hysteresis to 22 and 2
#   sudo /home/pi/lora/WIFI_GPT_lora/send_command_wifi_arg.py APPLEt2202

import sys
import requests
from datetime import datetime
import os

ESP32_URL = "http://192.168.1.123/command"
RAMDISK_DIR = "/var/www/html/ramdisk"

def parse_reply(reply: str):
    # Expect: ID,IN,GH,OUT,SETPOINT
    parts = [p.strip() for p in reply.split(",")]
    if len(parts) != 5:
        return None
    dev = parts[0]
    rest = ",".join(parts[1:])  # IN,GH,OUT,SETPOINT
    if len(dev) != 5:
        return None
    return dev, rest

def logfile_for_device(dev: str) -> str:
    return os.path.join(RAMDISK_DIR, f"lora_data_for_plot_{dev}.txt")

def maybe_truncate_new_day(path: str, now: datetime) -> str:
    if not os.path.exists(path):
        return "w"
    try:
        mtime = datetime.fromtimestamp(os.path.getmtime(path))
        if mtime.date() != now.date():
            return "w"
    except Exception:
        pass
    return "a"

def main():
    if len(sys.argv) < 2:
        print("Usage: send_command_wifi_arg.py <command>")
        sys.exit(1)

    command = sys.argv[1].strip()
    print("command is:", command)

    # Send command to ESP32
    try:
        response = requests.post(ESP32_URL, data=command, timeout=5)
        if response.status_code != 200:
            print("ESP32 returned HTTP", response.status_code)
            return
        raw = response.text.strip()
    except Exception as e:
        print("Failed to contact ESP32:", e)
        return

    print("raw reply:", raw)

    parsed = parse_reply(raw)
    if not parsed:
        print("Reply not in expected format 'ID,IN,GH,OUT,SET' — not logging.")
        return

    dev, data = parsed
    now = datetime.now()
    timestamp = now.strftime("%H:%M:%S")

    # Build line: time,IN,GH,OUT,SETPOINT
    line = f"{timestamp},{data}\n"

    path = logfile_for_device(dev)
    mode = maybe_truncate_new_day(path, now)

    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, mode) as f:
        f.write(line)

    print(f"Logged to {path}: {line.strip()}")

if __name__ == "__main__":
    main()