Skip to content

Instantly share code, notes, and snippets.

@marcszy91
Last active February 5, 2021 21:53
Show Gist options
  • Save marcszy91/aeaba9046667f699b8a0b366d80fd417 to your computer and use it in GitHub Desktop.
Save marcszy91/aeaba9046667f699b8a0b366d80fd417 to your computer and use it in GitHub Desktop.
Lua snippet for parsing latest temperature for DIY Cloud Weather Station with ESP32/ESP8266 https://randomnerdtutorials.com/cloud-weather-station-esp32-esp8266/ for neutrino hd receiver
-- Author Ezak (Marc Szymkowiak)
-- 2021
-- LICENSE MIT
function init()
--config
windowTitle = "Temperature"
url = "http://replace_url_to_weather_station.com/"
-- global vars
rowIndex = 0;
roomIndex = 0
temperatureIndex = 0;
roomTable = {};
temperatureTable = {};
n = neutrino();
end
function readFile(_File)
local fp, s
fp = io.open(_File, "r")
if fp == nil then
error("Error opening file '" .. _File .. "'.");
end
s = fp:read("*a");
fp:close();
return s;
end
function downloadHtml()
-- download html file to parse data from
filename = "/tmp/temperature.txt"
os.execute("wget -q -O " .. filename .. " '" .. url .. "'" );
end
function parseTemperature()
local htmlFile = "/tmp/temperature.txt"
-- read html code from file
local htmlSourceCode = readFile(htmlFile);
-- search for table with temperature readings
local tableReadings = htmlSourceCode:match("<table.*id=\"tableReadings\">(.*)</table>");
-- read table with temperatures
for row in string.gmatch(tableReadings, "<tr>(.-)</tr>") do
-- read table header (room name)
if (rowIndex == 0) then
for room in string.gmatch(row, "<th>(.-)</th>") do
-- rowIndex 0 is datename and no room
if (roomIndex > 0) then
roomTable[roomIndex] = room
end
roomIndex = roomIndex + 1
end
end
-- read latest temperature for all rooms
if (rowIndex == 1) then
for temperature in string.gmatch(row, "<td>(.-)</td>") do
-- read datetime
if (temperatureIndex == 0) then
datetime = temperature
else
-- read latest temperature
temperatureTable[temperatureIndex] = temperature
end
temperatureIndex = temperatureIndex + 1
end
end
rowIndex = rowIndex + 1
end
end
function neutrinoExec()
-- show window until exit key is pressed
repeat
msg, data = n:GetInput(500)
until msg == RC['home'] or msg == RC['setup'];
end
function getRoomString()
roomString = ""
for index, room in pairs(roomTable) do
roomString = roomString .. room .. "\t" .. "\t" .. "\t" .. "\t";
end
return roomString
end
function getTemperatureString()
temperatureString = ""
for index, temperature in pairs(temperatureTable) do
temperatureString = temperatureString .. temperature .. " ºC" .. "\t" .. "\t";
end
return temperatureString
end
function showWindow()
local roomsRow = getRoomString();
local temperatureRow = getTemperatureString();
local spacer = 8;
local x = 500;
local y = 150;
local dx = 800;
local dy = 300;
local fh_title = n:FontHeight(FONT['MENU_TITLE']);
-- create and show window
local window = cwindow.new{x=x, y=y, dx=dx, dy=dy, title=windowTitle};
window:paint();
local x1 = x + spacer;
local y1 = y + window:headerHeight() + spacer;
local dx1 = dx - spacer*2;
local dy1 = fh_title;
-- create rooms table (rooms)
roomCT = ctext.new{x=x1, y=y1, dx=dx1, dy=dy1, text=roomsRow,
mode="ALIGN_AUTO_WIDTH | ALIGN_AUTO_HIGH | ALIGN_TOP | ALIGN_CENTER",
font_text=FONT['MENU']};
roomCT:paint();
-- next line
y1 = y1 + fh_title + spacer*2;
-- create temperature table (temperatures)
temperatureCT = ctext.new{x=x1, y=y1, dx=dx1, dy=dy1, text=temperatureRow,
mode="ALIGN_AUTO_WIDTH | ALIGN_AUTO_HIGH | ALIGN_TOP | ALIGN_CENTER ",
font_text=FONT['MENU']};
temperatureCT:paint();
neutrinoExec();
roomCT:hide();
temperatureCT:hide();
window:hide();
end
function main()
init()
downloadHtml()
parseTemperature()
showWindow()
end
-- main
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment