Hi, my project is to controll 4 servos via WIFI with my smartphone. I'm very new to app design and wifi requests. The app i designed is visually working fine but the values I'm receiving on the nodeMCU are glitchy. The data sent by the app is the 4 positions that the servos need to target. The 4 variables are not glitching in the app, i can change them with joysticks and display them without any issues. But the values that are printed to the Serial monitor of the NodeMCU are glitchy. There is quite some lag (between 500ms and 1s) and the past values sometimes are printed again. this causes the Servos to do weird movement.
An example of the glitch when requesting the values 16, 21, 0, 0 for the servos :
{"pos1":16,"pos2":21,"pos3":0,"pos4":0}
{"pos1":16,"pos2":21,"pos3":0,"pos4":0}
{"pos1":16,"pos2":21,"pos3":0,"pos4":0}
{"pos1":16,"pos2":21,"pos3":0,"pos4":0}
{"pos1":16,"pos2":21,"pos3":0,"pos4":0}
{"pos1":16,"pos2":21,"pos3":0,"pos4":0}
{"pos1":16,"pos2":21,"pos3":0,"pos4":0}
{"pos1":133,"pos2":50,"pos3":0,"pos4":2}
{"pos1":16,"pos2":21,"pos3":0,"pos4":0}
{"pos1":128,"pos2":39,"pos3":0,"pos4":2}
{"pos1":128,"pos2":39,"pos3":0,"pos4":2}
{"pos1":128,"pos2":39,"pos3":0,"pos4":2}
{"pos1":128,"pos2":39,"pos3":0,"pos4":2}
{"pos1":128,"pos2":39,"pos3":0,"pos4":2}
{"pos1":16,"pos2":21,"pos3":0,"pos4":0}
{"pos1":16,"pos2":21,"pos3":0,"pos4":0}
{"pos1":16,"pos2":21,"pos3":0,"pos4":0}
{"pos1":16,"pos2":21,"pos3":0,"pos4":0}
{"pos1":16,"pos2":21,"pos3":0,"pos4":0}
Here is my code from the app :
Here is my code for the NodeMCU:
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "Servo.h"
const char* ssid = "...";
const char* password = "...";
ESP8266WebServer server(80);
// Define output pin
const int servoPin1 = D0;
const int servoPin2 = D1;
const int servoPin3 = D2;
const int servoPin4 = D3;
int servoPos1 = 0;
int servoPos2 = 0;
int servoPos3 = 0;
int servoPos4 = 0;
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
void setup() {
Serial.begin(115200);
servo1.attach(servoPin1, 500, 2400);
servo2.attach(servoPin2, 500, 2400);
servo3.attach(servoPin3, 500, 2400);
servo4.attach(servoPin4, 500, 2400);
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected. IP address: ");
Serial.println(WiFi.localIP());
// Define route: root
server.on("/", []() {
server.send(200, "text/plain", "NodeMCU is running");
});
server.on("/update", []() {
if (server.hasArg("pos1")) servoPos1 = server.arg("pos1").toInt();
if (server.hasArg("pos2")) servoPos2 = server.arg("pos2").toInt();
if (server.hasArg("pos3")) servoPos3 = server.arg("pos3").toInt();
if (server.hasArg("pos4")) servoPos4 = server.arg("pos4").toInt();
String json = "{\"pos1\":" + String(servoPos1) + ",\"pos2\":" + String(servoPos2) + ",\"pos3\":" + String(servoPos3) + ",\"pos4\":" + String(servoPos4) + "}";
Serial.println(json);
server.send(200, "application/json", json);
});
// Start server
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
servo1.write(servoPos1);
servo2.write(servoPos2);
servo3.write(servoPos3);
servo4.write(servoPos4);
}
Any idea what the problem could be?