『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 5057|回复: 3

[技术探讨] 求助7总@se7en基于esp的遥控器问题

[复制链接]

11

主题

150

帖子

715

积分

高级会员

Rank: 4

积分
715
金钱
560
HASS币
20
发表于 2018-9-19 12:29:08 | 显示全部楼层 |阅读模式
我按这个教程https://bbs.hassbian.com/forum.p ... 5%E6%8E%A7%E5%99%A8
刷的v3开发版  因为这个教程只有7个按键  我就增加了一个按键   我在源码上按相同格式增加了按键8 刷机通过
但是按键8变成了高电平触发  其它按键都是低电平触发  找不出问题 麻烦7总帮我看一下源码 谢谢
  
#include <ArduinoOTA.h>

#include <dummy.h>
#include <EEPROM.h>

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Ticker.h>

#define BUTTON1          0       
#define BUTTON2          2       
#define BUTTON3          5     
#define BUTTON4          14      
#define BUTTON5          12     
#define BUTTON6          13      
#define BUTTON7          4  
#define BUTTON8          15  
    
#define RELAY1           16                                
#define RELAY2           16                                 
#define RELAY3           16                                 
#define RELAY4           16    
#define RELAY5           16 
#define RELAY6           16 
#define RELAY7           16 
#define RELAY8           16   
                          
#define LED              16                              

#define MQTT_CLIENT     "8switch"         
#define MQTT_SERVER     "192.168.1.127"                      // MQTT服务器IP
#define MQTT_PORT       1883                                
#define MQTT_TOPIC      "home/8ch/2"         
#define MQTT_USER       "pi"                               // MQTT服务器用户名
#define MQTT_PASS       "hachina"                               //MQTT服务器的密码

#define WIFI_SSID       "Myroom"                           // wifi ssid(你家的wifi名字,注意不能是5G频段)
#define WIFI_PASS       "hahahaha"                       // wifi password(你家的wifi密码)

#define VERSION    "\n\n------------------ BY SE7EN  -------------------"

                          
bool sendStatus1 = false;
bool sendStatus2 = false;               
bool sendStatus3 = false;   
bool sendStatus4 = false;        
bool sendStatus5 = false;  
bool sendStatus6 = false;             
bool sendStatus7 = false;    
bool sendStatus8 = false; 
bool sendStatus9 = false; 


                                 
extern "C" { 
  #include "user_interface.h" 
}

bool sendStatus = false;
bool requestRestart = false;

int kUpdFreq = 1;
int kRetries = 10;

unsigned long TTasks;
unsigned long count = 0;
unsigned long count1 = 0;                                   
unsigned long count2 = 0;                                   
unsigned long count3 = 0;                                  
unsigned long count4 = 0;                                    
unsigned long count5 = 0;                                    
unsigned long count6 = 0;                                   
unsigned long count7 = 0;       
unsigned long count8 = 0;   
                          

WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient, MQTT_SERVER, MQTT_PORT);
Ticker btn_timer1, btn_timer2, btn_timer3, btn_timer4, btn_timer5, btn_timer6, btn_timer7, btn_timer8;

void callback(const MQTT::Publish& pub) {
  if (pub.payload_string() == "stat") {
  }
  else if (pub.payload_string() == "1on") {
    digitalWrite(RELAY1, HIGH);
    sendStatus1 = true;
  }
  else if (pub.payload_string() == "1off") {
    digitalWrite(RELAY1, LOW);
    sendStatus1 = true;
  }
  else if (pub.payload_string() == "2on") {
    digitalWrite(RELAY2, HIGH);
    sendStatus2 = true;
  }
  else if (pub.payload_string() == "2off") {   
    digitalWrite(RELAY2, LOW);
    sendStatus2 = true;
  }
  else if (pub.payload_string() == "3on") {
    digitalWrite(RELAY3, HIGH);
    sendStatus3 = true;
  }
  else if (pub.payload_string() == "3off") {
    digitalWrite(RELAY3, LOW);
    sendStatus3 = true;
  }
  else if (pub.payload_string() == "4on") {
    digitalWrite(RELAY4, HIGH);
    sendStatus4 = true;
  }
  else if (pub.payload_string() == "4off") {
    digitalWrite(RELAY4, LOW);
    sendStatus4 = true;
  }
  else if (pub.payload_string() == "5on") {
    digitalWrite(RELAY5, HIGH);
    sendStatus5 = true;
  }
  else if (pub.payload_string() == "5off") {
    digitalWrite(RELAY5, LOW);
    sendStatus5 = true;
  }
  else if (pub.payload_string() == "6on") {
    digitalWrite(RELAY6, HIGH);
    sendStatus6 = true;
  }
  else if (pub.payload_string() == "6off") { 
    digitalWrite(RELAY6, LOW);
    sendStatus6 = true;
  }
  else if (pub.payload_string() == "7on") {
    digitalWrite(RELAY7, HIGH);
    sendStatus7 = true;
  }
  else if (pub.payload_string() == "7off") {
    digitalWrite(RELAY7, LOW);
    sendStatus7 = true;
  } 
  else if (pub.payload_string() == "8on") {
    digitalWrite(RELAY8, HIGH);
    sendStatus8 = true;
  }
  else if (pub.payload_string() == "8off") {
    digitalWrite(RELAY8, LOW);
    sendStatus8 = true;   
  }  
 
  else if (pub.payload_string() == "reset") {
    requestRestart = true;
  }
}

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);  
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);
  pinMode(RELAY5, OUTPUT);  
  pinMode(RELAY6, OUTPUT);  
  pinMode(RELAY7, OUTPUT);  
  pinMode(RELAY8, OUTPUT);

  pinMode(BUTTON1, INPUT_PULLUP);
  pinMode(BUTTON2, INPUT_PULLUP);
  pinMode(BUTTON3, INPUT_PULLUP); 
  pinMode(BUTTON4, INPUT_PULLUP);  
  pinMode(BUTTON5, INPUT_PULLUP);
  pinMode(BUTTON6, INPUT_PULLUP);
  pinMode(BUTTON7, INPUT_PULLUP);
  pinMode(BUTTON8, INPUT_PULLUP);

  
  digitalWrite(LED, HIGH);
  digitalWrite(RELAY1, LOW);
  digitalWrite(RELAY2, LOW);
  digitalWrite(RELAY3, LOW);  
  digitalWrite(RELAY4, LOW);  
  digitalWrite(RELAY5, LOW);  
  digitalWrite(RELAY6, LOW);   
  digitalWrite(RELAY7, LOW); 
  digitalWrite(RELAY8, LOW); 
 
  btn_timer1.attach(0.05, button1);
  btn_timer2.attach(0.05, button2);
  btn_timer3.attach(0.05, button3); 
  btn_timer4.attach(0.05, button4);  
  btn_timer5.attach(0.05, button5); 
  btn_timer6.attach(0.05, button6); 
  btn_timer7.attach(0.05, button7);
  btn_timer8.attach(0.05, button8);

  mqttClient.set_callback(callback);
  
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  Serial.begin(115200);
  Serial.println(VERSION);
  Serial.print("\nESP ChipID: ");
  Serial.print(ESP.getChipId(), HEX);
  Serial.print("\nConnecting to "); Serial.print(WIFI_SSID); Serial.print(" Wifi"); 
  while ((WiFi.status() != WL_CONNECTED) && kRetries --) {
    delay(500);
    Serial.print(" .");
  }
  if (WiFi.status() == WL_CONNECTED) {  
    Serial.println(" DONE");
    Serial.print("IP Address is: "); Serial.println(WiFi.localIP());
    Serial.print("Connecting to ");Serial.print(MQTT_SERVER);Serial.print(" Broker . .");
    delay(500);
    while (!mqttClient.connect(MQTT::Connect(MQTT_CLIENT).set_keepalive(90).set_auth(MQTT_USER, MQTT_PASS)) && kRetries --) {
      Serial.print(" .");
      delay(1000);
    }
    if(mqttClient.connected()) {
      Serial.println(" DONE");
      Serial.println("\n----------------------------  Logs  ----------------------------");
      Serial.println();
      mqttClient.subscribe(MQTT_TOPIC);
      blinkLED(LED, 40, 8);
      digitalWrite(LED, LOW);
    }
    else {
      Serial.println(" FAILED!");
      Serial.println("\n----------------------------------------------------------------");
      Serial.println();
    }
  }
  else {
    Serial.println(" WiFi FAILED!");
    Serial.println("\n----------------------------------------------------------------");
    Serial.println();
  }
}

void loop() { 
  mqttClient.loop();
  timedTasks();
  checkStatus();
}

void blinkLED(int pin, int duration, int n) {             
  for(int i=0; i<n; i++)  {  
    digitalWrite(pin, HIGH);        
    delay(duration);
    digitalWrite(pin, LOW);
    delay(duration);
  }
}

void button1() {
  if (!digitalRead(BUTTON1)) {
    count1++;
  } 
  else {
    if (count1 > 1 && count1 <= 40) {   
      digitalWrite(RELAY1, !digitalRead(RELAY1));
      sendStatus1 = true;
    } 
    else if (count1 >40){
      Serial.println("\n\nSonoff Rebooting . . . . . . . . Please Wait"); 
      requestRestart = true;
    } 
    count1=0;
  }
}

void button2() {
  if (!digitalRead(BUTTON2)) {
    count2++;
  } 
  else {
    if (count2 > 1 && count2 <= 40) {   
      digitalWrite(RELAY2, !digitalRead(RELAY2));
      sendStatus2 = true;
    } 
    count2=0;
  }
}
void button3() {
  if (!digitalRead(BUTTON3)) {
    count3++;
  } 
  else {
    if (count3 > 1 && count3 <= 40) {   
      digitalWrite(RELAY3, !digitalRead(RELAY3));
      sendStatus3 = true;
    } 
    count3=0;
  }
}
void button4() {
  if (!digitalRead(BUTTON4)) {
    count4++;
  } 
  else {
    if (count4 > 1 && count4 <= 40) {   
      digitalWrite(RELAY4, !digitalRead(RELAY4));
      sendStatus4 = true;
    } 
    count4=0;
  }
}
void button5() {
  if (!digitalRead(BUTTON5)) {
    count5++;
  } 
  else {
    if (count5 > 1 && count5 <= 40) {   
      digitalWrite(RELAY5, !digitalRead(RELAY5));
      sendStatus5 = true;
    } 
    count5=0;
  }
}
void button6() {
  if (!digitalRead(BUTTON6)) {
    count6++;
  } 
  else {
    if (count6 > 1 && count6 <= 40) {   
      digitalWrite(RELAY6, !digitalRead(RELAY6));
      sendStatus6 = true;
    } 
    count6=0;
  }
}
void button7() {
  if (!digitalRead(BUTTON7)) {
    count7++;
  } 
  else {
    if (count7 > 1 && count7 <= 40) {   
      digitalWrite(RELAY7, !digitalRead(RELAY7));
      sendStatus7 = true;
    } 
    count7=0;
  }
}
void button8() {
  if (!digitalRead(BUTTON8)) {
    count8++;
  } 
  else {
    if (count8 > 1 && count8 <= 40) {   
      digitalWrite(RELAY8, !digitalRead(RELAY8));
      sendStatus8 = true;
    } 
    count8=0;
  }
}

void checkConnection() {
  if (WiFi.status() == WL_CONNECTED)  {
    if (mqttClient.connected()) {
      Serial.println("mqtt broker connection . . . . . . . . . . OK");
    } 
    else {
      Serial.println("mqtt broker connection . . . . . . . . . . LOST");
      requestRestart = true;
    }
  }
  else { 
    Serial.println("WiFi connection . . . . . . . . . . LOST");
    requestRestart = true;
  }
}

void checkStatus() {
  if (sendStatus1) {
    if(digitalRead(RELAY1) == LOW)  {
      mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "1off").set_retain().set_qos(1));
      Serial.println("Relay 1 . . . . . . . . . . . . . . . . . . OFF");
    } else {
    mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "1on").set_retain().set_qos(1));
    Serial.println("Relay 1 . . . . . . . . . . . . . . . . . . ON");
    }
    sendStatus1 = false;
  }
  if (sendStatus2) {
    if(digitalRead(RELAY2) == LOW)  {
      mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "2off").set_retain().set_qos(2));
      Serial.println("Relay 2 . . . . . . . . . . . . . . . . . . OFF");
    } else {
    mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "2on").set_retain().set_qos(2));
    Serial.println("Relay 2 . . . . . . . . . . . . . . . . . . ON");
    }
    sendStatus2 = false;
  }
  if (sendStatus3) {
    if(digitalRead(RELAY3) == LOW)  {
      mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "3off").set_retain().set_qos(3));
      Serial.println("Relay 3 . . . . . . . . . . . . . . . . . . OFF");
    } else {
    mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "3on").set_retain().set_qos(3));
    Serial.println("Relay 3 . . . . . . . . . . . . . . . . . . ON");
    }
    sendStatus3 = false;
  }
  if (sendStatus4) {
    if(digitalRead(RELAY4) == LOW)  {
      mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "4off").set_retain().set_qos(4));
      Serial.println("Relay 4 . . . . . . . . . . . . . . . . . . OFF");
    } else {
    mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "4on").set_retain().set_qos(4));
    Serial.println("Relay 4 . . . . . . . . . . . . . . . . . . ON");
    }
    sendStatus4 = false;
  }
  if (sendStatus5) {
    if(digitalRead(RELAY5) == LOW)  {
      mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "5off").set_retain().set_qos(5));
      Serial.println("Relay 5 . . . . . . . . . . . . . . . . . . OFF");
    } else {
    mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "5on").set_retain().set_qos(5));
    Serial.println("Relay 5 . . . . . . . . . . . . . . . . . . ON");
    }
    sendStatus5 = false;
  }  
  if (sendStatus6) {
    if(digitalRead(RELAY6) == LOW)  {
      mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "6off").set_retain().set_qos(6));
      Serial.println("Relay 6 . . . . . . . . . . . . . . . . . . OFF");
    } else {
    mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "6on").set_retain().set_qos(6));
    Serial.println("Relay 6 . . . . . . . . . . . . . . . . . . ON");
    }
    sendStatus6 = false;
  }  
  if (sendStatus7) {
    if(digitalRead(RELAY7) == LOW)  {
      mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "7off").set_retain().set_qos(7));
      Serial.println("Relay 7 . . . . . . . . . . . . . . . . . . OFF");
    } else {
    mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "7on").set_retain().set_qos(7));
    Serial.println("Relay 7 . . . . . . . . . . . . . . . . . . ON");
    }
    sendStatus7 = false;
  }  
  if (sendStatus8) {
    if(digitalRead(RELAY8) == LOW)  {
      mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "8off").set_retain().set_qos(8));
      Serial.println("Relay 8 . . . . . . . . . . . . . . . . . . OFF");
    } else {
    mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "8on").set_retain().set_qos(8));
    Serial.println("Relay 8 . . . . . . . . . . . . . . . . . . ON");
    }
    sendStatus8 = false;
  }  

  if (requestRestart) {
    blinkLED(LED, 400, 4);
    ESP.restart();
  }
}

void timedTasks() {
  if ((millis() > TTasks + (kUpdFreq*60000)) || (millis() < TTasks)) { 
    TTasks = millis();
    checkConnection();
  }
}

回复

使用道具 举报

31

主题

188

帖子

1798

积分

论坛技术达人

积分
1798
金钱
1590
HASS币
20

教程狂人论坛风云人物

发表于 2018-9-19 13:33:04 | 显示全部楼层
其实实现这个功能没必要单独写固件,随便刷个TASMOTA或者esp固件都可以。想添加几个按键都可以
回复

使用道具 举报

31

主题

188

帖子

1798

积分

论坛技术达人

积分
1798
金钱
1590
HASS币
20

教程狂人论坛风云人物

发表于 2018-9-19 13:34:52 | 显示全部楼层
倾微qiwe 发表于 2018-9-19 13:33
其实实现这个功能没必要单独写固件,随便刷个TASMOTA或者esp固件都可以。想添加几个按键都可以 ...

因为你看的这个教程毕竟已经一年多了
回复

使用道具 举报

11

主题

150

帖子

715

积分

高级会员

Rank: 4

积分
715
金钱
560
HASS币
20
 楼主| 发表于 2018-9-19 17:44:25 | 显示全部楼层
倾微qiwe 发表于 2018-9-19 13:34
因为你看的这个教程毕竟已经一年多了

我试试看
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-24 04:54 , Processed in 0.062941 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表