Advertisement
Guest User

Untitled

a guest
Jan 12th, 2019
1,082
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 <ESP8266mDNS.h>
  3. #include <WiFiUdp.h>
  4. #include <ArduinoOTA.h>
  5. #include <PubSubClient.h>
  6.  
  7. #ifndef STASSID
  8. #define STASSID "XXXX"
  9. #define STAPSK "zajamilajanew"
  10. #endif
  11.  
  12. const char* ssid = STASSID;
  13. const char* password = STAPSK;
  14.  
  15. const char* mqtt_server = "192.168.1.110";
  16. const char* mqtt_client_name = "Doorbell";
  17. const char* mqtt_user = "";
  18. const char* mqtt_pass = "";
  19.  
  20. WiFiClient espClient;
  21. PubSubClient client(espClient);
  22. long lastMsg = 0;
  23. char msg[50];
  24. int value = 0;
  25.  
  26.  
  27. int inputPin = D4; // LED connected to digital pin D4
  28. int valPin = 0; // variable to store the read value
  29.  
  30. void callback(char* topic, byte* payload, unsigned int length) {
  31. Serial.print("Message arrived [");
  32. Serial.print(topic);
  33. Serial.print("] ");
  34. for (int i = 0; i < length; i++) {
  35. Serial.print((char)payload[i]);
  36. }
  37. Serial.println();
  38.  
  39. // Switch on the LED if an 1 was received as first character
  40. if ((char)payload[0] == '1') {
  41. Serial.print("Payload == 1");
  42. //digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
  43. // but actually the LED is on; this is because
  44. // it is active low on the ESP-01)
  45. } else {
  46. Serial.print("Payload = 0");
  47. //digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
  48. }
  49.  
  50. }
  51.  
  52. void startRinging()
  53. {
  54. Serial.println("Got Ringing Status");
  55. client.publish("doorbell/status", "Ringing");
  56. }
  57.  
  58. void stopRinging()
  59. {
  60. Serial.println("Not Ringing");
  61. client.publish("doorbell/status", "Off");
  62. }
  63.  
  64. void reconnect() {
  65. // Loop until we're reconnected
  66. while (!client.connected()) {
  67. Serial.print("Attempting MQTT connection...");
  68. if (client.connect(mqtt_client_name, mqtt_user, mqtt_pass, "doorbell/status", 0, 0, "Dead Somewhere"))
  69. {
  70. client.publish("doorbell/status", "Online");
  71. Serial.println("Doorbel is Online");
  72. // ... and resubscribe
  73. client.subscribe("doorbell/commands");
  74. }
  75. else {
  76. Serial.print("failed, rc=");
  77. Serial.print(client.state());
  78. Serial.println(" try again in 5 seconds");
  79. // Wait 5 seconds before retrying
  80. delay(5000);
  81. }
  82. }
  83. }
  84.  
  85. void setup() {
  86. Serial.begin(115200);
  87. Serial.println("Booting");
  88. WiFi.hostname(mqtt_client_name);
  89. WiFi.mode(WIFI_STA);
  90. WiFi.begin(ssid, password);
  91. while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  92. Serial.println("Connection Failed! Rebooting...");
  93. delay(5000);
  94. ESP.restart();
  95. }
  96.  
  97. pinMode(inputPin, INPUT);
  98.  
  99. // Port defaults to 8266
  100. // ArduinoOTA.setPort(8266);
  101.  
  102. // Hostname defaults to esp8266-[ChipID]
  103. ArduinoOTA.setHostname("inbox.lv");
  104.  
  105. // No authentication by default
  106. ArduinoOTA.setPassword("admin");
  107.  
  108. // Password can be set with it's md5 value as well
  109. // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  110. // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
  111.  
  112. ArduinoOTA.onStart([]() {
  113. String type;
  114. if (ArduinoOTA.getCommand() == U_FLASH) {
  115. type = "sketch";
  116. } else { // U_SPIFFS
  117. type = "filesystem";
  118. }
  119.  
  120. // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
  121. Serial.println("Start updating " + type);
  122. });
  123. ArduinoOTA.onEnd([]() {
  124. Serial.println("\nEnd");
  125. });
  126. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  127. Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  128. });
  129. ArduinoOTA.onError([](ota_error_t error) {
  130. Serial.printf("Error[%u]: ", error);
  131. if (error == OTA_AUTH_ERROR) {
  132. Serial.println("Auth Failed");
  133. } else if (error == OTA_BEGIN_ERROR) {
  134. Serial.println("Begin Failed");
  135. } else if (error == OTA_CONNECT_ERROR) {
  136. Serial.println("Connect Failed");
  137. } else if (error == OTA_RECEIVE_ERROR) {
  138. Serial.println("Receive Failed");
  139. } else if (error == OTA_END_ERROR) {
  140. Serial.println("End Failed");
  141. }
  142. });
  143. ArduinoOTA.begin();
  144. Serial.println("Ready");
  145. Serial.print("IP address: ");
  146. Serial.println(WiFi.localIP());
  147. client.setServer(mqtt_server, 1883);
  148. client.setCallback(callback);
  149. }
  150.  
  151. void loop() {
  152.  
  153. Serial.print("State of inputPin: ");
  154. Serial.println(digitalRead(inputPin));
  155. delay(1000);
  156.  
  157.  
  158. ArduinoOTA.handle();
  159. if (!client.connected()) {
  160. reconnect();
  161. }
  162. client.loop();
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement