I get "Warning: Unsuccessful read: The specified amount of data was not returned within the Timeout period" error when using fread() & " A timeout occurred before the Terminator was reached" Error when using fscanf().

41 views (last 30 days)
Hello everybody,
I am running a data acquisition process code, the connection is done by UDP protocol between NodeMcu v1.0 (which gets the data from arduino mega via UART) and Matlab R2017b. My problem is that the data acquisition process stops sometimes after 30 min somtimes 1.5 hour , 4 hours... (after a random timing maybe random because i changed the code lot of times and maybe not) & i get "Warning: Unsuccessful read: The specified amount of data was not returned within the Timeout period" Error when using fread() & "Warning: Unsuccessful read: A timeout occurred before the Terminator was reached" Error when using fscanf().
I searched a lot about this error but unfortunately all the potential solutions wasn't the right one for my problem, including:
  • feeding the watchdog of the nodemcu by adding:
%addig in setup()
ESP.wdtDisable();
ESP.wdtEnable(15000);
%and in loop()
ESP.wdtFeed();
  • specefing the size of fread()
  • changing settings of UDP object (- changing the buffersize - activating/deactivating datagramterminatemode - changing the timeout ...)
  • blocking the firewall also the antivirus (AVG) while the code is running
  • running my microcontroller Nodemcu separately from arduino (using 5V 1A adapter)
* This is my matlab r2017b code:
%Linking code between Arduino monitor serial and Matlab workspace
clear
clc
disp('*****Data Acquisition process ACTIVATED*****')
delete(instrfindall);
HebiKeyboard;
% s = serial('COM5');
% set(s,'BaudRate',9600)
echoudp('off');
[y, Fs] = audioread('D:\MATLAB PHD Studies\Arduino and Matlab\NiallHoran.mp3');
player = audioplayer(y, Fs);
kb = HebiKeyboard();
i=1;
j=1;
while (1)
start = tic;
while(i<=6)
start1=tic;
% fopen(s);
% s.ReadAsyncMode = 'continuous';
% Start asynchronous reading
% readasync(s);
% Get the data via WIFI - UDP object
% fprintf(UDP_obj, 'Ready to recieve some data');
echoudp('on',5354);
UDP_obj = udp('192.168.1.2','localport',9091,'remoteport',5354,'timeout',15,'DatagramTerminateMode','on');
UDP_obj.InputBufferSize=200;
UDP_obj.OutputBufferSize=40;
fopen(UDP_obj);
fwrite(UDP_obj,'Ready to recieve');
UDP_out1=fread(UDP_obj);
UDP_out2=char(UDP_out1)';
out=str2num(UDP_out2)'
% out = fscanf(UDP_obj,'%f',12)
fclose(UDP_obj);
delete(UDP_obj);
clear UDP_obj;
echoudp('off');
% UDP_obj.ReadAsyncMode = 'continuous';
% readasync(UDP_obj);
% fprintf(s, 'Your serial data goes here');
%fprintf(UDP_obj, 'Your serial data goes here');
% Get the data from the serial object
% out = fscanf(s,'%f %f', 80)
%variables measurment
tin1(i)=out(1,1);
tin2(i)=out(2,1);
tout(i)=out(3,1);
hin1(i)=out(4,1);
hin2(i)=out(5,1);
hout(i)=out(6,1);
Ir1(i)=out(7,1);
Ir2(i)=out(8,1);
Ir3(i)=out(9,1);
Ir4(i)=out(10,1);
Wv(i)=out(11,1);
Rain(i)=out(12,1);
if isnan(tin1(i)) || isnan(tin2(i)) || isnan(tout(i))
k=i-1;
if k==0
k=6;
end
tin1(i)=tin1(k);
tin2(i)=tin2(k);
tout(i)=tout(k);
hin1(i)=hin1(k);
hin2(i)=hin2(k);
hout(i)=hout(k);
end
% if Ir1<10 & Ir2<10 & Ir3<10 & Ir4<10
% Ir1(i)=0;
% Ir2(i)=0;
% Ir3(i)=0;
% Ir4(i)=0;
% end
% fclose(s);
Tt=0;
while (Tt<10.00)
Tt=toc(start1);
state = read(kb);
if state.SHIFT
stop(player)
disp('*****Rain Alarm sound has stopped*****')
end
end
i=i+1
end
i=6;
Tin1(j)=tin1(i);
Tin2(j)=tin2(i);
Tout(j)=tout(i);
Hin1(j)=hin1(i);
Hin2(j)=hin2(i);
Hout(j)=hout(i);
IrrMoy(j)=((sum(Ir1)/6)+(sum(Ir2)/6)+(sum(Ir3)/6)+(sum(Ir4)/6))/4;
WvMoy(j)=sum(Wv)/6;
fid1=fopen('Greenhouse Variables (DATABASE).txt','a');
if j==1
header='Date Time Tin1 Tin2 Tout Hin1 Hin2 Hout Irr Wv Rain';
fprintf(fid1,'%s\r\n',header);
end
D=datetime('now')
DateString = datestr(now,'dd/mm/yyyy HH:MM:SS ');
par=[tin1(i) tin2(i) tout(i) hin1(i) hin2(i) hout(i) IrrMoy(j) WvMoy(j) Rain(i)]';
myformat='%3.2f %3.2f %3.2f %3.2f %3.2f %3.2f %4.2f %3.2f %3.2f\n';
fprintf(fid1,[DateString myformat],par);
fclose(fid1);
%Rain Alarm:::
if Rain(i)>100
play(player)
end
% Online_Plot;
j=j+1;
i=1;
toc(start)
end
*This is the code of NodeMcu v1.0:
//NodeMCU code
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <SoftwareSerial.h>
SoftwareSerial s(D6,D5); //RX TX
#include <ArduinoJson.h>
//Server initializations :::
const char* ssid = "Rida";
const char* password = "ridagues";
WiFiUDP Udp;
unsigned int localUdpPort = 5354; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
char replyPacket[240]; // a reply string to send back
WiFiServer server(localUdpPort);
void setup() {
ESP.wdtDisable();
ESP.wdtEnable(WDTO_8S);
// time1=millis(); // set time as now
// time2=millis();
//Router Mode:::
Serial.begin(115200);
s.begin(115200);
while (!Serial) continue;
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
////Access Point Mode:::
// Serial.begin(115200);
// s.begin(115200);
// while (!Serial) continue;
//
// Serial.print("Configuring access point...");
// WiFi.disconnect();
// delay(100);
// WiFi.mode(WIFI_STA);
// // You can remove the password parameter if you want the AP to be open.
// WiFi.softAP(ssid, password);
//
// IPAddress myIP = WiFi.softAPIP();
// Serial.print("AP IP address: ");
// Serial.println(myIP);
//
//// Start the server
// server.begin();
// Serial.println("Server started");
//// Print the IP address
// Serial.println(WiFi.localIP());
Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
}
void loop() {
ESP.wdtFeed();
yield();
//Waiting for incoming UDP packet is done by the following code:
int packetSize = Udp.parsePacket();
if (packetSize)
{
ESP.wdtFeed();
yield();
// receive incoming UDP packets
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len = Udp.read(incomingPacket, 255);
if (len > 0)
{
ESP.wdtFeed();
yield();
incomingPacket[len] = 0;
}
Serial.printf("UDP packet contents: %s\n", incomingPacket);
//send back a reply, to the IP address and port we got the packet from
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(replyPacket);
Udp.endPacket();
}
StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(s);
if (root == JsonObject::invalid())
{
ESP.wdtFeed();
yield();
return;
}
//root.prettyPrintTo(Serial);
Serial.println("");
float data1=root["data1"];
Serial.print(data1);
Serial.print(" ");
float data2=root["data2"];
Serial.print(data2);
Serial.print(" ");
float data3=root["data3"];
Serial.print(data3);
Serial.print(" ");
float data4=root["data4"];
Serial.print(data4);
Serial.print(" ");
float data5=root["data5"];
Serial.print(data5);
Serial.print(" ");
float data6=root["data6"];
Serial.print(data6);
Serial.print(" ");
float data7=root["data7"];
Serial.print(data7);
Serial.print(" ");
float data8=root["data8"];
Serial.print(data8);
Serial.print(" ");
float data9=root["data9"];
Serial.print(data9);
Serial.print(" ");
float data10=root["data10"];
Serial.print(data10);
Serial.print(" ");
float data11=root["data11"];
Serial.print(data11);
Serial.print(" ");
float data12=root["data12"];
Serial.println(data12);
//convert float values to char String:::
snprintf(replyPacket, sizeof(replyPacket), "%.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f", data1,data2,data3,data4,data5,data6,data7,data8,data9,data10,data11,data12);
ESP.wdtFeed();
yield();
delay(1000);
}
*This is the shape of my data where the size is always [12,1] and I get it at every 10 secondes loop:
out =
8.6000
8.4000
8.5000
44.5000
47.1000
72.0000
0
0
0
0
0
0
... So my friends I think I have mentiened any information that could help you to help me to find a solution, some one save me please ??? I appreciate any kind of help

Accepted Answer

Mounir guesbaya
Mounir guesbaya on 21 Jan 2019
Finaly, Solved ... The solution was by giving a space time till the data is recieved or in other way by making the code waiting for the data to be available, then continue the remaining sections of the code.
The solution was performed by this loop, where if there is an available data the code will continue but if there isn't the code will wait till the availibility of data.
while( true )
fwrite(UDP_obj,'Ready to recieve');
pause(2)
bytes = UDP_obj.BytesAvailable
% analysis
if bytes > 0
break
end
end

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!