#Stable version 0.4 Tested by Shanmukha Reddy-on 2-2-2023
#removed print in version 4
#implemented watch dog from version 0.5
import utime
import math
import machine
from machine import WDT
from machine import UART,Pin
from machine import I2C
from lcd_api import LcdApi
import binascii
from pico_i2c_lcd import I2cLcd
import os
import json
#Define Watch Dog Timer
#wdt = WDT(timeout=50000)
#LCD Code
I2C_ADDR = 0x27 # Decimal 39
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
SDA_PIN = 16
SCL_PIN = 17
#input buttons
ENTER_PIN = 28
SHIFT_START_PIN = 10
SHIFT_END_PIN = 11
LOAD_PIN = 12
UNLOAD_PIN = 13
RESET_PIN = 27
#key pressing Buffer
all_keys=""
trip_info_display=False
on_shift=False
init_status=True
trip_info= {
'shift_start_button_press_flag':False,
'start_hmr_entered':False,
'start_hmr_entering':False,
'start_hmr_val':0,
'start_kmr_val':0,
'end_hmr_val':0,
'end_kmr_val':0,
'shift_trips_count':0,
'start_kmr_entered':False,
'start_kmr_entering':False,
'button_load_pressed':False,
'button_unload_pressed':False,
'machine_no_entered':False,
'maching_no_entering':False,
'unloaded':True,
'shift_end_button_press_flag':False,
'end_hmr_entered':False,
'end_hmr_entering':False,
'end_kmr_entered':False,
'end_kmr_entering':False,
'shift_count':1
}
#print("Running StopWatch")
filename = "data.json"
try:
with open(filename, "r") as infile:
trip_info = json.load(infile)
if(trip_info["shift_start_button_press_flag"]==False):
trip_info["shift_count"]=trip_info["shift_count"]+1;
#print(trip_info)
except OSError:
#print("Error: Could not open file for reading.")
with open(filename, "w") as json_file:
json.dump(trip_info, json_file)
#print("File Created!")
#i2c = I2C(0, sda=machine.Pin(SDA_PIN), scl=machine.Pin(SCL_PIN), freq=400000)
i2c = machine.SoftI2C(scl=machine.Pin(SCL_PIN), sda=machine.Pin(SDA_PIN))
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
# Serial Communication
uart1 = UART(1, baudrate=9600, tx=Pin(8), rx=Pin(9))
#configure all the buttons
button_enter = Pin(ENTER_PIN, Pin.IN, Pin.PULL_UP)
button_reset = Pin(RESET_PIN, Pin.IN, Pin.PULL_UP)
button_shift_start = Pin(SHIFT_START_PIN, Pin.IN, Pin.PULL_UP)
button_shift_end = Pin(SHIFT_END_PIN, Pin.IN, Pin.PULL_UP)
button_load = Pin(LOAD_PIN, Pin.IN, Pin.PULL_UP)
button_unload = Pin(UNLOAD_PIN, Pin.IN, Pin.PULL_UP)
#data = {"name": "John Smith", "age": 36, "city": "New York"}
#with open("data.json", "w") as outfile:
# json.dump(data, outfile)
'''try:
with open("data.json", "r") as infile:
data = json.load(infile)
print(data)
except OSError:
print("Error: Could not open file for reading.")
uid = machine.unique_id()
uid_hex = uid.hex()
print(uid_hex)'''
#get Machine ID
uid = machine.unique_id()
uid_hex = binascii.hexlify(uid).decode()
#print("Device ID:"+uid_hex)
lcd.putstr("Trip Counting System")
utime.sleep(2)
lcd.clear()
lcd.hide_cursor()
timer = 0
running = False
lcd.clear()
lcd.putstr("Start Shift")
#current_directory = os.getcwd()
#print(current_directory)
# Create a map between keypad buttons and characters
matrix_keys = [['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['.', '0', '#', 'D']]
# PINs according to schematic - Change the pins to match with your connections
keypad_rows = [7,6,5,4]
keypad_columns = [3,2,1,0]
# Create two empty lists to set up pins ( Rows output and columns input )
col_pins = []
row_pins = []
# Loop to assign GPIO pins and setup input and outputs
for x in range(0,4):
row_pins.append(Pin(keypad_rows[x], Pin.OUT))
row_pins[x].value(1)
col_pins.append(Pin(keypad_columns[x], Pin.IN, Pin.PULL_DOWN))
col_pins[x].value(0)
def scankeys():
key_press=None
for row in range(4):
for col in range(4):
row_pins[row].high()
key = None
if col_pins[col].value() == 1:
#print("You have pressed:", matrix_keys[row][col])
key_press = matrix_keys[row][col]
utime.sleep(0.3)
row_pins[row].low()
return key_press
def send_rs232(msg):
#print("serial sent:"+msg)
uart1.write(msg+';\r\n')
with open(filename, "w") as json_file:
json.dump(trip_info, json_file)
def resetAllParams():
trip_info["start_hmr_entered"]=False
trip_info["start_hmr_entering"]=False
trip_info["start_kmr_entered"]=False
trip_info["start_kmr_entering"]=False
#print("clear Called")
trip_info["button_load_pressed"]=False
trip_info["button_unload_pressed"]=False
trip_info["machine_no_entered"]=False
trip_info["maching_no_entering"]=False
trip_info["unloaded"]=True
trip_info["shift_end_button_press_flag"]=False
trip_info["end_hmr_entered"]=False
trip_info["end_hmr_entering"]=False
trip_info["end_kmr_entered"]=False
trip_info["end_kmr_entering"]=False
def Lcd_Display(msg):
global lcd
global i2c
try:
lcd.clear()
lcd.putstr(msg)
except:
i2c = machine.SoftI2C(scl=machine.Pin(SCL_PIN), sda=machine.Pin(SDA_PIN))
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
lcd.clear()
lcd.putstr(msg)
while True:
#give feed to watchdog other wise restarts after 20 seconds
#wdt.feed()
key=scankeys()
if(key!=None):
#print("You have pressed:", key)
all_keys=str(all_keys) + str(key)
lcd.move_to(0,1)
lcd.putstr(all_keys)
if(button_shift_start.value()==False and trip_info["shift_start_button_press_flag"]==False):
#print("Reset all Called")
trip_info["shift_start_button_press_flag"]=True
trip_info["start_hmr_entered"]=False
trip_info["start_hmr_entering"]=False
trip_info["start_kmr_entered"]=False
trip_info["start_kmr_entering"]=False
trip_info["button_load_pressed"]=False
trip_info["button_unload_pressed"]=False
trip_info["machine_no_entered"]=False
trip_info["maching_no_entering"]=False
trip_info["unloaded"]=True
trip_info["shift_end_button_press_flag"]=False
trip_info["end_hmr_entered"]=False
trip_info["end_hmr_entering"] =False
trip_info["end_kmr_entered"] =False
trip_info["end_kmr_entering"] =False
trip_info["shift_trips_count"]=0
'''resetAllParams()
print("shift start pressed"+str(shift_start_button_press_flag))
print("start_hmr_entered "+str(start_hmr_entered))
print("start_hmr_entering "+str(start_hmr_entering))
print("start_kmr_entering "+str(start_kmr_entering))
print("start_kmr_entered "+str(start_kmr_entered))
print("button_load_pressed "+str(button_load_pressed))
print("button_unload_pressed "+str(button_unload_pressed))
print("machine_no_entered "+str(machine_no_entered))
print("maching_no_entering "+str(maching_no_entering))
print("unloaded"+str(unloaded))'''
#else:
# print("waiting for start button")
#if (button_reset.value()==False):
# all_keys=""
if(trip_info["shift_start_button_press_flag"]==True):
if(trip_info["start_hmr_entered"]==False):
if(trip_info["start_hmr_entering"]==False):
Lcd_Display("Enter Start HMR")
all_keys=""
trip_info["start_hmr_entering"]=True
else:
if (button_enter.value() == False):
if(all_keys!=""):
#print ("Start HMR Entered "+all_keys)
trip_info["start_hmr_val"]=float(all_keys)
all_keys=""
trip_info["start_hmr_entered"]=True
trip_info["start_hmr_entering"]=False
if (button_reset.value()==False):
if(all_keys==""):
trip_info["shift_start_button_press_flag"]=False
trip_info["start_hmr_entered"]=False
trip_info["start_hmr_entering"]=False
Lcd_Display("Start Shift")
all_keys=""
trip_info["start_hmr_entered"]=False
trip_info["start_hmr_entering"]=False
elif(trip_info["start_kmr_entered"]==False):
if(trip_info["start_kmr_entering"]==False):
lcd.clear()
lcd.putstr("Enter Start KMR")
all_keys=""
trip_info["start_kmr_entering"]=True
else:
if (button_enter.value() == False):
if(all_keys!=""):
#print ("Start KMR Entered "+all_keys)
trip_info["start_kmr_val"]=float(all_keys)
all_keys=""
trip_info["start_kmr_entered"]=True
trip_info["start_kmr_entering"]=False
#on Shift Start Send Shift Start in Serial Port
send_rs232("S,"+str(uid_hex)+","+str(trip_info["shift_count"])+","+str(trip_info["start_hmr_val"])+","+str(trip_info["start_kmr_val"]))
if (button_reset.value()==False):
if(all_keys==""):
trip_info["shift_start_button_press_flag"]=False
trip_info["start_hmr_entered"]=False
trip_info["start_hmr_entering"]=False
Lcd_Display("Start Shift")
all_keys=""
trip_info["start_kmr_entered"]=False
trip_info["start_kmr_entering"]=False
if(trip_info["start_hmr_entered"]==True and trip_info["start_kmr_entered"]==True and trip_info["end_hmr_entered"]==False and trip_info["end_kmr_entered"]==False):
if(button_load.value()==False):
trip_info["button_load_pressed"]=True
else:
if(trip_info["button_load_pressed"]==False and trip_info["shift_end_button_press_flag"]==False):
if(trip_info_display==False):
Lcd_Display("No Of Trips."+ str(trip_info["shift_trips_count"]))
trip_info_display=True
if(trip_info["button_load_pressed"]==True):
if(trip_info["machine_no_entered"]==False):
if(trip_info["maching_no_entering"]==False):
Lcd_Display("Enter MCH No:")
all_keys=""
trip_info["maching_no_entering"]=True
else:
if (button_enter.value() == False):
if(all_keys!=""):
#print ("MCH No Entered "+all_keys)
trip_info["machine_no"]=all_keys
trip_info["machine_no_entered"]=True
trip_info["maching_no_entering"]=False
trip_info["unloaded"]=False
#On Trip Create send_rs232("S,"+uid_hex+","+str(trip_info["shift_count"])+","+trip_info["start_hmr_val"]+","+trip_info["start_kmr_val"])
send_rs232("LS,"+str(uid_hex)+","+str(trip_info["shift_count"])+","+trip_info["machine_no"]+","+str(trip_info["shift_trips_count"]+1))
Lcd_Display("OnTrip MchNo:"+all_keys)
all_keys=""
if (button_reset.value()==False):
if(all_keys==""):
trip_info["button_load_pressed"]=False
trip_info["maching_no_entering"]=False
all_keys=""
trip_info["machine_no_entered"]=False
trip_info["maching_no_entering"]=False
else:
if init_status==True:
Lcd_Display("OnTrip MchNo:"+trip_info["machine_no"])
init_status=False
if(trip_info["button_load_pressed"]==True and trip_info["machine_no_entered"]==True and button_unload.value()==False):
trip_info["shift_trips_count"]=trip_info["shift_trips_count"]+1
utime.sleep(1)
trip_info["button_load_pressed"]=False
trip_info["machine_no_entered"]=False
trip_info["unloaded"]=True
trip_info_display=False
#send on Unload to serial port
send_rs232("LE,"+str(uid_hex)+","+str(trip_info["shift_count"])+","+trip_info["machine_no"]+","+str(trip_info["shift_trips_count"]))
Lcd_Display("Unloaded..")
if(trip_info["start_hmr_entered"]==True and trip_info["start_kmr_entered"]==True and trip_info["unloaded"]==True and button_shift_end.value()==False):
trip_info["shift_end_button_press_flag"]=True
if(trip_info["shift_end_button_press_flag"]==True):
if(trip_info["end_hmr_entered"]==False):
if(trip_info["end_hmr_entering"]==False):
Lcd_Display("Enter End HMR")
all_keys=""
trip_info["end_hmr_entering"]=True
else:
if (button_enter.value() == False):
if(all_keys!=""):
#print ("End HMR Entered "+all_keys)
trip_info["end_hmr_val"]=float(all_keys)
#if((trip_info["end_hmr_val"]-trip_info["start_hmr_val"])>=0 and (trip_info["end_hmr_val"]-trip_info["start_hmr_val"])<=8):
all_keys=""
trip_info["end_hmr_entered"]=True
trip_info["end_hmr_entering"]=False
'''
else:
lcd.clear()
lcd.putstr("Wrong HMR Entry")
all_keys=""
lcd.clear()
lcd.putstr("Enter End HMR")
'''
if (button_reset.value()==False):
if(all_keys==""):
trip_info["shift_end_button_press_flag"]=False
trip_info["end_hmr_entering"]=False
all_keys=""
trip_info["end_hmr_entered"]=False
trip_info["end_hmr_entering"]=False
elif(trip_info["end_kmr_entered"]==False):
if(trip_info["end_kmr_entering"]==False):
Lcd_Display("Enter end KMR")
all_keys=""
trip_info["end_kmr_entering"]=True
else:
if (button_enter.value() == False):
if(all_keys!=""):
#print ("end KMR Entered "+all_keys)
trip_info["end_kmr_val"]=float(all_keys)
#if((trip_info["end_kmr_val"]-trip_info["start_kmr_val"])>=0 and (trip_info["end_kmr_val"]-trip_info["start_kmr_val"])<=400):
all_keys=""
trip_info["end_kmr_entered"]=True
trip_info["end_kmr_entering"]=False
trip_info["shift_start_button_press_flag"]=False
#On Shift End sending Shift Info on Serial Port
send_rs232("E,"+str(uid_hex)+","+str(trip_info["shift_count"])+","+str(trip_info["end_hmr_val"])+","+str(trip_info["end_kmr_val"])+","+str(trip_info["shift_trips_count"]))
trip_info["shift_count"]=trip_info["shift_count"]+1;
Lcd_Display("Start Shift")
'''
else:
lcd.clear()
lcd.putstr("Wrong KMR Entry")
all_keys=""
lcd.clear()
lcd.putstr("Enter End KMR")'''
if (button_reset.value()==False):
if(all_keys==""):
trip_info["shift_end_button_press_flag"]=False
trip_info["end_hmr_entered"]=False
trip_info["end_kmr_entering"]=False
trip_info["end_hmr_val"]=0
all_keys=""
trip_info["end_kmr_entered"]=False
trip_info["end_kmr_entering"]=False
utime.sleep(0.1)
'''if(on_shift==False):
lcd.clear()
lcd.putstr("Enter Start HMR")
else:
lcd.clear()
lcd.putstr("Not Allowed.")
'''
# If button pressed then toggle
'''if (button_enter.value() == False):
#running = not running
print ("Pressed Keys "+all_keys)
all_keys=""
# sleep prevents single press causing multiple play / pause signals
# this is a simple, but basic method
utime.sleep(0.5)
if (button_reset.value() == False):
all_keys=""
print ("Reset")
timer = 0
lcd.clear()
lcd.putstr("Stopwatch")
utime.sleep(0.5)
lcd.move_to(0,1)
lcd.putstr(str(math.floor(timer)))
if (running):
timer += 0.1
utime.sleep(0.1)'''