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!