servo and software serial, conflicting libraries (timer)

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.

I think that Servo library and SoftWareSerial both use Timer1 so there is a conflict. I have seen it suggested that one use the ServoTimer2 library. Here is a page that shows how to install and use the library.

seems to not have resolved the issue, every time I connect with a client the servo briefly jumps back to its 0 position.

Is there a reason you can't use hardware serial ?

daba:
Is there a reason you can't use hardware serial ?

I'm not sure, indeed i'm not really using it, only for debug superpose. But i'm using one of those esp8266 boards directly connected to the uno

Can you connect those to the hardware serial?

directly connected to the uno

Shields can be directly connected to the UNO. Breakout boards, like the one you posted a URL for, instead of a link, can not.

So, yes, you can connect wires from that board to any pins on the UNO, including connecting them to the hardware serial pins.

PaulS:
Shields can be directly connected to the UNO. Breakout boards, like the one you posted a URL for, instead of a link, can not.

So, yes, you can connect wires from that board to any pins on the UNO, including connecting them to the hardware serial pins.

I've seen no one use the esp-01 board and directly connect them to rx and tx for use, only for programming that board itself. but I'm just sending json data from a computer program and passing it to the arduino. all the information I can find uses software serial?

to resolve this issue, I ended up using the PWNServo library instaid of the normal servo library.