Ok first full code:
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <IRremote.h>
/**************************************************************
* Ethernet Settings *
**************************************************************/
// Ethernet Card Mac Address
byte mac[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
// IPv4 address
byte ip[] = {192, 168, 0, 99};
// Subnet mask
byte subnet[] = {255, 255, 255, 0};
// Default gateway
byte gateway[] = {192, 168, 0, 1};
// MQTT Broker
byte MqttBroker[] = { 192, 168, 0, 12 };
// Preferred DNS sever
// byte dns[] = {192, 168, 0, 1};
/**************************************************************
* Declare *
**************************************************************/
IRsend irsend;
int test = 0;
/**************************************************************
* MQTT *
**************************************************************/
// MQTT Callback
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("New message from broker on topic: ");
Serial.println(topic);
Serial.print("Payload: ");
Serial.write(payload, length);
Serial.println();
Serial.print("Test: ");
Serial.println(test);
// IR Remote Sony TV
if (strcmp(topic,"skulltronics.net/hive/floor/optimus-prime/IR-SonyTV")==0) {
// Volume Up Nonstop
if (length == 10 && strncmp((char*)payload,"volume-ups",10)==0) {
for (int i = 0; i < 250; i++) {
Serial.println("running");
delay(50);
if (test == 1) {
break;
}
}
}// End Volume Up Nonstop
// Stop Volume Up Nonstop
if (length == 11 && strncmp((char*)payload,"volume-stop",11)==0) {
test = 1;
} // End Stop Volume Up Nonstop
} // End IR Remote Sony TV
} // End MQTT Callback
// Fire up PubSub client
EthernetClient ethClient;
PubSubClient client(MqttBroker, 1883, callback, ethClient);
/**************************************************************
* Setup *
**************************************************************/
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Ethernet.begin(mac, ip, subnet, gateway);
Serial.print(" IPv4 address: ");
Serial.println(Ethernet.localIP());
// Connect to Broker, give it Optimus Prime as the name
if (client.connect("Optimus Prime")) {
// Publish a message to the status topic
client.publish("status","Optimus Prime is now online");
// Listen for messages on the control topic
client.subscribe("skulltronics.net/hive/floor/optimus-prime/#");
}
}
void loop() {
client.loop();
}
If i don't do the "strncmp length test" then my "for loop" is not running.
There is another way with a running for loop:
// MQTT Callback
void callback(char* topic, byte* payload, unsigned int length) {
// ********** IR Remote Sony TV ********** //
// Volume Up Nonstop
if (strcmp(topic,"skulltronics.net/hive/floor/optimus-prime/IR-SonyTV/volume-ups")==0) {
if (payload[0] == '1') {
for (int i = 0; i < 250; i++) {
Serial.println("running");
irsend.sendSony(0x490, 12);
delay(50);
if (test == 1) {
break;
}
}
}
} // End Volume Up Nonstop
// Stop Volume Up Nonstop
if (strcmp(topic,"skulltronics.net/hive/floor/optimus-prime/IR-SonyTV/volume-stop")==0) {
if (payload[0] == '1') {
test = 1;
}
} // End Stop Volume Up Nonstop
// ********** End IR Remote Sony TV ********** //
} // End MQTT Callback
The second example means i have to call every command with his own topic and send "1" as message.
From my point of view it makes more sense to call the remote (IR-SonyTV) and send as message
what to do (volume-up) instead of calling the remote, then the button and tell the button what to do.
It has to do with null- termination and there is more about it here (comment 50 to 60): Arduino Client for MQTT · knolleary
Also i have to do it like in the first example if i want to send the IR-Remote hex code over MQTT.
Ok, what this code now does is printing "running" if i send "volume-ups".
I the send "volume-stop" and "running" is still printed.
After the 250 "running" prints i get my:
New message from broker on topic: skulltronics.net/hive/floor/optimus-prime/IR-SonyTV
Payload: volume-stop
Test: 0
print.
If i send then "volume-ups" as expected "running" is only printed once and Test: 1
So setting "test" works.
I need to send every command minimum 3 times to the IR-LED. That is how Sony remotes work.
You press a button and the IR-LED blinks 3 times.
So for my non-stop volume up i normally need:
3 blinks 50ms pause 3 blinks 50ms pause...
until i say "volume-stop"