Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WiFiMulti.h>
  3. #include <PubSubClient.h>
  4. #include <OneWire.h>
  5. #include <DallasTemperature.h>
  6.  
  7. // not important config (not used anywhere)
  8. const char* nazwa = "iotHOME_esp8266_relay_temps";
  9. const char* wersja = "0.6.9";
  10.  
  11. // wifi config
  12. const char* ssid = "PanRouter";
  13. const char* password = "odjedendoosiem";
  14.  
  15. // mqtt config
  16. const char* mqtt_server = "192.168.1.11";
  17. const int mqtt_port = 1883;
  18.  
  19. // one wire config
  20. #define ONE_WIRE_BUS 5
  21. OneWire oneWire(ONE_WIRE_BUS);
  22. DallasTemperature sensors(&oneWire);
  23. DeviceAddress sensor1 = { 0x28, 0x73, 0x81, 0x3A, 0xA, 0x0, 0x0, 0xE0 }; // na zewnątrz
  24. //DeviceAddress sensor1 = { 0x28, 0xDA, 0xEF, 0x3A, 0xA, 0x0, 0x0, 0x99 }; // wewnątrz
  25.  
  26. long lastMsg = 0;
  27. char msg[50];
  28. int value = 0;
  29.  
  30. String topic[5];
  31. String topic_status[5];
  32. int pin[5];
  33. int howManyPins = 1;
  34.  
  35. int sensor = 0;
  36. int lastSensor = 0;
  37. boolean alarm_occured = false;
  38. const char* outlet1_state;
  39.  
  40. bool czy_przycisk = false;
  41.  
  42. ESP8266WiFiMulti wifiMulti;
  43. WiFiClient espClient;
  44. PubSubClient client(espClient);
  45.  
  46. void mqttAssign() {
  47. topic[0] = "home/outlets/outlet1";
  48. pin[0] = D5;
  49. }
  50.  
  51. void outputState() {
  52. if (alarm_occured == false) {
  53. if (digitalRead(pin[0]) == HIGH) {
  54. outlet1_state = "1";
  55. } else outlet1_state = "0";
  56. } else outlet1_state = "alarm";
  57.  
  58. client.publish("home/outlets/outlet1/state", outlet1_state);
  59. }
  60.  
  61. void getAlarms() {
  62. if (alarm_occured == false) {
  63. delay(100);
  64. if (analogRead(A0) > 800) {
  65. client.publish("home/outlets/outlet1/alarm", "Circuit breaker has been tripped.");
  66. Serial.println("Circuit breaker has been tripped.");
  67. alarm_occured = true;
  68. }
  69. } else if (analogRead(A0) < 800) alarm_occured = false;
  70. delay(100);
  71. }
  72.  
  73. // funkcje do czujników i innych rzeczy
  74. void getTemperature() {
  75. float temp_1;
  76. char temp_1_string[7];
  77.  
  78. sensors.requestTemperatures(); // Send the command to get temperatures
  79.  
  80. temp_1 = sensors.getTempC(sensor1);
  81.  
  82. dtostrf(temp_1, 2, 2, temp_1_string);
  83.  
  84. Serial.println(temp_1_string);
  85. client.publish("home/temps/outside", temp_1_string);
  86. }
  87. // koniec
  88.  
  89. void mqttSubscribe() {
  90. for (int i = 0; i < howManyPins; i++) {
  91. char mqtt_topic[40];
  92. topic[i].toCharArray(mqtt_topic, 40);
  93. client.subscribe(mqtt_topic);
  94. }
  95. }
  96.  
  97. void mqttAction(char* topic_received, char* payload) {
  98. for (int i = 0; i < howManyPins; i++) {
  99. char mqtt_topic[40];
  100. topic[i].toCharArray(mqtt_topic, 40);
  101. if (topic[i] == String(topic_received)) {
  102. if ((char)payload[0] == '1') {
  103. digitalWrite(pin[i], HIGH);
  104. }
  105. else if ((char)payload[0] == '0') {
  106. digitalWrite(pin[i], LOW);
  107. }
  108. }
  109. }
  110. }
  111.  
  112. void callback(char* topic, byte* payload, unsigned int length) {
  113. char payload_all[30];
  114. Serial.print("Message arrived [");
  115. Serial.print(topic);
  116. Serial.print("] ");
  117. for (int i = 0; i < length; i++) {
  118. Serial.print((char)payload[i]);
  119. payload_all[i] = + payload[i];
  120. }
  121. Serial.println();
  122. mqttAction(topic, payload_all);
  123. }
  124.  
  125. void reconnect() {
  126. while (!client.connected()) {
  127. Serial.println("Attempting MQTT connection...");
  128. String clientId = nazwa;
  129. if (client.connect(clientId.c_str())) {
  130. Serial.println("Connected!");
  131.  
  132. mqttSubscribe();
  133.  
  134. } else {
  135. delay(5000);
  136. }
  137. }
  138. }
  139.  
  140. void przycisk() {
  141. if (digitalRead(D6) == LOW) {
  142. delay(200);
  143. if (digitalRead(D6) == LOW) {
  144. if (czy_przycisk == false) {
  145. client.publish("home/outlets/outlet1","1");
  146. czy_przycisk = true;
  147. }
  148. else {
  149. client.publish("home/outlets/outlet1","0");
  150. czy_przycisk = false;
  151. }
  152. }
  153. }
  154. }
  155.  
  156. void setup() {
  157. mqttAssign();
  158. pinMode(pin[0], OUTPUT);
  159. pinMode(A0,INPUT);
  160. pinMode(D6, INPUT_PULLUP);
  161.  
  162. Serial.begin(115200);
  163. WiFi.mode(WIFI_STA);
  164. wifiMulti.addAP(ssid, password);
  165. while (wifiMulti.run() != WL_CONNECTED) delay(500);
  166. Serial.println(WiFi.localIP());
  167.  
  168. // one wire temperature sensors
  169. sensors.begin();
  170.  
  171. client.setServer(mqtt_server, mqtt_port);
  172. client.setCallback(callback);
  173. }
  174.  
  175. void loop() {
  176. if (!client.connected()) {
  177. reconnect();
  178. }
  179. client.loop();
  180. przycisk();
  181. getAlarms();
  182.  
  183. // do every 5 seconds
  184. long now = millis();
  185. if (now - lastMsg > 5000) {
  186. lastMsg = now;
  187. getTemperature();
  188. outputState();
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement