I'am currently trying to program a dev board based on ESP8266, which should be irrelevent to my question as I have all the required libraries working. The idea is that the baord connects to wifi, it then connects to an MQTT broker and subscribes to a topic, the topic payload is stored to a variable then the irsend command sends the payload as an 8 digit hex precedded by 0x. The problem I am having is storing the payload to a proper variable to be used in
irsend.sendNEC(Command_code, 32); // Send LGTV 1
with the variable type for Command_code being an unsigned int.
I am able to print the payload to the serial console using
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++) {
Serial.print((char)payload[i]);
}
But whenever I try to manipulate the contents of payload I get weird responses. Please see the full code below for my current (not working attempt). The wifi connection and the MQTT connection and monitoring all working just fine. Please help and I am quite new to programming so keep it simple if you can.
#include <IRremoteESP8266.h>
#include <IRremoteInt.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <stdlib.h> /* strtoul */
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
const char* mqtt_server = "10.10.10.110";
unsigned int Command_code;
unsigned char code;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
IRsend irsend(4);
void setup_wifi() {
// put your setup code here, to run once:
Serial.begin(115200);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset saved settings
//wifiManager.resetSettings();
//set custom ip for portal
wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
//fetches ssid and pass from eeprom and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
wifiManager.autoConnect("LinkNodeAP");
//or use this for auto generated name ESP + ChipID
//wifiManager.autoConnect();
//if you get here you have connected to the WiFi
Serial.println("connected... :)");
}
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++) {
Serial.print((char)payload[i]);
unsigned char code = (code & (char)payload[i]) ;
}
unsigned int Command_code = (strtoul((code, NULL, 16));
Serial.println();
// Send the code to the IR emitter
if (payload != 0) {
irsend.sendNEC(Command_code, 32); // Send LGTV 1
Serial.print("Sending code ");
Serial.print(Command_code, HEX);
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str(), "homeassistant", "benjamin")) {
Serial.println("connected");
client.subscribe("TV/command");
} 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() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}