Thanks to user @fabaff on the Gitter chat I think I’m getting a little closer.
His original sketch-
[code]/*
Basic ESP8266 MQTT example NodeMCU ESP8266-12E development board
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
int inputPins[] = { 1, 3, 12, 13, 14, 15 };
int pinInputCount = 6;
int outputPins[] = { 0, 2, 4, 5 };
int pinOutputCount = 4;
// Update these with values suitable for your network.
const char* ssid = “…”;
const char* password = “”;
const char* mqtt_server = “…”;
char* deviceId = “unit007”;
char* stateTopic = “home/unit007”;
char* commandTopic = “home/unit007/set”;
int sensorPin = A0;
char buf[4];
int updateInterval = 2000;
char* stateTopicA0 = “home/unit007/a0”;
char* stateTopic0 = “home/unit007/0”;
char* stateTopic1 = “home/unit007/1”;
char* stateTopic2 = “home/unit007/2”;
char* stateTopic3 = “home/unit007/3”;
char* stateTopic4 = “home/unit007/4”;
char* stateTopic5 = “home/unit007/5”;
char* stateTopic12 = “home/unit007/12”;
char* stateTopic13 = “home/unit007/13”;
char* stateTopic14 = “home/unit007/14”;
char* stateTopic15 = “home/unit007/15”;
char* stateTopic16 = “home/unit007/16”;
char* commandTopic0 = “home/unit007/0/set”;
char* commandTopic2 = “home/unit007/2/set”;
char* commandTopic4 = “home/unit007/4/set”;
char* commandTopic5 = “home/unit007/5/set”;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msga0[20];
char msg0[20];
char msg1[20];
char msg2[20];
char msg3[20];
char msg4[20];
char msg5[20];
char msg12[20];
char msg13[20];
char msg14[20];
char msg15[20];
char msg16[20];
int sensorValue = 0;
const int buttonPin1 = 12;
const int buttonPin2 = 13;
const int ledPin1 = 2;
const int ledPin2 = 5;
int buttonState1 = 0;
int buttonState2 = 0;
int lastButtonState1 = 0;
int lastButtonState2 = 0;
int ledState1 = 0;
int ledState2 = 0;
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println(“WiFi connected”);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived: “);
Serial.print(topic);
Serial.print(” ");
String recieveTopic = (char*)topic;
Serial.print("Payload: ");
Serial.write(payload, length);
Serial.println();
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message§;
if (recieveTopic == (char*)commandTopic0) {
if (message.equals(“1”)) {
digitalWrite(0, HIGH);
} else {
digitalWrite(0, LOW);
}
} else if (recieveTopic == (char*)commandTopic2) {
if (message.equals(“1”)) {
digitalWrite(2, HIGH);
} else {
digitalWrite(2, LOW);
}
} else if (recieveTopic == (char*)commandTopic4) {
if (message.equals(“1”)) {
digitalWrite(4, HIGH);
} else {
digitalWrite(4, LOW);
}
} else if (recieveTopic == (char*)commandTopic5) {
if (message.equals(“1”)) {
digitalWrite(5, HIGH);
} else {
digitalWrite(5, LOW);
}
} else {
Serial.print(“Topic unknown”);
}
}
void reconnect() {
while (!client.connected()) {
Serial.print(“Attempting MQTT connection…”);
if (client.connect(deviceId)) {
Serial.println(“connected”);
client.publish(stateTopic, “ready”);
client.subscribe(commandTopic0);
client.subscribe(commandTopic2);
client.subscribe(commandTopic4);
client.subscribe(commandTopic5);
} else {
Serial.print(“failed, rc=”);
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup() {
// Input pins
for (int thisPin = 0; thisPin < pinInputCount; thisPin++) {
pinMode(inputPins[thisPin], INPUT);
}
// Output pins
for (int thisPin = 0; thisPin < pinOutputCount; thisPin++) {
pinMode(outputPins[thisPin], OUTPUT);
}
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
buttonState1 = digitalRead(buttonPin1);
if (buttonState1 != lastButtonState1) {
if (buttonState1 == HIGH) {
if(ledState1 == HIGH) {
ledState1 = LOW;
} else {
ledState1 = HIGH;
}
}
lastButtonState1 = buttonState1;
}
digitalWrite(ledPin1, ledState1);
delay(20);
buttonState2 = digitalRead(buttonPin2);
if (buttonState2 != lastButtonState2) {
if (buttonState2 == HIGH) {
if(ledState2 == HIGH) {
ledState2 = LOW;
} else {
ledState2 = HIGH;
}
}
lastButtonState2 = buttonState2;
}
digitalWrite(ledPin2, ledState2);
delay(20);
//////////////////////////////////////////////////////////////
long now = millis();
if (now - lastMsg > updateInterval) {
lastMsg = now;
snprintf (msga0, 75, "%ld", analogRead(sensorPin));
client.publish(stateTopicA0, msga0);
// Input pins
snprintf(msg0, 75, "%ld", digitalRead(0));
client.publish(stateTopic0, msg0);
snprintf(msg1, 75, "%ld", digitalRead(1));
client.publish(stateTopic1, msg1);
snprintf(msg2, 75, "%ld", digitalRead(2));
client.publish(stateTopic2, msg2);
snprintf(msg3, 75, "%ld", digitalRead(3));
client.publish(stateTopic0, msg3);
snprintf(msg4, 75, "%ld", digitalRead(4));
client.publish(stateTopic4, msg4);
snprintf(msg5, 75, "%ld", digitalRead(5));
client.publish(stateTopic5, msg5);
snprintf(msg12, 75, "%ld", digitalRead(12));
client.publish(stateTopic12, msg12);
snprintf(msg13, 75, "%ld", digitalRead(13));
client.publish(stateTopic13, msg13);
snprintf(msg14, 75, "%ld", digitalRead(14));
client.publish(stateTopic14, msg14);
snprintf(msg15, 75, "%ld", digitalRead(15));
client.publish(stateTopic15, msg15);
snprintf(msg16, 75, "%ld", digitalRead(16));
client.publish(stateTopic16, msg16);
}
}[/code]
His code was written for the NodeMCU board however which has many more open ports, so I had to modify for the ESP v01 with only two open pins. My current version is as follows-
[code]/*
Basic ESP8266 MQTT example NodeMCU ESP8266-12E development board
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
int inputPins[] = { 0 };
int pinInputCount = 1;
int outputPins[] = { 2 };
int pinOutputCount = 4;
// Update these with values suitable for your network.
const char* ssid = “ssid”;
const char* password = “password”;
const char* mqtt_server = “192.168.1.1”;
char* deviceId = “Button”;
char* stateTopic = “home/bedroom/buttonswitch”;
char* commandTopic = “home/bedroom/buttonswitch/set”;
int sensorPin = A0;
char buf[4];
int updateInterval = 2000;
char* stateTopicA0 = “home/bedroom/buttonswitch/a0”;
char* stateTopic0 = “home/bedroom/buttonswitch/0”;
char* stateTopic1 = “home/bedroom/buttonswitch/1”;
char* commandTopic0 = “home/bedroom/buttonswitch/0/set”;
char* commandTopic2 = “home/bedroom/buttonswitch/2/set”;
char* commandTopic4 = “home/bedroom/buttonswitch/4/set”;
char* commandTopic5 = “home/bedroom/buttonswitch/5/set”;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msga0[20];
char msg0[20];
char msg1[20];
char msg2[20];
char msg3[20];
char msg4[20];
char msg5[20];
char msg12[20];
char msg13[20];
char msg14[20];
char msg15[20];
char msg16[20];
int sensorValue = 0;
const int buttonPin1 = 0;
const int ledPin1 = 2;
int buttonState1 = 0;
int buttonState2 = 0;
int lastButtonState1 = 0;
int lastButtonState2 = 0;
int ledState1 = 0;
int ledState2 = 0;
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println(“WiFi connected”);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived: “);
Serial.print(topic);
Serial.print(” ");
String recieveTopic = (char*)topic;
Serial.print("Payload: ");
Serial.write(payload, length);
Serial.println();
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message§;
if (recieveTopic == (char*)commandTopic0) {
if (message.equals(“1”)) {
digitalWrite(0, HIGH);
} else {
digitalWrite(0, LOW);
}
} else if (recieveTopic == (char*)commandTopic2) {
if (message.equals(“1”)) {
digitalWrite(2, HIGH);
} else {
digitalWrite(2, LOW);
}
} else if (recieveTopic == (char*)commandTopic4) {
if (message.equals(“1”)) {
digitalWrite(4, HIGH);
} else {
digitalWrite(4, LOW);
}
} else if (recieveTopic == (char*)commandTopic5) {
if (message.equals(“1”)) {
digitalWrite(5, HIGH);
} else {
digitalWrite(5, LOW);
}
} else {
Serial.print(“Topic unknown”);
}
}
void reconnect() {
while (!client.connected()) {
Serial.print(“Attempting MQTT connection…”);
if (client.connect(deviceId)) {
Serial.println(“connected”);
client.publish(stateTopic, “ready”);
client.subscribe(commandTopic0);
client.subscribe(commandTopic2);
client.subscribe(commandTopic4);
client.subscribe(commandTopic5);
} else {
Serial.print(“failed, rc=”);
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup() {
// Input pins
for (int thisPin = 0; thisPin < pinInputCount; thisPin++) {
pinMode(inputPins[thisPin], INPUT);
}
// Output pins
for (int thisPin = 0; thisPin < pinOutputCount; thisPin++) {
pinMode(outputPins[thisPin], OUTPUT);
}
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
buttonState1 = digitalRead(buttonPin1);
if (buttonState1 != lastButtonState1) {
if (buttonState1 == HIGH) {
if(ledState1 == HIGH) {
ledState1 = LOW;
} else {
ledState1 = HIGH;
}
}
lastButtonState1 = buttonState1;
}
digitalWrite(ledPin1, ledState1);
delay(20);
/////////////////////////////////////////////////////////////
long now = millis();
if (now - lastMsg > updateInterval) {
lastMsg = now;
snprintf (msga0, 75, "%ld", analogRead(sensorPin));
client.publish(stateTopicA0, msga0);
// Input pins
snprintf(msg0, 75, "%ld", digitalRead(0));
client.publish(stateTopic0, msg0);
snprintf(msg1, 75, "%ld", digitalRead(1));
client.publish(stateTopic1, msg1);
snprintf(msg3, 75, "%ld", digitalRead(3));
client.publish(stateTopic0, msg3);
}
}[/code]
Now, in the Mosquitto CLI, at least instead of publishing a “connection” message every other second, I get a string of digits like so-
[quote]1
0
0
47
1
0
0
46[/quote]
When I push the button I get-
[quote]1
0
0
47
ready
1
0
0
46[/quote]