Skip to content

Instantly share code, notes, and snippets.

@Ames
Ames / mqtt_publish.lua
Last active October 24, 2016 20:44
Publishing to MQTT on NodeMCU
-- Publish a message to the MQTT Broker
client:publish(
"temp/0", -- topic name
tF, -- message payload
1, -- QOS level
1, -- retain
function(conn) -- on-send callback
print("sent.")
end
)
@Ames
Ames / mqtt_connect.lua
Created July 19, 2015 19:00
Connecting to MQTT on NodeMCU
-- Create an MQTT client with a 2m keep-alive interval.
client = mqtt.Client(wifi.sta.getmac(), 120)
-- Connect to the broker
client:connect(
"amesbielenberg.com", -- broker hostname
1883, -- default MQTT port
0, -- no security
function(client) -- on-connect callback
print ("connected.")
@Ames
Ames / wifi_setup.lua
Created July 19, 2015 18:17
Connecting to Wi-Fi on NodeMCU
wifi.setmode(wifi.STATION)
wifi.sta.config("2337","clearfield")
wifi.sta.connect()
wifi.sta.autoconnect(1) -- connect on boot
@Ames
Ames / ds18b20_example.lua
Created July 19, 2015 16:59
Reading temperature from ds18b20 on NodeMCU
-- Pin Mapping
gpio2 = 4
-- Prepare the Thermo Module
ds18b20 = require("ds18b20")
ds18b20.setup(gpio2)
-- The 1-Wire protocol allows many sensors to communicate over the same wire.
-- If addr is nil, a search is performed and the first device is used.
addr = nil