Kashish Tilokani
Published

Smart Social Distancing Bot

Social distancing is one of the important things to observe in this pandemic. This device will help you maintain it smartly

IntermediateProtip4 hours477

Things used in this project

Story

Read more

Schematics

circuit diagram

circuit schematic

Code

social.py

Python
import requests                 # for making HTTP requests
import json                     # library for handling JSON data
import time                     # module for sleep operation

from boltiot import Bolt        # importing Bolt from boltiot module
api_key = "XXXX"               # This is your Bolt Cloud API Key
device_id = "XXXX"                  # This is the device ID and will be similar to BOLTXXXX where XXXX is some numbers
telegram_chat_id = "@XXXX"          # This is the channel ID of the created Telegram channel. Paste after @
telegram_bot_id = "botXXXX"         # This is the bot ID of the created Telegram Bot. Paste after bot
threshold = 100                   # Threshold beyond which the alert should be sent

mybolt = Bolt(api_key,device_id)
response=mybolt.serialRead('13')
print(response)


def get_sensor_value_from_pin(pin):
    """Returns the sensor value. Returns -999 if request fails"""
    try:
        response = mybolt.serialRead(pin)
        data = json.loads(response)
        if data["success"] != 1:
            print("Request not successfull")
            print("This is the response->", data)
            return -999
        sensor_value = int(data["value"])
        return sensor_value
    except Exception as e:
        print("Something went wrong when returning the sensor value")
        print(e)
        return -999


def send_telegram_message(message):
    """Sends message via Telegram"""
    url = "https://api.telegram.org/" + telegram_bot_id + "/sendMessage"
    data = {
        "chat_id": telegram_chat_id,
        "text": message
    }
    try:
        response = requests.request(
            "POST",
            url,
            params=data
        )
        print("This is the Telegram URL")
        print(url)
        print("This is the Telegram response")
        print(response.text)
        telegram_data = json.loads(response.text)
        return telegram_data["ok"]
    except Exception as e:
        print("An error occurred in sending the alert message via Telegram")
        print(e)
        return False


while True:
  sensor_value = get_sensor_value_from_pin('13')
  print("The current sensor value is :", sensor_value)
  
  if sensor_value <= threshold:
    message=" Stay away .You are too close .Distance is " +str(sensor_value)
    telegram_status=send_telegram_message(message)
    mybolt.digitalWrite("1","HIGH")
    time.sleep(0.1)
    mybolt.digitalWrite("1","LOW")
  else:
    message = " You are safe .Distance is " str(sensor_value)
    telegram_status=send_telegram_message(message)
  time.sleep(5)

ultrasonic sensor

Arduino
int trigPin=12;
int echoPin=13;
long duration,cm,inches;

void setup()
{ Serial.begin(9600);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
}

void loop()
{
  digitalWrite(trigPin,LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);
 
/* Taking value of echoPin */
  pinMode(echoPin,INPUT);
  duration=pulseIn(echoPin,HIGH);
  /* TO display in cm */
  cm = (duration/2) /29.1;
  /* sending to the bolt iot module via serial communication*/
  Serial.println(cm);
  delay(250);
}

Credits

Kashish Tilokani
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.