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

 找回密码
 立即注册
查看: 7426|回复: 15

[已解决] 按照老高的帖子刷开关出错,请教

[复制链接]

19

主题

689

帖子

2989

积分

金牌会员

Rank: 6Rank: 6

积分
2989
金钱
2300
HASS币
0
发表于 2017-8-9 23:30:09 | 显示全部楼层 |阅读模式
本帖最后由 gzroc 于 2017-8-10 19:56 编辑

出现这个是怎么回事?
image.jpeg
回复

使用道具 举报

123

主题

4629

帖子

1万

积分

管理员

囧死

Rank: 9Rank: 9Rank: 9

积分
16079
金钱
11365
HASS币
45
发表于 2017-8-10 19:39:29 | 显示全部楼层
线接错了,或没有正确进入刷机模式。

把rx和tx调换下位置,一定要在插入usb前,按住微动开关。
回复

使用道具 举报

19

主题

689

帖子

2989

积分

金牌会员

Rank: 6Rank: 6

积分
2989
金钱
2300
HASS币
0
 楼主| 发表于 2017-8-10 19:56:08 | 显示全部楼层
Jones 发表于 2017-8-10 19:39
线接错了,或没有正确进入刷机模式。

把rx和tx调换下位置,一定要在插入usb前,按住微动开关。 ...

感谢版主,已经搞定
回复

使用道具 举报

123

主题

4629

帖子

1万

积分

管理员

囧死

Rank: 9Rank: 9Rank: 9

积分
16079
金钱
11365
HASS币
45
发表于 2017-8-10 20:02:03 | 显示全部楼层
gzroc 发表于 2017-8-10 19:56
感谢版主,已经搞定

搞定编辑下帖子,改为已解决吧
回复

使用道具 举报

19

主题

689

帖子

2989

积分

金牌会员

Rank: 6Rank: 6

积分
2989
金钱
2300
HASS币
0
 楼主| 发表于 2017-8-10 20:18:51 | 显示全部楼层
Jones 发表于 2017-8-10 20:02
搞定编辑下帖子,改为已解决吧

已编辑。。。。。懂的

评分

参与人数 1金钱 +6 收起 理由
+ 6 666!

查看全部评分

回复

使用道具 举报

0

主题

5

帖子

24

积分

新手上路

Rank: 1

积分
24
金钱
19
HASS币
0
发表于 2017-8-11 01:03:03 | 显示全部楼层
本帖最后由 lwh1993 于 2017-8-11 01:04 编辑
Jones 发表于 2017-8-10 20:02
搞定编辑下帖子,改为已解决吧

求助...我有同样的问题..我轻按了再插USB..之后还是出现问题.
线的顺序由按键  往下数 是插了3V3, RXD, TXD, GND
12121.jpg



回复

使用道具 举报

123

主题

4629

帖子

1万

积分

管理员

囧死

Rank: 9Rank: 9Rank: 9

积分
16079
金钱
11365
HASS币
45
发表于 2017-8-11 01:39:15 | 显示全部楼层
lwh1993 发表于 2017-8-11 01:03
求助...我有同样的问题..我轻按了再插USB..之后还是出现问题.
线的顺序由按键  往下数 是插了3V3, RXD, T ...

同样处理。
回复

使用道具 举报

0

主题

5

帖子

24

积分

新手上路

Rank: 1

积分
24
金钱
19
HASS币
0
发表于 2017-8-12 20:04:47 | 显示全部楼层

感谢 已经成功刷到
另外再问问 我到老高最后那一步 可是homeassistant 的介面没显示出SONOFF的开关..
重新输入过资料 再刷一次 还是找不到
之后看到 http://www.instructables.com/id/ ... me-Assistant-and-M/
可是用WIFI也找不到...是代表我SONOFF的WIFI部件报废吗?
回复

使用道具 举报

123

主题

4629

帖子

1万

积分

管理员

囧死

Rank: 9Rank: 9Rank: 9

积分
16079
金钱
11365
HASS币
45
发表于 2017-8-12 21:49:42 | 显示全部楼层
lwh1993 发表于 2017-8-12 20:04
感谢 已经成功刷到
另外再问问 我到老高最后那一步 可是homeassistant 的介面没显示出SONOFF的开关..
重 ...

能刷成功说明sonoff没有损坏,hass里没有,可能是你配置的问题,可以贴出你的配置文件,让大家帮你看下。
回复

使用道具 举报

0

主题

5

帖子

24

积分

新手上路

Rank: 1

积分
24
金钱
19
HASS币
0
发表于 2017-8-13 00:26:44 | 显示全部楼层
先再次感谢 JON大大的帮助
homeassistant   configuration.yaml
homeassistant:
  # Name of the location where Home Assistant is running
  name: Home
  # Location required to calculate the time the sun rises and sets
  latitude: 22.291
  longitude: 114.15
  # Impacts weather/sunrise data (altitude above sea level in meters)
  elevation: 0
  # metric for Metric, imperial for Imperial
  unit_system: metric
  # Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
  time_zone: Asia/Hong_Kong
  # customize own chinese name
  customize:
  switch_sonoff:
   friendly_name:light
  
# Show links to resources in log and frontend
introduction:

# Enables the frontend
frontend:

# Enables configuration UI
config:


http:
  # Uncomment this to add a password (recommended!)
  # api_password: PASSWORD
  # Uncomment this if you are using SSL/TLS, running in Docker container, etc.
  # base_url: example.duckdns.org:8123

# Checks for available updates
# Note: This component will send some information about your system to
# the developers to assist with development of Home Assistant.
# For more information, please see:
# https://home-assistant.io/blog/2016/10/25/explaining-the-updater/
updater:
  # Optional, allows Home Assistant developers to focus on popular components.
  # include_used_components: true

# Discover some devices automatically
discovery:

# Allows you to issue voice commands from the frontend in enabled browsers
conversation:

# Enables support for tracking state changes over time
history:

# View all events in a logbook
logbook:

# Track the sun
sun:

# Weather prediction

mqtt:
broker: 192.168.1.105
port: 1883
client_id: home-assistant-1
keepalibe: 60
username: username
password: 1qaz2wsx
protocol: 2.0

#sonoff switch fish tank light
switch:
platform: mqtt
name: "fish_tank_light"
state_topic: "home/sonoff/fish_tank_light/stat"
command_topic: "home/sonoff/fish_tank_light"
qos:1
payload_on: "on"
payload_off: "off"
retain: true

sensor:
  - platform: yr

# Text to speech
tts:
  - platform: google

group: !include groups.yaml
automation: !include automations.yaml
mosquitto连线测试
12122351.jpg

Sonoff刷入的INO
#include <FS.h>                   //this needs to be first, or it all crashes and burns...
#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager
#include <ubSubClient.h>

#define D0              16        //WAKE  =>  16
#define D1              5         //IOS   =>  5
#define D2              4         //      =>  4
#define D3              0         //      =>  0
#define D4              2         //      =>  2
#define D5              14        //CLK   =>  14
#define D6              12        //MISO  =>  12
#define D7              13        //MOSI  =>  13
#define D8              15        //CS    =>  15
#define D9              3         //RX    =>  3

#define RELAY_PIN       D6
#define TOGGLE_PIN      D3
#define LED_PIN         D7

#define MQTT_VERSION    MQTT_VERSION_2_0
const PROGMEM uint16_t  MQTT_SERVER_PORT          = 1883;
const PROGMEM char*     MQTT_CLIENT_ID            = "sonoff";
const PROGMEM char*     MQTT_USER                 = "username";
const PROGMEM char*     MQTT_PASSWORD             = "1qaz2wsx";
const char*             MQTT_LIGHT_STATE_TOPIC    = "room/sonoff/status";
const char*             MQTT_LIGHT_COMMAND_TOPIC  = "room/sonoff/switch";
const char*             LIGHT_ON                  = "ON";
const char*             LIGHT_OFF                 = "OFF";
boolean                 m_light_state             = false;
boolean                 fDebug                    = true;
long                    lastMsg                   = 0;
long unsigned int       pause                     = 5000;
int                     chk;
int                     TOGGLE_COUNT              = 0;

WiFiClient espClient;
PubSubClient client(espClient);
IPAddress MQTT_SERVER_IP;

void ISR_Reset(){
  WiFiManager wifiManager;
  wifiManager.resetSettings();
  ESP.reset();  
}

void setup() {
  ConfigHardware();
  if ( fDebug ) {
    Serial.println();
    Serial.println("Ver: 1.3");
  }
  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;
  if ( fDebug == true ) {
    wifiManager.setDebugOutput(true);
  }else{
    wifiManager.setDebugOutput(false);
  }
  //exit after config instead of connecting
  //wifiManager.setBreakAfterConfig(true);
  //reset settings - for testing
  //wifiManager.resetSettings();

  //tries to connect to last known settings
  //if it does not connect it starts an access point with the specified name
  //here  "AutoConnectAP" with password "password"
  //and goes into a blocking loop awaiting configuration
  if (!wifiManager.autoConnect("ThingMaBob")) {
    //Debugger "failed to connect, we should reset as see if it connects",NULL;
    delay(3000);
    ESP.reset();
  }
  //if you get here you have connected to the WiFi
  // init the MQTT connection
  WiFi.hostByName("home-assistant-1", MQTT_SERVER_IP );
  if ( fDebug ) {
    Serial.print("MQTT_SERVER_IP: ");
    Serial.println(MQTT_SERVER_IP);
    client.setServer(MQTT_SERVER_IP , MQTT_SERVER_PORT);
    client.setCallback(callback);
  }
}

void loop() {
  int mqttState = client.state();
  if (mqttState != 0){
    Serial.print("State: ");
    Serial.println(mqttState);
  }
  if (!client.connected()) {
    reconnect();
  }
  if (digitalRead(TOGGLE_PIN) == LOW) {
      m_light_state = !m_light_state;
      setLightState();
  }
  delay(250);
}

void ConfigHardware(){

  if ( fDebug ) {
    Serial.begin(115200);
    Serial.println();
    Serial.print("Service started");
    Serial.println();
  }
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(TOGGLE_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
  client.setCallback(callback);
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    if ( fDebug ) {Serial.print("INFO: Attempting MQTT connection...");}
    // Attempt to connect
    if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
      if ( fDebug ) {Serial.println("INFO: connected");}
      // Once connected, publish an announcement...
      publishLightState();
      // ... and resubscribe
      client.subscribe(MQTT_LIGHT_COMMAND_TOPIC);
    } else {
      if ( fDebug ) {
        Serial.print("ERROR: failed, rc=");
        Serial.print(client.state());
        Serial.println("DEBUG: try again in 5 seconds");
      }
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
// function called to publish the state of the light (on/off)
void publishLightState() {
  if (m_light_state) {
    client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_ON, true);
  } else {
    client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_OFF, true);
  }
  client.loop();
}

// function called to turn on/off the light
void setLightState() {
  if (m_light_state) {
    digitalWrite(RELAY_PIN, HIGH);
    if ( fDebug ) {Serial.println("INFO: Turn light on...");}
    digitalWrite(LED_PIN,LOW);
  } else {
    digitalWrite(RELAY_PIN, LOW);
    if ( fDebug ) {Serial.println("INFO: Turn light off...");}
    digitalWrite(LED_PIN,HIGH);
  }
  publishLightState();
  client.loop();
}

// function called when a MQTT message arrived
void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
  // concat the payload into a string
  if ( fDebug ) {
    Serial.println("Callback");
    Serial.print("ayload: ");
  }
  String payload;
  for (uint8_t i = 0; i < p_length; i++) {
    payload.concat((char)p_payload);
  }
  if ( fDebug ) {Serial.println(payload);}
  // handle message topic
  if (String(MQTT_LIGHT_COMMAND_TOPIC).equals(p_topic)) {
    // test if the payload is equal to "ON" or "OFF"
    if (payload.equals(String(LIGHT_ON))) {
      if (m_light_state != true) {
        m_light_state = true;
        setLightState();
      }
    } else if (payload.equals(String(LIGHT_OFF))) {
      if (m_light_state != false) {
        m_light_state = false;
        setLightState();
      }
    }
    publishLightState();  
  }
}



回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-5-15 03:01 , Processed in 0.522002 second(s), 35 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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