Skip to content

Random crash ESP286 WEMOS D1 DHT 22 server  #1594

@hymby

Description

@hymby

Random crash ESP286 WEMOS D1

a couple of hours Tested result: crash Wemos D1
what causes this?

///////////////////////////////////////////////////////////////////////////
Arduino side

# include <Ethernet.h>
# include <SPI.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE1};///////////!!
IPAddress ip(192, 168, 1, 175);
IPAddress gateway(192, 168, 1, 254);
IPAddress subnet(255, 255, 255, 0);
EthernetClient client;

IPAddress server(192, 168, 1, 11); ////////////////// WEMOS D1 Side

long mintavetelIdeje = 0;
const long mintavetelSzunet = 5000;

void setup() {
  Serial.begin(9600);
  Serial.println("start");
  Ethernet.begin(mac, ip);
  delay(1000);
}

String request(char what) {
  String value = "";
  if (client.connect(server, 85)) {
    client.println(what);
    delay(500);
    while (client.available()) {
      char c = client.read();
      value.concat(c);
    delay(200);
  }
    client.stop();
  }
  return value;
}

void loop() {
  if (millis() - mintavetelIdeje > mintavetelSzunet) {
    mintavetelIdeje = millis();
    String temp = request('h');
    String hum = request('l');
    String pinState = request('p');
    Serial.println(temp + "\n" + hum + "\n" + pinState + "\n");

 }
}

//////////////////////////////////////////////////////////////////////////////////////////////

WEMOS D1 R2 side

# include <ESP8266WiFi.h>

WiFiServer server(85);////////////////////////////Arduino  Side!!
# include "DHT.h"   // dht  1.1.1   !!!!!
# define DHTPIN D2
# define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

const char\* ssid = ".......";
const char\* password = "........";

String temp;
float humidity_h;
float temp_c;
String value ;
unsigned long dht_read_time = 0;
unsigned long dht_read_time_when = 5000;
int ledState = LOW;
unsigned long previousMillis = 0;
unsigned long interval = 100;
const int led = D6;
long kul;
String data;
String pinState(int pin) {
  pinMode(pin, INPUT);
  pinMode(pin, HIGH);
  switch (digitalRead(pin)) {
    case 0: return "LOW";
    case 1: return "HIGH";
  }
}

void Strobe() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
    digitalWrite(led, ledState);
  }
}

void setup(void) {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  dht.begin();
  Serial.println("");
  pinMode ( led, OUTPUT );
  digitalWrite ( led, 0 );

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}

void dht_read() {
  if (millis() - dht_read_time > dht_read_time_when) {
    dht_read_time = millis();
    temp_c = dht.readTemperature();
    humidity_h = dht.readHumidity();
  }
}

void accept_request() {
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  Serial.println("new client");
  while (!client.available()) {
    delay(1);
  }
  char c = client.read();
  switch (c) {
    case 'h':
      value = String(temp_c, 1);
      delay(100);
      break;
    case 'l':
      value = String(humidity_h, 1);
      delay(100);
      break;
    case 'p':
      value = pinState(D7);
      delay(100);
      break;
    default:
      value = "";
      delay(100);
      break;
  }
  if (!value.equals("")) {
    client.print(value);
    delay(1);
    Serial.print(c);
    Serial.println(": " + value);
  }
  delay(1);
  Serial.println("Client disonnected");
}

void loop(void) {
  dht_read();
  accept_request();
  Strobe();

  if (millis() - kul > 1000) {
    kul = millis();
    Serial.print("Free heap:");
    Serial.println(ESP.getFreeHeap(), DEC);
  }
}

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions