Skip to content
Snippets Groups Projects
Verified Commit ed4672f3 authored by Tuxilio's avatar Tuxilio
Browse files

LED colors are changing on click, send to right MQTT topic

parent e1131d5e
No related branches found
No related tags found
No related merge requests found
...@@ -12,16 +12,33 @@ local data = { ...@@ -12,16 +12,33 @@ local data = {
password = "eZcKn1zBE6KtbeFnZ33lV1EKnr9Bcn8B", password = "eZcKn1zBE6KtbeFnZ33lV1EKnr9Bcn8B",
} }
local ledCount = 10
local led_textures = {
"black",
"blue",
"cyan",
"gray",
"green",
"magenta",
"red",
"white",
"yellow",
}
local led_colors = {}
local active_pos
local active_id
local mqttBaseTopic = core.settings:get("ledtree_base_topic") or "ledtree" local mqttBaseTopic = core.settings:get("ledtree_base_topic") or "ledtree"
local client = mqtt.connect(data.url, data.clientId, data.username, data.password) local client = mqtt.connect(data.url, data.clientId, data.username, data.password)
mqtt.subscribe(client, "ledtree/led/1", function(topic, payload) local change_color = function(player, led_id, pos)
core.chat_send_all("Received message from topic "..topic..": "..payload) local pos_string = core.serialize(pos)
end active_pos = pos
) active_id = led_id
local change_color = function(player, led_id)
local formspec = "size[3,3]" .. local formspec = "size[3,3]" ..
"image_button[0,0;1,1;red.png;red;red;false;false;]" .. "image_button[0,0;1,1;red.png;red;red;false;false;]" ..
"image_button[1,0;1,1;green.png;green;green;false;false;]" .. "image_button[1,0;1,1;green.png;green;green;false;false;]" ..
...@@ -40,14 +57,23 @@ local function send_mqtt(id, rgb) ...@@ -40,14 +57,23 @@ local function send_mqtt(id, rgb)
mqtt.publish(client, mqttBaseTopic.."/led/"..id, "["..rgb.."]") mqtt.publish(client, mqttBaseTopic.."/led/"..id, "["..rgb.."]")
end end
for id=1, 10, 1 do local function replace_block(pos, id, new_texture)
core.register_node("ledtree:led"..id, { --local node = core.get_node(pos)
description = "LED"..id, local node_name = "ledtree:led" .. id .. "_" .. new_texture
--tiles = "", core.set_node(pos, {name = node_name})
on_rightclick = function(pos, node, player, itemstack, pointed_thing) end
change_color(player, id)
end, for id = 1, ledCount do
}) led_colors[id] = ""
for index, texture in ipairs(led_textures) do
core.register_node("ledtree:led" .. id .. "_" .. texture, {
description = "LED" .. id .." " .. texture,
tiles = {texture .. ".png",},
on_rightclick = function(pos, node, player, itemstack, pointed_thing)
change_color(player, id, pos)
end,
})
end
end end
core.register_on_player_receive_fields(function(player, formname, fields) core.register_on_player_receive_fields(function(player, formname, fields)
...@@ -76,8 +102,11 @@ core.register_on_player_receive_fields(function(player, formname, fields) ...@@ -76,8 +102,11 @@ core.register_on_player_receive_fields(function(player, formname, fields)
end end
if rgb ~= "" then if rgb ~= "" then
core.chat_send_player(player:get_player_name(), "RGB: " .. rgb) core.chat_send_player(player:get_player_name(), "LED: ".. active_id .. " - RGB: " .. rgb .. " ("..color..")")
send_mqtt(1, rgb) send_mqtt(active_id, rgb)
replace_block(active_pos, active_id, color)
core.close_formspec(player:get_player_name(), "ledtree:colorpicker")
end end
end end
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment