Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:43 13 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Picromite TCP socket failure-bug?

Author Message
MikeO
Senior Member

Joined: 11/09/2011
Location: Australia
Posts: 275
Posted: 08:54am 27 Oct 2019
Copy link to clipboard 
Print this post

[EDIT]
Peter, I just had another failure, I then clicked New instead of my usual ctrl/Z and I got this error in the session window, sorry its not in line but if I had altered the window I think I would have lost the message.




I have a problem in a routine running in Picromite ver 5.0504, Raspian Buster Minimal.
The routine receives a UDP datagram from two sources (My Fronius PV and photo sensor on my electric meter). The program processes the data and then outputs 4 data fields via a HTTP Get message to the logger API. The program is running in a screen session.

The problem is that all runs ok processing the data every 10 secs until 3 hours later where it fails with a UDP socket error message. I have ensured that only one socket is running at one time and also protected the UDP/TCP commands with error traps but its still fails in the same time frame. The memory stats seems to be similar/not unusual to startup when it fails. The program will not run again after failure without exiting (ctrl/Z) and restarting mmbasic. The RPI does NOT require a reboot.






Electric Power processor for Raspberry Pi
 
 'Receives UDP transmissions from the Power network sensor
 'http://www.codenquilts.com.au
 'Michael Ogden Aug 2019
 'ver 0.7
 'Use of TCP sockets reworked to use one at a time
 '
 option explicit
 option default none
 dim integer nogridpower
 dim integer upower,spower,ginpower,goutpower
 dim r$,d$,s$,f1$,f2$,f3$,f4$
 dim cr$=chr$(13)+chr$(10)
 dim string q=chr$(34)
 Dim APIKEY$="&apikey=d0fd780e6da5c4bccb6c726dd5dbb21c"
 
 '  Dim integer i,j,rlen
 '  Dim m%(4000)
 '  dim l%,count%
 '  'dim ret$,d$,s$,,f5$,f6$,f7$,f8$
 '  'dim p10$,s10$
 '
 randomize timer
 Do
   nogridpower=0
   'first get latest solar and Grid power values
   spower=val(GetUDP$("solar"))
   upower=val(GetUDP$("count"))
   if upower=0 then nogridpower=1
   upower=upower*3600/10
   'check solar contribution
   if nogridpower<>1 then
     ginpower=upower
     goutpower=0
     upower=upower+spower  'add solar to grid power
   else                    'solar is greater than grid input
     print "else calc"
     'so apply just an average grid usage and the balance as grid out
     upower=Int((500-400 * Rnd) + 400)
     ginpower=checkpos(upower-spower)
     goutpower=checkpos(spower-upower)
   endif
   '
   logpower
 loop
 
end
 
 
function checkpos(v%)as integer
 checkpos=v%
 if v%<0 then checkpos=0
end function
 
function GetUDP$(Pair$)
 'open UDP Socket and process inputs
 'exit when selected pair received
 local a1$,b$
 local c%
 on error skip
 udp close
 on error skip
 tcp close
 udp server 5001
 do
   pause 500
   on error skip
   UDP receive a1$,b$,c%
   'print a1$,b$,c%
   '
   if c%>0 then
     'print a1$
     r$=valuepair$(a1$,pair$)
     if r$<>"" then
       GetUDP$=r$
       exit do
     end if
   end if
 loop
 on error skip
 udp close
end function
 
 
sub logpower
 print "latest watts";upower
 print "latest Solar";spower
 print "to Grid";goutpower
 print "from Grid";ginpower
 '
 f1$="usage_W:"+str$(upower)+","
 f2$="solar_W:"+str$(spower)+","
 f3$="grid_out:"+str$(goutpower)+","
 f4$="grid_in:"+str$(ginpower)
 '
 d$="{"+f1$+f2$+f3$+f4$+"}"
 on error skip
 udp close
 on error skip
 tcp close
 on error skip
 tcp client "192.168.0.18",80
 s$= "GET /input/post.json?node=emontx&json="+d$+apikey$
 s$=s$+" HTTP/1.0"+cr$+cr$
 print s$
 on error skip
 TCP send s$
 Print ""
 on error skip
 tcp close
end sub
 
 
 
 'replaceString$(string$, replaceWhat$, replaceWith$)
FUNCTION replace$(R$, a$, b$)
 local integer i,j,c,d
 d=1
 replace$=R$
 i = LEN(a$) : j = LEN(b$)
 do while INSTR(d,replace$, a$) > 0
   c = INSTR(d,replace$, a$)
   replace$ = LEFT$(replace$, c-1) + b$ + MID$(replace$, c+i)
   d = c+j
 loop
END FUNCTION
 
 'ValuePair Function: Returns the value in ValuePair$ of Vparam$
 'in the string Vpair$
function ValuePair$(Vpair$,VParam$)
 local integer a,c,b
 Vpair$=replace$(Vpair$,"{","")
 Vpair$=replace$(Vpair$,"}",",")
 Vpair$=replace$(Vpair$,":","")
 b=len(Vparam$)+1
 a=instr(Vpair$,Vparam$)
 if a>0 then 'retrieve value
   if right$(Vpair$,1)<>"," then
     Vpair$=Vpair$+","
   end if
   c=instr(a+b,Vpair$,",")
   if c>0 then
     ValuePair$=mid$(Vpair$,a+b,c-(a+b))
   endif
 endif
end function

Edited 2019-10-27 19:50 by MikeO
Codenquilts
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10573
Posted: 02:04pm 27 Oct 2019
Copy link to clipboard 
Print this post

Mike

Can you have a look at the MMBasic processes as time progresses (linux PS command). It sounds like a memory leak within the linux environment rather than within MMbasic
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4137
Posted: 05:55pm 27 Oct 2019
Copy link to clipboard 
Print this post

It may well not be a Linux problem.  It sounds more like there is a library (Windows DLLs are the same and borrow the same ideas) missing.  It is either wanted by pigpio or mmbasic or some other thing you're using.

It doesn't sound so far to be a memory leak.

The library (shared object) libgcc_s.so.1 is at least missing or not in the path being searched.  You may be able to create a link to a similarly named one or, better, find out which thing you use is wanting it.

John
Edited 2019-10-28 03:58 by JohnS
 
MikeO
Senior Member

Joined: 11/09/2011
Location: Australia
Posts: 275
Posted: 08:38am 28 Oct 2019
Copy link to clipboard 
Print this post

John, the  libgcc_s.so.1 does exist on the system, there does seem to be some chatter out on the web about some incompatibilities with this library but I am not sure it is a problem in this case?

I can report a few things;
I had another failure at 3 hours memory on the system at the failure are not really any different than during normal running.
I made some changes to the code so UDP socket is permanently open, TCP being used as required. I had some confusion on this subject , I thought I remembered from early posts that only a single socket was allowed to be open (Peter?) hence, why I was switching them however to try and advance this I set UDP permanent and TCP on / off. OK 3 hrs later, it failed, however UDP was still receiving it was TCP that was not and would not restart without a ctrl-z and restart (of mmbasic)

I have changed code again and decided to dump calculations to a file and process further via Node-Red, would be neater to stay within mmbasic but..

It has been well over 3 hours still going now so we shall see.

Mike
Codenquilts
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4137
Posted: 10:43am 28 Oct 2019
Copy link to clipboard 
Print this post

It's either not found (wrong path etc) or I suppose could be looking for 64-bit and can't find it / looking for 32-bit and can't find it (where presumably the existing one would be the other size i.e. 32-bit/64-bit respectively).

There are probably other reasons the message could occur (er, old version of lib, corrupted file, ...).

I can see reports about it but due to tools you probably are not using, so next it would be rather helpful to figure _which_ thing you're using triggers the message so we can find out if it wants a different size or just doesn't have it in its (library) path.

BTW if you find it's the latter you can potentially use ldd or as a short term thing such as:
LD_LIBRARY_PATH=/some/path program args

(which can be handy if you have several versions of a library)

John
Edited 2019-10-28 20:44 by JohnS
 
MikeO
Senior Member

Joined: 11/09/2011
Location: Australia
Posts: 275
Posted: 11:27am 28 Oct 2019
Copy link to clipboard 
Print this post

Thanks John,

I am not a Linux person, just learning to make my way aground and I although I would like to solve the problem and use the program the way I had originally intended I know how long these things can take and sometimes "programming around it" is an option if not the best one. Also the error message I showed actually only appeared when i did a NEW command from the mmbasic command line, not when the failure occurred, so whilst no doubt a result of the fail may not actually be the cause.

BTW code is still running , some 6-7 hours later so it seems fairly conclusive the problem is in the TCP socket area.

Mike
Codenquilts
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025