Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Twilio Sync for Emergency Vehicle Control
def systemInit():
global port, loraM, client
obtain_port()
gpio_init()
emergencyVehicleState.setdefault("state",0)
#loraM handles all the loraEvents
loraM = MCLoRa(port)
success = loraM.testOK()
if success:
print "Traffic Controller Gateway Init Success"
print (success)
else:
print("Traffic Controller Gateway Init FAILURE")
loraM.pause()
#Twilio Client
client = mqtt.Client(client_id="rpi", clean_session=False)
client.tls_set(None, pem_location, key_location)
client.on_message = handleEmergencyMessage
#
# Use qos=1 to get your device caught up right away.
#
client.connect('mqtt-sync.us1.twilio.com', 8883, 60)
client.subscribe('sync/docs/gpsData', qos=1)
client.loop_start()
def handleEmergencyMessage(client, userdata, msg):
print(msg.topic + ' ' + str(msg.payload))
dataReceived = json.loads(msg.payload)
gpsLocation[0] = dataReceived["lat"]
gpsLocation[1] = dataReceived["lon"]
gps_tuple = tuple(gpsLocation)
distanceCalculated = int(vincenty(TRAFFIC_SIGNAL, gps_tuple).meters)
print ("Emergency Vehicle Location: ", distanceCalculated)
if distanceCalculated >= 150:
emergencyVehicleState["state"] = 0
trafficLightNormalCurrState = LIGHT_RED
else:
emergencyVehicleState["state"] = 1
location_list = [(37.753925, -122.399412),(37.753925, -122.399577),
(37.754228, -122.399612),(37.754660, -122.399898),
(37.754579, -122.401350),(37.754541, -122.402021),
(37.754512, -122.402560),(37.754485, -122.402862),
(37.754430, -122.403747),(37.754289, -122.405862),
(37.754586, -122.406395)]
def loraReceive():
global loraM
count = 0
while True:
print "LoRa Packet Receive Start"
try:
data = str(loraM.recv())
if data == "01": #Code 01 indicates primary communication failure
print "Received LoRa Signal from Emergeny Vehicle"
emergencyVehicleState["state"] = 1
count = 0
else:
count = count + 1
if count == 20:
count = 0
emergencyVehicleState["state"] = 0
except Exception as error:
print error
document = client.sync \
.services(SERVICE_SID) \
.documents("gpsData") \
.update(data=gps_Data)
def sys_init():
global loraM, client
obtain_port()
#Twilio Client Initilize
client = Client(ACCOUNT_SID, AUTH_TOKEN)
print("Primary Comm Mode, Twilio Sync Initialized\n")
# LoRa module Initialze on the given Port
loraM = MCLoRa(port)
success = loraM.testOK()
if success:
print("Secondary Comm Mode, LoRa RF Initialized\n")
print(success)
print(loraM.getUniqueID())
else:
print("Secondary Comm Mode, LoRa RF FAILED....Exiting\n")
sys.exit(0)
loraM.pause()
def updateTrafficSignal():
global trafficLightNormalCurrState
state = emergencyVehicleState["state"]
if state == NORMAL:
if (LIGHT_RED == trafficLightNormalCurrState):
print "Switching to RED\n"
set_red()
elif (LIGHT_YELLOW == trafficLightNormalCurrState):
print "Switching to YELLOW\n"
set_yellow()
elif (LIGHT_GREEN == trafficLightNormalCurrState):
print "Switching to GREEN\n"
set_green()
time.sleep(trafficLightNormalCycleTime[trafficLightNormalCurrState])
trafficLightNormalCurrState = trafficLightNormalCycle[ (trafficLightNormalCurrState + 1) % 3]
elif state == CRITICAL:
set_green()
print "Detected Critical Distance from Emergency Vehicle\n"
print "Switching to GREEN and Hold\n"
time.sleep(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment