-1

I have a setup where I have a couple of Arduinos communicating with Node Red on a Pi, using UDP messages (both sending and receiving).

I want to extend this with some wireless devices (esp8266). This esp8266 is connected to an Arduino using SoftwareSerial. The baudrate of the esp8266 has been set to 9600. The esp8266 has a separate 3.3V power supply and the Rx communication with the Arduino is done at 3.3V (voltage divider).

I am experiencing strange behavior: if I comment the line (in the loop function)

Serial.println("ok")

the script seems to hang, or at least is not capturing incoming UDP messages, see this output from the Serial monitor: enter image description here.

In case the Serial.println("ok") is enabled, the script does capture incoming UDP messages, see below output: enter image description here

#include <WiFiEsp.h>
#include <WiFiEspUdp.h>
#include "DHT.h"
#include "SoftwareSerial.h"
#define RX 3  //  connected to the TX of the ESP8266
#define TX 2 // connected to the RX of the ESP8266
#define DHTPIN A5     // pin used for the sensor
#define relayACT1 7   // this is relay 1
#define relayACT2 6   // this is relay 2
#define relayLED 5    // this is relay 3
#define relayFAN 4    // this is relay 4
#define inButton 13   // input from pushbutton
#define DEBUG true
#define DHTTYPE DHT22     // DHT 22  (AM2302)
#define localPort 1235  // local port to listen on for incoming UDP messages // 1235 voor badkamer
#define sendPort 1236  // receiver's port to  send UDP messages to

const char ssid[] = "first_floor2Ghz";
const char pass[] = "xxx";        // your network password
int statusWifi = WL_IDLE_STATUS;       // the Wifi radio's status

unsigned long lastPing = 0; // hold timer for ping response


bool statusRoof = LOW;
bool statusFan = LOW;
bool previousButton = LOW;

char packetBuffer[255];          // buffer to hold incoming UDPpacket

DHT dht(DHTPIN, DHTTYPE);
SoftwareSerial esp8266(RX, TX); // RX, TX
WiFiEspUDP Udp;

void setup() {
  pinMode(relayACT1, OUTPUT);
  pinMode(relayACT2, OUTPUT);
  pinMode(relayLED, OUTPUT);
  pinMode(relayFAN, OUTPUT);
  pinMode(inButton, INPUT_PULLUP);
  digitalWrite(relayACT1, LOW);
  digitalWrite(relayACT2, HIGH);
  digitalWrite(relayLED, HIGH);
  digitalWrite(relayFAN, HIGH);
  Serial.begin(9600);
  esp8266.begin(9600);
  WiFi.init(&esp8266);
  setwifiudp();
  roofclose();
  fanoff();
}

void loop() {
  if ( WiFi.status() != WL_CONNECTED ) {
    if (DEBUG) {
      Serial.println("!!!!WIFI DISCONNECTED!!!!");
    }
    delay(50000);
    setwifiudp();
    return;
  }
  if (!checkping()) {
    if (DEBUG) {
      Serial.println("can not ping....");
    }
    delay(50000);
    setwifiudp();
  }
  ////////////////////////////////////////////  BELOW LINE MAKES A BIG DIFFERENCE....
  Serial.println("ok");

  char* returnboodschap = getUDPmessage();
  if (DEBUG) {
    if (returnboodschap != "") {
      Serial.print(F("volgende boodschap kwam binnen: "));
      Serial.println(returnboodschap);
    }
  }
  float humidity;
  float temperature;
  if (strcmp(returnboodschap, "REQUEST=temp") == 0) {
    getvalues(&humidity, &temperature);
    char totalmessage[30] = {0};
    char temp[10] = {0};
    sprintf(totalmessage, "REPLY=temp:%s", dtostrf(temperature, 5, 2, temp));
    sendUDPmessage(totalmessage);
  }
  if (strcmp(returnboodschap, "REQUEST=hum") == 0) {
    getvalues(&humidity, &temperature);
    char totalmessage[30] = {0};
    char hum[10] = {0};
    sprintf(totalmessage, "REPLY=humi:%s", dtostrf(humidity, 5, 2, hum));
    sendUDPmessage(totalmessage);
  }
  if (strcmp(returnboodschap, "SEND=dak:open") == 0) {
    roofopen();
  }
  if (strcmp(returnboodschap, "SEND=dak:close") == 0) {
    roofclose();
  }
  if (strcmp(returnboodschap, "SEND=fan:on") == 0) {
    fanon();
  }
  if (strcmp(returnboodschap, "SEND=fan:off") == 0) {
    fanoff();
  }
}

void sendUDPmessage(char* message) {
  Udp.beginPacket(IPAddress(192, 168, 15, 39), sendPort);
  Udp.write(message);
  Udp.endPacket();
  delay(100);
}

char* getUDPmessage() {
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    if (DEBUG) {
      Serial.print(F("Received packet of size "));
      Serial.println(packetSize);
      Serial.print(F("From "));
      IPAddress remoteIp = Udp.remoteIP();
      Serial.print(remoteIp);
      Serial.print(F(", port "));
      Serial.println(Udp.remotePort());
    }
    // read the packet into packetBufffer
    int len = Udp.read(packetBuffer, 255);
    if (len > 0) {
      packetBuffer[len] = 0;
    }
    delay(100);
    return packetBuffer;
  }
  return "";
}

void setwifiudp() {
  statusWifi = WiFi.status();
  Serial.print(F("status WIFI: "));
  Serial.println(statusWifi);
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println(F("WiFi shield not present"));
    while (true);
  }
  while ( statusWifi != WL_CONNECTED) {
    Serial.print(F("Attempting to connect to WPA SSID: "));
    Serial.println(ssid);
    statusWifi = WiFi.begin(ssid, pass);
    WiFi.config(IPAddress(192, 168, 15, 40));
  }
  delay(1000);
  Serial.println(F("Connected to wifi"));

  Serial.println(F("\nStarting connection to server..."));
  Serial.print(F("SSID: "));
  Serial.println(WiFi.SSID());
  long rssi = WiFi.RSSI();
  Serial.print(F("signal strength (RSSI):"));
  Serial.print(rssi);
  Serial.println(F(" dBm"));

  Udp.begin(localPort);
  Serial.print(F("Listening on port "));
  Serial.println(localPort);
  sendUDPmessage("BADKAMER:live!!");
}

bool checkping() {
  if (millis() - lastPing > 100000) {
    lastPing = millis();
    int pingResult = WiFi.ping("192.168.15.1");
    if (pingResult >= 0) {
      return 1;
    } else {
      return 0;
    }
  }
  else {
    return 1;
  }
}

void getvalues(float * h, float * t) {
  *h = dht.readHumidity();
  *t = dht.readTemperature();
}

void roofopen() {
  if (DEBUG)
  {
    Serial.println(F("dak moet nu omhoog"));
  }
  digitalWrite(relayACT1, HIGH);
  digitalWrite(relayACT2, LOW);
  digitalWrite(relayLED, LOW);
  statusRoof = 1;
  sendUDPmessage("REPLY=dak:open");
  delay(200);
}

void roofclose() {
  if (DEBUG)
  {
    Serial.println(F("dak moet nu omlaag"));
  }
  digitalWrite(relayACT1, LOW);
  digitalWrite(relayACT2, HIGH);
  digitalWrite(relayLED, HIGH);
  statusRoof = 0;
  sendUDPmessage("REPLY=dak:close");
  delay(200);
}

void fanon() {
  if (DEBUG)
  {
    Serial.println("fan moet nu aan");
  }
  digitalWrite(relayFAN, LOW);
  statusFan = 1;
  sendUDPmessage("REPLY=fan:on");
  delay(200);
}

void fanoff() {
  if (DEBUG)
  {
    Serial.println(F("fan moet nu uit"));
  }
  digitalWrite(relayFAN, HIGH);
  statusFan = 0;
  sendUDPmessage("REPLY=fan:off");
  delay(200);
}

I presume there must be somehow an issue with the Serial buffer, but can't pinpoint the issue.

Note that I had a working set-up before (6 weeks working without any interruption); the thing I changed since then, is: 1) using the F() macro for the debug messages to save some dynamic memory 2) adding the first IF statement in the loop to reconnect to the network in case the connection would be lost (e.g. router reset)

Any thoughts on what could cause this behaviour?

  • Try a higher baud rate if you can. From what I have read 9600 soft serial can be a problem. You would think slow would be ok but that doesn't seem to be the case. – Rudy Dec 14 '18 at 17:26
  • the println("ok") creates a small pause between status() and parsePacket() – Juraj Dec 14 '18 at 17:44
  • thanks @Juraj, adding a delay(1) resolves the issue. I am surprised this makes a difference here, but the issue got solved. – pieterdp Dec 14 '18 at 18:15

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.