Skip to main content

Thingsboard Esp8266智能鱼缸

B站效果视频:

Esp8266代码实现

直接拷贝到Arduino里烧录既可以运行。通过Thingsboard发送RPC命令 setKC {'cmd':15} 就可以控制4路继电器

控制的算法如下(逻辑运算,对二进制与)
二进制:0 0 0 0 分别对应4路继电器
1 1 1 1 四路继电器全部打开,转为十进制就是15
0 1 1 1 三路继电器全部打开,转为十进制就是14
1 1 1 0 前三位继电器全部打开,转为十进制就是7
Arduino代码
/*
SimpleMQTTClient.ino
The purpose of this exemple is to illustrate a simple handling of MQTT and Wifi connection.
Once it connects successfully to a Wifi network and a MQTT broker, it subscribe to a topic and send a message to it.
It will also send a message delayed 5 seconds later.
*/
#include <Arduino_JSON.h>
#include <assert.h>

#include "EspMQTTClient.h"
#define PIN_YQ 16
#define PIN_D 14
#define PIN_XHS 12
#define PIN_YM 13

const String RPC_TOPIC = "v1/devices/me/rpc/request/+";
const String RPC_TOPIC_RESPONSE = "v1/devices/me/rpc/response/";
const String TELEMETRY = "v1/devices/me/telemetry";
const int RPC_TOPIC_LEN = RPC_TOPIC.length();
JSONVar KC;

EspMQTTClient client(
"mitang",
"joson_Mt_7410",
"tb.javabase.cn", // tb.javabase.cn
"H8dbsuMyOB8mpiFEokTT",
"", // Can be omitted if not needed
"yugang", // Client name that uniquely identify your device
1883 // The MQTT port, default to 1883. this line can be omitted
);

void setup()
{
Serial.begin(115200);
// Optional functionalities of EspMQTTClient
//client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overridded with enableHTTPWebUpdater("user", "password").
client.enableOTA(); // Enable OTA (Over The Air) updates. Password defaults to MQTTPassword. Port is the default OTA port. Can be overridden with enableOTA("password", port).
client.enableLastWillMessage("TestClient/lastwill", "I am going offline"); // You can activate the retain flag by setting the third parameter to true
pinMode(PIN_YQ,OUTPUT);
pinMode(PIN_D,OUTPUT);
pinMode(PIN_XHS,OUTPUT);
pinMode(PIN_YM,OUTPUT);
KC["status"] = 0x00;
}


void onConnectionEstablished()
{

client.subscribe(RPC_TOPIC, [](const String & topic, const String & payload) {

String cpTopic = topic;
cpTopic.trim();

int tlen = cpTopic.length();
// char 数组转字符串需要在结尾加\0,所以在初始化数组的时候需要多初始化一个,为下面添加\0做准备
int arrLen = tlen - (RPC_TOPIC_LEN - 1) + 1;
char requestId[arrLen] = {0};
// 添加\0 表示char数组为字符串结尾
requestId[arrLen - 1] = '\0';
for (int i = RPC_TOPIC_LEN - 1, j = 0; i < tlen; i++, j++) {
requestId[j] = cpTopic[i];
}
JSONVar obj = JSON.parse(payload);
callRPC(requestId, (const char*)obj["method"], (JSONVar)obj["params"]);
});

//client.publish("v1/devices/me/rpc/response/243", "{'a':100}",0); // You can activate the retain flag by setting the third parameter to true
}


void loop()
{
client.loop();
}

void callRPC(char *requestId, const char *methodName, JSONVar params) {
if (strcmp(methodName, "setKC") == 0) {
byte cmd = (int)params["cmd"];
if (cmd & 0x01){
digitalWrite(PIN_YQ,HIGH);
}else{
digitalWrite(PIN_YQ,LOW);
}
if (cmd & 0x02){
digitalWrite(PIN_D,HIGH);
}else{
digitalWrite(PIN_D,LOW);
}
if (cmd & 0x04){
digitalWrite(PIN_XHS,HIGH);
}else{
digitalWrite(PIN_XHS,LOW);
}
if (cmd & 0x8){
digitalWrite(PIN_YM,HIGH);
}else{
digitalWrite(PIN_YM,LOW);
}
KC["status"] = cmd;
Serial.print(cmd);
}
String resStr = JSON.stringify(KC);
char byt[resStr.length() + 1];
resStr.toCharArray(byt, resStr.length() + 1);
client.publish(RPC_TOPIC_RESPONSE + requestId, byt, 0);
}

添加一个控制部件

新建控制部件

选择设备

调用RPC