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

 找回密码
 立即注册
查看: 10568|回复: 34

ESPsonoff 加装DHT22 温湿度

[复制链接]

12

主题

397

帖子

2289

积分

金牌会员

Rank: 6Rank: 6

积分
2289
金钱
1892
HASS币
10
发表于 2017-5-21 14:56:58 | 显示全部楼层 |阅读模式
本帖最后由 iciness 于 2017-5-21 15:01 编辑

QQ截图20170521145324.jpg


/*

  Copyright (c) 2017 @KmanOz
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.

  ==============================================================================
  Changes in v1.01
  
    - Relay state now stored in EEPROM and will power up with last relay state
    - Remote ON & OFF via Wallswitch on pin 14. No debounce provided. Assumes
      operation via proper electrical wallswitch which has mechanical debounce.
  ==============================================================================

  **** USE THIS Firmware for: Sonof TH10/16 and additional wallswitch ****

*/

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

#define BUTTON          0                                    // (Don't Change for Sonoff TH Series)
#define RELAY           12                                   // (Don't Change for Sonoff TH Series)
#define LED             13                                   // (Don't Change for Sonoff TH Series)
#define WALLSWITCH      14                                   // (Don't Change for Sonoff TH Series)

#define MQTT_CLIENT     "Sonoff_Living_Room_v1.0p"           // mqtt client_id (Must be unique for each Sonoff)
#define MQTT_SERVER     "192.168.0.100"                      // mqtt server
#define MQTT_PORT       1883                                 // mqtt port
#define MQTT_TOPIC      "home/sonoff/living_room/1"          // mqtt topic (Must be unique for each Sonoff)
#define MQTT_USER       "user"                               // mqtt user
#define MQTT_PASS       "pass"                               // mqtt password

#define WIFI_SSID       "homewifi"                           // wifi ssid
#define WIFI_PASS       "homepass"                           // wifi password

#define VERSION    "\n\n----------------  Sonoff TH Powerpoint v1.01p  -----------------"

bool rememberRelayState = true;                              // If 'true' remembers the state of the relay before power loss.
bool requestRestart = false;                                 // (Do not Change)
bool sendStatus = false;                                     // (Do not Change)

int kUpdFreq = 1;                                            // Update frequency in Mintes to check for mqtt connection
int kRetries = 10;                                           // WiFi retry count. Increase if not connecting to router.
int wallSwitch = 1;                                          // (Do not Change)
int lastWallSwitch = 1;                                      // (Do not Change)
int lastRelayState;                                          // (Do not Change)

unsigned long TTasks;                                        // (Do not Change)
unsigned long count = 0;                                     // (Do not Change)

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

WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient, MQTT_SERVER, MQTT_PORT);
Ticker btn_timer;

void callback(const MQTT::Publish& pub) {
  if (pub.payload_string() == "stat") {
  }
  else if (pub.payload_string() == "on") {
    digitalWrite(RELAY, HIGH);
  }
  else if (pub.payload_string() == "off") {
    digitalWrite(RELAY, LOW);
  }
  else if (pub.payload_string() == "reset") {
    requestRestart = true;
  }
  sendStatus = true;
}

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(RELAY, OUTPUT);
  pinMode(BUTTON, INPUT);
  pinMode(WALLSWITCH, INPUT);
  digitalWrite(LED, HIGH);
  digitalWrite(RELAY, LOW);
  Serial.begin(115200);
  EEPROM.begin(8);
  lastRelayState = EEPROM.read(0);
  if (rememberRelayState && lastRelayState == 1) {
     digitalWrite(RELAY, HIGH);
  }
  btn_timer.attach(0.05, button);
  mqttClient.set_callback(callback);
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  Serial.println(VERSION);
  Serial.print("\nUnit ID: ");
  Serial.print("esp8266-");
  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();
  checkWallSwitch();
}

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 button() {
  if (!digitalRead(BUTTON)) {
    count++;
  } 
  else {
    if (count > 1 && count <= 40) {   
      digitalWrite(RELAY, !digitalRead(RELAY));
      sendStatus = true;
    } 
    else if (count >40){
      Serial.println("\n\nSonoff Rebooting . . . . . . . . Please Wait"); 
      requestRestart = true;
    } 
    count=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 (sendStatus) {
    if(digitalRead(RELAY) == LOW)  {
      if (rememberRelayState) {
        EEPROM.write(0, 0);
      }
      mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "off").set_retain().set_qos(1));
      Serial.println("Relay . . . . . . . . . . . . . . . . . . OFF");
    } else {
      if (rememberRelayState) {
        EEPROM.write(0, 1);
      }
      mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "on").set_retain().set_qos(1));
      Serial.println("Relay . . . . . . . . . . . . . . . . . . ON");
    }
    if (rememberRelayState) {
      EEPROM.commit();
    }
    sendStatus = false;
  }
  if (requestRestart) {
    blinkLED(LED, 400, 4);
    ESP.restart();
  }
}

void checkWallSwitch() {
  wallSwitch = digitalRead(WALLSWITCH);
  if (wallSwitch != lastWallSwitch) {
    digitalWrite(RELAY, !digitalRead(RELAY));
    sendStatus = true;
  }
  lastWallSwitch = wallSwitch;
}

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


温湿度传感器

Sonoff TH 10/16 -------------------------------

- platform: mqtt
  name: "Living Room Temp"
  state_topic: "home/sonoff/living_room/1/temp"
  qos: 1
  unit_of_measurement: "°C"
  value_template: "{{ value_json.Temp }}"

- platform: mqtt
  name: "Living Room Humidity"
  state_topic: "home/sonoff/living_room/1/temp"
  qos: 1
  unit_of_measurement: "%"
  value_template: "{{ value_json.Humidity }}"

Sonoff Pow -------------------------------------

- platform: mqtt
  name: "Living Room Power"
  state_topic: "home/sonoff/living_room/1/power"
  qos: 1
  unit_of_measurement: "W"
  value_template: "{{ value_json.Power }}"

- platform: mqtt
  name: "Living Room Voltage"
  state_topic: "home/sonoff/living_room/1/power"
  qos: 1
  unit_of_measurement: "V"
  value_template: "{{ value_json.Voltage }}"



开关

switch:
  platform: mqtt
  name: "Living Room"
  state_topic: "home/sonoff/living_room/1/stat"
  command_topic: "home/sonoff/living_room/1"
  qos: 1
  payload_on: "on"
  payload_off: "off"
  retain: true

回复

使用道具 举报

30

主题

997

帖子

4149

积分

论坛元老

Rank: 8Rank: 8

积分
4149
金钱
3147
HASS币
0

活跃会员

发表于 2017-5-21 16:59:44 | 显示全部楼层
谢谢分享!!!
回复

使用道具 举报

0

主题

34

帖子

254

积分

中级会员

Rank: 3Rank: 3

积分
254
金钱
220
HASS币
0
发表于 2017-5-21 17:36:30 | 显示全部楼层
感谢分享,大家都是好样的
回复

使用道具 举报

5

主题

253

帖子

1946

积分

金牌会员

Rank: 6Rank: 6

积分
1946
金钱
1693
HASS币
0
发表于 2017-5-21 18:10:54 | 显示全部楼层

感谢分享,大家都是好样的
回复

使用道具 举报

1

主题

190

帖子

1702

积分

金牌会员

Rank: 6Rank: 6

积分
1702
金钱
1512
HASS币
0
发表于 2017-5-21 19:54:41 | 显示全部楼层
这个可以有,感谢分享
回复

使用道具 举报

3

主题

95

帖子

669

积分

高级会员

Rank: 4

积分
669
金钱
574
HASS币
0
发表于 2017-5-21 20:31:31 | 显示全部楼层

谢谢分享,支持!
回复

使用道具 举报

3

主题

219

帖子

932

积分

高级会员

Rank: 4

积分
932
金钱
713
HASS币
0
发表于 2017-5-22 11:17:46 来自手机 | 显示全部楼层
谢谢分享!
回复

使用道具 举报

6

主题

484

帖子

4038

积分

论坛元老

Rank: 8Rank: 8

积分
4038
金钱
3554
HASS币
0
发表于 2017-5-22 16:12:45 | 显示全部楼层
请问第一段代码用什么软件烧写在哪个硬件上的?谢谢!
回复

使用道具 举报

3

主题

219

帖子

932

积分

高级会员

Rank: 4

积分
932
金钱
713
HASS币
0
发表于 2017-5-22 21:59:07 来自手机 | 显示全部楼层
都是高手啊
回复

使用道具 举报

1

主题

281

帖子

1224

积分

金牌会员

Rank: 6Rank: 6

积分
1224
金钱
942
HASS币
0
发表于 2017-5-25 06:19:37 来自手机 | 显示全部楼层
谢谢分享!
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-22 20:26 , Processed in 0.374732 second(s), 35 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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