电子发烧友App

硬声App

0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示
电子发烧友网>电子资料下载>电子资料>量化桌面物联网项目

量化桌面物联网项目

2022-12-19 | zip | 0.23 MB | 次下载 | 免费

资料介绍

描述

作为一个量化的自我爱好者,我想跟踪我在站立式办公桌上坐着和站着的时间,并在图表中很好地可视化。

我也一直想使用 Microsoft Azure 开展个人物联网项目,因此我将我的 Genuino MKR1000 掸掉,将其连接到超声波传感器并提出了这个项目。

pYYBAGOYiyqAPiERAALNcTi5fLY110.png
最终结果
 

这个概念很简单:

  • 使用超声波传感器通过将桌子放在桌面下方来测量桌子的高度并计算到地板的距离
  • 定期将该数据发送到 Azure IoT 中心,并将其路由到队列服务(Azure 服务总线队列)
  • 使用 Python 应用程序使用数据来构建实时仪表板

接线非常简单:

pYYBAGOaZe6AIt_CAAEFx7_tVMg897.png
原理图
 

设置:

poYBAGOaZfOAeFU9AAa3mP5BN7g149.jpg
 
pYYBAGOaZfaAYHG-AALWLQWe0Do668.jpg
 

您只需确保将传感器放置在桌子上与地板之间没有障碍物的位置(例如:地板上的任何物体、椅子或您坐着时的腿)。所以把它放在一个角落里。

为了避免计算中的“噪音”(即您的腿或手臂可能在传感器下方的短暂时刻,或者由于测量高度较低而暂时阻碍程序认为您处于坐姿的任何东西),进行了测量每 5 秒一次,并且仅使用最后 12 次测量的平均值来确定当前位置并计算坐/站时间,然后将该数据发送到 azure(12 * 5 = 60 秒,因此每分钟)。

这是 Arduino 代码(或从此链接获取):

/*******************************************************************
This code implements the Quantified Desk project, which consists of
an Arduino/Genuino MKR1000 hooked up to an ultrasonic sensor to measure
the distance to the floor (height) of a standing desk.
It keeps track of the time duration (in minutes) in each state (Sitting/Standing)
which is defined by a distance threshold above which the desk is considered to be
in standing position, and below which it is considered in sitting position.
A time counter for each state increases if the corresponding state is the current state.
This data is periodically sent to Azure IoT Hub along with a timestamp.
Complete Project with Dashboard visualization
https://github.com/vmehmeri/az-iot/QuantifiedDesk
PRE-REQUISITE
Setup Azure IoT Hub and upload SSL certificate to device (for your
IoT Hub hostname). With older firmware versions the HTTP Post function
will be unreliable and may fail on several requests, so it's recommended
to update your MKR1000 firmware to the latest version.
- Instructions for setting up Azure IoT Hub:
https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-create-through-portal
- Instructions for updating Firmware and adding SSL certificate to device:
https://www.arduino.cc/en/Tutorial/FirmwareUpdater
*******************************************************************/
#include 
#include 
#include 
#include 
#include "arduino_secrets.h"
// SECRETS CONFIG -- PLEASE SET THESE IN arduino_secrets.h FILE
//WiFi creds -------------------------------------------------------------------------------------------
char ssid[] = SECRET_SSID; //  your network SSID (name)
char pass[] = SECRET_WIFIPASSWD;    // your network password (use for WPA, or use as key for WEP)
//Azure IoT Hub Secrets Config -------------------------------------------------------------------------
char hostname[] = SECRET_IOTHUB_HOSTNAME;
char uri[]= SECRET_DEVICE_POST_URI;
char authSAS[] = SECRET_DEVICE_SAS;
//------------------------------------------------------------------------------------------------------
#define slotNumber 1 //This will vary for multi slot devices
// Project Config variables
unsigned int distanceThreshold = 82; // Define here the distance (in cm) that marks the threshold between sitting and standing
const int GMT = 2; //change this to adapt it to your time zone
const int TrigPin = 4; //number of the Trigger pin
const int EchoPin = 5; //number of the Echo pin
RTCZero rtc;
WiFiSSLClient client;
Ultrasonic ultrasonic(TrigPin, EchoPin);
unsigned int count = 0;
unsigned long distanceSum = 0;
unsigned int timeStanding = 0;
unsigned int timeSitting = 0;
unsigned long startMillis;
unsigned long distanceAvg;
unsigned long distance;
int status = WL_IDLE_STATUS;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
startMillis = millis();
rtc.begin();
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Connected to Wi-Fi");
// Get Real-Time from NTP using built-in RTC module
unsigned long epoch;
int numberOfTries = 0, maxTries = 6;
do {
epoch = WiFi.getTime();
numberOfTries++;
}
while ((epoch == 0) && (numberOfTries < maxTries));
if (numberOfTries == maxTries) {
Serial.print("NTP unreachable!!");
while (1);
}
else {
Serial.print("Epoch received: ");
Serial.println(epoch);
rtc.setEpoch(epoch);
Serial.println();
}
}
void loop() {
delay(5000); // wait 5 seconds
ultrasonic.measure();
distance = ultrasonic.get_cm();
Serial.print("Sensor(cm): ");
Serial.println(distance);
distanceSum = distanceSum + distance;
count = count + 1;
/* Takes the average of the last 12 measurements (the number 12 is arbitrary, but
*  with a 5-second delay between measurements and 12 measurements, that means
*  data is aggregated and sent to Azure every 60 seconds, which seems reasonable for
*  this project.
*/
if (count == 12) {
distanceAvg = distanceSum / count;
count = 0;
distanceSum = 0;
if (distanceAvg < distanceThreshold) {
// Add elapsed time since last measurement to sitting time
timeSitting = timeSitting + ((millis()-startMillis)/1000);
} else {
// Add elapsed time since last measurement to standing time
timeStanding = timeStanding + ((millis()-startMillis)/1000);
}
startMillis = millis();
// Show current aggregate numbers
printRTCDate();
printRTCTime();
Serial.println();
Serial.print("Time sitting: ");
Serial.print(timeSitting/60);
Serial.println("min");
Serial.print("Time standing: ");
Serial.print(timeStanding/60);
Serial.println("min");
Serial.println("");
// Creates a string to send to Azure IoT HUB.
// It's simply comma-separated string of values for sitting and standing, followed by date and time (for timestamping)
String data_string = (String(timeSitting/60) + "," + String(timeStanding/60) + "," + getRTCDate() + "," + getRTCTime());
//Serial.println(data_string);
// Send to Azure
httpPost(data_string);
String response = "";
char c;
while (client.available()) {
c = client.read();
response.concat(c);
}
}
}
void httpPost(String content)
{
if (client.connectSSL(hostname, 443)) {
client.print("POST ");
client.print(uri);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(hostname);
client.print("Authorization: ");
client.println(authSAS);
client.println("Connection: close");
client.print("Content-Type: ");
client.println("text/plain");
client.print("Content-Length: ");
client.println(content.length());
client.println();
client.println(content);
delay(500);
} else {
Serial.println("HTTP POST connection failed");
Serial.println(client.read());
}
// close connection
client.stop();
}
void printRTCTime()
{
print2digits(rtc.getHours() + GMT);
Serial.print(":");
print2digits(rtc.getMinutes());
Serial.print(":");
print2digits(rtc.getSeconds());
Serial.println();
}
void printRTCDate()
{
Serial.print(rtc.getDay());
Serial.print("/");
Serial.print(rtc.getMonth());
Serial.print("/");
Serial.print(rtc.getYear());
Serial.print(" ");
}
void print2digits(int number) {
if (number < 10) {
Serial.print("0");
}
Serial.print(number);
}
String getRTCDate()
{
String date_str = String(rtc.getDay()) + "/" + String(rtc.getMonth()) + "/" + String(rtc.getYear());
return date_str;
}
String getRTCTime()
{
String time_str = get2digits(rtc.getHours() + GMT) + ":" + get2digits(rtc.getMinutes()) + ":" + get2digits(rtc.getSeconds());
return time_str;
}
String get2digits(int number) {
if (number < 10) {
return "0" + String(number);
}
return String(number);
}

请注意,必须提供 Azure 连接详细信息,因此您需要先设置 Azure IoT 中心。

以下是设置 Azure IoT 中心的说明:

https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-create-through-portal

免费层就可以满足这一需求。所以它不会花费你任何东西!

仪表板

对于实时仪表板,我使用了 Pusher (pusher.com)。我按照教程修改了我的应用程序的示例代码。

您可以使用我的代码(如下提供)作为参考,但您需要创建一个帐户,创建一个应用程序,并记下您的应用程序连接密钥,如下所示:

pYYBAGOaZfiAUkEGAACWhhwsDyI079.png
推送应用程序密钥
 

别担心,这应该花费您不到 5 分钟的时间来完成设置。

完成后,下载我的 Github 存储库中可用的代码:

https://github.com/vmehmeri/az-iot/tree/master/QuantifiedDesk

修改以下文件:

frontend/app.py
device_handler.py

使用您的应用程序密钥(对于设备处理程序,还有您的 Azure 连接信息)。您会注意到需要有关 Azure 服务总线队列的信息。在我的应用程序中,我让 Azure IoT 中心将消息路由到服务总线队列,然后从那里使用它们。设置 Azure 服务总线队列非常简单,您可以按照本教程进行操作:

在“消息传递”>“消息传递路由”>“添加”下配置 IoT 中心消息路由(然后选择您的队列)

之后,您就可以运行该应用程序了。首先,运行前端:

python frontend/app.py

在另一个终端中,运行设备处理程序,即后端应用程序:

python device_handler.py

现在,您可以在localhost:5000上打开一个浏览器窗口,然后盯着图表,同时看到条形的增长取决于您站在或坐在办公桌前的时间长短 :)(可能需要 1-2 分钟才能看到任何变化在仪表板中)

您还可以打开 Arduino 串行监视器以获得更直接和详细的输出。device_handler 的 python 代码在从设备获取消息时也应该输出一些文本。

pYYBAGOYiyqAPiERAALNcTi5fLY110.png
一切都在一起
 

 


下载该资料的人也在下载 下载该资料的人还在阅读
更多 >

评论

查看更多

下载排行

本周

  1. 1山景DSP芯片AP8248A2数据手册
  2. 1.06 MB  |  532次下载  |  免费
  3. 2RK3399完整板原理图(支持平板,盒子VR)
  4. 3.28 MB  |  339次下载  |  免费
  5. 3TC358743XBG评估板参考手册
  6. 1.36 MB  |  330次下载  |  免费
  7. 4DFM软件使用教程
  8. 0.84 MB  |  295次下载  |  免费
  9. 5元宇宙深度解析—未来的未来-风口还是泡沫
  10. 6.40 MB  |  227次下载  |  免费
  11. 6迪文DGUS开发指南
  12. 31.67 MB  |  194次下载  |  免费
  13. 7元宇宙底层硬件系列报告
  14. 13.42 MB  |  182次下载  |  免费
  15. 8FP5207XR-G1中文应用手册
  16. 1.09 MB  |  178次下载  |  免费

本月

  1. 1OrCAD10.5下载OrCAD10.5中文版软件
  2. 0.00 MB  |  234315次下载  |  免费
  3. 2555集成电路应用800例(新编版)
  4. 0.00 MB  |  33566次下载  |  免费
  5. 3接口电路图大全
  6. 未知  |  30323次下载  |  免费
  7. 4开关电源设计实例指南
  8. 未知  |  21549次下载  |  免费
  9. 5电气工程师手册免费下载(新编第二版pdf电子书)
  10. 0.00 MB  |  15349次下载  |  免费
  11. 6数字电路基础pdf(下载)
  12. 未知  |  13750次下载  |  免费
  13. 7电子制作实例集锦 下载
  14. 未知  |  8113次下载  |  免费
  15. 8《LED驱动电路设计》 温德尔著
  16. 0.00 MB  |  6656次下载  |  免费

总榜

  1. 1matlab软件下载入口
  2. 未知  |  935054次下载  |  免费
  3. 2protel99se软件下载(可英文版转中文版)
  4. 78.1 MB  |  537798次下载  |  免费
  5. 3MATLAB 7.1 下载 (含软件介绍)
  6. 未知  |  420027次下载  |  免费
  7. 4OrCAD10.5下载OrCAD10.5中文版软件
  8. 0.00 MB  |  234315次下载  |  免费
  9. 5Altium DXP2002下载入口
  10. 未知  |  233046次下载  |  免费
  11. 6电路仿真软件multisim 10.0免费下载
  12. 340992  |  191187次下载  |  免费
  13. 7十天学会AVR单片机与C语言视频教程 下载
  14. 158M  |  183279次下载  |  免费
  15. 8proe5.0野火版下载(中文版免费下载)
  16. 未知  |  138040次下载  |  免费