Jakob GaziÄŤAMADEJ VIDICFanche Ilievska
Published

TruckSense - Trucker's Sixth Sense

TS is a service built to prevent accidents and other dangerous situations on the road. Designed for truck drivers. Worry-free trucking!

ExpertShowcase (no instructions)250
TruckSense - Trucker's Sixth Sense

Things used in this project

Story

Read more

Schematics

Schematics v1.02

Code

Tinkercad Sim - TX

C/C++
char mystr[101] = "ST46.2353487,14.4077023";

void setup() {
  // Begin the Serial at 9600 Baud
  Serial.begin(36400);
}
bool tmp;
void loop() {
  
  int senzor = analogRead(A3);
  //Serial.println(senzor);
  if(senzor>500){
  	Serial.write(mystr,23); //Write the serial data
    delay(2000);
       
    }
 
  delay(50);
}

Tinkercad Sim - RX

C/C++
#include <stdlib.h>
#include <stdio.h>

char mystr[24]; //Initialized variable to store recieved data
char GPS_lon[11];
char GPS_lat[11];
char GPS_lon_n[11];
char GPS_lat_n[11];
char event_T[2];
char event_T_n[2];

char test[1];
void setup() {
  // Begin the Serial at 9600 Baud
  Serial.begin(36400);
}


void loop() {
  Serial.readBytes(test,1);
  
  if(test[0]=='S'){
  Serial.readBytes(event_T,1); //Read the serial data and store in var
  Serial.readBytes(GPS_lon,10); //Read the serial data and store in var
  Serial.readBytes(mystr,1); 
  Serial.readBytes(GPS_lat,10);
    
  
strncpy(event_T_n, event_T, 1);
event_T_n[1] = 0; // null terminate destination

strncpy(GPS_lat_n, GPS_lat, 10);
 // null terminate destination
    
strncpy(GPS_lon_n, GPS_lon, 10);
// null terminate destination
GPS_lat_n[10] = 0;
GPS_lon_n[10] = 0;
    


  Serial.println("\nNEW EVENT!");
  Serial.println("TYPE: ");
  Serial.print(event_T_n);
  Serial.println("\nGPS Lon: ");
  Serial.print(GPS_lon_n);
  Serial.println("\nGPS Lat: ");
  Serial.print(GPS_lat_n);
  
  //Print data on Serial Monitor
   
  delay(10000);
  }
}

Raspberry Pi - Concept (Main code)

Python
Example of running the script: python3 ts_ubidots.py ST46.2524020,14.4267060 46.0461990,14.4528380
First parameter: Coordinates from the vehicle in a collision
Second parameter: Coordinates from its own GPS receiver
import sys
import ubidots_funkcije as u
event_string = sys.argv[1] #ST46.2353487,14.4077023

def razdalja(lat1,lon1,lat2,lon2): #pove razdaljo v km
    from math import sin, cos, sqrt, atan2, radians

    R = 6373.0

    lat1 = radians(float(lat1))
    lon1 = radians(float(lon1))
    lat2 = radians(float(lat2))
    lon2 = radians(float(lon2))

    dlon = lon2 - lon1
    dlat = lat2 - lat1

    a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
    c = 2 * atan2(sqrt(a), sqrt(1 - a))

    distance = R * c
    return distance


my_loc=[0,0]
my_loc[0] = sys.argv[2][0:9]
my_loc[1] = sys.argv[2][11:21]
print(my_loc)
print("Nov dogodek: "+event_string)

if event_string[0]!='S':
    print("NAPAKA: Napacno vnesen dogodek")
else:
    print("Zacenjam..")
    tip=str(event_string[1])
    GPS_lon_ev = event_string[13:23]
    print("GPS latitude: "+GPS_lon_ev)
    GPS_lat_ev = event_string[2:12]
    print("GPS lontitude: "+GPS_lat_ev)
    if tip == 'Z':
        print("Tip dogodka: Zaviranje v sili")
    else:
        print("Neznani tip dogodka: "+str(tip))
    
    razdalja = razdalja(GPS_lat_ev,GPS_lon_ev,my_loc[0],my_loc[1])
    #razdalja = 0.8
    print("Razdalja: ",razdalja,"km")
    
    if(razdalja<1):
        if(razdalja<0.300):
            print("OPOZORILO: Nevarnost v neposredni blizini!")
        elif(razdalja<0.700):
            print("OPOZORILO: Nevarnost v blizini!")
        else:
            print("OPOZORILO: Nevarnost v okolici!")
        print("Zazenjam z HMI opozorili v kabini!")
    else:
        print("Dogodek je predalec. HMI opozoril v kabini ni")
    print("Dogodek prijavljam na Ubidots..")



    #payload = u.build_payload( u.VARIABLE_LABEL_1, u.VARIABLE_LABEL_2, u.VARIABLE_LABEL_3)
    payload = {"Zaviranje": 1,
               "Dogodek-GPS_Lat": GPS_lat_ev,
               "Dogodek-GPS_Lon":GPS_lon_ev,
               "Sporocevalec-GPS_Lat":my_loc[0],
               "Sporocevalec-GPS_Lon":my_loc[1],
               "Razdalja_km-sporocevalec--dogodek":razdalja
               }
                

    print("[INFO] Posiljam podatke")
    u.post_request(payload)




    

Raspberry Pi - Concept (additional functions)

Python
import time
import requests
import math


TOKEN = "xxxxxxxxxxxxxxxxxxxxxx"  # Put your TOKEN here
DEVICE_LABEL = "TS"  # Put your device label here 


def post_request(payload):
    # Creates the headers for the HTTP requests
    url = "http://industrial.api.ubidots.com"
    url = "{}/api/v1.6/devices/{}".format(url, DEVICE_LABEL)
    headers = {"X-Auth-Token": TOKEN, "Content-Type": "application/json"}

    # Makes the HTTP requests
    status = 400
    attempts = 0
    while status >= 400 and attempts <= 5:
        req = requests.post(url=url, headers=headers, json=payload)
        status = req.status_code
        attempts += 1
        time.sleep(1)

    # Processes results
    if status >= 400:
        print("[ERROR] Could not send data after 5 attempts, please check \
            your token credentials and internet connection")
        return False

    print("[INFO] Uspesno poslano!")
    return True

Credits

Jakob GaziÄŤ

Jakob GaziÄŤ

1 project • 2 followers
AMADEJ VIDIC

AMADEJ VIDIC

1 project • 2 followers
no replacement for displacement
Fanche Ilievska

Fanche Ilievska

1 project • 2 followers

Comments