import time
import socket # for socket communication
import wifi # assuming you have a library for ESP32 Wifi connection
# Replace with your network credentials
SSID = "REPLACE_WITH_YOUR_SSID"
PASSWORD = "REPLACE_WITH_YOUR_PASSWORD"
# Replace with your Node-RED server details
NODE_RED_HOST = "YOUR_NODE_RED_HOSTNAME"
NODE_RED_PORT = 1880 # Default port for Node-RED
# Define camera configuration (assuming OV2640 sensor)
CAMERA_CONFIG = {
'resolution': (320, 240), # Adjust resolution as needed
'frame_format': 'jpg', # JPEG format
'jpeg_quality': 10, # Adjust quality (0-100)
' φωτεινή': 0, # Assuming 'φωτεινή' translates to 'ledc_channel'
'χρονόμετρο': 0, # Assuming 'χρονόμετρο' translates to 'ledc_timer'
'pin_d0': Y2_GPIO_NUM, # Replace with actual GPIO pin numbers
'pin_d1': Y3_GPIO_NUM,
'pin_d2': Y4_GPIO_NUM,
'pin_d3': Y5_GPIO_NUM,
'pin_d4': Y6_GPIO_NUM,
'pin_d5': Y7_GPIO_NUM,
'pin_d6': Y8_GPIO_NUM,
'pin_d7': Y9_GPIO_NUM,
'pin_xclk': XCLK_GPIO_NUM,
'pin_pclk': PCLK_GPIO_NUM,
'pin_vsync': VSYNC_GPIO_NUM,
'pin_href': HREF_GPIO_NUM,
'pin_sscb_sda': SIOD_GPIO_NUM,
'pin_sscb_scl': SIOC_GPIO_NUM,
'pin_pwdn': PWDN_GPIO_NUM,
'pin_reset': RESET_GPIO_NUM,
'xclk_freq_hz': 20000000,
}
# Sensor pins for detection (replace with actual pin numbers)
MOTION_DETECT_PIN = motionDetectAlert
HEAT_DETECT_PIN = heatDetectAlert
FLAME_DETECT_PIN = flameDetectAlert
GAS_DETECT_PIN = gasDetectAlert
WATER_OVERFLOW_PIN = waterOverDetectAlert
def connect_to_wifi():
"""Connects to the specified WiFi network"""
print("Connecting to WiFi...")
wifi.connect(SSID, PASSWORD)
while not wifi.isconnected():
time.sleep(0.5)
print(".", end="")
print("\nWiFi connected!")
print(f"IP address: {wifi.getipaddress()}")
def send_data_to_node_red(path, data):
"""Sends data to Node-RED server using a GET request"""
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
address = (NODE_RED_HOST, NODE_RED_PORT)
sock.connect(address)
url = f"/api/{path}?value={data}"
request = f"GET {url} HTTP/1.1\r\nHost: {NODE_RED_HOST}\r\nConnection: close\r\n\r\n"
sock.sendall(request.encode())
response = sock.recv(1024).decode()
print(f"Node-RED response: {response}")
except Exception as e:
print(f"Error sending data to Node-RED: {e}")
def setup():
"""Initializes camera, WiFi, and sensor pins"""
# Disable brownout detection (if applicable)
# ... (replace with appropriate library call)
print("Initializing camera...")
# Assuming you have a library to handle camera initialization
# initialize_camera(CAMERA_CONFIG)
# Set sensor pins as inputs
pinMode(MOTION_DETECT_PIN, INPUT)
pinMode(HEAT_DETECT_PIN, INPUT)
pinMode(FLAME_DETECT_PIN, INPUT)
pinMode(GAS_DETECT_PIN, INPUT)
pinMode(WATER_OVERFLOW_PIN, INPUT)
connect_to_wifi