这里采用Arduino 编写/烧录程序 未来想着在此基础上添加推力采集部分(推力采集部分写进新的核心里去,以防止继电器控制函数带来的单核阻塞)
#include <PubSubClient.h>
#include <WiFi.h>
#include <SimpleTimer.h>
const char *mqtt_broker = "192.168.2.3";//mqqt服务器地址
const char *mqtt_username = "mqtt";//用户,密码
const char *mqtt_password = "mqtt";
const int mqtt_port = 1883;//端口
String mqtt_topic = "";
String mqtt_data = "";
boolean mqtt_status = false;
String project = "esp";
volatile boolean qi;//继电器控制函数
WiFiClient espClient;
PubSubClient client(espClient);
void callback(char *topic, byte *payload, unsigned int length) {//mqtt消息读取
String data = "";
for (int i = 0; i < length; i++) {
data = String(data) + String((char) payload[i]);
}
mqtt_topic = String(topic);
mqtt_data = data;
mqtt_status = true;
}
SimpleTimer timer;
extern "C"
{
uint8_t temprature_sens_read();
}
uint8_t temprature_sens_read();
void Simple_timer_1() {//状态报告
client.publish("状态",String("检验是否连接,并报告状态").c_str());
client.publish("状态",String(String("mac地址:") + String(WiFi.macAddress())).c_str());
client.publish("状态",String(String("温度:") + String((temprature_sens_read() - 32) / 1.8)).c_str());
}
void qibao()//继电器函数
{
digitalWrite(13,LOW);//打开
delay(1000);
digitalWrite(13,HIGH);//复位
}
void setup(){
pinMode(21, OUTPUT);//声明21引脚使用(sda/scl)
pinMode(22, OUTPUT);//(sda/scl)
pinMode(13, OUTPUT);//继电器控制开关
digitalWrite(13,HIGH);//我的继电器为高电平关闭,低电平打开(这里事先设置为高电平,防止微弱的闭合)(应在上电一段时间且对应继电器状态灯为关闭后在安入点火头)
//或者建议在安全距离外选择接入点火头
Serial.begin(9600);//串口开启(后期建议放入线程来提升主频)
WiFi.begin("1402", "msm53547880");//wifi连接 wifi名,密码
while (WiFi.status() != WL_CONNECTED) {//wifi等待连接
delay(500);
Serial.print(".");
}
Serial.println("Local IP:");
Serial.print(WiFi.localIP());
Serial.println("hello");
Serial.println(WiFi.status());
Serial.println(WiFi.localIP());
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
while (!client.connected()) {//mqtt连接
String client_id = "esp-client-";
client_id += String(WiFi.macAddress());
Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str());
if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
Serial.println("Public emqx mqtt broker connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
timer.setInterval(500L, Simple_timer_1);//每五秒启动Simple_timer_1(报告函数)
client.subscribe("报告");//发布订阅
client.subscribe("状态");
client.subscribe("数据");
client.subscribe("引爆");
}
void loop(){
client.loop();
client.loop();
timer.run();
if (mqtt_status) {
if (String(mqtt_topic).equals("报告")) {//判断消息传回
Serial.println(mqtt_data);
client.publish("数据",String("以接收").c_str());//测试消息服务器用
mqtt_status = false;
}
if (String(mqtt_topic).equals("报告")) {//判断消息传回
Serial.println(mqtt_data);
if(mqtt_data=="001")
{
client.publish("数据",String("以收到001代码,准备引爆").c_str());
qibao();
client.publish("数据",String("以接收,执行完毕").c_str());
}
mqtt_status = false;
}
}
//client.publish("esp",String("检验是否连接").c_str());
}
(部分为省事,直接从一个可视化编程软件复制过来了)
由于继电器使用电压为5v,所以需要一个外接电源
VID_20240729_185313.mp4 点击下载
大体样子
VID_20240729_202437(0).mp4 点击下载
实验点火部分
未来可以结合mqtt客户端,或者开发内嵌支持mqtt的应用实现便捷的联网点火/实时数据采集与记录