Compare commits
7 Commits
efea4a1384
...
03b26b5339
| Author | SHA1 | Date | |
|---|---|---|---|
| 03b26b5339 | |||
| 5a8d14eb4d | |||
| 79445bf879 | |||
| 4400fb5a74 | |||
| c6f46e097b | |||
| d2c0f68488 | |||
| 13e3a56fa6 |
@@ -1,5 +1,72 @@
|
||||
# Minimal module-level state (only what we need)
|
||||
_CONFIG = {"discord_webhook_url": None, "discord_alert_webhook_url": None}
|
||||
# Cooldown after low-memory failures (epoch seconds)
|
||||
_NEXT_ALLOWED_SEND_TS = 0
|
||||
|
||||
def debug_force_send(message):
|
||||
"""
|
||||
Force one send attempt and print gc.mem_free() at key points.
|
||||
Bypasses cooldown and pre-checks so you can measure peak allocations.
|
||||
Use from REPL after WiFi connects:
|
||||
import scripts.discord_webhook as d
|
||||
d.set_config(config)
|
||||
d.debug_force_send("memory test")
|
||||
WARNING: this can trigger ENOMEM and crash if device free RAM is too low.
|
||||
"""
|
||||
global _NEXT_ALLOWED_SEND_TS
|
||||
resp = None
|
||||
try:
|
||||
import gc # type: ignore
|
||||
import time # type: ignore
|
||||
|
||||
url = _get_webhook_url(False)
|
||||
if not url:
|
||||
print("DBG_FORCE: no webhook URL configured")
|
||||
return False
|
||||
|
||||
print("DBG_FORCE: mem before gc:", getattr(gc, "mem_free", lambda: 0)() // 1024, "KB")
|
||||
gc.collect(); gc.collect()
|
||||
print("DBG_FORCE: mem after gc:", getattr(gc, "mem_free", lambda: 0)() // 1024, "KB")
|
||||
|
||||
# Try importing urequests and show mem impact
|
||||
try:
|
||||
print("DBG_FORCE: importing urequests...")
|
||||
import urequests as requests # type: ignore
|
||||
gc.collect()
|
||||
print("DBG_FORCE: mem after import:", getattr(gc, "mem_free", lambda: 0)() // 1024, "KB")
|
||||
except Exception as e:
|
||||
print("DBG_FORCE: urequests import failed:", e)
|
||||
return False
|
||||
|
||||
# Build tiny payload
|
||||
body_bytes = ('{"content":"%s","username":"%s"}' % (str(message)[:140], "DBG")).encode("utf-8")
|
||||
print("DBG_FORCE: mem before post:", getattr(gc, "mem_free", lambda: 0)() // 1024, "KB")
|
||||
|
||||
try:
|
||||
resp = requests.post(str(url).strip().strip('\'"'), data=body_bytes, headers={"Content-Type": "application/json"})
|
||||
print("DBG_FORCE: mem after post:", getattr(gc, "mem_free", lambda: 0)() // 1024, "KB", "status:", getattr(resp, "status", None))
|
||||
status = getattr(resp, "status", getattr(resp, "status_code", None))
|
||||
return bool(status and 200 <= status < 300)
|
||||
except Exception as e:
|
||||
print("DBG_FORCE: exception during post:", e)
|
||||
return False
|
||||
|
||||
finally:
|
||||
try:
|
||||
if resp:
|
||||
resp.close()
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
if 'requests' in globals():
|
||||
del requests
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
gc.collect()
|
||||
print("DBG_FORCE: mem final:", getattr(gc, "mem_free", lambda: 0)() // 1024, "KB")
|
||||
except:
|
||||
pass
|
||||
|
||||
def set_config(cfg: dict):
|
||||
"""Initialize module with minimal values from loaded config (call from main)."""
|
||||
@@ -25,47 +92,101 @@ def _escape_json_str(s: str) -> str:
|
||||
s = s.replace("\t", "\\t")
|
||||
return s
|
||||
|
||||
def send_discord_message(message, username="Auto Garden Bot", is_alert=False):
|
||||
def send_discord_message(message, username="Auto Garden Bot", is_alert=False, debug: bool = False):
|
||||
"""
|
||||
Send Discord message with aggressive GC and low-memory guard to avoid ENOMEM.
|
||||
When debug=True prints mem_free at important steps so you can see peak usage.
|
||||
Returns True on success, False otherwise.
|
||||
"""
|
||||
global _NEXT_ALLOWED_SEND_TS
|
||||
resp = None
|
||||
url = _get_webhook_url(is_alert=is_alert)
|
||||
if not url:
|
||||
if debug: print("DBG: no webhook URL configured")
|
||||
return False
|
||||
|
||||
# Respect cooldown if we recently saw ENOMEM
|
||||
try:
|
||||
# 1) Free heap before TLS
|
||||
import time # type: ignore
|
||||
now = time.time()
|
||||
if _NEXT_ALLOWED_SEND_TS and now < _NEXT_ALLOWED_SEND_TS:
|
||||
if debug: print("DBG: backing off until", _NEXT_ALLOWED_SEND_TS)
|
||||
return False
|
||||
except:
|
||||
pass
|
||||
|
||||
try:
|
||||
# Lightweight local imports and GC
|
||||
import gc # type: ignore
|
||||
gc.collect()
|
||||
import time # type: ignore
|
||||
|
||||
gc.collect(); gc.collect()
|
||||
if debug:
|
||||
try: print("DBG: mem after gc:", gc.mem_free() // 1024, "KB")
|
||||
except: pass
|
||||
|
||||
# Quick mem check before importing urequests/SSL
|
||||
mem = getattr(gc, "mem_free", lambda: None)()
|
||||
if debug:
|
||||
try: print("DBG: mem before import check:", (mem or 0) // 1024, "KB")
|
||||
except: pass
|
||||
|
||||
# Conservative threshold — adjust as needed
|
||||
if mem is not None and mem < 90000:
|
||||
if debug: print("DBG: skip send (low mem)")
|
||||
return False
|
||||
|
||||
# Import urequests only when we plan to send
|
||||
try:
|
||||
# If MicroPython provides mem_free, skip send if heap is very low
|
||||
if hasattr(gc, "mem_free") and gc.mem_free() < 60000: # ~60KB threshold
|
||||
return False
|
||||
except:
|
||||
pass
|
||||
if debug: print("DBG: importing urequests...")
|
||||
import urequests as requests # type: ignore
|
||||
except Exception as e:
|
||||
# Back off when import fails (likely low-memory)
|
||||
try:
|
||||
_NEXT_ALLOWED_SEND_TS = time.time() + 60
|
||||
except:
|
||||
pass
|
||||
if debug: print("DBG: urequests import failed:", e)
|
||||
print("Discord webhook import failed (backing off)")
|
||||
return False
|
||||
|
||||
# 2) Import urequests locally (keeps RAM free when idle)
|
||||
import urequests as requests # type: ignore
|
||||
gc.collect()
|
||||
if debug:
|
||||
try: print("DBG: mem after import:", gc.mem_free() // 1024, "KB")
|
||||
except: pass
|
||||
|
||||
# 3) Keep payload tiny
|
||||
# Build tiny payload
|
||||
url = str(url).strip().strip('\'"')
|
||||
content = _escape_json_str(str(message)[:160])
|
||||
user = _escape_json_str(str(username)[:40])
|
||||
content = _escape_json_str(str(message)[:140])
|
||||
user = _escape_json_str(str(username)[:32])
|
||||
body_bytes = ('{"content":"%s","username":"%s"}' % (content, user)).encode("utf-8")
|
||||
|
||||
# Minimal headers to reduce allocations
|
||||
headers = {"Content-Type": "application/json"}
|
||||
|
||||
# 4) Send
|
||||
if debug:
|
||||
try: print("DBG: mem before post:", gc.mem_free() // 1024, "KB")
|
||||
except: pass
|
||||
|
||||
resp = requests.post(url, data=body_bytes, headers=headers)
|
||||
|
||||
if debug:
|
||||
try: print("DBG: mem after post:", gc.mem_free() // 1024, "KB", "status:", getattr(resp, "status", None))
|
||||
except: pass
|
||||
|
||||
status = getattr(resp, "status", getattr(resp, "status_code", None))
|
||||
return bool(status and 200 <= status < 300)
|
||||
|
||||
except Exception as e:
|
||||
print("Discord webhook exception:", e)
|
||||
# On ENOMEM/MemoryError back off
|
||||
try:
|
||||
if ("ENOMEM" in str(e)) or isinstance(e, MemoryError):
|
||||
import time # type: ignore
|
||||
_NEXT_ALLOWED_SEND_TS = time.time() + 60
|
||||
except:
|
||||
pass
|
||||
if debug:
|
||||
try: print("DBG: exception in send:", e)
|
||||
except: pass
|
||||
print("Discord webhook exception (backing off)")
|
||||
return False
|
||||
|
||||
finally:
|
||||
@@ -74,12 +195,15 @@ def send_discord_message(message, username="Auto Garden Bot", is_alert=False):
|
||||
resp.close()
|
||||
except:
|
||||
pass
|
||||
# Free refs and force GC
|
||||
try:
|
||||
del resp, body_bytes
|
||||
# remove large refs and force GC
|
||||
if 'resp' in locals(): del resp
|
||||
if 'body_bytes' in locals(): del body_bytes
|
||||
if 'requests' in locals(): del requests
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
gc.collect()
|
||||
import gc as _gc # type: ignore
|
||||
_gc.collect()
|
||||
except:
|
||||
pass
|
||||
16
Scripts/test_send.py
Normal file
16
Scripts/test_send.py
Normal file
@@ -0,0 +1,16 @@
|
||||
import ujson
|
||||
# Reload discord module fresh and run the forced debug send once.
|
||||
try:
|
||||
# ensure we use latest module on device
|
||||
import sys
|
||||
if "scripts.discord_webhook" in sys.modules:
|
||||
del sys.modules["scripts.discord_webhook"]
|
||||
import scripts.discord_webhook as d
|
||||
# load config.json to populate webhook URL
|
||||
with open("config.json", "r") as f:
|
||||
cfg = ujson.load(f)
|
||||
d.set_config(cfg)
|
||||
print("Running debug_force_send() — may trigger ENOMEM, run once only")
|
||||
d.debug_force_send("memory test")
|
||||
except Exception as e:
|
||||
print("test_send error:", e)
|
||||
45
main.py
45
main.py
@@ -200,15 +200,16 @@ if wifi and wifi.isconnected():
|
||||
print(f"Web Interface: http://{ifconfig[0]}")
|
||||
print("="*50 + "\n")
|
||||
|
||||
# Send startup notification to Discord (with timeout, non-blocking)
|
||||
# Send startup notification to Discord (schedule for later to avoid ENOMEM)
|
||||
try:
|
||||
success = discord_webhook.send_discord_message(f"Pico W online at http://{ifconfig[0]} ✅")
|
||||
if success:
|
||||
print("Discord startup notification sent")
|
||||
else:
|
||||
print("Discord startup notification failed (continuing anyway)")
|
||||
gc.collect() # free heap before HTTPS attempt
|
||||
# don't attempt HTTP/TLS immediately — schedule for retry from main loop
|
||||
pending_discord_message = "Pico W online at http://{}".format(ifconfig[0])
|
||||
discord_send_attempts = 0
|
||||
discord_sent = False
|
||||
print("Startup discord message queued (will send when memory available)")
|
||||
except Exception as e:
|
||||
print("Discord notification error: {}".format(e))
|
||||
print("Discord notification scheduling error: {}".format(e))
|
||||
|
||||
# Start web server early so page can load even if time sync is slow
|
||||
web_server = TempWebServer(port=80)
|
||||
@@ -233,6 +234,8 @@ else:
|
||||
print("="*50 + "\n")
|
||||
# ===== END: WiFi Connection =====
|
||||
|
||||
|
||||
|
||||
# ===== START: Sensor Configuration =====
|
||||
# Define all temperature sensors and their alert thresholds
|
||||
SENSOR_CONFIG = {
|
||||
@@ -397,6 +400,34 @@ last_ntp_sync = time.time() # Track when we last synced
|
||||
# Main monitoring loop (runs forever until Ctrl+C)
|
||||
while True:
|
||||
try:
|
||||
# Try to send pending discord startup message when memory permits
|
||||
try:
|
||||
if not globals().get('discord_sent', True) and globals().get('pending_discord_message'):
|
||||
import gc as _gc # type: ignore
|
||||
# run GC before measuring free memory
|
||||
_gc.collect()
|
||||
_gc.collect()
|
||||
# require a conservative free memory threshold before TLS (adjust to your device)
|
||||
mem_ok = getattr(_gc, 'mem_free', lambda: 0)() > 90000
|
||||
if mem_ok:
|
||||
try:
|
||||
ok = discord_webhook.send_discord_message(pending_discord_message, debug=True)
|
||||
if ok:
|
||||
print("Discord startup notification sent")
|
||||
discord_sent = True
|
||||
else:
|
||||
discord_send_attempts += 1
|
||||
if discord_send_attempts >= 3:
|
||||
print("Discord startup notification failed after retries")
|
||||
discord_sent = True
|
||||
except Exception:
|
||||
# swallow errors here; discord module already handles backoff
|
||||
discord_send_attempts += 1
|
||||
if discord_send_attempts >= 3:
|
||||
discord_sent = True
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Run all monitors (each checks if it's time to run via should_run())
|
||||
run_monitors(monitors)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user