[UPDATE] Thermostatsteuerung mit ESP8266-01

Ein kleines Update zur Heizkörperthermostatsteuerung

Ich habe die Schaltung vereinfacht und die Transistoren weggelassen und das ganze für den ESP8266-01, also die kleine „Classic“-Platine, die nur 2 GPIOs hat, angepasst.

Das Serverscript hat nun eine Abfragemöglichkeit über den Parameter …?a=gt, mit dem man die zuletzt eingestellte Temperatur  – also den Ist-Zustand – abfragen kann.

Die beiden aktuell verwendeten lua – Scripte sind auch noch einmal angefügt.

Es muss mit ESP8266Flasher.exe NodeMCU geflasht werden, um luaScript zu verwenden. Hier meine Settings beim Flashen:

NodeMCU Flasher Settings

Esp8266Flasher.exe settings für ESP8266-01

Das aktuelle Layout ohne Transistoren:

 

ESP8266_classic_Thermostatsteuerung_2018_Steckplatine

Hier die beiden Scripte, die auf dem ESP8266 werkeln:

init.lua

print('Hello! init.lua started.')
wifi.setmode(wifi.STATION)
wifi.sta.config("MY_AP_NAME", "MY_WIFI_PASSWORD")
wifi.sta.connect()

tmr.alarm(0, 1000, 1, function()
 if wifi.sta.getip()==nil then
   print("connecting to AP...")
 else
   print("AP successfully connected.")
   print ("ip: ", wifi.sta.getip())
   tmr.stop(0)

   dofile("server_v4.lua")
 end
end)

server_v4.lua

p1=3 -- 1
p2=4 -- 2
gpio.mode(p1, gpio.OUTPUT);
gpio.mode(p2, gpio.OUTPUT);

wt=21
gt=0

srv=net.createServer(net.TCP)
print ("WEBSERVER started.")
srv:listen(80,function(conn)
 conn:on("receive", function(client,request)
 local c = ""
 local buf = "";
 local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
 if(method == nil)then
   _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
 end
 local _GET = {}
 if (vars ~= nil)then
   for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
     _GET[k] = v
   end
   if(_GET.t ~= nil)then
   wt = _GET.t
   gt = wt
   if _GET.p == "on" then
       gt=wt + 0.5
       c = " checked"
   end
   if wt == "off" then
       wt = 0
       gt = 0
       c = ""
   end
     print("Argument 't' was "..gt);
     settemp(gt)
 end
end

if(_GET.a == nil)then if(_GET.a == nil)then
  buf = buf.."HTTP/1.1 200 OK\n\n"
  buf = buf.."<html><head><title>Schlafzimmer Thermostat</title>";
  buf = buf.."<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1\">"
  buf = buf.."<!--[if lt IE 9]>http://html5shim.googlecode.com/svn/trunk/html5.js<![endif]-->"
  buf = buf.."<link rel=\"shortcut icon\" href=\"http://SOME_URL/favicon.ico\"></head>"
  buf = buf.."<body bgcolor=\"#D0D0D0\"><font face=\"arial\"><center><h1>SCHLAFZIMMER</h1><p>Temperature is set to "
  buf = buf..gt       
  buf = buf.."&ordm;</p><form action=\"?\">"
  buf = buf.."<input type=\"button\" style=\"height:70px;width:120px;font-size:40px;\" value=\"+\" onClick=\"javascript:document.getElementsByName('t')[0].value++;\"><br>"
  buf = buf.."Temp:&nbsp;&nbsp;&nbsp;<input style=\"height:50px;width:60px;font-size:40px;margin-top:8px;margin-bottom:8px;\" type=\"text\" name=\"t\" value=\""
  buf = buf..wt
  buf = buf.."\">&nbsp;&nbsp;&nbsp;&nbsp;"
  buf = buf.."<input style=\"transform: scale(4); -webkit-transform: scale(4);\" type=\"checkbox\" name=\"p\" value=\"on\""
  buf = buf..c
  buf = buf..">&nbsp;&nbsp;&nbsp;&nbsp;+ 0.5&ordm;" buf = buf.."<br><input type=\"button\" style=\"height:70px;width:120px;font-size:40px;\" value=\"-\" onClick=\"javascript:document.getElementsByName('t')[0].value--;\">"
  buf = buf.."<br><input type=\"button\" style=\"height:40px;width:120px;font-size:28px;margin-top:8px;color:red;\" value=\"OFF\" onClick=\"javascript:location.href='?t=off'\"><br><br>"
  buf = buf.."<input style=\"height:60px;width:120px;font-size:32px;\" type=\"submit\" value=\"Set\"></form></p>"
  buf = buf.."</center></font></body></html>"

if(_GET.a ~= nil)then
  if(_GET.a ~= nil)then
    action = _GET.a;
    if action == "gt" then
      buf = buf.."HTTP/1.1 200 OK\n\n"
      buf = buf..gt
    end
end

function pulse(dir, anz)
  z=gpio.read(p1)
  for i=1, anz*2, 1 do
   if z == 0 then
     z=1
   else
     z=0
   end
   if dir == "+" then
     gpio.write(p1, z);
     tmr.delay(10000); -- 10ms
     gpio.write(p2, z);
     tmr.delay(10000);
   else
     gpio.write(p2, z);
     tmr.delay(10000);
     gpio.write(p1, z);
     tmr.delay(10000);
   end
  end
end

function settemp(temp)
 print("Setting Temp to "..temp..".")
 tt = (temp*2)-9; 
 pulse("-", 60) --init
 pulse("+", tt)
end