I'm running mosquitto MQTT from a raspberry pi Zero W. Trying to turn on/off a set of LEDs on separate Wemos D-1 mini (ESP8266) modules.
I'm having 2 issues.
a. I cannot get more than two clients to connect to the MQTT server. When I publish via SSH to the Pi, the 2 connected clients works amazing! I am excited, however, I can't seem to get the others connected
b. I want 1 Wemos to read the input from a toggle switch, client.publish "topic", Message = value of the "button state"?
Here's the sketch with button press:
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
// Connect to the WiFi
const char* ssid = "XXXXXXXXX";
const char* password = "XXXXXXXXX";
const char* mqtt_server = "Mosquitto_broker";
WiFiClient espClient;
PubSubClient client(espClient);
const int chartsPin = D1; // LED on D1 of Wemos D1 mini
const int buttonPin = D2; // Adafruit Capacitive Touch Toggle on D2
int buttonState = 0;
int lastButtonState = 0;
int buttonPushCounter = 0; // counter for the number of button presses
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.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("PSB")) {//assign a "client name". Each wemos must have unique name
Serial.println("connected");
// ... and subscribe to topic
client.subscribe("Charts/PS");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 3 seconds");
// Wait 3 seconds before retrying
delay(3000);
}
}
}
void setup()
{
Serial.begin(9600);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(buttonPin, INPUT);
pinMode(chartsPin, OUTPUT);
}
void 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("Charts/PS", "0");
Serial.println("LED is On");
// 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("Charts/PS", "1");
Serial.println("LED is off");
}
// Do I need a delay for debounce with Adafruit Capacitive Touch Toggle Switch?
delay(50);//if not I can delete this line.
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
Did I ask the right question? I googled, read, tried for at least 8 hours on my own.
Thanks in advance.
Arduino.ide doesn't count? I am in the programming section, or at least I thought so. There are some PRETTY SMART people in here that may be able to help.
My on/off toggle sketch worked on my arduino uno and now I'm implementing that into my project. Just so happens to be MQTT at the moment. Since I have very limited experience in programming... I'm asking for assistance.
Update: 04/03/17
Yesterday afternoon I dug out 4 more Wemos minis. 1 of those worked with my project, so I now have 3 working and communicating with the MQTT broker. I'm thinking maybe an issue with the Wemos themselves. Took that issue up on the Wemos forum. Thanks.
So, your "programming" problems were mostly hardware-related.
Now back to Button "state".
Now that we are down to the programming issues, perhaps you can describe exactly what the problem is.
const int buttonPin = D2; // Adafruit Capacitive Touch Toggle on D2
Or, maybe there are STILL hardware problems.
If you connect a real switch, do you still have the same issue?
While I accept that, in the end, you might want to use a capacitive touch switch, for now, you need to separate the hardware issues from the software issues.
Sorry; had to travel away for 2 days.
Got it all sorted out. Have a look:
Control desk
/*
* Reception
*/
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
// Connect to the WiFi
const char* ssid = "MYSSID";
const char* password = "MYPASSWORD";
const char* mqtt_server = "Mosquitto_broker";
// this constant won't change:
const int buttonPin = D2; // the pin that the pushbutton is attached to
const int chartsPin = D1; // the pin that the LED is attached to
// 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
WiFiClient espClient;
PubSubClient client(espClient);
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("Reception Desk")) {
Serial.println("Connected");
// ... and subscribe to topic
client.subscribe("Charts/PS");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
{
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(chartsPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
// Connect to WiFinetwork
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
//Serial.begin(9600);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
//Serial.begin(9600);
// Start the server
// server.begin();
// Serial.println("Server started");
//Serial.begin(9600);
// Print the IP address
Serial.print("Local IP: ");
Serial.println(WiFi.localIP());
}
void 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("Charts/PS", "0"); //
// 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("Charts/PS", "1"); //
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
{
if (!client.connected()) {
reconnect();
}
client.loop();
}
}
Clients
/*
* Phys-Sub A
*/
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
// Connect to the WiFi
const char* ssid = "MYSSID";
const char* password = "MYPASSWORD";
const char* mqtt_server = "Mosquitto_broker";
WiFiClient espClient;
PubSubClient client(espClient);
const byte chartsPin = D1; // LED on D1 of Wemos D1 mini
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.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("Phys-Sub A")) {
Serial.println("connected");
// ... and subscribe to topic
client.subscribe("Charts/PS");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
Serial.begin(9600);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(chartsPin, OUTPUT);
}
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
}