Edited the post by @AvdH .
@AvdH. You can nicely post codes if you put four spaces in front of each line.
contentLength = 0
-- user:password (pimatic) in BASE64; ex. "dXNlcjpwYXNzd29yZA=="
-- This online tool can encode/decode base 64 for you http://www.motobit.com/util/base64-decoder-encoder.asp
-- admin:A2kwadraat = YWRtaW46QTJrd2FkcmFhdA==
base64login = "YWRtaW46QTJrd2FkcmFhdA==" -- UTF8
-- pimatic server IP
-- according to docs can also be domain
--pimaticServer = "192.168.178.64"
pimaticServer = "pimatic"
-- pimaticPort = "80"
pimaticPort = 80
-- Pimatic variable which should be updated
variableName = "scene"
-- send data every X seconds
interval = 60
-- Show Script version
function showVersion()
print("")
print("esp8266-pimatic Scene switch")
print("v 0.01.11")
print("")
end
--- calc content-length
function calcLength(type)
print(type)
contentLength = string.len(type) + 40
print("Content-Length: "..contentLength)
end
--- send data
function sendData(type, name)
calcLength(type)
print("Sending data ...")
-- conn=net.createConnection(net.TCP, 0)
-- conn:on("receive", function(conn, payload) print(payload) end)
-- conn:connect(pimaticPort,pimaticServer)
-- conn:connect(80,"192.168.2.187")
-- conn:send("PATCH /api/variables/"..name.." HTTP/1.1\r\n")
-- conn:send("Authorization: Basic "..base64login.."\r\n")
-- conn:send("Host: "..pimaticServer.."\r\n")
-- conn:send("Content-Type:application/json\r\n")
-- conn:send("Content-Length: "..contentLength.."\r\n\r\n")
-- conn:send("{"type": "value", "valueOrExpression": "..type.."}")
---
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) end)
conn:connect(pimaticPort,pimaticServer)
conn:send("PATCH /api/variables/"..name.." HTTP/1.1\r\n")
conn:send("Authorization: Basic "..base64login.."\r\n")
conn:send("Host: "..pimaticServer.."\r\n")
conn:send("Content-Type:application/json\r\n")
conn:send("Content-Length: "..contentLength.."\r\n\r\n")
conn:send("\{\"type\"\: \"value\"\, \"valueOrExpression\"\: "..type.."\}")
---
conn:on("sent",function(conn)
print("Closing connection")
conn:close()
end)
conn:on("disconnection", function(conn)
print("Got disconnection...")
end)
---
end
--- main loop
print("main.lua started")
showVersion()
print("IP address is "..wifi.sta.getip())
tmr.alarm(0, (interval*1000), 1, function()
sendData("Test", variableName)
end )