Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Random Interrupt while turning OFF Load manually
- #include <ESP8266WiFi.h>
- #include <Wire.h>
- #include <PubSubClient.h>
- int wifiStatusLed = 16;
- int loadPin = 14;
- int loadStatusLed = 2; //Debug
- const byte interruptPin = 12;
- volatile int interruptState = LOW;
- const char* ssid = "myNetwork";
- const char* pass = "84613246581r";
- const char* brokerUser = "";
- const char* brokerPass = "";
- const char* broker = "142.93.214.20";
- const char* inTopic = "RCU00001";
- char Recdata[100];
- String response;
- WiFiClient espClient;
- PubSubClient client(espClient);
- long currentTime, lastTime;
- int count = 0;
- char messages[50];
- void setupWifi() {
- delay(100);
- Serial.print("\nConnecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, pass);
- while (WiFi.status() != WL_CONNECTED) {
- delay(100);
- Serial.print("-");
- }
- Serial.print("\nConnected to ");
- Serial.println(ssid);
- }
- void reconnect() {
- while (!client.connected()) {
- Serial.print("\nConnecting to ");
- Serial.println(broker);
- if (client.connect("esp32", brokerUser, brokerPass)) {
- Serial.print("\nConnected to ");
- Serial.println(broker);
- client.subscribe(inTopic);
- digitalWrite(wifiStatusLed, HIGH);
- } else {
- Serial.println("\n Trying to reconnect");
- digitalWrite(wifiStatusLed, LOW);
- delay(5000);
- }
- }
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- Serial.print("Message received : ");
- for (int i = 0; i < length; i++) {
- response += (char)payload[i];
- }
- Serial.println(response);
- String tempResponse = response;
- if (tempResponse == "mwOn") {
- turnON(); //Turn ON Load using WiFi
- }
- if (tempResponse == "mwOff") {
- turnOFF(); //Turn OFF Load using WiFi
- }
- response = "";
- }
- void setup() {
- Serial.begin(115200);
- Wire.begin();
- setupWifi();
- client.setServer(broker, 1883);
- client.setCallback(callback);
- pinMode(wifiStatusLed, OUTPUT);
- pinMode(loadPin, OUTPUT);
- pinMode(loadStatusLed, OUTPUT);
- digitalWrite(loadStatusLed, HIGH); //Default OFF, Active LOW
- pinMode(interruptPin, INPUT);
- attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, CHANGE);
- }
- ICACHE_RAM_ATTR void handleInterrupt() {
- interruptState = digitalRead(interruptPin);
- if (interruptState == HIGH) {
- turnONint(); //Turn ON Load using Interrupt
- }
- else if (interruptState == LOW) {
- turnOFFint(); //Turn OFF Load using Interrupt
- }
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- }
- // Separate function to identify how load was turned ON/OFF using Interrupt or WiFi
- void turnOFF() {
- digitalWrite(loadPin, LOW);
- digitalWrite(loadStatusLed, HIGH); //Onboard LED Active LOW
- Serial.println("OFF via WiFI");
- }
- void turnON() {
- digitalWrite(loadPin, HIGH);
- digitalWrite(loadStatusLed, LOW);
- Serial.println("ON via WiFI");
- }
- void turnOFFint() {
- digitalWrite(loadPin, LOW);
- digitalWrite(loadStatusLed, HIGH);
- Serial.println("OFF via INT");
- }
- void turnONint() {
- Serial.println("ON via INT");
- digitalWrite(loadPin, HIGH);
- digitalWrite(loadStatusLed, LOW);
- }
Add Comment
Please, Sign In to add comment