Sure, here's an example of implementing a PID temperature controller using Easy ESP rules type programming for ESP8266:
1. First, we need to define the variables for the PID controller:
Code: Select all
On System#Boot do
TaskValueSet 1,1,0
TaskValueSet 1,2,0
TaskValueSet 1,3,0
TaskValueSet 1,4,0
TaskValueSet 1,5,0
EndOn
2. Next, we need to read the current temperature:
Code: Select all
On System#Boot do
TaskValueSet 1,6,0
TaskValueSet 1,7,0
EndOn
On ADC#Temperature do
TaskValueSet 1,6,[ADC#Temperature]
EndOn
3. We also need to set the desired temperature setpoint:
Code: Select all
On System#Boot do
TaskValueSet 1,8,25
EndOn
4. Now, we can implement the PID controller using the following rules:
Code: Select all
On System#Boot do
TaskValueSet 1,9,0.5
TaskValueSet 1,10,0.05
TaskValueSet 1,11,0.1
EndOn
On Rules#Timer=1 do
TaskValueSet 1,12,[TaskValue(1,1)] // Error
TaskValueSet 1,13,[TaskValue(1,2)] // Integral
TaskValueSet 1,14,[TaskValue(1,3)] // Derivative
TaskValueSet 1,15,[TaskValue(1,4)] // Output
TaskValueSet 1,16,[TaskValue(1,6)] // Temperature
TaskValueSet 1,17,[TaskValue(1,8)] // Setpoint
// Calculate error
TaskValueSet 1,12,[TaskValue(1,17)]-[TaskValue(1,16)]
// Calculate integral
TaskValueSet 1,13,[TaskValue(1,13)]+[TaskValue(1,12)]
// Calculate derivative
TaskValueSet 1,14,[TaskValue(1,12)]-[TaskValue(1,15)]
// Calculate output
TaskValueSet 1,15,[TaskValue(1,9)]*[TaskValue(1,12)]+[TaskValue(1,10)]*[TaskValue(1,13)]+[TaskValue(1,11)]*[TaskValue(1,14)]
// Output to pin (use PWM pin for best results)
PWM,12,[TaskValue(1,15)],500
// Update variables
TaskValueSet 1,2,[TaskValue(1,13)]
TaskValueSet 1,3,[TaskValue(1,14)]
TaskValueSet 1,4,[TaskValue(1,15)]
EndOn
Within the event, we calculate the error between
Is this original code or does anyone recognise it from somewhere on the web?