Keypad mqtt code

Hi! I am trying to send inputs from my keypad using MQTT. I've tried to write the code,but i'm new in arduino and i dont get what im doing wrong. Im using nodemcu but writing with arduino ide. The sketch can be sent but afterwards in the serial monitor i keep getting "⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮".. can someone help me out?

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

#define wifi_ssid "MI 8"
#define wifi_password "umdoistrescinco"
#define mqtt_server "test.mosquitto.org"
#define mortos_topic "mortos"
WiFiClient espClient;
PubSubClient client(espClient);

const byte ROWS = 4; 
const byte COLS = 4; 
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

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

Keypad keypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

char key;
byte keyAsANumber;
boolean entryComplete;
unsigned int numbpad = 0;
int count = 0;
int count2;
int carga = 1;
int carga2;

void setup(){
  Serial.begin(115200);
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.print(wifi_ssid);
  WiFi.begin(wifi_ssid, wifi_password);
  while (WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }
  Serial.print("");
  Serial.println("WiFi connected");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
  client.setServer(mqtt_server, 1883);
}

void loop(){
  entryComplete = false;
  readKeypad();
  if (entryComplete){
    numbpad=0;
  }  
}

void readKeypad(){
  key = keypad.getKey();
  if (key == '#'){
    if (numbpad == 0){
      Serial.println("No input");
    }
    if (numbpad != 0){
      Serial.println(" - Entry complete!");
      count = count + numbpad;
      Serial.println((String) "Carga: " + carga + " Mortos: " + count );
      entryComplete = true; 
      return;
    }
  }
  if (key >= '0' && key <= '9'){
    keyAsANumber = key - 48;
    numbpad = numbpad *10;
    numbpad = numbpad + keyAsANumber;
    Serial.print(keyAsANumber);
  }
  if (key == 'A') {
    count += 1;
    Serial.println((String) "Carga: " + carga + " Mortos: " + count ) ;
  }
  
  if ((key == 'B') && (count>0)) {
    count -= 1;
    Serial.println((String) "Carga: " + carga + " Mortos: " + count ) ;
  }
  
  if (key == 'C') {
    carga2 = carga;
    count2 = count;
    carga += 1;
    count = 0;
    Serial.println((String) "Carga: " + carga + " Mortos: " + count ) ;
    client.publish(mortos_topic, String(count2).c_str(), true);
  }
  
  if ((key == 'D') && (carga>1)) {
    carga -= 1;
    count = count2;
    Serial.println((String) "Carga: " + carga + " Mortos: " + count ) ;
  }
  
  if (key == '*') {
    carga = 1;
    count = 0;
    carga2 = 0;
    count2= 0;
    Serial.println((String) "Carga: " + carga + " Mortos: " + count) ;
  }
}

Dumb question but you always gotta ask with serial monitor issues. Have you verified that you have your monitor set to 115200 as well?

1 Like

what's dumber? me or the question xD anwser: its me ahahah. didn't knew about that thanx!!!! issue fixed!

Awesome!
I do that about once a week so don't feel bad.
Happy Programming!

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.