DIY 远程点火器
注:
此方案不是最佳方案,可以使用ESP12F等直接对IO控制,因为手头只有01S且想加入1602(12F可以使用 I2C连接)故用NANO+ESP
原理图:
实物图:
ESP01S代码:
/*
尊重原创,转载请注明出处!
本文作者:OhWMF 链接:https://www.ohwmf.cn/p/389.html
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
WiFiClient espClient;
PubSubClient client(espClient);
#ifndef AP_CONFIG
String UID = "Firework_Fire_001";
#endif
const char* UserName = "xxxxxx";
const char* PassWord = "xxxxxxxxxxxxx";
bool subTopicCenterFlag = false;
unsigned long bemfa_mqtt_current_millis;
unsigned long bemfa_mqtt_last_re_millis = 0;
void reConnect() {
if (!client.connected()) {
if(client.connect(UID.c_str(),UserName,PassWord)){
Serial.println("connected");
subTopicCenterFlag = true;
} else {
bemfa_mqtt_current_millis = millis();
if (bemfa_mqtt_current_millis - bemfa_mqtt_last_re_millis > 2000) {
bemfa_mqtt_last_re_millis = millis();
Serial.println(" try again reconnect");
}
}
}
}
#include <WiFiManager.h>
WiFiServer server(80);
void subMqttTopicCenter() {
if(subTopicCenterFlag == true){
subTopicCenterFlag = false;
Serial.print("sub topic:");Serial.println(String("fire"));
client.subscribe(String("fire").c_str());delay(15);
}}
void callback(char* rtopic, byte* payload, unsigned int length) {
String msg = "";
String topic = rtopic;
for (int i = 0; i < length; i++) {
msg += (char)payload[i];
}
if (topic == "fire") {
Serial.println(msg);
}
msg = "";
topic = "";
}
void setup(){
client.setServer("xxxxxxxxxxx", 1883);
client.setCallback(callback);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFiManager wm;
bool res;
res=wm.autoConnect();
pinMode(LED_BUILTIN, OUTPUT);
}
void loop(){
reConnect();
client.loop(); subMqttTopicCenter();
if (Serial.available() > 0) {
client.publish( String("back").c_str() , String(Serial.readString()).c_str());
Serial.println("A message has been published");
}
}
NANO 代码:
/*
尊重原创,转载请注明出处!
本文作者:OhWMF 链接:https://www.ohwmf.cn/p/389.html
*/
#include <Arduino.h>
char cmd[13];
bool valid_cmd = false;
long previousTime = 0;
long interval = 30000;
void setup()
{
Serial.begin(115200);
pinMode(A0, OUTPUT);
digitalWrite(A0, LOW);
pinMode(A1, OUTPUT);
digitalWrite(A1, LOW);
pinMode(A2, OUTPUT);
digitalWrite(A2, LOW);
pinMode(A3, OUTPUT);
digitalWrite(A3, LOW);
pinMode(A4, OUTPUT);
digitalWrite(A4, LOW);
pinMode(A5, OUTPUT);
digitalWrite(A5, LOW);
pinMode(A6, OUTPUT);
digitalWrite(A6, LOW);
pinMode(A7, OUTPUT);
digitalWrite(A6, LOW);
Serial.println("ready");
}
void loop()
{
unsigned long currentTime = millis();
if (currentTime - previousTime > interval)
{
previousTime = currentTime;
Serial.println("online");
}
int i;
if (Serial.available() > 0)
{
for (i = 0; i < 13; i++)
{
cmd[i] = '\0';
}
for (i = 0; i < 12; i++)
{
if (Serial.available() > 0)
{
cmd[i] = Serial.read();
delay(1);
}
else
{
break;
}
}
valid_cmd = true;
}
if (valid_cmd)
{
if (0 == strncmp(cmd, "fire1", 5))
{
digitalWrite(A0, HIGH);
delay(2000);
digitalWrite(A0, LOW);
Serial.println("ok");
}
else if (0 == strncmp(cmd, "fire2", 5))
{
digitalWrite(A1, HIGH);
delay(2000);
digitalWrite(A1, LOW);
Serial.println("ok");
}
else if (0 == strncmp(cmd, "fire3", 5))
{
digitalWrite(A2, HIGH);
delay(2000);
digitalWrite(A2, LOW);
Serial.println("ok");
}
else if (0 == strncmp(cmd, "fire4", 5))
{
digitalWrite(A3, HIGH);
delay(2000);
digitalWrite(A3, LOW);
Serial.println("ok");
}
else if (0 == strncmp(cmd, "fire5", 5))
{
digitalWrite(A4, HIGH);
delay(2000);
digitalWrite(A4, LOW);
Serial.println("ok");
}
else if (0 == strncmp(cmd, "fire6", 5))
{
digitalWrite(A5, HIGH);
delay(2000);
digitalWrite(A5, LOW);
Serial.println("ok");
}
else if (0 == strncmp(cmd, "fire7", 5))
{
digitalWrite(A6, HIGH);
delay(2000);
digitalWrite(A6, LOW);
Serial.println("ok");
}
else if (0 == strncmp(cmd, "fire8", 5))
{
digitalWrite(A7, HIGH);
delay(2000);
digitalWrite(A7, LOW);
Serial.println("ok");
}
else if (0 == strncmp(cmd, "fireall", 7))
{
digitalWrite(A0, HIGH);
delay(2000);
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
delay(2000);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
delay(2000);
digitalWrite(A2, LOW);
digitalWrite(A3, HIGH);
delay(2000);
digitalWrite(A3, LOW);
digitalWrite(A4, HIGH);
delay(2000);
digitalWrite(A4, LOW);
digitalWrite(A5, HIGH);
delay(2000);
digitalWrite(A5, LOW);
digitalWrite(A6, HIGH);
delay(2000);
digitalWrite(A6, LOW);
digitalWrite(A7, HIGH);
delay(2000);
digitalWrite(A7, LOW);
Serial.println("ok");
}
else if (0 == strncmp(cmd, "firealldelay", 12))
{
digitalWrite(A0, HIGH);
delay(2000);
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
delay(2000);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
delay(2000);
digitalWrite(A2, LOW);
digitalWrite(A3, HIGH);
delay(2000);
digitalWrite(A3, LOW);
digitalWrite(A4, HIGH);
delay(2000);
digitalWrite(A4, LOW);
digitalWrite(A5, HIGH);
delay(2000);
digitalWrite(A5, LOW);
digitalWrite(A6, HIGH);
delay(2000);
digitalWrite(A6, LOW);
digitalWrite(A7, HIGH);
delay(2000);
digitalWrite(A7, LOW);
Serial.println("ok");
}
valid_cmd = false;
}
delay(10);
}
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 solitud.es
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果