hey!
I'm working on a project that's using an uno and an ESP8266 board to wirelessly control some things, including a servo. the problem that I'm having is that every time i get a client connection from the esp, using softwareSerial, the serve resets itself for a brief second. I've read somewhere that softwareSerial blocks the interrupts that are necessary for the servo. hopefully my code will make everything a but more clear.
#include "WiFiEsp.h"
#include <ArduinoJson.h>
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
#include <Servo.h>
Servo myservo;
const int STEERING = 7;
const int FORWARD = 8;
const int BACKWARD = 9;
// set up software serial to allow serial communication to our TX and RX pins
SoftwareSerial Serial1(10, 11);
#endif
#define ESP8266_BAUD 9600
char ssid[] = "mySsid";
char pass[] = "myPassword";
int status = WL_IDLE_STATUS;
WiFiEspServer server(80);
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
WiFi.init(&Serial1);
while (status != WL_CONNECTED){
Serial.print("Conecting to wifi network: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
}
// Once we are connected log the IP address of the ESP module
Serial.print("IP Address of ESP8266 Module is: ");
Serial.println(WiFi.localIP());
Serial.println("You're connected to the network");
// Start the server
server.begin();
myservo.attach(STEERING);
pinMode(FORWARD, OUTPUT);
pinMode(BACKWARD, OUTPUT);
myservo.write(90);
}
void loop() {
WiFiEspClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()){
// get json data
client.readStringUntil('{');
String jsonStrWithoutBrackets = client.readStringUntil('}');
String jsonStr = "{" + jsonStrWithoutBrackets + "}";
if (jsonStr.indexOf('{', 0) >= 0) {
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 20;
DynamicJsonBuffer jsonBuffer(bufferSize);
JsonObject &root = jsonBuffer.parseObject(jsonStr);
// values from json data
char Y = root["Ycontrol"];
char X = root["Xcontrol"];
Serial.println(X + ", " + Y);
// e.g. if X = 1, then change the servo to ...
// send response and close connection
client.print(
"HTTP/1.1 200 OK\r\n"
"Connection: close\r\n"
"\r\n");
client.stop();
} else {
client.print(
"HTTP/1.1 500 ERROR\r\n"
"Connection: close\r\n"
"\r\n");
Serial.println("[Error] bad or missing json");
client.stop();
}
}
}
client.stop();
}
}
I know a mega would solve this problem, but I don't have one. I'm asking if there is a better approach that would fix my issue.