Current Lua downloadable firmware will be posted here

User avatar
By madarax64
#20622 Hello El42,
Err, okay, well this page; https://github.com/esp8266/Arduino has the general steps you need to get started. You should probably read this too (https://learn.adafruit.com/adafruit-huz ... rduino-ide), and when you get to the part about installing the Adafruit boards, use the board index URL supplied in the first link (http://arduino.esp8266.com/package_esp8 ... index.json) NOT THIS ONE(https://adafruit.github.io/arduino-boar ... index.json). Restart the IDE and set the options for your board as described in the rest of the Adafruit tutorial (set the port number, baud rate and board type as NodeMCU).
Next, you'll need to install the PubSubClient library from here: https://github.com/Imroy/pubsubclient . To install it, download the zip file from that link (the button is on the right side of the page). When you run the Arduino IDE for the first time, it should create a folder in My Documents (on windows) or similar on other OSes, and inside that folder is another folder called libraries. Place the unzipped folder in this libraries subfolder so that your directory tree looks something like this: ..My Documents/arduino/libraries/PubSubClient/<keywords.txt, MQTT.cpp, etc etc>
Here's the code I used to test my own setup. Simply replace your wiFi SSID, Password, MQTT broker address, publish and subscribe topics as required. When you do that, click the Upload button (its on the toolbar and is next to the tick). Having never used the nodeMCU board myself (I use the ESP01) you might need to hold down the GPIO0 button to get the board into bootloader mode and then reset the module while depressing the GPIO0 button until the IDE reports that the upload is complete. Essentially, get the board into bootloader mode then click the upload button and the IDE will take it from there.


Code: Select all
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char *ssid =   "network_name_here";      // cannot be longer than 32 characters!
const char *pass =   "network_password_here";      //

// Update these with values suitable for your network.
IPAddress server(192, 168, 1, 249); //MQTT BROKER IP ADDRESS GOES HERE
long currTime = millis();
bool blinkState = false;

PubSubClient client(server);

// Callback function
void callback(const MQTT::Publish& pub) {
  MQTT::Publish newpub("/pong", "Hollerback"); //I am publishing the message "Hollerback" to the topic "/pong"
  client.publish(newpub);
  yield();
}

void setup()
{
  // Setup console
  Serial.begin(115200);
  delay(10);
  Serial.println();
  Serial.println();
  pinMode(2, OUTPUT);
  client.set_callback(callback);

  WiFi.begin(ssid, pass);

  int retries = 0;
  while ((WiFi.status() != WL_CONNECTED) && (retries < 10)) {
    retries++;
    delay(500);
    Serial.print(".");
  }
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("");
    Serial.println("WiFi connected");
  }

  if (client.connect("esp")) { //replace "esp" with the desired client ID of your ESP8266
    Serial.println("Subscribe success.");
    client.subscribe("/ping"); //I'm subscribing to the /ping topic
  }
}

void loop()
{
  client.loop(); //this must be called regularly to service the MQTT internal loop
 
  if ((millis() - currTime) > 500) {
    blinkState = !blinkState;
    digitalWrite(2,blinkState); //blinky on GPIO2
    currTime = millis();
  }
}



Conversely you could try checking (and if necessary, changing) your WiFi settings like I mentioned in the previous post, if you don't want to tangle with Arduino at the moment.
Sorry for the verbose post...the process can be quite involved.
Let me know how it goes.
Good luck!
User avatar
By El42
#20669 Hello madarax64,
thank you for the help, I'll try later.
My "old" wifi router is only 802.11g so it's already configured as you suggest.

Regards
User avatar
By El42
#21299 Hello madarax64,
I'm trying Arduino IDE and if I use your sketch, it's all ok.

Now I want publish periodic messages for example with 1 minute period.
I use this :
Code: Select allvoid loop()
{
 client.publish("topic","message");
 int cnt = 60;
 while(cnt--)
    delay(1000);
}


My issue is that only first PUBLISH has sent to MQTT broker. What is my error?

Thank you

PS. Apologize me for the OT into this section (LUA Firmware)