Help with a MQTT Keypad sketch

Hello everyone,

I have been trying to setup a esp8266 with a 4x4 Keypad Matrix (they cost around 0,50€ on AliExpress) to publish which key is being pressed to a MQTT broker.

I plan to use it for example with a LCD for Alarm Control Panel component and maybe a inwall multi-switches.
With Home-Assistant automations package or Node-RED I can decide what each key will do.

My current sketch is here and when I try to compile on Arduino IDE I get this msg:

/MQTT-Keypad/MQTT-Keypad.ino: In function 'void setup()':
MQTT-Keypad:40: error: no matching function for call to 'PubSubClient::setCallback(void (&)())'
   client.setCallback(callback);
                              ^
/MQTT-Keypad/MQTT-Keypad.ino:40:30: note: candidate is:
In file included from /MQTT-Keypad/MQTT-Keypad.ino:12:0:
/arduino-1.8.5/libraries/pubsubclient/src/PubSubClient.h:121:18: note: PubSubClient& PubSubClient::setCallback(std::function<void(char*, unsigned char*, unsigned int)>)
    PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE);
                  ^
/arduino-1.8.5/libraries/pubsubclient/src/PubSubClient.h:121:18: note:   no known conversion for argument 1 from 'void()' to 'std::function<void(char*, unsigned char*, unsigned int)>'
/MQTT-Keypad/MQTT-Keypad.ino: In function 'void callback()':
MQTT-Keypad:67: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
     client.publish(mqtt_state, myKey);    
                                     ^
In file included from /MQTT-Keypad/MQTT-Keypad.ino:12:0:
/arduino-1.8.5/libraries/pubsubclient/src/PubSubClient.h:130:12: error:   initializing argument 2 of 'boolean PubSubClient::publish(const char*, const char*)' [-fpermissive]
    boolean publish(const char* topic, const char* payload);
            ^
MQTT-Keypad:70: error: a function-definition is not allowed here before '{' token
 void loop() {
             ^
MQTT-Keypad:96: error: expected '}' at end of input
 }
 ^
exit status 1
no matching function for call to 'PubSubClient::setCallback(void (&)())'

I “know” it is something wrong with the Callback function but I have no idea what it is and how to fix it.
Maybe someone could help me and and guide me in the correct way.

Thanks in advance :slight_smile:

void callback(){
  char myKey = myKeypad.getKey();
 
  if (myKey != NULL){
    Serial.print("Key pressed: ");
    Serial.println(myKey);
    delay(500);
    client.publish(mqtt_state, myKey);    
  }

}<----------- missing

1 Like

Thank you keithh666, but that wasn’t the only problem!

I had to make more changes into the sketch but I manage to get it working and updated the sketch.

1 Like

Can someone help me get this started? This is almost what I am looking for only I would like to be able to enter at least 4 digits and then send the message.