|
| 1 | +import smtplib |
| 2 | +from email.message import EmailMessage |
| 3 | +from datetime import datetime |
| 4 | +import socket |
| 5 | + |
| 6 | +# Email configuration |
| 7 | +server = smtplib.SMTP_SSL('smtp.gmail.com', 465) |
| 8 | +server.login('coderinfo123@gmail.com', 'replace yours') |
| 9 | +vendor_email = 'coderinfo123@gmail.com' |
| 10 | +user_email = 'mohannekkanti101@gmail.com' |
| 11 | + |
| 12 | +# Email message setup |
| 13 | +usubject = "Low Quantity of Stock - Sugar" |
| 14 | +vsubject = "New Order Placed - Sugar" |
| 15 | +now = datetime.now() |
| 16 | +current_time = now.strftime("%H:%M:%S") |
| 17 | + |
| 18 | +def send_email(content, email_receiver, subject): |
| 19 | + msg = EmailMessage() |
| 20 | + msg.set_content(content) |
| 21 | + msg['To'] = email_receiver |
| 22 | + msg['From'] = 'coderinfo123@gmail.com' |
| 23 | + msg['Subject'] = subject |
| 24 | + server.send_message(msg) |
| 25 | + |
| 26 | +# Configure NodeMCU's Wi-Fi connection |
| 27 | +NODEMCU_IP = '192.168.1.10' # Adjust with your NodeMCU's IP address |
| 28 | +NODEMCU_PORT = 12345 # Choose a suitable port |
| 29 | + |
| 30 | +# Create a socket object |
| 31 | +sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 32 | + |
| 33 | +# Bind the socket to the NodeMCU's IP and port |
| 34 | +sock.bind((NODEMCU_IP, NODEMCU_PORT)) |
| 35 | + |
| 36 | +# Listen for incoming connections |
| 37 | +sock.listen(1) |
| 38 | + |
| 39 | +# Accept connection from NodeMCU |
| 40 | +client_socket, client_address = sock.accept() |
| 41 | +print("NodeMCU connected:", client_address) |
| 42 | + |
| 43 | +while True: |
| 44 | + # Receive data from NodeMCU |
| 45 | + data = client_socket.recv(1024).decode().strip() |
| 46 | + height = abs(float(data)) |
| 47 | + mass = height |
| 48 | + print("Current Stock Quantity:", height) |
| 49 | + |
| 50 | + # Compose email content |
| 51 | + user_content = f"Sugar is low in quantity.\nRemaining quantity: {mass} grams\nRequired quantity: 1000 grams\nOrder placed to vendor." |
| 52 | + vendor_content = f"Stock Name: Sugar\nRequired quantity: 1000 grams\nOrder placed at {current_time}." |
| 53 | + |
| 54 | + # Send email notifications if stock quantity is low |
| 55 | + if height <= 59: |
| 56 | + send_email(user_content, user_email, usubject) |
| 57 | + send_email(vendor_content, vendor_email, vsubject) |
| 58 | + |
| 59 | +# Close the socket connection and server connection |
| 60 | +client_socket.close() |
| 61 | +server.quit() |
0 commit comments