Hallo
ich hab folgendes Problem. Der unten angeführte Code funktioniert solange ich den Wemos über den PC angeschlossen habe. Sobald ich diese aber über eine USB Ladegerät anschließe funktioniert der er nicht mehr zuverlässig. Teilweise wird die falsche Zahle auf dem Keypad erkannt oder er schickt teilweise 10-20mal die gleiche Zahl zum Mqtt server…
Hat jemand eine Idee? Danke
#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.72";
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");
}