PID algorithm with Time Proportioned output in Sonoff device

695 views
Skip to first unread message

Colin Law

unread,
Mar 17, 2018, 5:41:57 AM3/17/18
to SonoffUsers
Following on from a previous thread [1] I have my PID algorithm running in a TH10 using the plug-in DS18B20 temperature sensor and driving the relay as a Time Proportioned output to drive a heater.  I have also tested it in a Sonoff Basic passing it current temperature values via MQTT rather than use the wired sensor.  It should work in other devices without major effort.  I am controlling it from node-red via MQTT to adjust the setpoint, tune the loop parameters etc, but any MQTT client could be used.  All the parameters can also be hard wired in user_config.h so it can run entirely stand-alone if required.

If you want to try it then clone my fork of Sonoff-Tasmota and switch to the branch pid_branch
https://github.com/colinl/Sonoff-Tasmota/tree/pid_branch
Alternatively just pick up from that link:
sonoff/xdrv_91_timeprop.ino
sonoff/xdrv_92_pid.ino
and the files in lib/ProcessControl, which are from my process-control repository [2].
It needs at least 5.12.0 of sonoff-tasmota.
The PID code adds 4.3k and the Timeprop code another 1.2k

Instructions for setup are in the two xdrv files.  Any problems ask here or via issues on the sonoff-tasmota fork.

I am amazed how powerful the processor in the ESP8266 is.  It will run the PID algorithm (which is full of double float arithmetic) at 1 cycle per second, which is much faster than is needed for the sort of processes Sonoff devices are usually associated with.  It rather clobbers the Sonoff-Tasmota terminal output in the web browser at that rate so it is getting near to the limit.  The maximum anyone is likely to need it running at is maybe once every 5 seconds, and the majority of home IoT applications probably nearer once per minute would be sufficient, so it is well up to the task.  Remarkable.

Help with using the PID algorithm and with loop tuning can be found at  http://blog.clanlaw.org.uk/2018/01/09/PID-tuning-with-node-red-contrib-pid.html.  This is directed towards using the algorithm in the node-red node node-red-contrib-pid but the algorithm here is based on the same code so the tuning technique described there should work just the same.

Should I add a notes on this to arendst's Sonoff-Tasmota wiki?  If so can I just go ahead and do that or do I need access?

George Ioakimedes

unread,
Mar 17, 2018, 9:47:43 AM3/17/18
to SonoffUsers
Nice work Colin! I plan on trying this out for a garage heater once I get some other projects finished up.

I think you can edit the Wiki but maybe the changes will need approval before they get merged in? I'm not sure but I'm curious how that works too.

Colin Law

unread,
Apr 2, 2018, 6:18:50 AM4/2/18
to George Ioakimedes, SonoffUsers
It seems that Wiki is open to all to edit, I have added pages for the Time Proportioned Output extension [1] and PID Control [2]

Colin

[1] https://github.com/arendst/Sonoff-Tasmota/wiki/Time-Proportioned-Output-support
[2] https://github.com/arendst/Sonoff-Tasmota/wiki/PID-Control-with-Sonoff-Devices


Andy Maddick

unread,
Apr 2, 2018, 7:25:57 AM4/2/18
to SonoffUsers
Well done Collin, a great contribution to the project.

Recently Tasmota has added 'Internal Timers', 16 timers are offered..

I really need 'Internal Timers' like this, but more like the timers ESPeasy firmware has..
Every day, every hour at *.00, *.15, *.30 & *.45 - that's 4 times every hour, all day & all night
Adjustable would be cool sometimes maybe hourly, sometimes half hourly..
I could use the pulsetime command with each on, but I will break that pulsetime early anyway with a change in a GPIO via a switch..
ESPeasy firmware can do this, but I've had instability issues with ESPeasy & still trying to pin point the cause..
Currently back tracking firmware revisions to pin point the cause.
Maybe I need a hybrid :D

PF

unread,
May 9, 2018, 10:47:37 PM5/9/18
to SonoffUsers
I found a little bug in the code in an odd use case.  I have a freezer that I got for free that I put it in my garage to make a beer fridge.  I put a sonoff TH to control the compressor and have used that for with Espurna firmware since last year.  I changed their code to make a crude on / off control to keep the beer from freezing but got it below 0C without forming slush so the beer was really cold after mowing.  Over the winter the code somehow screwed up and it reset back to the stock wifi and the stock password wouldn't work.  So I did some searching and happened on Tosmota.  I did a quick switch since I had to open it up and USB flash it anyway.  First impression was really good so I spent the time learning Tosmota.  Then I saw this PID control and thought how perfect as my old code had the temp control points hard set so it always took a new flash to change anything as the temp points changed depending on how warm it got for the day or even if the sun was hitting the side of the barn at certain hours of the day.

Well Tosmota is really well done well so I figured I would try the PID code but it was always turning the compressor on when it shouldn't have.  After some hours of fighting it I happened to use the console window  (dang it should of looked at that earlier) and found it wasn't actually get the temperature from the PID portion.  I looked at the pid.ino and found that if the first digit isn't a number then it doesn't accept it.  Well my temps were going negative so my first digit wasn't a number.  I did a quick code change and this seems to work:

if (value != NULL && strlen(value) > 0 && (isdigit(value[0]) || value[0] == "-") )

Again thanks for the awesome code.  Now it seems to run how it should so I can get to tuning my values in the PID.  I would like for it to use a wider band so when it turns the compressor on it stays on for a little longer periods and off for longer periods as the freezer holds temp pretty good.  I don't mind a little wider temperature swing as the beer doesn't freeze fast.

Thanks again for the awesome software.

Adrian Scillato

unread,
May 13, 2018, 9:13:20 PM5/13/18
to SonoffUsers
Hi, 

Thanks for sharing. Very good job!

Very interesting.

In your tasmota fork (https://github.com/colinl/Sonoff-Tasmota) the issues tab is deactivated. Can you turn it on?

Thanks!

Colin Law

unread,
May 21, 2018, 4:58:20 PM5/21/18
to SonoffUsers
I have activated the Issues tab, I thought it was already enabled but apparently not, and opened an issue for the reported bug
https://github.com/colinl/Sonoff-Tasmota/issues/2

Colin

--
You received this message because you are subscribed to the Google Groups "SonoffUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/sonoffusers.
To view this discussion on the web, visit https://groups.google.com/d/msgid/sonoffusers/fb0e62ac-a63d-4b3f-83c3-8be51422c7e4%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages