Hello,
I am developing device for my home automation project, which would accept commands received from MQTT and would switch on/off 8 relays. I am using Arduino UNO, with Ethernet shield W5100 and i2c port extender PCF8574P, which is managing relays.
Test code without MQTT support, which is managing i2c using wire.h library, perfectly works.
Test code with MQTT support perfectly works as well. I am able to publish topics to MQTT server, subscribe topics and print received topics to serial, but it stops working after I send anything to i2c. I was trying 2 different MQTT libraries PubSubClient and arduino-mqtt (GitHub - 256dpi/arduino-mqtt: MQTT library for Arduino), but got the same result with both.
My code is below. The line of code which is causing problems is indicated with comment.
Any ideas what could be wrong?
Thank You in advance.
#define CLIENT_ID "a2"
#include <SPI.h>
#include <Ethernet.h>
#include <MQTT.h>
#include <Wire.h> // I2C biblioteka
// Update these with values suitable for your network.
const byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
EthernetClient ethClient;
MQTTClient client;
const byte i2c_io_address = 0x20;
byte i2c_io_output_Status = 0;
byte statusr = 0;
void messageReceived(String &topic, String &payload) {
Serial.println("MQTT incoming message: " + topic + " - " + payload);
if (topic == "a2/r0"){
// whatever you want for this topic
update_output(0, payload);
client.publish("a2/rr0", statusr);
}
// #### IF LINE BELOW IS UNCOMMENTED, PROBLEM #####
//expanderWrite(i2c_io_output_Status, i2c_io_address);
Serial.println("Message processed");
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(CLIENT_ID, "try", "try")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("a2/connected","CONNECTED");
// ... and resubscribe
client.subscribe("a2/r0");
Serial.println("..subscribed");
} else {
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
Serial.begin(115200);
Serial.println(F("----------------------------------"));
Serial.println(F("-------------START----------------"));
// setup ethernet communication using DHCP
if(Ethernet.begin(mac) == 0) {
Serial.println(F("Ethernet configuration using DHCP failed"));
}
Serial.println(F("Ethernet configured via DHCP"));
Serial.print(F("IP address: "));
Serial.println(Ethernet.localIP());
Serial.println();
// setup mqtt client
client.begin("10.11.11.10", ethClient);
client.onMessage(messageReceived);
reconnect();
// Allow the hardware to sort itself out
delay(1500);
}
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
delay(50);
}
// Funkcija rasanti viena baita i i2c kanalu ispleteja
// Function which writes to i2c port extender PCF8574P/AP
void expanderWrite(byte _data, byte expanderAddress) {
Wire.beginTransmission(expanderAddress);
Wire.write(_data);
Wire.endTransmission();
}
//Function which sets required bit of byte depending on payload received from MQTT
void update_output(byte i2c_output_pinNO, String payload) {
byte task = 0;
byte ok = 0;
if (payload == "ON"){
task = 1;
ok = 1;
}
if (payload == "OFF"){
task = 0;
ok = 1;
}
byte current_state = bitRead(i2c_io_output_Status, i2c_output_pinNO);
if ((task == 0 || task == 2) && current_state == 1 && ok == 1) {
bitWrite(i2c_io_output_Status, i2c_output_pinNO, 0);
statusr = 0;
}
if ((task == 1 || task == 2) && current_state == 0 && ok == 1) {
bitWrite(i2c_io_output_Status, i2c_output_pinNO, 1);
statusr = 1;
}
Serial.print("Byte to be sent to i2c port extender: ");
Serial.println(i2c_io_output_Status);
}
If code line causing problems is commented, everything works fine. Serial output is below.
-------------START----------------
Ethernet configured via DHCP
IP address: 10.11.11.183
Attempting MQTT connection...connected
..subscribed
MQTT incoming message: a2/r0 - OFF
Byte to be sent to i2c port extender: 0
Message processed
MQTT incoming message: a2/r0 - ON
Byte to be sent to i2c port extender: 1
Message processed
MQTT incoming message: a2/r0 - OFF
Byte to be sent to i2c port extender: 0
Message processed
If I uncomment line causing problems, eveything stops during execution of that line. Next line ("Message processed") is not executed.
-------------START----------------
Ethernet configured via DHCP
IP address: 10.11.11.183
Attempting MQTT connection...connected
..subscribed
MQTT incoming message: a2/r0 - OFF
Byte to be sent to i2c port extender: 0
Any ideas what could be wrong?
Thank You in advance.
Sincerely,
Ignas