0

This code works fine without any freezes:

#include <ESP8266WiFi.h>
#include <Servo.h>
#include <WiFiClient.h>
#include <ESP8266WiFiMulti.h>

Servo turn;

ESP8266WiFiMulti wifiMulti;

const char* host = "192.168.1.148";
WiFiClient client;

int turnPos = 90;
unsigned long ts = 0;

void setup() {
  startWiFi();
  turn.attach(14);
  turn.write(90);
}

void loop() {

  if (millis() - ts > 500 && millis() - ts < 1000) {
    turnPos = 160;
  }
  else if (millis() - ts >= 1000) {
    ts = millis();
  }
  else {
    turnPos = 20;
  }


  /*if (client.connected()) {
    }
    else {
    if (!client.connect(host, 3000)) {
    }
    }*/


  turn.write(turnPos);
  yield();
}

void startWiFi() {
  wifiMulti.addAP("DVW3201BE8", "###");   // password is filled in on mny version
  while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
    delay(250);
    Serial.print('.');
  }
}

While this code (with the wificlient bit commented out) freezes every so often:

#include <ESP8266WiFi.h>
#include <Servo.h>
#include <WiFiClient.h>
#include <ESP8266WiFiMulti.h>

Servo turn;

ESP8266WiFiMulti wifiMulti;

const char* host = "192.168.1.148";
WiFiClient client;

int turnPos = 90;
unsigned long ts = 0;

void setup() {
  startWiFi();
  turn.attach(14);
  turn.write(90);
}

void loop() {

  if (millis() - ts > 500 && millis() - ts < 1000) {
    turnPos = 160;
  }
  else if (millis() - ts >= 1000) {
    ts = millis();
  }
  else {
    turnPos = 20;
  }


  if (client.connected()) {
    }
    else {
    if (!client.connect(host, 3000)) {
    }
    }


  turn.write(turnPos);
  yield();
}

void startWiFi() {
  wifiMulti.addAP("DVW3201BE8", "###");   // password is filled in on mny version
  while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
    delay(250);
    Serial.print('.');
  }
}

Only difference is in the working one the WifiClient bit of code is commented out. How would WifiClient cause this problem?

  • fun with esp8266. try new core package 2.4.2 – Juraj Aug 4 '18 at 9:26
  • I see you're using String in a terrible way. If this snippet is anything to go by you're probably doing the same thing elsewhere, and making swiss cheese of your heap. – Majenko Aug 4 '18 at 9:53
  • 1
    The problem still occurs without the String bit -- I have edited my question to include all of my code. – Murey Tasroc Aug 4 '18 at 20:49
  • yes and this is fixed in latest version of arduino esp8266 core – Juraj Aug 5 '18 at 13:09
  • Are you sure? How can I check the version? Because I think I have the most updated version. – Murey Tasroc Aug 5 '18 at 13:11
2

It is a bug and it was fixed in Arduino esp8266 core package 2.4.2 published 2 days ago.

see first line in ESP8266WiFi section of the changes list

2.4.2 ESP8266WiFi: Fix memory leak when reusing WiFiClient object (#4497, #4516, #4549)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.