babyyoda_

1CH_Wifi_INT

Sep 6th, 2021 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Random Interrupt while turning OFF Load manually
  2.  
  3.  
  4. #include <ESP8266WiFi.h>
  5. #include <Wire.h>
  6. #include <PubSubClient.h>
  7.  
  8. int wifiStatusLed = 16;
  9. int loadPin = 14;
  10. int loadStatusLed = 2;  //Debug
  11.  
  12. const byte interruptPin = 12;
  13. volatile int interruptState = LOW;
  14.  
  15. const char* ssid = "myNetwork";
  16. const char* pass = "84613246581r";
  17. const char* brokerUser = "";
  18. const char* brokerPass = "";
  19. const char* broker = "142.93.214.20";
  20. const char* inTopic = "RCU00001";
  21. char Recdata[100];
  22. String response;
  23. WiFiClient espClient;
  24. PubSubClient client(espClient);
  25. long currentTime, lastTime;
  26. int count = 0;
  27. char messages[50];
  28.  
  29. void setupWifi() {
  30.   delay(100);
  31.   Serial.print("\nConnecting to ");
  32.   Serial.println(ssid);
  33.   WiFi.begin(ssid, pass);
  34.  
  35.   while (WiFi.status() != WL_CONNECTED) {
  36.     delay(100);
  37.     Serial.print("-");
  38.   }
  39.  
  40.   Serial.print("\nConnected to ");
  41.   Serial.println(ssid);
  42. }
  43.  
  44.  
  45. void reconnect() {
  46.   while (!client.connected()) {
  47.     Serial.print("\nConnecting to ");
  48.     Serial.println(broker);
  49.     if (client.connect("esp32", brokerUser, brokerPass)) {
  50.       Serial.print("\nConnected to ");
  51.       Serial.println(broker);
  52.       client.subscribe(inTopic);
  53.       digitalWrite(wifiStatusLed, HIGH);
  54.     } else {
  55.       Serial.println("\n Trying to reconnect");
  56.       digitalWrite(wifiStatusLed, LOW);
  57.       delay(5000);
  58.     }
  59.   }
  60. }
  61.  
  62. void callback(char* topic, byte* payload, unsigned int length) {
  63.  
  64.   Serial.print("Message received : ");
  65.   for (int i = 0; i < length; i++) {
  66.     response += (char)payload[i];
  67.   }
  68.   Serial.println(response);
  69.   String tempResponse = response;
  70.   if (tempResponse == "mwOn") {
  71.     turnON();    //Turn ON Load using WiFi    
  72.   }
  73.   if (tempResponse == "mwOff") {
  74.     turnOFF();    //Turn OFF Load using WiFi          
  75.   }
  76.   response = "";
  77. }
  78.  
  79. void setup() {
  80.  
  81.   Serial.begin(115200);
  82.   Wire.begin();
  83.   setupWifi();
  84.   client.setServer(broker, 1883);
  85.   client.setCallback(callback);
  86.  
  87.   pinMode(wifiStatusLed, OUTPUT);
  88.   pinMode(loadPin, OUTPUT);
  89.   pinMode(loadStatusLed, OUTPUT);
  90.   digitalWrite(loadStatusLed, HIGH);    //Default OFF, Active LOW
  91.   pinMode(interruptPin, INPUT);
  92.  
  93.   attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, CHANGE);
  94. }
  95.  
  96. ICACHE_RAM_ATTR void handleInterrupt() {
  97.   interruptState = digitalRead(interruptPin);  
  98.   if (interruptState == HIGH) {
  99.     turnONint();    //Turn ON Load using Interrupt          
  100.   }
  101.   else if (interruptState == LOW) {
  102.     turnOFFint();  //Turn OFF Load using Interrupt
  103.   }
  104. }
  105.  
  106. void loop() {
  107.   if (!client.connected()) {
  108.     reconnect();
  109.   }
  110.   client.loop();
  111. }
  112.  
  113. // Separate function to identify how load was turned ON/OFF using Interrupt or WiFi
  114.  
  115. void turnOFF() {
  116.   digitalWrite(loadPin, LOW);
  117.   digitalWrite(loadStatusLed, HIGH);    //Onboard LED Active LOW
  118.   Serial.println("OFF via WiFI");
  119. }
  120.  
  121. void turnON() {
  122.   digitalWrite(loadPin, HIGH);
  123.   digitalWrite(loadStatusLed, LOW);
  124.   Serial.println("ON via WiFI");
  125. }
  126.  
  127.  
  128. void turnOFFint() {
  129.   digitalWrite(loadPin, LOW);
  130.   digitalWrite(loadStatusLed, HIGH);
  131.   Serial.println("OFF via INT");
  132. }
  133.  
  134. void turnONint() {
  135.   Serial.println("ON via INT");
  136.   digitalWrite(loadPin, HIGH);
  137.   digitalWrite(loadStatusLed, LOW);
  138. }
Add Comment
Please, Sign In to add comment