Keypad am Wemos D1 ( Sketch Fehlermeldung )

Hallo Zusammen,

ich habe die Verbindung zwischen dem Keypad Amazon Link und dem Wemos hinbekommen. Im Monitor sehe ich die gedrückten Zahlen :slight_smile:
Nun habe ich bisher folgenden Code zusammen kopiert:

#include <ESP8266WiFi.h>
#include <Keypad.h>
#include <PubSubClient.h>

const char* ssid = "xxx";
const char* password = "xxxx";

const char* mqttServer = "192.168.0.222";
const int mqttPort = 1883;
const char* mqttUser = "xxx";
const char* mqttPassword = "xxx";

WiFiClient espClient;
PubSubClient client(espClient);
 
const byte n_rows = 4;
const byte n_cols = 4;
 
char keys[n_rows][n_cols] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
 
byte colPins[n_rows] = {D3, D2, D1, D0};
byte rowPins[n_cols] = {D7, D6, D5, D4};
 
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, n_rows, n_cols);


 
void setup(){
Serial.begin(115200);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");
 
  client.setServer(mqttServer, mqttPort);
 
  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");
 
    if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
 
      Serial.println("connected");  
 
    } else {
 
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);
 
    }
  }
 
  client.publish("esp/test", "Hello from ESP8266");
  client.subscribe("esp/test");
 
}
 
void loop(){
  char myKey = myKeypad.getKey();
 
  if (myKey != NULL){
    Serial.print("Key pressed: ");
    Serial.println(myKey);
  }    
}

Die Verbindung zum WLAN wird aufgebaut ( sehe ich in der Fritzbox ) und der Text "Hello from ESP8266" wird per MQTT gesendet ( das sehe ich in iobroker ). Nun möchte ich aber die Tasteneingaben als MQTT an iobroker senden und ich dachte mir machst einfach:

void loop(){
  char myKey = myKeypad.getKey();
 
  if (myKey != NULL){
    Serial.print("Key pressed: ");
    Serial.println(myKey);
    client.publish("esp/test", myKey);
  }    
}

Das geht aber leider nicht, folgende Meldung kommt:
invalid conversion from 'char' to 'const char*' [-fpermissive]

Ich ersuche gerade Tante Google um Rat aber noch komme ich nicht weiter.

Befasse Dich mal mit ein paar Grundlagen https://www.wikinger-tommy.de/arduino/tut_zeichenketten.htmlzu Zeichen und Zeichenketten.
Infos hier.

Gruß Tommy

Ebenfalls empfehle ich dir , dich mit dem Beispiel: pubsubclient -> mqtt_basic auseinander zu setzten.

Beim Neustart des Arduinos Werte zu setzte. z.B. mit:

void Inizial()
{
  String payload = "";
  char attributes[50];
  payload.toCharArray( attributes, 50 );
  client.publish( "Relaiskarte1/1", "0", true );
  payload.toCharArray( attributes, 50 );

diese dann abzurufen z.B.

void reconnect() 
{
  while (!client.connected()) 
  {
    Serial.println(" Counter bei: "+ String(rcounter) + ";");
    Serial.print("MQTT connection...");
    if (client.connect("arduinoClient")) 
    {
      Serial.println("connected");
      client.subscribe("Relaiskarte1/1");

und abzufragen was der Status von den jeweiligen Channels ist. z.B.:

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]);
  }
  Serial.println();
  if(strcmp(topic,"Relaiskarte1/1")==0)
  {
    if((char)payload[0]== '1')
    {
    digitalWrite(relaisPin1, LOW);
    delay(100); 
    }
    if((char)payload[0]== '0')
    {
    digitalWrite(relaisPin1, HIGH);
    delay(100); 
    }
  }

Hoffe , das ich helfen konnte und es einigermaßen verständlich war.

Ich habs nun so gelöst :wink: :

  if (myKey == '1')
    client.publish("esp/test", "1");
  if (myKey == '2')
    client.publish("esp/test", "2");
  if (myKey == '3')
    client.publish("esp/test", "3");
  if (myKey == '4')
    client.publish("esp/test", "4");
  if (myKey == '5')
    client.publish("esp/test", "5");
  if (myKey == '6')
    client.publish("esp/test", "6");
  if (myKey == '7')
    client.publish("esp/test", "7");
  if (myKey == '8')
    client.publish("esp/test", "8");
  if (myKey == '9')
    client.publish("esp/test", "9");
  if (myKey == '0')
    client.publish("esp/test", "0");
  if (myKey == '*')
    client.publish("esp/test", "*");
  if (myKey == '#')
    client.publish("esp/test", "#");
  if (myKey == 'A')
    client.publish("esp/test", "A");
  if (myKey == 'B')
    client.publish("esp/test", "B");
  if (myKey == 'C')
    client.publish("esp/test", "C");
  if (myKey == 'D')
    client.publish("esp/test", "D");
}

Kürzer:

char code[2];
code[1] = '\0'
...
// myKey einlesen
code[0] = myKey;
client.publish("esp/test", code);

Gruß Tommy

Hallo,

ich hab den code nun wie folgt erstellt. Leider empfange ich mittels mqtt auf "Garage/Keypad" nichts?
Findet jemand einen Fehler oder kann man den Code noch vereinfachen .

Danke schon mal vorab

#include <ESP8266WiFi.h>
#include <Keypad.h>
#include <PubSubClient.h>

//#include <WiFiClient.h>
//#include <WiFiUdp.h>
#include <ArduinoOTA.h>
//#include <ESP8266mDNS.h>

const char* ssid = "XXX";
const char* password = "XXX";

const char* mqttServer = "10.0.0.XX";
const int mqttPort = 1883;

WiFiClient espClient;
PubSubClient client(espClient);

const byte n_rows = 4;
const byte n_cols = 4;

char keys[n_rows][n_cols] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte colPins[n_rows] = {D3, D2, D1, D0};
byte rowPins[n_cols] = {D7, D6, D5, D4};

//byte colPins[n_rows] = {D7, D6, D5, D4};
//byte rowPins[n_cols] = {D3, D2, D1, D0};

Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, n_rows, n_cols);

void setup(){
Serial.begin(115200);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");
 
  client.setServer(mqttServer, mqttPort);
 
  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");
 
    if (client.connect("ESP8266Client")) {
 
      Serial.println("connected");  
 
    } else {
 
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);
 
    }
  }

ArduinoOTA.setHostname("GarageKeypad");
ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else { // U_SPIFFS
      type = "filesystem";
    }

    // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
    Serial.println("Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) {
      Serial.println("Auth Failed");
    } else if (error == OTA_BEGIN_ERROR) {
      Serial.println("Begin Failed");
    } else if (error == OTA_CONNECT_ERROR) {
      Serial.println("Connect Failed");
    } else if (error == OTA_RECEIVE_ERROR) {
      Serial.println("Receive Failed");
    } else if (error == OTA_END_ERROR) {
      Serial.println("End Failed");
    }
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}


void loop() {
  ArduinoOTA.handle();

  char myKey = myKeypad.getKey();
 
   if (myKey == '1')
    client.publish("Garage/Keypad", "1");
  if (myKey == '2')
    client.publish("Garage/Keypad", "2");
  if (myKey == '3')
    client.publish("Garage/Keypad", "3");
  if (myKey == '4')
    client.publish("Garage/Keypad", "4");
  if (myKey == '5')
    client.publish("Garage/Keypad", "5");
  if (myKey == '6')
    client.publish("Garage/Keypad", "6");
  if (myKey == '7')
    client.publish("Garage/Keypad", "7");
  if (myKey == '8')
    client.publish("Garage/Keypad", "8");
  if (myKey == '9')
    client.publish("Garage/Keypad", "9");
  if (myKey == '0')
    client.publish("Garage/Keypad", "0");
  if (myKey == '*')
    client.publish("Garage/Keypad", "*");
  if (myKey == '#')
    client.publish("Garage/Keypad", "#");
  if (myKey == 'A')
    client.publish("Garage/Keypad", "A");
  if (myKey == 'B')
    client.publish("Garage/Keypad", "B");
  if (myKey == 'C')
    client.publish("Garage/Keypad", "C");
  if (myKey == 'D')
    client.publish("Garage/Keypad", "D");
  }