ESP8266 and MQTT, Google that and see what happens (you most likely already did and that is how you got here). Shit is everywhere. It’s gobbin up the bandwidth of the intarwebs. And you know what? I couldn’t get any of that shit to work on my standard ESP8266-01. I have a few of them, I tried them all with all kinds of shit. I would get close but never a damn cigar (I can’t smoke it anyways). Until today for months I have been stuck on this. So if you are like me and have not been able to get the damn thing to work, for whatever reason, try this. Hope it helps.
This is the page that saved it all for me, thank these guys (I did not read or follow any guidelines on this page, I merely snagged their code):
https://developer.ibm.com/recipes/tutorials/connect-espressif-esp8266-to-ibm-iot-foundation/
I copied “mainIoTF.lua” and made some minor tweaks to get it working. I removed the orgID and changed the broker to my raspi IP address. Don’t forget to change the topic. I made those tweaks and uploaded it. BAM!! I was getting data send via MQTT to the Pi every 10 seconds.
This is what I was getting on Node-Reds debug window every 10 seconds (the number will change): {"d": {"data":69}}
This is where it comes from, should be easy enough to modify:
m:publish(topic ,'{"d": {"data":'..t1..'}}', 0, 0,
I came back and edited this post because I hate it when I find a site that says they found the fix, but never posts it.. so heres the code. Like I said, its 99% the same code from the other site I linked/mentioned. But here it is for you anyways.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
orgID = "ockton" -- IoT Foundation organization ID broker = "10.0.0.151" -- IP or hostname of IoTF service mqttPort = 1883 -- MQTT port (default 1883: non-secure) userID = "" -- blank for quickstart userPWD = "" -- blank for quickstart macID = "DEADBEEF" -- unique Device ID or Ethernet Mac Address <==== Modify this! clientID = node.chipid() -- Client ID count = 0 -- Test number of mqtt_do cycles mqttState = 0 -- State control topic = "home/test" t0 = tmr.time() -- Wifi credentials SSID = "Tomato Express" -- <========== Modify this! wifiPWD = "notyournetwork" -- <========== Modify this! function wifi_connect() wifi.setmode(wifi.STATION) wifi.sta.config(SSID,wifiPWD) wifi.sta.connect() end function mqtt_do() count = count + 1 -- tmr.alarm counter if mqttState < 5 then mqttState = wifi.sta.status() --State: Waiting for wifi wifi_connect() elseif mqttState == 5 then print("Starting to connect...") m = mqtt.Client(clientID, 120, userID, userPWD) m:on("offline", function(conn) print ("Checking IoTF server...") mqttState = 0 -- Starting all over again end) m:connect(broker , mqttPort, 0, function(conn) print("Connected to " .. broker .. ":" .. mqttPort) mqttState = 20 -- Go to publish state end) elseif mqttState == 20 then mqttState = 25 -- Publishing... t1 = tmr.time() - t0 if t1 > 100 then t1 = 0 t0 = tmr.time() end -- t1 is used as emulated sensor data, real application will use GPIOx to sense external sensor m:publish(topic ,'{"d": {"data":'..t1..'}}', 0, 0, function(conn) -- Print confirmation of data published print("Sent message #"..count.." data:"..t1) mqttState = 20 -- Finished publishing - go back to publish state. end) else print("Waiting..."..mqttState) mqttState = mqttState - 1 -- takes us gradually back to publish state to retry end end tmr.alarm(2, 10000, 1, function() mqtt_do() end) -- send data every 10s |
Discover more from Its_All.Lost
Subscribe to get the latest posts sent to your email.
2 Comments