惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
V
V2EX
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
小众软件
小众软件
Vercel News
Vercel News
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
J
Java Code Geeks
P
Proofpoint News Feed
MongoDB | Blog
MongoDB | Blog
B
Blog
美团技术团队
量子位

博客园 - 孤独的猫咪神

自定义贪吃蛇环境进行PPO模型训练 通过TensorBoard进行PPO参数优化 TensorBoard PPO log 解析 macos(M4)上跑强化学习 - PyTorch LunarLander-v3 Stable-Baselines3 部分机型下,Flutter的flutter_inappwebview在使用evaluateJavascript调用postMessage失败的情况 HarmonyOS中,html 与 ets 桥接沟通 Flutter中,html 与 dart 桥接沟通 Flutter 环境变量 Mole,清理Mac的小工具 Flutter自定义主题Theme最佳实践 Ubuntu 20.04 开机自动添加git的ssh Flutter 第三方库 Jenkins CLI 通过ssh方式链接时的证书 阿里云Ubuntu 14.04 + Nginx + .net core + MySql 移动开发网络接口 经验总结 阿里云Ubuntu 14.04 + Nginx + let's encrypt 搭建https访问 Pathoto项目:AWS+golang+beego搭建 osx开发,skport项目记录 使用Jekyll在Github上搭建博客 iOS搜索附近的位置(类似微博朋友圈位置) retrofit2中ssl的Trust anchor for certification path not found问题 Android中的Semaphore React Native 在现有项目中的探路 博客园 Linux客户端 2.0 正式发布! 博客园 Windows客户端 2.0 正式发布! 博客园 Mac客户端 2.0 正式发布!
米家 + arduino + 自定义服务器
孤独的猫咪神 · 2020-10-20 · via 博客园 - 孤独的猫咪神

无聊突然想把米家的一些功能链接到我自己的服务器上,查了一下资料发现,木有开放的api。然后,我想到一个黑操作。

整体思路

通过米家app中的“智能”功能,可以触发开关米家台灯(这里我只有米家台灯感觉合适),然后通过硬件(光敏电阻),获取灯光是否打开,就可以通过自己的硬件,把指定的内容发送到服务器咯。我真棒!

购买硬件

最早想买一个树莓派来玩,不过价格让我有点不开心···
然后,我找到了Arduino。

开发

接下来就是开发咯。

服务器

我自己有一台阿里云的服务器,所以,开搞。
这里使用了Python3。 https://github.com/LunaGao/arduino-server 代码我上传到了git。
其实就是简单的一个数据展示。 数据库是我白嫖MongoDB的。嘿嘿。

硬件

硬件开发对我这种新手来说,有点困难···主要是从来没搞过···然后,硬着头皮搞了一下,咦,真香···
从开始的硬件串口都找不到(其实是使用的数据线有问题),到最终出成果,包括焊接,大约花费6个小时。

焊接硬件

当时忘记拍照了,很久之前改造大方摄像头的时候买了电焊台(当然最终以大方摄像头报废,电焊台吃灰结束),这次正好派上用场。
焊接技术简直渣到不行···

光敏电阻的电源接的是3.3v,然后这里有个坑,光敏电阻的宝贝说明上,给我的是一个数字信号接口,但是板子上其实在低电平一直也可以接收到电压,不过真的是低电压,但是板子认为这个高电压,所以一直有信号。后来我改成接模拟信号接口才搞定。所以光敏电阻的D0接口,我接到了板子的A0上。(很有可能我搞错了)

代码编写

写代码这个就相对简单一些

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#include <WiFiClient.h>

const char* ssid = "你的wifi名";
const char* password = "你的wifi密码";

bool isSent = false;
bool isFirst = true;
bool isDark = false;

void setup() {

Serial.begin(19200);
Serial.setDebugOutput(true);

Serial.println();
Serial.println();
Serial.println();

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
}

void loop() {
// wait for WiFi connection
if ((WiFi.status() == WL_CONNECTED)) {
int lightValue = analogRead(0); // 1024 is dark
if (lightValue > 1000) { // dark mode
if (isDark) {
isSent = true;
} else {
isSent = false;
}
isDark = true;
} else { // light mode
isDark = false;
}
if (isFirst) { // If is first start
// do nothing
isSent = false;
isFirst = false;
}
if (!isSent) { // is sent value to server
isSent = true;
sendToServer();
}
}
delay(1000);
}

void sendToServer() {
WiFiClient client;

HTTPClient http;

Serial.print("[HTTP] begin...\n");
if (http.begin(client, "这里是你的服务器地址")) { // HTTP


Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();

// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.print("[HTTP] GET... code: \n");

// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.print(payload);
Serial.println();
}
} else {
Serial.print("error start");
Serial.print(http.errorToString(httpCode).c_str());
Serial.print("error end");
}

http.end();
} else {
Serial.print("[HTTP} Unable to connect\n");
}
}

硬件改造

由于使用光敏电阻来判断,肯定要黑暗的环境哦,所以,我机智的把光敏电阻和台灯关在了盒子里。

米家App设置

在米家app中,设置一个智能,当某个动作执行时(我这里使用了门锁),让灯打开然后再关闭,这样就可以发送数据啦。

这里要注意的是,这个间隔1秒好像没啥用···灯没关···我设置到了3秒,正常。(2秒没试过)

结束

然后就成功啦。每次从外面开门,我的服务器都可以收到一条信息啦。

第一条是测试数据,第二条和第三条是真实数据。哇咔咔