This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A basic tutorial on MQTT please
#2
its just publish & subscribe..
there is a example of a nodemcu with a 4way relay and temperature sensor arduino ide.code at bottom of post.. subscribe topic A,B,C,D, outTopictemp ..
it would probably be easier to use espeasy and set that up, here is an example of a node subscribing to a esp32 with a bmp280..you can use the same message topic to subscribe in open plotter /controller name/device/data then choose an appropriate sk key to use it in the sys.
i have it working on op 0.17 no problem reading temperature from the arduino code below and from a esp32 with espeasy. ..
either way you are going to have to learn how it works to use it that means google...
almost forgot you need to set it up in op.." broker localhost, port 1883, name, password "

input nodes for node-red
Code:
[
   {
       "id": "95912b82.6dfec8",
       "type": "mqtt in",
       "z": "53e0326c.92abec",
       "name": "",
       "topic": "/esp_easy32/bmp280/Pressure",
       "qos": "2",
       "broker": "ada9afc8.e86e7",
       "x": 143.1667022705078,
       "y": 416.33335876464844,
       "wires": [
           [
               "69626e2f.ea66b",
               "489fd682.9a5378"
           ]
       ]
   },
   {
       "id": "81831f2e.35142",
       "type": "mqtt in",
       "z": "53e0326c.92abec",
       "name": "",
       "topic": "/esp_easy32/bmp280/Temperature",
       "qos": "2",
       "broker": "ada9afc8.e86e7",
       "x": 148.00006866455078,
       "y": 482.0000705718994,
       "wires": [
           [
               "69626e2f.ea66b",
               "777eddb8.f5d7b4"
           ]
       ]
   },
   {
       "id": "ada9afc8.e86e7",
       "type": "mqtt-broker",
       "z": "",
       "broker": "192.168.1.140",
       "port": "1883",
       "clientid": "",
       "usetls": false,
       "compatmode": true,
       "keepalive": "60",
       "cleansession": true,
       "willTopic": "",
       "willQos": "0",
       "willPayload": "",
       "birthTopic": "",
       "birthQos": "0",
       "birthPayload": ""
   }
]


arduino code for mqtt
Code:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// wifi and MQTT broker vars

const char* ssid = "OpenPlotter";              // your network ssid
const char* password = "*******";             // password
const char* mqtt_server = "10.10.10.1";    // your mqtt server
boolean pin1 = false;
boolean pin2 = false;
boolean pin3 = false;                          
boolean pin4 = false;
boolean pin5 = false;
//boolean pin6 = false;
const int   misc_sens_pin = 12;                // temp was after thought 12 was free,. change to pin you like
const int   mqtt_interval = 60000;



WiFiClient espClient;
PubSubClient client(espClient);
OneWire oneWire(misc_sens_pin);
DallasTemperature DS18B20(&oneWire);

long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {

delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
if ( (char)topic[0] == 'A') {
  if ((char)payload[0] == '1') {
    digitalWrite(16, LOW);  // you will need to change pin # if done elswere, it is 16 now thats the onboard led for testing
  }
  if ((char)payload[0] == '0') {
    digitalWrite(16, HIGH);          // and here same as above and for any pin changes
  }
}

if ( (char)topic[0] == 'B') {
  if ((char)payload[0] == '1') {
    digitalWrite(4, LOW);   // Turn the LED on (Note that LOW is the voltage level
  }
  if ((char)payload[0] == '0') {
    digitalWrite(4, HIGH);   // Turn the LED on (Note that LOW is the voltage level
  }
}

if ( (char)topic[0] == 'C') {
  if ((char)payload[0] == '1') {
    digitalWrite(0, LOW);   // Turn the LED on (Note that LOW is the voltage level
  }
  if ((char)payload[0] == '0') {
    digitalWrite(0, HIGH);   // Turn the LED on (Note that LOW is the voltage level
  }
}

if ( (char)topic[0] == 'D') {
  if ((char)payload[0] == '1') {
    digitalWrite(2, LOW);   // Turn the LED on (Note that LOW is the voltage level
  }
  if ((char)payload[0] == '0') {
    digitalWrite(2, HIGH);   // Turn the LED on (Note that LOW is the voltage level
  }
}
}

void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
  Serial.print("Attempting MQTT connection...");
  // Attempt to connect
  //if (client.connect("ESP8266Client5"))
  if (client.connect("esp82661","pi", "*******")){          //Your mqtt password in op*****
    Serial.println("connected");


    // ... and resubscribe
    client.subscribe("A");
    client.subscribe("B");
    client.subscribe("C");
    client.subscribe("D");

  } else {
    Serial.print("failed, rc=");
    Serial.print(client.state());
    Serial.println(" try again in 5 seconds");
    // Wait 5 seconds before retrying
    delay(5000);
  }
}
}


void setup() {

pinMode(16, OUTPUT);
pinMode(4, OUTPUT);
pinMode(0, OUTPUT);                    
pinMode(2, OUTPUT);
pinMode(14, OUTPUT);                    // if you changed temp pin then it needs to be input..
pinMode(12, INPUT);                     //temp pin





Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}

void loop() {
float temp;
char tempStr[5];

if (!client.connected()) {
  reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > mqtt_interval) {
  lastMsg = now;
  ++value;
  DS18B20.requestTemperatures();
  temp = DS18B20.getTempCByIndex(0);

  temp = (temp * 9.0) / 5.0 + 32.0;       // temp C to F

  String(temp).toCharArray(tempStr, 2);

  String(temp).toCharArray(tempStr, 5);
  client.publish("outTopictemp", tempStr);
  client.publish("outTopic", "Relay Board OK");
 
}
}

Reply


Messages In This Thread
A basic tutorial on MQTT please - by affinite - 2018-01-25, 08:28 PM
RE: A basic tutorial on MQTT please - by jim321 - 2018-01-25, 09:41 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)