Recent

Author Topic: [SOLVED] TTCPClient and Stream Write Error (Raspberry Pi 3B+)  (Read 3211 times)

washburn_it

  • New Member
  • *
  • Posts: 29
[SOLVED] TTCPClient and Stream Write Error (Raspberry Pi 3B+)
« on: September 29, 2021, 12:03:58 pm »
Hi,

almost 2 years ago I developed a small Lazarus program that reads electric consumptions data from an Eastron SDM-120C and transmits those data to a local python program that sends those data via MQTT to Cayenne "MyDevices" web service.
The "thing" has worked correctly for all this time, now the error in the subject started to appear.
The Lazarus program, the python program and Raspbian were not updated or modified during all this time.
The communication between the Lazarus program and the python program is made through TCP where the python program acts as server.
The component that I used is the "TTCPClient", Lazarus is the v2.0 (the one present in the Raspbian Buster repository).
The error "stream write error" appears after some minutes (never after the same amount of time) but I can't find the reason why, apparently it's all ok.
The thing that I'm sure of is that the python program is running and "listening" for a client to connect (I can read it on the screen).
When the error appears I have to close and execute the Lazarus program again, it works for 10/15 minutes and then back again the error.
The sequence is:

- data are read from the SDM120C
- data are sent to the python program via TCP (port 9000)
- connection is closed
- delay of 2 seconds then the cycle repeats

what should I check?
Surely something happened but, at the moment, I have no idea what happened.
Thank you, regards.


Roberto
« Last Edit: September 29, 2021, 09:05:11 pm by washburn_it »

rvk

  • Hero Member
  • *****
  • Posts: 6171
Re: TTCPClient and Stream Write Error (Raspberry Pi 3B+)
« Reply #1 on: September 29, 2021, 04:45:41 pm »
I don't use Lazarus for my SDM120C but I use a small c++ program to read from it (https://github.com/gianfrdp/SDM120C). Then I just use a bash file which reads the SDM120C and directly push it into a influxdb server every few seconds.

But your problems are with writing to the python program... Why don't you directly send data to MQTT from Lazarus in that case? It would think that would be much simpler. https://github.com/bkeevil/mqtt Or does that python program do something additional?

washburn_it

  • New Member
  • *
  • Posts: 29
Re: TTCPClient and Stream Write Error (Raspberry Pi 3B+)
« Reply #2 on: September 29, 2021, 05:00:18 pm »
I used this way because I never found a MQTT implementation for Lazarus under Raspberry so since python had already specific Cayenne libraries, I choose to send data from "Lazarus" to "python" using a simple tcp communication.
I'm also using the c++ program that you mentioned to read from the SDM, I put the readings into a text file that the Lazarus program reads and extracts data.
It's not the python program that does something "more" but the Lazarus program that activates an alarm (piezo) if the current power gets over 3.3 kW (it's the max power allowed in a house) and display current data (and some other calculated "statistics") on a screen.

I had a look at that github link but he used LNet libraries that I used in the past and I had to uninstall due to a lot of issues.

« Last Edit: September 29, 2021, 05:04:11 pm by washburn_it »

rvk

  • Hero Member
  • *****
  • Posts: 6171
Re: TTCPClient and Stream Write Error (Raspberry Pi 3B+)
« Reply #3 on: September 29, 2021, 05:12:48 pm »
I'm also using the c++ program that you mentioned to read from the SDM, I put the readings into a text file that the Lazarus program reads and extracts data.
Yikes. That's another (two) layer(s)/program(s)  :D

So, from SDM120C program to text-file to Lazarus to Python to MQTT  :D
A lot of places things can go wrong.

It's not the python program that does something "more" but the Lazarus program that activates an alarm (piezo) if the current power gets over 3.3 kW (it's the max power allowed in a house) and display current data on a screen.
Ah, ok. I would at least eliminate one program. So either directly read the SDM120C output into python (which sends it to MQTT but you would need to know how to program in python) or do the same with Lazarus. I would also read the SDM120C output directly from input and not via a file (https://wiki.freepascal.org/Executing_External_Programs).

BTW. The github link for MQTT in Lazarus was just the first one I could find. There are (a lot) more I'm sure. Searching for MQTT on this forum would help too.
(This is another one with synapse instead of LNET https://github.com/ZiCog/mqtt-free-pascal)

O, and why are you communicating via port 9000 with that python program? Is that a standard program? Can't it take other forms of input?

And what does MQTT do with the data?
« Last Edit: September 29, 2021, 05:17:50 pm by rvk »

washburn_it

  • New Member
  • *
  • Posts: 29
Re: TTCPClient and Stream Write Error (Raspberry Pi 3B+)
« Reply #4 on: September 29, 2021, 05:50:59 pm »
Quote
output directly from input and not via a file (https://wiki.freepascal.org/Executing_External_Programs).

well, it may be an idea  :D

Quote
O, and why are you communicating via port 9000 with that python program? Is that a standard program? Can't it take other forms of input?

I used that port value because I know that certainly it's not used locally, I could have used another port (e.g 9080) but it's just the port to communicate with the python program that is acting as TCP server.

Could you suggest other ways to communicate with python?

Quote
And what does MQTT do with the data?

MQTT is the protocol used by Cayenne to acquire data from a device and display them in a web page with several widgets and gauges (also graphs).


washburn_it

  • New Member
  • *
  • Posts: 29
[SOLVED] Re: TTCPClient and Stream Write Error (Raspberry Pi 3B+)
« Reply #5 on: September 29, 2021, 09:04:50 pm »
I think I found the fix for the issue.
I have another Pi that is used as "fisk tank controller" that sends water data (temperature, pH, EC, TDS) to Cayenne with the same way of the "power Pi" but it is working without errors.
So I compared the two programs and I found that the "Tank Pi" sends data to the python program every 10 seconds while the "Power Pi" every 5 secs.
I increased the time and now it's almost 2 hours that is working without errors.
Maybe something has changed from "Cayenne side" and this change impacted on the python program even though this last never stopped working.
Anyway, I've modified the way used to get data from the SDM according to the link that you sent me.
Thanks for your suggestion, regards.

Roberto

 

TinyPortal © 2005-2018