BrainyLED是一种接近启用二极管,其中 HC-SR04 传感器与 arduino 连接,如果在传感器附近检测到物体,则提供触发器。此触发器的设置方式使用户可以获得准确的读数。
因此,用户定义了一个阈值,以便只要传感器值小于该阈值,它就会提供一个用于打开LED 的触发器。
使用BOLT云设置警报系统,以便在 LED亮起时提供电报消息并发送消息“ObjectDetectedInProximity!” .
配置文件(Conf.py)
此文件包含有关 Api 的所有详细信息和提供成功连接的其他必要值。
"""Configurations"""
bolt_api_key = "" # This is your Bolt Cloud API Key
device_id = "BOLTXX" # This is the device ID
telegram_chat_id = "@XXXX" # This is the channel ID of the created Telegram channel.
telegram_bot_id = "botXXXXX" # This is the bot ID of the created Telegram Bot.
Python 文件 ( proximity.py )
包含用于使用串行输入与 arduino 建立连接的 BOLT IoT 代码。
import requests
import conf
from boltiot import Bolt
import json, time
mybolt = Bolt(conf.bolt_api_key, conf.device_id) #Create object to fetch data
response = mybolt.serialRead('10')
print (response)
def send_telegram_message(message):
"""Sends message via Telegram"""
url = "https://api.telegram.org/" + conf.telegram_bot_id + "/sendMessage"
data = {
"chat_id": conf.telegram_chat_id,
"text": message
}
try:
response = requests.request(
"POST",
url,
params=data
)
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:
response = mybolt.serialRead('10') #Fetching the value from Arduino
data = json.loads(response)
status_value = data['value'].rstrip()
if str(status_value) == 'HIGH':
print ("Status is", status_value)
message = "Object detected in proximity!"
telegram_status = send_telegram_message(message)
else:
print ("Status is LOW!",status_value)
time.sleep(10)
为带超声波传感器的智能 LED 提供的 Arduino 代码。
#include
Ultrasonic ultrasonic(5, 6);
int LED = 2;
int threshold = 100;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int distance = ultrasonic.distanceRead();
if(distance< threshold)
{
digitalWrite(LED, HIGH);
Serial.println("HIGH");
delay(10000);
}
else{
digitalWrite(LED,LOW);
}
delay(1000);
}
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
全部0条评论
快来发表一下你的评论吧 !