電子發(fā)燒友App

硬聲App

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內(nèi)不再提示
創(chuàng)作
電子發(fā)燒友網(wǎng)>電子資料下載>電子資料>量化桌面物聯(lián)網(wǎng)項目

量化桌面物聯(lián)網(wǎng)項目

2022-12-19 | zip | 0.23 MB | 次下載 | 免費

資料介紹

描述

作為一個量化的自我愛好者,我想跟蹤我在站立式辦公桌上坐著和站著的時間,并在圖表中很好地可視化。

我也一直想使用 Microsoft Azure 開展個人物聯(lián)網(wǎng)項目,因此我將我的 Genuino MKR1000 撣掉,將其連接到超聲波傳感器并提出了這個項目。

pYYBAGOYiyqAPiERAALNcTi5fLY110.png
最終結果
?

這個概念很簡單:

  • 使用超聲波傳感器通過將桌子放在桌面下方來測量桌子的高度并計算到地板的距離
  • 定期將該數(shù)據(jù)發(fā)送到 Azure IoT 中心,并將其路由到隊列服務(Azure 服務總線隊列)
  • 使用 Python 應用程序使用數(shù)據(jù)來構建實時儀表板

接線非常簡單:

pYYBAGOaZe6AIt_CAAEFx7_tVMg897.png
原理圖
?

設置:

poYBAGOaZfOAeFU9AAa3mP5BN7g149.jpg
?
pYYBAGOaZfaAYHG-AALWLQWe0Do668.jpg
?

您只需確保將傳感器放置在桌子上與地板之間沒有障礙物的位置(例如:地板上的任何物體、椅子或您坐著時的腿)。所以把它放在一個角落里。

為了避免計算中的“噪音”(即您的腿或手臂可能在傳感器下方的短暫時刻,或者由于測量高度較低而暫時阻礙程序認為您處于坐姿的任何東西),進行了測量每 5 秒一次,并且僅使用最后 12 次測量的平均值來確定當前位置并計算坐/站時間,然后將該數(shù)據(jù)發(fā)送到 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)。我按照教程修改了我的應用程序的示例代碼。

您可以使用我的代碼(如下提供)作為參考,但您需要創(chuàng)建一個帳戶,創(chuàng)建一個應用程序,并記下您的應用程序連接密鑰,如下所示:

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

現(xiàn)在,您可以在localhost:5000上打開一個瀏覽器窗口,然后盯著圖表,同時看到條形的增長取決于您站在或坐在辦公桌前的時間長短 :)(可能需要 1-2 分鐘才能看到任何變化在儀表板中)

您還可以打開 Arduino 串行監(jiān)視器以獲得更直接和詳細的輸出。device_handler 的 python 代碼在從設備獲取消息時也應該輸出一些文本。

pYYBAGOYiyqAPiERAALNcTi5fLY110.png
一切都在一起
?

?


下載該資料的人也在下載 下載該資料的人還在閱讀
更多 >

評論

查看更多

下載排行

本周

  1. 1山景DSP芯片AP8248A2數(shù)據(jù)手冊
  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開發(fā)指南
  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數(shù)字電路基礎pdf(下載)
  12. 未知  |  13750次下載  |  免費
  13. 7電子制作實例集錦 下載
  14. 未知  |  8113次下載  |  免費
  15. 8《LED驅(qū)動電路設計》 溫德爾著
  16. 0.00 MB  |  6656次下載  |  免費

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935054次下載  |  免費
  3. 2protel99se軟件下載(可英文版轉(zhuǎn)中文版)
  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次下載  |  免費