Buenos días a todos, les cuento tengo un pequeño proyecto de un autito con un NodeMCU V3 que se maneja con el celu usando el NodeMCU en modo AP.
El problema que estoy teniendo es que hay lugares en donde el NodeMCU se vuelve loco, mejor dicho nunca se conecta al celu, o se conecta y desconecta, o el websocket se conecta y cuando mando una instrucción se desconecta. lo que me llama la atención.
Para intentar solucionar el problema fue cambiar de canal la WIFI pero no tuve resultados positivos.
Abajo dejo el codigo si les sirve de algo. Saludos Joaquin
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsServer.h>
#include <ESP8266WebServer.h>
#include <ESP8266WebServerSecure.h>
#include <ESP8266mDNS.h>
#include <Hash.h>
#include "FS.h"
#include <Servo.h>
//VELOCIDAD
#define PIN_ENABLED_VELOCIDAD 16
#define PIN_CONTROL_A_VELOCIDAD 5 //ADELANTE -> A=1, B=0
#define PIN_CONTROL_B_VELOCIDAD 4 //ATRAS -> A=0, B=1
//GIRO
#define PIN_CONTROL_GIRO 0 //DERECHA -> A=1, B=0
//LED LUCES(CONECTADO WIFI)
#define PIN_LUCES 2 //LUCES -> CONECTO WIFI = 1, DESCONECTO WIFI = 0
#define PIN_BOCINA 14
Servo servoGiro;
ESP8266WiFiMulti WiFiMulti;
ESP8266WebServer server (80);
WebSocketsServer webSocket = WebSocketsServer(81);
String html_home;
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
switch(type) {
case WStype_DISCONNECTED:
{
Serial.printf("[%u] Disconnected!\n", num);
break;
}
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
// send message to client
webSocket.sendTXT(num, "Connected");
break;
}
case WStype_TEXT:
{
Serial.printf("[%u] get Text: %s\n", num, payload);
if(payload[0] == 'V') {
uint32_t velocidad = (uint32_t) strtol((const char *) &payload[1], NULL, 0);
Serial.printf("Velocidad:[%u]\n", velocidad);
analogWrite(PIN_ENABLED_VELOCIDAD, velocidad);
}
else if(payload[0] == 'G') {
uint32_t giro = (uint32_t) strtol((const char *) &payload[1], NULL, 0);
Serial.printf("Giro:[%u]\n", giro);
servoGiro.write(giro);
delay(5);
}
else if(payload[0] == 'R') {
uint8_t isReversa = (uint8_t) strtol((const char *) &payload[1], NULL, 0);
Serial.printf("Reversa:[%u]\n", isReversa);
reversaVelocidad((bool)isReversa);
}
break;
}
}
}
void reversaVelocidad(uint8_t isReversa)
{
if(isReversa == 1)
{
digitalWrite(PIN_CONTROL_A_VELOCIDAD, 0);
digitalWrite(PIN_CONTROL_B_VELOCIDAD, 1);
}
else
{
digitalWrite(PIN_CONTROL_A_VELOCIDAD, 1);
digitalWrite(PIN_CONTROL_B_VELOCIDAD, 0);
}
}
void prepareFile(){
Serial.println("Prepare file system");
SPIFFS.begin();
File file = SPIFFS.open("/home.html", "r");
if (!file) {
Serial.println("file open failed");
} else{
Serial.println("file open success");
html_home = "";
while (file.available()) {
//Serial.write(file.read());
String line = file.readStringUntil('\n');
html_home += line + "\n";
}
file.close();
Serial.print(html_home);
}
}
void setup() {
Serial.begin(115200);
Serial.println();
for(uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] BOOT WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
//Configuro funcionamiento pin
//Velocidad
pinMode(PIN_ENABLED_VELOCIDAD, OUTPUT);
pinMode(PIN_CONTROL_A_VELOCIDAD, OUTPUT);
pinMode(PIN_CONTROL_B_VELOCIDAD, OUTPUT);
//Giro
servoGiro.attach(PIN_CONTROL_GIRO);
//Cargo HTML
prepareFile();
//Configuro WIFI
WiFi.softAP("CamionMan", "12345678");
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
// start webSocket server
webSocket.begin();
webSocket.onEvent(webSocketEvent);
//MDNS
if(MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
// handle index
server.on("/", []() {
// send home.html
server.send(200, "text/html", html_home);
});
server.begin();
// Add service to MDNS
MDNS.addService("http", "tcp", 80);
MDNS.addService("ws", "tcp", 81);
Serial.printf("Server Start\n");
//Inicializo PINES por defecto
//Velocidad
digitalWrite(PIN_ENABLED_VELOCIDAD,LOW);
digitalWrite(PIN_CONTROL_A_VELOCIDAD,HIGH);
digitalWrite(PIN_CONTROL_B_VELOCIDAD, LOW);
//Giro
digitalWrite(PIN_CONTROL_GIRO, LOW);
servoGiro.write(90);
}
void loop() {
webSocket.loop();
server.handleClient();
}