3 local random = math.random
4 local json = require "json"
5 math.randomseed(os.time())
8 local template ='xxxx-xxxx'
9 return string.gsub(template, '[x]', function (c)
10 local v = random(0, 0xf)
11 return string.format('%x', v)
15 function process_MSG(mid, topic, payload)
17 pcall(function(topic,payload)
18 payload = json.decode(payload)
19 local time = os.date(payload['Time'])
20 for sensor_type,sensor_data in pairs(payload) do
21 if sensor_type ~= "Time" and sensor_type ~= "TempUnit" then
22 for param,value in pairs(sensor_data) do
23 conn:execute(string.format("CALL meteo.submit_mqtt('%s','%s','%s','%s',NULL)", topic,sensor_type,param,value))
25 print(topic,sensor_type,param,value)
35 if not config_name then
36 config_name = "weathermon_mqtt"
39 mqtt_host = uci.get(config_name,"mqtt","server")
44 mqtt_port = uci.get(config_name,"mqtt","port")
49 mqtt_user = uci.get(config_name,"mqtt","username")
50 mqtt_pwd = uci.get(config_name,"mqtt","password")
52 mqtt_id = "wm-mqtt-"..uuid()
54 db_server = uci.get(config_name,"db","server")
55 db_name = uci.get(config_name,"db","db")
56 db_user = uci.get(config_name,"db","username")
57 db_pwd = uci.get(config_name,"db","password")
59 env = require("luasql.mysql").mysql()
60 conn = env:connect(db_name,db_user,db_pwd,db_server)
61 conn:execute("SET NAMES utf8")
63 MQTT = require "mosquitto"
64 mqtt_client = MQTT.new(mqtt_id)
66 mqtt_client:login_set(mqtt_user, mqtt_pwd)
69 mqtt_client:connect("estia.rvb-home.lan",1883)
70 mqtt_client.ON_MESSAGE = process_MSG
72 cur = conn:execute("SELECT DISTINCT topic FROM mqtt_topics WHERE topic<>''")
75 mqtt_client:subscribe(rec)
79 mqtt_client:loop_forever()