import time import serial import requests # Constants SERIAL_PORT = 'COM12' # Change this as needed for your system BAUD_RATE = 156500 TIMEOUT = 1 CHUNK_SIZE = 1024 # Adjust the chunk size based on your needs DELAY_BETWEEN_CHUNK = 1 # Adjust the delay if needed # Image URLs image_urls = { 1: "https://download.rechargegrid.in/download/dq111welcome.jpg", # Show Welcome 2: "https://download.rechargegrid.in/download/dq111qr.jpg", # Show QR 3: "https://download.rechargegrid.in/download/dq111bogo.jpg", # Show BOGO 4: "https://download.rechargegrid.in/download/dq111menu.jpg" # Show Menu } # Open serial connection def open_serial_connection(): try: ser = serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=TIMEOUT) if ser.is_open: print(f"Connected to {SERIAL_PORT}") return ser except serial.SerialException as e: print(f"Error connecting to serial port {SERIAL_PORT}: {e}") return None # Close the serial connection def close_serial_connection(ser): if ser and ser.is_open: ser.close() print(f"Disconnected from {SERIAL_PORT}") # Fetch image from URL def fetch_image(url): try: print(f"Fetching image from {url}...") response = requests.get(url, stream=True) response.raise_for_status() # Check if the request was successful return response.content # Return image as bytes except requests.exceptions.RequestException as e: print(f"Error fetching image: {e}") return None # Send image to device over serial port in chunks def write_to_serial_image(ser, data): if ser: try: print("Sending image in chunks to device...") # Prepare the first command with filename and data length first_command = f"StartSendingFile {time.time()}.jpeg {len(data)}\n" ser.write(first_command.encode()) print(f"Sent command: {first_command}") # time.sleep(1) # Wait for the device to respond (this could be adjusted based on the device's protocol) response = ser.readline().decode().strip() # Read the next line and decode it print(f"Device response: {response}") # If the device is ready to receive data, send the image in chunks if "Command received. Preparing to receive file..." in response: for i in range(0, len(data), CHUNK_SIZE): chunk = data[i:i+CHUNK_SIZE] ser.write(chunk) print(f"Sent chunk of size {len(chunk)} bytes") time.sleep(DELAY_BETWEEN_CHUNK / 1000) # Convert milliseconds to seconds print("Image transfer complete.") response = ser.readline().decode().strip() # Read the next line and decode it print(f"Device response after transfer: {response}") # Wait for the device to indicate it is ready for the next file time.sleep(1) # Add a delay to ensure the device has processed the file # Check again if device is ready before sending the next file ready_response = ser.readline().decode().strip() print(f"Device readiness check: {ready_response}") if "File size verified" in ready_response: print("Device is ready for the next file.") else: print("Device is still not ready, retrying after delay.") time.sleep(5) # Longer delay before retrying else: print("Device is not ready to receive data.") except Exception as e: print(f"Error while sending data: {e}") # Main function def main(): ser = open_serial_connection() # Open the serial connection if not ser: print("Could not establish serial connection. Exiting.") return try: while True: print("\nSelect an option:") print("1. Show Welcome") print("2. Show QR") print("3. Show BOGO") print("4. Show Menu") print("5. Exit") try: choice = int(input("Enter your choice: ")) if choice == 5: print("Exiting program...") break elif choice in image_urls: url = image_urls[choice] # Fetch the image URL based on user selection image_data = fetch_image(url) # Fetch the selected image if image_data: write_to_serial_image(ser, image_data) # Send the image to the device else: print("Invalid option, please try again.") except ValueError: print("Invalid input. Please enter a number between 1 and 5.") finally: print("Connection Closed") close_serial_connection(ser) # Close the serial connection when done if __name__ == "__main__": main()