Advertisement
Guest User

Untitled

a guest
Mar 8th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. switch 1:
  2. - platform: mqtt
  3. name: "1.Radiator IR"
  4. state_topic: "/Nicolina/switchConfirm10/"
  5. command_topic: "/Nicolina/switch10/"
  6. payload_on: "1"
  7. payload_off: "0"
  8. qos: 0
  9. retain: true
  10. switch:
  11.  
  12. sensor:
  13. - platform: yr
  14. - platform: mqtt
  15. state_topic: 'ha/temperature1'
  16. name: 'Temp DHT22'
  17. unit_of_measurement: '°C'
  18. value_template: '{{ value_json.temperature }}'
  19.  
  20.  
  21. pt cod din attach ai nevoie de fisierele astea:
  22.  
  23. #include <ESP8266WiFi.h>
  24. #include <PubSubClient.h>
  25. #include <DallasTemperature.h>
  26. #include <ArduinoJson.h>
  27.  
  28. gasesti ultimele versiuni cautand pe google esp8266 si numele. apoi le descarci de pe github si le import in soft ul Arduino IDE inainte sa dai push la cod. pffff tu ai mai lucrat cu Arduino inainte deci stii.... eu nu stiam.
  29.  
  30.  
  31. ==============================================================================================
  32. ESP8266 Temp + MQ5 Sensor template.txt
  33. ===============================================================================================
  34.  
  35. #include <ESP8266WiFi.h>
  36.  
  37. #include <PubSubClient.h>
  38.  
  39. //#include <OneWire.h>
  40.  
  41. #include <DallasTemperature.h>
  42.  
  43. #include <ArduinoJson.h>
  44.  
  45. // Data wire is plugged into pin 2 on the Arduino
  46. #define ONE_WIRE_BUS 5
  47.  
  48. // Setup a oneWire instance to communicate with any OneWire devices
  49. // (not just Maxim/Dallas temperature ICs)
  50. OneWire oneWire(ONE_WIRE_BUS);
  51.  
  52. DallasTemperature sensors(&oneWire);
  53.  
  54. // Update these with values suitable for your network.
  55. const char* ssid = "WIFI";
  56. const char* password = "WIFIpassword";
  57. const char* mqtt_server = "HA-Server-IP-address";
  58. const char* mqtt_client_id = "Client-ID-Unique-per-device";
  59. const char* mqtt_user = "Username (mine-is-homeassistant)";
  60. const char* mqtt_password = "password4HA";
  61. const char* mqtt_sensor_topic = "weather/station1";
  62. const char* mqtt_sensor_topic_gas = "weather/gas";
  63.  
  64. WiFiClient espClient;
  65. PubSubClient client(espClient);
  66. long lastMsg = 0;
  67. float temp = 0;
  68. int inPin = 5; // DHT - D1/GPIO5
  69.  
  70. void setup_wifi() {
  71. delay(10);
  72. // We start by connecting to a WiFi network
  73. Serial.println();
  74. Serial.print("Connecting to ");
  75. Serial.println(ssid);
  76. WiFi.begin(ssid, password);
  77.  
  78. while (WiFi.status() != WL_CONNECTED)
  79. {
  80. delay(500);
  81. Serial.print(".");
  82. }
  83. Serial.println("");
  84. Serial.println("WiFi connected");
  85. Serial.println("IP address: ");
  86. Serial.println(WiFi.localIP());
  87. }
  88.  
  89. void reconnect() {
  90. // Loop until we're reconnected
  91. while (!client.connected()) {
  92. Serial.print("Attempting MQTT connection...");
  93. // Attempt to connect
  94. if (client.connect("mqtt_client_id, mqtt_user, mqtt_password")) {
  95. Serial.println("connected");
  96. } else {
  97. Serial.print("failed, rc=");
  98. Serial.print(client.state());
  99. Serial.println(" try again in 5 seconds");
  100. // Wait 5 seconds before retrying
  101. delay(5000);
  102. }
  103. }
  104. }
  105.  
  106. void setup()
  107. {
  108. Serial.begin(115200);
  109. setup_wifi();
  110. client.setServer(mqtt_server, 1883);
  111. pinMode(inPin, INPUT);
  112. sensors.begin();
  113. }
  114.  
  115. String sPayload;
  116. char* cPayload;
  117. String sPayload_gas;
  118. char* cPayload_gas;
  119.  
  120. void loop()
  121. {
  122.  
  123. if (!client.connected()) {
  124. reconnect();
  125. }
  126. client.loop();
  127.  
  128. long now = millis();
  129. if (now - lastMsg > 6000) {
  130. lastMsg = now;
  131. sensors.setResolution(12);
  132. sensors.requestTemperatures(); // Send the command to get temperatures
  133. temp = sensors.getTempCByIndex(0);
  134. Serial.println("");
  135. if((temp > -20) && (temp <60))
  136. {
  137. StaticJsonBuffer<200> jsonBuffer;
  138. JsonObject& payload = jsonBuffer.createObject();
  139. payload["temperature"] = temp;
  140.  
  141. sPayload = "";
  142. payload.printTo(sPayload);
  143. cPayload = &sPayload[0u];
  144. client.publish(mqtt_sensor_topic, cPayload, true);
  145. // Now we can publish stuff!
  146. Serial.print(F("\nPublishing "));
  147. Serial.print(cPayload);
  148. Serial.print("...");
  149. }
  150. }
  151. float sensor_volt;
  152. float sensorValue;
  153.  
  154. sensorValue = analogRead(A0);
  155. sensor_volt = sensorValue/1024*5.0;
  156.  
  157. StaticJsonBuffer<200> jsonBuffer;
  158. JsonObject& payload_gas = jsonBuffer.createObject();
  159. payload_gas["sensorValue"] = sensor_volt;
  160. sPayload_gas = "";
  161. payload_gas.printTo(sPayload);
  162. cPayload_gas = &sPayload_gas[0u];
  163.  
  164. Serial.print("sensor_volt = ");
  165. Serial.print(sensor_volt);
  166. Serial.println("V");
  167. client.publish(mqtt_sensor_topic_gas, cPayload_gas, true);
  168. Serial.print(F("\nPublishing "));
  169. Serial.print(cPayload_gas);
  170. Serial.print("...");
  171. delay(1000);
  172. }
  173.  
  174.  
  175. =====================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement