Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 21:41 27 Nov 2024 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 : 18650 UPS module

     Page 1 of 2    
Author Message
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2914
Posted: 12:50am 04 Aug 2023
Copy link to clipboard 
Print this post

Hi All,

I recently discovered on Aliexpress an 18650x2 UPS module that may be quite useful to some out there.

The module is L/W/H 89mm(3.5") x 42mm(1.65") x 24mm (0.95") in size and has a USB-C input for charging of the 2 x 18650 cells and outputs a constant 5V to your device. They also have 9V out and 15V out types (select the option you need) however I am fairly certain it is a simple resistor (SMD 0805 type) change that selects the desired output voltage. I bought a 5V type and have ordered 2 more to play with.





The seller I bought my unit off also had a single 18650 cell type but I didn't buy one of them to play with.

The Price? Under $5AU inc shipping to Australia, No 18650 batteries of course.

The link I used to buy my module is this one.

18650 UPS

However this link will not stay in place forever, I suggest that you search for "Type-C 15W 3A 18650 Lithium Battery Charger Module" and there should be several sellers offering them.

I am thinking of using them in a remote (like "up the pole") installations and trying a solar panel to keep the 18650's topped up.

I hope these are useful to someone out there. Of course you should be able to cascade them to give a greater capacity.

Kind Regards,

Mick
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 07:25am 04 Aug 2023
Copy link to clipboard 
Print this post

Interesting!  
Smoke makes things work. When the smoke gets out, it stops!
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 08:14am 04 Aug 2023
Copy link to clipboard 
Print this post

It looks like R7 / R9 might be a potential divider for the feedback signal, so they may be setting the output voltage. Bit of a fiddly area to get to.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6100
Posted: 08:22am 04 Aug 2023
Copy link to clipboard 
Print this post



freight free for one but $7 if I buy 2 or more

Jim
Edited 2023-08-04 18:24 by TassyJim
VK7JH
MMedit   MMBasic Help
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 08:36am 04 Aug 2023
Copy link to clipboard 
Print this post

Wasn't a bad guess. :)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 09:14am 04 Aug 2023
Copy link to clipboard 
Print this post

Now if only there was such a beast for LifePo4 batteries
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 09:31am 04 Aug 2023
Copy link to clipboard 
Print this post

You could probably make one fairly easily. The output half is the same, only the battery & charger circuit is different. You could do the same with lead-acid batteries. Either change would have to respect the required voltage required by the mosfet driver circuit, of course. You couldn't use, say, a 24V lead-acid battery.

Basically it's a charger, battery and dc/dc converter. The charger has to be able to supply enough current to drive the dc/dc converter as well as have some spare to charge the battery. Often these are two different circuits in a UPS.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2914
Posted: 10:21am 04 Aug 2023
Copy link to clipboard 
Print this post

Hi All,

I have been testing it, with one of Tom’s up and coming BinTendo Tomboys, am I am very impressed, the power input can be removed and plugged in at random and the output doesn’t change even 0.001V.

I left it running, with no input charge connected, on fully charged batteries driving the BinTendo Tomboy with 2.8” display (no sound) drawing roughly 150mA for 51hrs and it was still going with an output of 5.001V but I shut it down as the batteries had dropped to 2.8xx V and I didn’t want to risk them getting damaged, I suspect it must have been near its cutoff voltage anyway. I might dig out a couple of near matched salvaged 18650 and test if and when it does cut the output..

I had previously looked up the specs of the DC-DC converter chip  and saw the resistor R7 to change for different output voltages and it matched my value exactly.

Anyway, it is small and so far works very well.

Regards,

Mick
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3150
Posted: 11:55am 04 Aug 2023
Copy link to clipboard 
Print this post

These look nice. A problem which I had with the single 18650 versions when used as a UPS was that if the power went out and the battery drained down, restoration of power would not result in the device being powered or the battery being recharged if the load was too great.

In my case, I was able to fix this by wiring a schottky diode from the 5V USB input power to the USB 5V output lead, and cutting the connection from the 18650 boost circuit to the USB 5V output and inserting another diode. With this, the load would come right up with power restoration, and the 18560 battery would trickle up to charged.

I wonder if this unit has the same issue?

I've ordered 2 of the 5V and 2 of the 12V units ($2.03USD each and $1.68 total shipping to Canada) and will test, but if anyone is able to test this sooner, I'd be happy to hear the result.

Also, any suggestions for an appropriate solar charger?

~
Edited 2023-08-04 22:00 by lizby
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 12:50pm 04 Aug 2023
Copy link to clipboard 
Print this post

I'm not sure about feeding it from solar. The output is always being taken from the battery, so your charging supply has to provide both that and sufficient charging current to ensure that the load isn't going to flatten the battery overnight or if the weather is dull for a day or two. That's quite a lot to ask of a solar panel. Remember that, at best, the dc/dc converter is probably about 86% efficient so you will need to provide more load current than your load is drawing.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4246
Posted: 01:18pm 04 Aug 2023
Copy link to clipboard 
Print this post

All,

Not sure if you connect a solar panel to a charger that is designed to work from 5V.
When charge is almost done, current will be low, and solar voltage may be high. Potentially destroying the board.

Volhout
PicomiteVGA PETSCII ROBOTS
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 01:24pm 04 Aug 2023
Copy link to clipboard 
Print this post

You'd have to use some sort of regulator first - possibly a buck converter as a solar panel will almost always exceed 5V.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Martin H.

Guru

Joined: 04/06/2022
Location: Germany
Posts: 1114
Posted: 02:44pm 04 Aug 2023
Copy link to clipboard 
Print this post

Most portable electric drill or cordless electric screwdriver or older Notebooks uses 18650 Lithium-Batteries.
Most time just one Cell is bad..
A good way to give your e-waste a new purpose  
Please be careful when opening the case and handling the batteries
(just ordered a 5V one)
Edited 2023-08-05 00:50 by Martin H.
'no comment
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2914
Posted: 11:38pm 04 Aug 2023
Copy link to clipboard 
Print this post

Hi Volhout, Mick (the other one), All,

If I was going to connect a solar panel (I have a nice commercial one that was donated to me) I would have always run it through a buck/boost converter to supply 5V to the `UPS’ module.

As tested, the unit I have lasted 51hrs (and was still going) on batteries alone (litoKala 3400mAhr — they measured on my tester at just over 3000) drawing a constant current of roughly 150mA.

In Aus we get quite a bit of sun even in the winter so I think my solar panel `might’ be able to keep the cells charged, certainly in the summer there would be no issue keeping them topped up. I figure at worst case they keep the “up the pole” unit topped up for some time and if necessary you change out the batteries with fresh ones if it does go flat. I see that the ‘UPS’ should be accessible rather than “up the pole”

@Lizby,
I can test this for you but it will take some time (over 51hrs) unless I can find a heavier load (say 300mA). Actually I have a USB load tester that I might bring into play here. What sort of load output are you looking at?

Regards,

Mick
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3150
Posted: 01:17am 05 Aug 2023
Copy link to clipboard 
Print this post

Mick--my test (3 years ago) was with a single 18650 (module) running a Raspberry Pi Zero W. I didn't note how long it ran, just that after running down, it didn't restart when charging power was re-applied. I'm not sure what the current draw was on the pi-zw.

I noted that I also ran a ESP8266 D1 Mini (doing nothing but checking for an ON signal from a switch) for 23 hours and 4 minutes with a dual 18650 module.

It's possible that with a load lighter than the pi-zw, the load + charger would start up again when power is restored.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2914
Posted: 05:24am 06 Aug 2023
Copy link to clipboard 
Print this post

Hi Lizby, All,

I loaded the output with an 8R 10W resistor (should be 625mA) and let the battery run flat (I only fitted one battery to expedite the test) and the result was interesting but worthwhile.

I ran the battery until it cut off (output was 0v LED was off) battery Voltage was 2.66V when it cut off. I applied a 5VDC input (from a PC USB port) and output immediately came up but was at 3.66V and output led was dim, it gradually raised over a period of approximately 5 minutes then it jumped suddenly to a 5V output.

I let it charge a while and pondered this and I have discovered that just before it shuts the output completely it drops the voltage down slowly over a similar ~3 to 5 minute period (down to 2.1V) then shuts off the output completely.

I powered the input from a plug pack and the output immediately switched on and was at 4.991V so I figure the PC USB input couldn't handle the charge of the battery plus supply enough to boost to 5V output but using the plugpack it was capable of both.

The bottom line is that with no input power source the UPS will drop down to 0v at a battery level of ~2.66V and once the input power is back the UPS will resume 5v output.

That will suit my applications I think.

Kind Regards,

Mick
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 05:34am 06 Aug 2023
Copy link to clipboard 
Print this post

I've ordered one of these DC 3A Buck Module DC 6-35V to 5V Solar Voltage Regulator Charging Power Module
Hopefully it should charge it from Solar
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 06:38am 06 Aug 2023
Copy link to clipboard 
Print this post

It may be that the dc/dc converter needs 3V or so to produce a 5V output. The characteristics of lithium cells mean that they maintain their terminal voltage until quite late then drop suddenly. When charging the terminal voltage will be well down for a short time (it's a constant current charger, not constant voltage) before it climbs to the charging voltage of around 4V. If the battery is very low this could take some time.

If you are doing any data collection you need to detect the beginning of the discharge drop, really, as you may not have much running time left - particularly if you are backing up to SDcard.

-----------------

Still might be a good idea to stick a big 30V zener across the input, Lew, unless you can be certain that the solar panel can never give more than 35V.
Edited 2023-08-06 16:41 by Mixtel90
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3150
Posted: 11:42am 06 Aug 2023
Copy link to clipboard 
Print this post

Thanks (Big) Mick. Sounds like you could detect imminent loss of power and undertake whatever measures are necessary.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
MikeO
Senior Member

Joined: 11/09/2011
Location: Australia
Posts: 275
Posted: 12:00am 07 Aug 2023
Copy link to clipboard 
Print this post

Some time ago I wrote code (picomite) to control the power supply and UPS running a Raspberry pi server. I used similar to these , I monitor the 12v supply into the ups so that I can react to a power fail closing down the pi after a period of time, also issue shutdown signal on the network(NUT) to close a Synology NAS. The UPS I used were a bit pricey but all I could find at the time, I have ordered some of these UPS in this thread and will try with these also.

 
 '  ' ***** uMitePi *****
 ' *******************************
 '    Filename: uMitePi.bas
 '    Date: Aug 2021
 '    File Version:
 '     Written by: M.Ogden
 '    Function: uMitePi controller
 '   Device: micromite pico
 '    Last Revision:
 '    Initial v1.31 ported from mx170
 '   MMBasic Ver 5.05.04 ,raspberry Pi running  Node Red
 'adds some LED indicators, Fan control and Shutdown signaling
 'plus user code
 ''V2 PCB adds access to all unused MX170 IO pins, adds RTC connector
 'V0.7 release
 'v0.8 add I/O changes for version 2 PCB
 'v0.9 add standby state retention
 'v1.0 add ups-power
 'v1.1 add prop fan control
 'v1.2 add Nokia LCD v3 pcb compatable
 '
 'v1.31 add shutdown-seq , respond to linux shutdown command
 'v1.32p Pico version
 'v1.32 add power flag,errflag & countdown clock
 'v1.33 fix jsonvalue, replace serial interupt mode
 'v1.34p pico updated
 'v1.35p ups management
 'v1.36p Alligned MX170 & Pico versions (apart from Pin#)
'v1.37p add textmsg (Synology Chat)
'v1.38p add cdc message (timeout)
'v1.39p mod textmsg to json msg , return rpitemp (formated)
 ' *******************************
 'IO Pin List
 
 'dim P1=GP19 'Led (Button)
 'dim P2=GP18 'Button
 'dim P3=GP12 'rpi-running (sig)
 'dim P4=GP13 'rpi-shutdown (out)
 'dim P5=GP17 'rpi-poweron (out)
 'dim P6=GP22 'ups-nopower (sig)
 'Dim P7=GP2 'Fan Drive
 'dim P8=GP0 'RX
 'dim P9=GP1 'TX
 
 
 
 dim ver$="1.39p"
 'Variables
 dim q$=chr$(34),J$="&",temp$="",lastline$,newline$
 'prop temp control
 dim ffan=70
 dim fan_cycle=60
 dim temp_run=40
 dim stall_percent=30
 dim temp_max=55
 dim fan_ratio=((99-stall_percent)/(temp_max-temp_run))
 
 '
 dim integer secs=0,flag=0 'periods
 dim integer uptime=0  'time up in seconds
 dim integer upspower=0  'ups power on
 dim integer upstime=0 'time on battery
 dim integer upsexpired=60  'time allowed on battery
 dim integer errflag=0 'error
 Dim Integer userprocessflag
 dim integer cdclock=20 ' seconds
 dim rpitemp$="45"
 dim rpiload$
 dim rpidate$,rpitime$,rpiutc$
 dim integer standby,powerflag,powerfail
 dim powermsgflag=1
 
 'periodic triggers
 dim secs5=5
 dim secs10=10
 dim secs20=20
 dim secs60=60
 dim min5=300'5 min in secs
 
 '***** User Variables ******
 
 '***** User Variables ******
 
 'LEDs & Outputs
 setpin GP2, PWM1A
 setpin GP0,GP1,COM1
 setpin GP19, dout 'Led 1
 setpin GP13, dout 'rpi-shutdown
 setpin GP17, dout 'rpi-poweron
 pin(GP13)=1 'rpi-shutdown (not-shutdown)
 pin(GP19)=0 'Led(button) off
 '  'Inputs
 setpin GP18, intl,buttonpress,PULLUP  'S2 Button
 setpin GP12, intl,notrunning        'rpi-running
 setpin GP22, intb,nopower,pulldown 'ups-nopower
 '*********************************************************************************
 ' cls
 Print
 print "mmPi "
 print "ver: "+ver$
 'font 5
 'Text 0,0, "mmPi "+ver$
 'Text 0,8, time$
 'text 0,16, "Temp: "+str$(val(rpitemp$),2,1)
 'initialise
 var restore
 if standby=1 then
   print "Standby"
   'cpu 5
   led3 0
   pin(GP17)=1  'shutdown power supply
   PWM 1,ffan,stop  'off
 else
   'start up via Power Source check
   if pin(GP22)=1 then
     powerflag=1
     startupseq
   end if
 end if
 
 'debug
 'pin(GP17)=0 'take control of power supply On
 'end
 
 
 'serial Com1
 open "com1:115200" as #5
 settick 1000, T1,1  '1 sec timer
 'settick 60000,runonce,2
 'main loop processing
 do
   'looptime=timer
   'print "Loop"
   Watchdog 20000 ' set the WDT at 20 secs
   if standby =0 then
     if upstime>upsexpired then shutdownseq
     'if upstime>some-triggerpoint then some-event
     'eg close down some other server
     if loc(5)>0 then rec1
     '***** periodic processes *****
     if secs10=0 then
       secs10=10
       adjFanSpeed val(rpitemp$)
       textmsg "cpucore",str$(val(rpitemp$),2,1)
       '***** User Code *****
     end if
     '
     if secs5=0 then
       secs5=5
       '***** User Code *****
     end if
     '
     if secs60=0 then
       secs60=60
       '***** User Code *****
     end if
     '
     if min5=0 then
       min5=300
       '***** User Code *****
     end if
   else
     'standby only
   end if
   ' Print "Elapsed:";timer-looptime/1000
 loop
 
 
 'core routines
sub runonce
 settick 0,0,2 'disable timer
end sub
 
 'power plug interupt
sub nopower
 if pin(GP22)=1 then
   print "Power OK"
   powerflag=1
'powermsgflag=1  
   if powerfail=1 then startupseq  'only start if in standby
 else
   print "Power Disconnected"
   pause 1000
   print "UPS Power"
 textmsg "text","Power has been disconnected"
   if standby =0 then
     print "Will close down in ";upsexpired;" secs"
     powerfail=1
     powerflag=0
     upstime=0 'reset time
   end if
 end if
end sub
 
sub notrunning
 print "Pi Not-running"
 shutdownseq
end sub
 
 'Heartbeat
sub T1
 if standby=0  then  'normal running
   'print "Countdown:";cdclock
   if secs5>0 then secs5=secs5-1
   if secs10>0 then secs10=secs10-1
   if secs20>0 then secs20=secs20-1
   if secs60>0 then secs60=secs60-1
   if min5>0 then min5=min5-1
   if cdclock>0 then
     led3 1 'Power Led On
     errflag=0 'reset the flag
     cdclock=cdclock-1 'countdown clock decrement
  print #5, "{"+q$+"cdc"+q$+":"+Str$(cdclock)+"}"
   else
     errflag=1 'set the error flag
   end if
   uptime=uptime+1 'time while running
   if uptime=60 and powermsgflag=0 then powermsgflag=1 'set flag 60secs after power restored
 else      'in standby or No Power
   '
 endif
 if powerflag=0 then upstime=upstime+1 'time in disconnected and ups
 if errflag=1 then led2
 'print "Uptime:";uptime;" upstime:";upstime
end sub
 
 
 'Led1 pulse p=pin
sub led1(d)
 if d=0 then d=50
 pulse GP19,d
end sub
 
 'Led2 toggle
sub led2
 if pin(GP19)=1 then
   pin(GP19)=0
 else
   pin(GP19)=1
 end if
end sub
 
 'Led3 set
sub led3(hl)
 pin(GP19)=hl
end sub
 
 # Set the PWM of fan pin
sub adjFanSpeed(CPU_temp)
 if  CPU_temp > temp_run then
   FAN_cycle = (((CPU_temp - temp_run)*fan_ratio)+stall_percent)
   FAN_cycle = min(100,max(stall_percent,FAN_cycle))
   PWM 1,ffan,fan_cycle
 else
   PWM 1,ffan,stop  'off
   FAN_cycle=0
 end if
end sub
 
 
Sub rec1
 'reads bytes from comms buffer , data terminated by CR will output a NewLine$ and set the UserFlag
 'if userprocessflag=1 then exit sub
 local a$
 local integer p,l,i
 'print "Buffer:";loc(5)
 if loc(5)=0 then exit sub
 if len(a$)=>235 then
   i=255-len(a$)
 else
   i=20
 endif
 'print "i:";i
 a$=Input$(i,#5)  'user supplied processing routine
 'pause 100
 'print a$;
 on error skip 'DEBUG
 lastline$=lastline$ + a$    'add to data packet 'DEBUG
 'print lastline$
packet:
 l=len(lastline$)
 p=instr(lastline$,chr$(13))
 '  print
 '  print "Line Lenght:";l
 '  print "CR?:";p
 '  print "Buffer:";loc(5)
 '  print lastline$
 '  print newline$
 '  print
 
 if l=>235 and p=0 then   'long line and no CR
   'force line to be processed
   newline$=lastline$
   lastline$=""
   userprocess 'force data process
   exit sub
 end if
 
 if p>0 then   'CR found so process
   'lastline$=replace$(lastline$,chr$(10),"")  'remove LF
   Newline$=left$(Lastline$,p-1)
   'check comms using temperature string from Pi
   'if good reset the countdown clock
   if instr(newline$,"{"+q$+"pi-temp"+q$+":")>0 then cdclock=20
    if powermsgflag=1 then
     textmsg "text","Power has been restored"
               powermsgflag=0
     end if
   end if
   if l>p+1 then
     lastline$=mid$(lastline$,p+1) 'save the rest of the line
   else
     lastline$=""
   end if
   'recover standard data pairs from Pi
   print "Process:";newline$
   getjson newline$,rpitemp$,"pi-temp"
   getjson newline$,rpiload$,"usage"
   getjson newline$,rpidate$,"Date"
   getjson newline$,rpitime$,"Time"
   getjson newline$,rpiutc$,"DateUTC"
   'recover any User program data pairs
   'uncomment next line as required
   userprocess
 endif
End Sub
 
 sub textmsg(obj$,tmsg$)
print #5, "{"+q$+obj$+q$+":"+q$+tmsg$+q$+"}"  
end sub

 
 
sub buttonpress
 pause 50  'debounce
 if pin(GP18)=0 then presstime
end sub
 
sub presstime
 pause 1000
 if pin(GP18)=0 then
   if pin(GP12)=1 then   'is pi running
     shutdownseq
   else
     'start up via Power Source check
     print "Check Power:";pin(gp22)
     if pin(GP22)=1 then
       powerflag=1
       startupseq
     end if
   end if
 else
   print "shortpress"
   led1 100
 end if
end sub
 
sub shutdownseq
 Watchdog 20000 ' set the WDT at 20 secs
 print "Shutdown"
 textmsg "text","Shutting down RPI"
 led1 1000
 pin(GP13)=0  'shutdown RPI
 pause 1000
 pin(GP13)=1
 print "Standby"
 errflag=0
 standby=1
 pause 15000' Wait for pi to shutdown
 led3 0
 print "shutdown supply"
 pin(GP17)=1  'shutdown power supply
 PWM 1,ffan,stop  'off
 
 var save standby,powerfail
 'cpu 5
end sub
 
sub startupseq
 print "Startup"
 print "Running"
 'cpu 48
 led1 1000
 uptime=0
 pin(GP17)=0  'power supply on
 uptime=0
 upstime=0
 standby=0
 powerfail=0
 var save standby,powerfail
 pause 2000
 led3 1
 PWM 1,ffan,60 'Run fan default
end sub
 
 
function jsonValue$(Jstring$,jParam$)
 local a,c,s$,d$,b,json$
 json$=jstring$
 s$=":"
 d$=","
 'locate parameter
  a=instr(JSON$,jparam$)
 'print "A is:";a
 json$=replace$(json$,chr$(34),"")  'strip quotes
 json$=replace$(json$,"}",",")  'replace closing curly brackets with comma
 if a>0 then 'retrieve value
   'look for separator :
   b=instr(a,json$,s$)
   if b>0 then
     'look for deliminator
     c=instr(b,JSON$,d$)
     if c>0 then
       jsonvalue$=mid$(JSON$,b+1,c-(b+1))
     endif
   endif
 else
   jsonvalue$="not found"
 endif
end function
 
 'gjson$ checks for w$ in a json string j$
 'replaces contents of r$ if found
sub getjson(j$,r$,w$)
 local x$
 'print "getjson:";j$
 x$=jsonvalue$(j$,w$)
 if x$ <> "not found" then
   r$=x$
 end if
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
 
 '**************** End Core routines and functions ********
 
 
 
 'user subs and tasks
 '*** User subs and other code here ***
 'process json strings from RPI
Sub userprocess
 print "UserProcess:";newline$
 
end sub
 
 'End user subs and tasks
 


Codenquilts
 
     Page 1 of 2    
Print this page
© JAQ Software 2024