Quantcast
Channel: 我表达 –抽筋儿的话语权
Viewing all articles
Browse latest Browse all 13

用MATLAB实现便便时间间隔的定期统计分析

$
0
0

为了达成如题的目的,实现方法已经有了早期的两个版本:

  • 2014年:手机端手动输入便便时间,经Dropbox同步至本地PC;利用Windows计划任务定期执行统计分析的MATLAB脚本;将结果发邮箱,再利用IFTTT发Twitter上。(见Github)
  • 2017年3月:应对Dropbox被墙,将便便时间记录放到Notepad上;MATLAB里利用正则把页面上的数据读下来,再进行统计分析;同样用Windows计划任务实现定期执行。

上述两个方案中最难坚持的部分在于:要打开手机浏览器,访问相应记录网页,再按10个数字手动输入便便时间。

去年10月搞定了第三版实现方法,借助Thingspeak数据上载的timestamp,完美回避手动输入时间数据;同时脚本执行也丢去了Thingspeak;分析结果推送则通过IFTTT的webhooks发至Telegram;Thingspeak还具备利用数据作图的功能。具体如下:

便便时间记录
  • Thingspeak上开1个field的public Channel,取得API_write_key(Note: private Channel的读取需要API_read_key)。
  • 方法一:在手机浏览器中收藏以下Thingspeak数据上载链接(API key替换为你自己的);每次如厕,掏出手机,打开浏览器的收藏夹,点击一下上述收藏链接,即完成一次便便时间记录(Note:每访问一次即在Thingspeak上记录一次数据,故千万别刷新)。

    https://api.thingspeak.com/update.json?api_key={API_write_key}&field1=poo

  • 方法二:iOS设备的话,建立一个workflow,add workflow to home screen,每次如厕点击生成的workflow icon即可。

Telegram推送准备
  • 在IFTTT关联Telegram,即TG上有@ifttt这个bot。
  • 在IFTTT启用webhooks,后面需调用的url可在这里看到
  • 在IFTTT建立一个if Webhooks then Telegram的Applet,设定一个Event Name。
统计分析和推送
  • Thingspeak上再开一个新app - MATLAB Analysis,将如下MATLAB代码填入,其中频道ID和Telegram Webhooks部分换成你自己的:
% PooAnalysis
%
% thingSpeak Channel ID
ID={your_ID};

%读取最近一周的数据
[poo1,pooT] = thingSpeakRead(ID,'Fields',1,'NumDays',7);
%求这一周平均隔多少小时拉一次
weekresult=hours(mean(diff(pooT)));

clear poo1 pooT
%读取所有数据
[poo1,pooT] = thingSpeakRead(ID,'Fields',1,'NumPoints',8000);
%求整个统计期间平均隔多少小时啦一次
allresult=hours(mean(diff(pooT)));

%个性化推送字符串生成
if weekresult>48
    string2=['X桶啊你!'];
elseif weekresult>24&weekresult<=48
    string2=['你拉屎还不够勤快!'];
else
    string2=['赏你朵勤快拉屎小红花!'];
end

string1=['#poo 你上周平均隔',num2str(weekresult),'小时拉一次屎,',string2,' | 整个统计期间平均隔',num2str(allresult),'小时拉一次。'];

%推送至Telegram
options = weboptions('RequestMethod','post');
response = webwrite('https://maker.ifttt.com/trigger/{your_event_name}/with/key/{your_token}', 'value1',string1, options);

给这个MATLAB Analysis加一个Time Control,设定每周执行一次。

推送效果如图:

锦上添花部分:利用Thingspeak Apps - MATLAB Visualizations绘统计图
  • 在Thingspeak添加一个MATLAB Visualization,放入以下代码:
% Prior to running this MATLAB code template, assign the channel ID to read
% data from to the 'readChannelID' variable. Also, assign the field ID
% within the channel that you want to read data from to plot.

% TODO - Replace the [] with channel ID to read data from:
readChannelID = {your_channel_ID};
% TODO - Replace the [] with the Field ID to read data from:
fieldID = 1;

% Channel Read API Key 
% If your channel is private, then enter the read API
% Key between the '' below: 
% readAPIKey = ' ';

%% Read Data %%

[poo1, pooT] = thingSpeakRead(readChannelID, 'Field', fieldID, 'NumPoints', 8000, 'ReadKey', readAPIKey);

%% Visualize Data %%

[N,X]=hist(hour(pooT),0:23);
XTickN = 0 : 23;
axes('Position',[0.1 0.3 0.8 0.6],...
   'XLim',[0 23],...
   'XTick',XTickN,...
   'Box','On')
hold on
bar(N,'FaceColor',[0.9 0.5 0.3]);
title('When you poo in a day!');
xlabel('hour');
hold off

效果如图:

:虽说是便便时间间隔统计,但这个思路一样可以用于记录和统计分析些别的。


Viewing all articles
Browse latest Browse all 13

Latest Images

Trending Articles





Latest Images