简介

想给几个乌龟做喂食器很久了,一直抽不出时间,只能偶尔搜一下,想想方案。在公司用 Arduino 搞了几个控制方案后,熟悉了就开始干起来了,最主要是又 AI 帮忙了。
为什么不在淘宝买成品?觉得不够好,觉得自己 DIY 更帅,觉得陈年废品可以重获新生了。
test1

结构设计

在淘宝看了一下各种自动喂食器,感觉还是用螺旋推食出来比较好一点,结构简洁装配简单不容易出问题,喂食多少只需要调整电机转动时间就好了。
设计软件 Solid Works,设计理念就是结构件越少越好,安装简单,还要方便 3D 打印。
结构分为下面几个部分:

  • 底座。安装电机,电路板,容器,螺旋推杆等等,3D 一体打印,比较方便。
    dock
    test
  • 螺旋推杆。和电机一起固定。
    spiral
  • 电路盒。固定电路板。
    PCB
    pcbbox
  • 其他配件。食物容器盖子,电机盖子,支撑夹具等。

硬件设计

手上有好几个 ESP8266 模块,还是几年前买的,一直都没用。所以方案就是基于 ESP8266 联网,控制电机转动。
电路设计很简单,ESP8266 模块负责联网和 PWM 控制电机,我加了一个 USB 转串口的芯片,方便调试(因为我 CP2102 芯片有好多)。因为我的电机是 12V 的减速电机,所以还需要 DC-DC 电源芯片,可以直接转到 3.3V,我是因为搞了个 USB 调试,所以又加了一个 3.3V 的 LDO,其实没必要。
sch
电路很简单,仅供 DIY 用。

软件设计

软件设计一种方案是 ESP8266 模块作为 Arduino 板联网控制;另一种方案是 ESPHOME,连到 HomeAssistant 控制。
两种方案软件都相对简单,Arduino 连物联网比较麻烦点,因为很少有给个人免费用的,花钱又没那个必要。而 ESPHOME 连到 HA 就有现成的 APP 控制了。
但是我喜欢把鸡蛋放到不同的篮子里,比如很多人搞软路由,黑群晖,HomeAssistant 等都放一块板上,我就不喜欢 ALL IN ONE,很可能一个挂,全部挂。所以我没有选择 ESPHOME 而用了 Arduino。
物联网云我选了好久,国内的几乎没有个人用的可以选的了,知名的都要收费,不知名的也不敢用啊。最后选了 Arduino IoT Cloud,免费用户支持 2 个设备,对我来说已经够了,测试了一个多月,还是比较稳定的,响应速度也快。但是,Android 端 APP 需要 GMS,没有框架是运行不起来的,我估计是出于安全性的考虑吧。
首先要在 https://app.arduino.cc/ 注册账号,然后再添加代码,烧录到设备。
ESP8266 的 IO 定义如下图:
esp8266

Arduino 代码很简单,就控制电机转动时间,如下:

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
92
// Arduino cloud 的物联网函数,有 key 及 WiFi 用户名密码等
#include "arduino_secrets.h"
#include "thingProperties.h"
// 定义引脚
#define pwm1Pin 12
#define pwm2Pin 14
#define keyPin 2

bool motorRunning = false;

void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
pinMode(pwm1Pin, OUTPUT);
pinMode(pwm2Pin, OUTPUT);
pinMode(keyPin, INPUT_PULLUP);
analogWrite(pwm1Pin, 0);
digitalWrite(pwm2Pin, LOW);

// Defined in thingProperties.h
initProperties();

// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);

/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}

void loop() {
ArduinoCloud.update();
// 按键喂食,按一下电机开始转动
// 读取 key 状态并进行去抖动处理
int keyState = digitalRead(keyPin);
delay(50);
keyState = digitalRead(keyPin);

//检测到 key 信号为低电平并且电机未转动时,驱动电机转动 10s
if (keyState == LOW && motorRunning == false) {
analogWrite(pwm1Pin, 200); // 设置电机顺时针转动,直接设数值在没有网络的时候也可以使用
digitalWrite(pwm2Pin, LOW);

//记录当前时间电机状态为运行
motorRunning = true;

delay(10000);
}else {
analogWrite(pwm1Pin, 0); // 停止电机
digitalWrite(pwm2Pin, LOW);
// 设置电机状态为停止
motorRunning = false;
}
}

/*
Since Pushbutton is READ_WRITE variable, onPushbuttonChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPushbuttonChange() {
// 当物联网按键触发时,电机转动,speed 速度可以在 app 上调整 0-255
if (pushbutton == true && motorRunning == false) {
analogWrite(pwm1Pin, speed); // 设置电机顺时针转动
digitalWrite(pwm2Pin, LOW);

//记录当前时间电机状态为运行
motorRunning = true;

delay(10000);
} else {
analogWrite(pwm1Pin, 0); // 停止电机
digitalWrite(pwm2Pin, LOW);
// 设置电机状态为停止
motorRunning = false;
}
}

/*
Since Speed is READ_WRITE variable, onSpeedChange() is
executed every time a new value is received from IoT Cloud.
*/
void onSpeedChange() {
// Add your code here to act upon Speed change
}

ESP8266 可以通过浏览器直接烧固件(要安装官方插件),不过要先按住设备的 flash 按键(上电时 GPIO0 为低时 ESP8266 模块进入烧写模式) ,再给设备上电,然后点击上传固件。

注:

经过我长时间使用,发现 cloud 烧录这种方式还有 bug,例如会出现:
No associated device yet
但实际上我确定已经关联设备了,并且更换电脑,更换账号都不好用,之前连接好的设备修改了一下参数也显示这个。还是建议本地编译上传到设备。

下载 Arduino APP,登录,可以看到设备上线了,点击按键控制。

安装后的喂食视频:

至于 Arduino IoT Cloud 怎么用呢?似乎很少有人写,且听我下回分解吧。
以上所有原始文件在此,仅供参考,有问题自己搜索或问 AI:
https://github.com/harry10086/Autofeed