Need help with either timing within the program or the LOOP

My sketch works fine, sort of. Worked fine would be more like it. I did something that now causes my led to come on every 13 minutes. Possible that line 75 is commented out and the chip rests every 13 minutes or so. So the LED comes on and stays on?

/*
 * EXAM ROOM A
 */

#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>


// Connect to the WiFi
const char* ssid = "PiCharts";
const char* password = "Espressif";
const char* mqtt_server = "192.168.4.1";

WiFiClient espClient;
PubSubClient client(espClient);


// this constant won't change:
const int chartsPin = D1; // LED to notify "Charts-Up!"
const int chapPin = D5 ; // LED to call for chaperone
const int buttonPin = D2;    // the pin that the pushbutton is attached to [NOT an actual pushbutton
                             // but rather an Adafruit capacitive touch toggle switch]


// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button


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++) {
    char receivedChar = (char)payload[i];
    Serial.print(receivedChar);
    if (receivedChar == '0')
      digitalWrite(chartsPin, HIGH);
    if (receivedChar == '1')
      digitalWrite(chartsPin, LOW);
  }
  Serial.println();
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.println("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("Phys-Sub A")) {
      Serial.println("Phys-Sub A is connected to MQTT server");
      // ... and subscribe to topic
      client.subscribe("Charts/PS");
    } else {
      Serial.print("Failed to connect to MQTT...");
      Serial.print(client.state());
      Serial.println("Attempting to connect again in 3 seconds.");
      // Wait  seconds before retrying
      delay(3000);
    }
  }
}

void setup()
{
  Serial.begin(9600);
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(chartsPin, OUTPUT);
  pinMode(chapPin, OUTPUT);
//  digitalWrite(D5, LOW);// Set led off at start-up

//****************************************************
  // Connect to WiFinetwork
  Serial.println();
  Serial.println();
  Serial.print (F("Connecting to "));
  Serial.println(ssid);

  WiFi.begin(ssid, password);
  WiFi.mode(WIFI_STA);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    //Serial.begin(9600);
    Serial.print (F("."));
  }
  Serial.println (F(""));
  Serial.println (F("WiFi connected"));
  // Print the IP address
  Serial.print (F("Local IP: "));
  Serial.println(WiFi.localIP());
}









void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  {
    // read the pushbutton input pin:
    buttonState = digitalRead(buttonPin);

    // compare the buttonState to its previous state
    if (buttonState != lastButtonState) {
      // if the state has changed, increment the counter
      if (buttonState == HIGH) {
        client.publish("Chaperone/A", "0");
        Serial.println("Exam Room A LED is On");
        digitalWrite(chapPin, HIGH);
        // if the current state is HIGH then the button
        // went from off to on:
      } else {
        // if the current state is LOW then the button
        // went from on to off:
        client.publish("Chaperone/A", "1");
        Serial.println("Exam Room A LED is off");
        digitalWrite(chapPin, LOW);
      }
      // save the current state as the last state,
      //for next time through the loop
      lastButtonState = buttonState;
      // Do I need a delay for debounce with Adafruit Capacitive Touch Toggle Switch?
      delay(50);//if not I can delete this line.
    }
  }
}

I may have figured it out myself. Will have to wait till I go back to work and upload it to the ESP8266. As long as the boards don't disappear on me again. WTF is up with that!

Hi,
What did it do before this problem occurred, what is your code supposed to do?

Thanks.. Tom.. :slight_smile:

It does 2 things.

  1. Subscribes to MQT topic "PSCharts". So when charts are placed in a rack, The sensor in the rack publishes "PSCharts=0" causing a red LED come on. When no more charts, the Chart sensor publishes "PSCharts=1" causing the LED to turn off. That part works extremely well. No Issues.

  2. Publishes to MQTT via Capacitive-touch toggle switch; "Chaperone=1" causing the white LED to come on. Previously, I had it so when you plugged it in via USB, it would connect to the access point and then connect to MQTT server and flash the white light for 0.5 seconds so I knew it connected, then it would go out until I touched the capacitive-touch toggle switch, then the light would come on and publish to MQTT "Chaperone=0" and the white LED would stay on until the touch sensor was press again, then it would go out, Publish "Chaperone=1". That's where I'm having trouble. The capacitive touch works as it should. Press it the LED comes on, press it again and the white LED goes out. worked great. BUT now..., after 13 minutes. the White LED comes on and stays on by it'self until the button is pressed to turn it off. And again, after 13 minutes it comes on again.