from machine import Pin, ADC
import time
import math
import urequests
import network
import urequests
import usocket as socket
from time import sleep
from machine import Pin
from time import sleep_ms
buzzer_pin = Pin(18, Pin.OUT)
# Define GPIO pins for LCD
RS = 21
EN = 22
D4 = 14
D5 = 27
D6 = 26
D7 = 25
# Create Pin objects for each pin
rs_pin = Pin(RS, Pin.OUT)
en_pin = Pin(EN, Pin.OUT)
d4_pin = Pin(D4, Pin.OUT)
d5_pin = Pin(D5, Pin.OUT)
d6_pin = Pin(D6, Pin.OUT)
d7_pin = Pin(D7, Pin.OUT)
# Function to send 4-bit data to the LCD
def lcd_nibble(data):
d4_pin.value(data & 0x01)
d5_pin.value((data >> 1) & 0x01)
d6_pin.value((data >> 2) & 0x01)
d7_pin.value((data >> 3) & 0x01)
pulse_enable()
# Function to send command to the LCD
def lcd_command(command):
rs_pin.off()
lcd_nibble(command >> 4)
lcd_nibble(command)
# Function to send data to the LCD
def lcd_write(data):
rs_pin.on()
lcd_nibble(data >> 4)
lcd_nibble(data)
# Function to pulse the enable pin
def pulse_enable():
en_pin.on()
sleep_ms(1) # Adjust this delay as needed
en_pin.off()
sleep_ms(1) # Adjust this delay as needed
# Initialize LCD
def lcd_init():
lcd_command(0x03) # Initialize
lcd_command(0x03) # Initialize
lcd_command(0x03) # Initialize
lcd_command(0x02) # Set to 4-bit mode
lcd_command(0x28) # 2 Line, 5x8 Matrix
lcd_command(0x0C) # Display On, Cursor Off, Blink Off
lcd_command(0x06) # Shift Cursor Right
lcd_command(0x01) # Clear Screen
def update_lcd_speed(speed):
lcd_command(0x80) # Set cursor to first line
# Convert speed to string and display on LCD
speed_str = str(speed)
for char in speed_str:
lcd_write(ord(char))
sleep(0.1)
lcd_command(0x01)
# Main
lcd_init()
flag=0
def activate_buzzer(speed, road_type):
if "NH" in road_type or "National Highway" in road_type or "Outer Ring rd" in road_type:
if speed > 120:
print("High speed on National Highway! Activating buzzer...")
buzzer_pin.on()
# send_email("High Speed Alert", "High speed on National Highway!")
else:
buzzer_pin.off()
elif "Rd" in road_type or "road" in road_type:
if speed > 80:
print("High speed on regular road! Activating buzzer...")
buzzer_pin.on()
# send_email("High Speed Alert", "High speed on regular road!")
else:
buzzer_pin.off()
elif "Hwy" in road_type or "SH" in road_type:
if speed > 100:
print("High speed on state highway! Activating buzzer...")
buzzer_pin.on()
# send_email("High Speed Alert", "High speed on state highway!")
else:
buzzer_pin.off()
else:
print("Road type not identified.")
buzzer_pin.off()
wifi_ssid = "YourWiFiSSID"
wifi_password = "YourWiFiPassword"
# Email configuration
email_server = "smtp.example.com"
email_port = 587
email_username = "[email protected]"
email_password = "your_email_password"
email_recipient = "[email protected]"
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
if not wlan.isconnected():
print('Connecting to WiFi...')
wlan.active(True)
wlan.connect(wifi_ssid, wifi_password)
while not wlan.isconnected():
pass
print('WiFi connected:', wlan.ifconfig())
# connect_wifi()
def send_email(subject, message):
try:
smtp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
smtp_socket.connect(socket.getaddrinfo(email_server, email_port)[0][-1])
smtp_socket.send(b'EHLO example.com\n')
smtp_socket.send(b'STARTTLS\n')
response = smtp_socket.recv(1024)
print(response)
smtp_socket.send(b'AUTH LOGIN\n')
smtp_socket.send(email_username + '\n')
smtp_socket.send(email_password + '\n')
smtp_socket.send('MAIL FROM: <' + email_username + '>\n')
smtp_socket.send('RCPT TO: <' + email_recipient + '>\n')
smtp_socket.send('DATA\n')
smtp_socket.send('From: <' + email_username + '>\n')
smtp_socket.send('To: <' + email_recipient + '>\n')
smtp_socket.send('Subject: ' + subject + '\n')
smtp_socket.send('\n')
smtp_socket.send(message + '\n')
smtp_socket.send('.\n')
smtp_socket.send('QUIT\n')
smtp_socket.close()
print("Email sent successfully!")
except Exception as e:
print("Error sending email:", e)
potentiometer_pin = 32 # Example pin, adjust to your setup
sda_pin = 21 # Example pin, adjust to your setup
scl_pin = 22
api_key = '313c6aff042f40adb4e7dfaf4fef07bd'
# Initialize ADC for potentiometer reading
adc = ADC(Pin(potentiometer_pin))
adc.atten(ADC.ATTN_11DB) # Set voltage range to 0-3.3V
# Define the number of steps per revolution and motor pins
steps_per_revolution = 100
motor_pins = [17, 16, 4, 0] # Change pin numbers to match your setup
# Create a list of Pin objects for motor control
motor_pins_obj = [Pin(pin, Pin.OUT) for pin in motor_pins]
# Define the sequence of steps for clockwise rotation
# Note: This sequence is for full stepping. You may need to adjust for different motor types.
# You can also use half-step or micro-stepping sequences for smoother motion.
sequence = [
[1, 0, 0, 1],
[0, 1, 0, 1],
[0, 1, 1, 0],
[1, 0, 1, 0]
]
# Function to step the motor clockwise by one step
def step_clockwise(s):
global sequence
for step in sequence:
for pin, value in zip(motor_pins_obj, step):
pin.value(value)
sleep(s) # Adjust delay as needed for your motor and application
# Set up initial motor state
for pin in motor_pins_obj:
pin.off()
def get_current_location():
latitude = 0
longitude = 0
try:
response = urequests.get('http://ip-api.com/json/')
data = response.json()
latitude = data['lat']
longitude = data['lon']
except Exception as e:
print("Error getting current location:", e)
return latitude, longitude
def get_location_info(latitude, longitude):
location_info = "Unknown"
try:
url = f'https://api.opencagedata.com/geocode/v1/json?q={latitude}+{longitude}&key={api_key}'
response = urequests.get(url)
data = response.json()
location_info = data['results'][0]['formatted']
except Exception as e:
print("Error getting location info:", e)
return location_info
# Main loop
while True:
pot_value = adc.read()
# Map potentiometer value to desired speed range (e.g., 0-1000 steps per second)
speed = pot_value * 1000 // 4095
# Adjust speed control signal sent to stepper motor driver
# Write code to control the stepper motor speed using PWM or other method
print("Potentiometer Value:", pot_value)
print("Speed:", speed//3)
speed += 1
print("Clockwise")
step_clockwise(1 / speed)
latitude, longitude = 17.308332, 78.700808
location_info = get_location_info(latitude, longitude)
print("Current Location:", location_info)
road_details = location_info.split(",")[0]
print(road_details)
# activate_buzzer(speed, road_details)
update_lcd_speed(speed//3)
activate_buzzer(speed//3,"NH 46")