local lcd = require("i2clcdpcf") local json = require('sjson') local SSID = "XXXXXXXXXXXXXX" local SSID_PASSWORD = "XXXXXXXXXXXXXX" local API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" local ROUTES = { { name = "Stecke 1", origin = "52.520490,13.413232", destination = "52.513226,13.2399238", waypoints = { "52.515109,13.350030" } }, { name = "Stecke 2", origin = "52.520490,13.413232", destination = "52.513226,13.2399238", waypoints = { "52.525538,13.419398", "52.546504,13.359172" } } } local SIGNAL_MODE = wifi.PHYMODE_N INTERVAL = 60 -- seconds LCD_LENGTH = 20 function wait_for_wifi_conn ( ) tmr.alarm (1, 1000, 1, function ( ) if wifi.sta.getip ( ) == nil then print ("Waiting for Wifi connection") else tmr.stop (1) print ("ESP8266 mode is: " .. wifi.getmode ( )) print ("The module MAC address is: " .. wifi.ap.getmac ( )) print ("Config done, IP is " .. wifi.sta.getip ( )) end end) end function concat(str) local len = string.len(str) if len > LCD_LENGTH then return string.sub(str, LCD_LENGTH) else local p = string.rep(" ", LCD_LENGTH - len) return str .. p end end function main(key) route = ROUTES[key] local waypoints = "" for k,w in pairs(route["waypoints"]) do waypoints = waypoints .. w .. "|" end local url = "https://tutorials-raspberrypi.de/wp-content/uploads/scripts/maps.php?" .. "origin=" .. route["origin"] .. "&destination=" .. route["destination"] .. "&waypoints=" .. waypoints .. "&mode=driving&departure_time=now&key=" .. API_KEY http.get(url, nil, function(code, data) print(key) if (code >= 0) then local tab = json.decode(data) local text = "n.v." if tab["routes"][1]["duration"] ~= nil then local val = tonumber(tab["routes"][1]["duration"]) local mins = math.floor(val/60 + 0.5) local hours = (mins - (mins%60)) / 60 mins = mins%60 if hours > 0 then text = string.format("%dh %dmin", hours, mins) else text = string.format("%dmin", mins) end end route["text"] = concat( route["name"] .. ": " .. text ) if (key < table.getn(ROUTES)) then main(key+1) else lcd.begin(4,3) lcd.setBacklight(1) for k, r in pairs(ROUTES) do lcd.setCursor(0, k-1) lcd.print(r["text"]) end end end end) end -- Configure the ESP as a station (client) wifi.setmode(wifi.STATION) wifi.setphymode(SIGNAL_MODE) wifi.sta.config {ssid=SSID, pwd=SSID_PASSWORD} wifi.sta.autoconnect(1) -- Hang out until we get a wifi connection before the httpd server is started. wait_for_wifi_conn ( ) tmr.alarm(2, INTERVAL * 1000, tmr.ALARM_AUTO, function () main(1) end)