i try to control a lamp with mqtt
When i send a 1 or 2 the script below works
But when i send a more complex command like lamp/on it's not working
I think it has to do with a string?? But no clue to solve that ![]()
/*
Basic MQTT example with Authentication
- connects to an MQTT server, providing username
and password
- publishes "hello world" to the topic "outTopic"
- subscribes to the topic "inTopic"
*/
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
int wait = 50;
int location = "test";
int i = 0;
// set pin numbers:
int ledPin = 15;
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 50);
IPAddress server(192, 168, 1, 49);
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 == '101'){
digitalWrite(ledPin, HIGH);
} else if (receivedChar == '102'){
digitalWrite(ledPin, LOW);
}
}
Serial.println();
}
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
void setup()
{
// initialize serial communication:
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Ethernet.begin(mac);
// Note - the default maximum packet size is 128 bytes. If the
// combined length of clientId, username and password exceed this,
// you will need to increase the value of MQTT_MAX_PACKET_SIZE in
// PubSubClient.h
//print out the IP address
Serial.print("IP = ");
Serial.println(Ethernet.localIP());
if (client.connect("arduinoClient", "USERNAME", "PASSWORD")) {
client.publish("test","test");
client.subscribe("test2");
}
}
void loop()
{
client.loop();
}