Advertisement
Guest User

Untitled

a guest
Feb 6th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>  // Include the Wi-Fi library
  2. #include <PubSubClient.h> // Include the MQTT library
  3. #include <Ticker.h>       // Include timer library
  4. #include <FastLED.h>
  5.  
  6. Ticker secondTick;
  7. WiFiClient espClient;
  8.  
  9. //IPAddress brokerAddress();
  10. IPAddress brokerAddress();
  11.  
  12. //#define NUM_LEDS 2
  13. //CRGB leds[NUM_LEDS];
  14.  
  15. volatile int watchdogCount = 0;
  16. const char* ssid     = "";
  17. const char* password = "";
  18. const char* clientID = "";
  19. const char* brokerUsername = "";
  20. const char* brokerPassword = "";
  21.  
  22. void callback(char* topic, byte* payload, unsigned int length) {
  23.   Serial.printf("Message recieved on topic: %s!\r\n", topic);
  24.  
  25.   for (int i = 0; i < length; i++) {
  26.  
  27.     char iteratedChar = (char) payload[i];
  28.  
  29.     Serial.print(iteratedChar);
  30.  
  31.     if (iteratedChar == '1') {
  32.       digitalWrite(4, HIGH);
  33.     }
  34.  
  35.     if (iteratedChar == '0') {
  36.       digitalWrite(4, LOW);
  37.     }
  38.   }
  39. }
  40.  
  41. PubSubClient client(brokerAddress, 18475, callback, espClient);
  42.  
  43. void setup() {
  44.   Serial.begin(115200); // Start the Serial communication to send messages to the computer
  45.   secondTick.attach(1, ISRwatchdog); // Set the watchdog bite checker
  46.   delay(10);
  47.  
  48.   while (WiFi.status() != WL_CONNECTED) {
  49.     connectToWiFi();
  50.   }
  51.  
  52.   connectToBroker(clientID, brokerUsername, brokerPassword);
  53.   subscribeToTopic("/lights");
  54.  
  55.   pinMode(5, OUTPUT);
  56. //  pinMode(5, INPUT);
  57.  
  58. //  FastLED.addLeds<WS2812B, 5>(leds, NUM_LEDS);
  59. }
  60.  
  61. void connectToWiFi() {
  62.   feedWatchdog();
  63.   WiFi.begin(ssid, password); // Connect to the Wi-Fi network
  64.   Serial.printf("Connecting to Wi-Fi network: %s ", ssid);
  65.  
  66.   while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
  67.     delay(1000);
  68.     Serial.print(".");
  69.   }
  70.  
  71.   Serial.println("\nConnection with Wi-Fi established!");
  72.   Serial.printf("IP address: %s\n", (char*) WiFi.localIP().toString().c_str());
  73. }
  74.  
  75. void connectToBroker(const char* clientID, const char* brokerUsername, const char* brokerPassword) {
  76.   feedWatchdog();
  77.  
  78.   while (!client.connected()) {
  79.     Serial.println("Connecting to MQTT...");
  80.  
  81.     if (client.connect(clientID, brokerUsername, brokerPassword)) {
  82.  
  83.       Serial.println("Succesfully connected with broker!");
  84.  
  85.     } else {
  86.  
  87.       Serial.print("Failed to connect with the broker, current state: ");
  88.       Serial.print(client.state());
  89.       delay(2000);
  90.  
  91.     }
  92.   }
  93. }
  94.  
  95. void subscribeToTopic(char* topic) {
  96.   feedWatchdog();
  97.  
  98.   Serial.printf("Subscribing to topic: %s ..\r\n", topic);
  99.   if (client.subscribe(topic)) {
  100.     Serial.printf("Successfully subscribed to topic: %s!", topic);
  101.   } else {
  102.     Serial.printf("Failed subscribing to topic: %s", topic);
  103.     subscribeToTopic(topic);
  104.   }
  105. }
  106.  
  107. bool messagePublished = false;
  108.  
  109. void loop() {
  110.   feedWatchdog();
  111.  
  112. //  leds[0] = CRGB::White; FastLED.show(); delay(30);
  113. //  leds[1] = CRGB::Red; FastLED.show(); delay(30);
  114.   digitalWrite(5, HIGH);
  115.   delay(1000);
  116.   digitalWrite(5, LOW);
  117.   delay(1000);
  118.   Serial.print("Next loop iteration ..");
  119.  
  120.   // Check for incoming message and process them
  121. //  client.loop();
  122. //
  123. //  int pirVal = digitalRead(5);
  124. //
  125. //  if (pirVal == LOW && messagePublished == true) {
  126. //    client.publish("/lights", "0");
  127. //    messagePublished = false;
  128. //  }
  129. //
  130. //  if (pirVal == HIGH && messagePublished == false) {
  131. //    client.publish("/lights", "1");
  132. //    messagePublished = true;
  133. //  }
  134.  
  135. }
  136.  
  137. void startPartyMode() {
  138.  
  139. }
  140.  
  141. void stopPartyMode() {
  142.  
  143. }
  144.  
  145. void ISRwatchdog() {
  146.   watchdogCount++;
  147.   if (watchdogCount > 5) {
  148.     Serial.println();
  149.     Serial.println("The watchdog bites!");
  150.     ESP.reset();
  151.   }
  152. }
  153.  
  154. void feedWatchdog() {
  155.   watchdogCount = 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement