Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266mDNS.h>
- #include <WiFiUdp.h>
- #include <ArduinoOTA.h>
- #include <PubSubClient.h>
- #ifndef STASSID
- #define STASSID "XXXX"
- #define STAPSK "zajamilajanew"
- #endif
- const char* ssid = STASSID;
- const char* password = STAPSK;
- const char* mqtt_server = "192.168.1.110";
- const char* mqtt_client_name = "Doorbell";
- const char* mqtt_user = "";
- const char* mqtt_pass = "";
- WiFiClient espClient;
- PubSubClient client(espClient);
- long lastMsg = 0;
- char msg[50];
- int value = 0;
- int inputPin = D4; // LED connected to digital pin D4
- int valPin = 0; // variable to store the read value
- void callback(char* topic, byte* payload, unsigned int length) {
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- for (int i = 0; i < length; i++) {
- Serial.print((char)payload[i]);
- }
- Serial.println();
- // Switch on the LED if an 1 was received as first character
- if ((char)payload[0] == '1') {
- Serial.print("Payload == 1");
- //digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
- // but actually the LED is on; this is because
- // it is active low on the ESP-01)
- } else {
- Serial.print("Payload = 0");
- //digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
- }
- }
- void startRinging()
- {
- Serial.println("Got Ringing Status");
- client.publish("doorbell/status", "Ringing");
- }
- void stopRinging()
- {
- Serial.println("Not Ringing");
- client.publish("doorbell/status", "Off");
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- if (client.connect(mqtt_client_name, mqtt_user, mqtt_pass, "doorbell/status", 0, 0, "Dead Somewhere"))
- {
- client.publish("doorbell/status", "Online");
- Serial.println("Doorbel is Online");
- // ... and resubscribe
- client.subscribe("doorbell/commands");
- }
- else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void setup() {
- Serial.begin(115200);
- Serial.println("Booting");
- WiFi.hostname(mqtt_client_name);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.waitForConnectResult() != WL_CONNECTED) {
- Serial.println("Connection Failed! Rebooting...");
- delay(5000);
- ESP.restart();
- }
- pinMode(inputPin, INPUT);
- // Port defaults to 8266
- // ArduinoOTA.setPort(8266);
- // Hostname defaults to esp8266-[ChipID]
- ArduinoOTA.setHostname("inbox.lv");
- // No authentication by default
- ArduinoOTA.setPassword("admin");
- // Password can be set with it's md5 value as well
- // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
- // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
- ArduinoOTA.onStart([]() {
- String type;
- if (ArduinoOTA.getCommand() == U_FLASH) {
- type = "sketch";
- } else { // U_SPIFFS
- type = "filesystem";
- }
- // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
- Serial.println("Start updating " + type);
- });
- ArduinoOTA.onEnd([]() {
- Serial.println("\nEnd");
- });
- ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
- Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
- });
- ArduinoOTA.onError([](ota_error_t error) {
- Serial.printf("Error[%u]: ", error);
- if (error == OTA_AUTH_ERROR) {
- Serial.println("Auth Failed");
- } else if (error == OTA_BEGIN_ERROR) {
- Serial.println("Begin Failed");
- } else if (error == OTA_CONNECT_ERROR) {
- Serial.println("Connect Failed");
- } else if (error == OTA_RECEIVE_ERROR) {
- Serial.println("Receive Failed");
- } else if (error == OTA_END_ERROR) {
- Serial.println("End Failed");
- }
- });
- ArduinoOTA.begin();
- Serial.println("Ready");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- client.setServer(mqtt_server, 1883);
- client.setCallback(callback);
- }
- void loop() {
- Serial.print("State of inputPin: ");
- Serial.println(digitalRead(inputPin));
- delay(1000);
- ArduinoOTA.handle();
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement