Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h> // Include the Wi-Fi library
- #include <PubSubClient.h> // Include the MQTT library
- #include <Ticker.h> // Include timer library
- #include <FastLED.h>
- Ticker secondTick;
- WiFiClient espClient;
- //IPAddress brokerAddress();
- IPAddress brokerAddress();
- //#define NUM_LEDS 2
- //CRGB leds[NUM_LEDS];
- volatile int watchdogCount = 0;
- const char* ssid = "";
- const char* password = "";
- const char* clientID = "";
- const char* brokerUsername = "";
- const char* brokerPassword = "";
- void callback(char* topic, byte* payload, unsigned int length) {
- Serial.printf("Message recieved on topic: %s!\r\n", topic);
- for (int i = 0; i < length; i++) {
- char iteratedChar = (char) payload[i];
- Serial.print(iteratedChar);
- if (iteratedChar == '1') {
- digitalWrite(4, HIGH);
- }
- if (iteratedChar == '0') {
- digitalWrite(4, LOW);
- }
- }
- }
- PubSubClient client(brokerAddress, 18475, callback, espClient);
- void setup() {
- Serial.begin(115200); // Start the Serial communication to send messages to the computer
- secondTick.attach(1, ISRwatchdog); // Set the watchdog bite checker
- delay(10);
- while (WiFi.status() != WL_CONNECTED) {
- connectToWiFi();
- }
- connectToBroker(clientID, brokerUsername, brokerPassword);
- subscribeToTopic("/lights");
- pinMode(5, OUTPUT);
- // pinMode(5, INPUT);
- // FastLED.addLeds<WS2812B, 5>(leds, NUM_LEDS);
- }
- void connectToWiFi() {
- feedWatchdog();
- WiFi.begin(ssid, password); // Connect to the Wi-Fi network
- Serial.printf("Connecting to Wi-Fi network: %s ", ssid);
- while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
- delay(1000);
- Serial.print(".");
- }
- Serial.println("\nConnection with Wi-Fi established!");
- Serial.printf("IP address: %s\n", (char*) WiFi.localIP().toString().c_str());
- }
- void connectToBroker(const char* clientID, const char* brokerUsername, const char* brokerPassword) {
- feedWatchdog();
- while (!client.connected()) {
- Serial.println("Connecting to MQTT...");
- if (client.connect(clientID, brokerUsername, brokerPassword)) {
- Serial.println("Succesfully connected with broker!");
- } else {
- Serial.print("Failed to connect with the broker, current state: ");
- Serial.print(client.state());
- delay(2000);
- }
- }
- }
- void subscribeToTopic(char* topic) {
- feedWatchdog();
- Serial.printf("Subscribing to topic: %s ..\r\n", topic);
- if (client.subscribe(topic)) {
- Serial.printf("Successfully subscribed to topic: %s!", topic);
- } else {
- Serial.printf("Failed subscribing to topic: %s", topic);
- subscribeToTopic(topic);
- }
- }
- bool messagePublished = false;
- void loop() {
- feedWatchdog();
- // leds[0] = CRGB::White; FastLED.show(); delay(30);
- // leds[1] = CRGB::Red; FastLED.show(); delay(30);
- digitalWrite(5, HIGH);
- delay(1000);
- digitalWrite(5, LOW);
- delay(1000);
- Serial.print("Next loop iteration ..");
- // Check for incoming message and process them
- // client.loop();
- //
- // int pirVal = digitalRead(5);
- //
- // if (pirVal == LOW && messagePublished == true) {
- // client.publish("/lights", "0");
- // messagePublished = false;
- // }
- //
- // if (pirVal == HIGH && messagePublished == false) {
- // client.publish("/lights", "1");
- // messagePublished = true;
- // }
- }
- void startPartyMode() {
- }
- void stopPartyMode() {
- }
- void ISRwatchdog() {
- watchdogCount++;
- if (watchdogCount > 5) {
- Serial.println();
- Serial.println("The watchdog bites!");
- ESP.reset();
- }
- }
- void feedWatchdog() {
- watchdogCount = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement