My plan is to modify the master scetch such that the transmitted info would be like "TemperatureOutdoor,21.3", and that the ESP to MQTT part will use the TemperatureOutdoor as the MQTT topic and 21.3 as the payload. I do not know if "," is the best character to use for this.
My set-up is the slave and master scetches from above and an additional ESP2866 connected to the slave by Tx-> Rx, Rx->Tx and gnd->Gnd.
Using the code below I can transfere the complete transmission from the master to MQTT, it will be posted in the MQTT topic "test". However, despite many many atempts I have not been able to spit the message into two parts (i.e "TemperatureOutdoor" and use this as the MQTT topic instead of "test" and use "21.3" as MQTT payload).
Is there some one that can help me to do this?
The Slave to MQTT code is attached below.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "yyy";
const char* password = "xxxxx";
const char* mqtt_server = "192.168.2.171";
const byte numChars = 100;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void setup_wifi() {
delay(10);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void callback(char* topic, byte* payload, unsigned int length) {
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}
void reconnect() {
String clientName;
clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
clientName += "-";
clientName += String(micros() & 0xff, 16);
while (!client.connected()) {
if (client.connect((char*) clientName.c_str())) { // random client id
digitalWrite(BUILTIN_LED, LOW); // low = high, so this turns on the led
client.subscribe("test"); // callback: mqtt bus -> arduino
} else {
digitalWrite(BUILTIN_LED, HIGH); // high = low, so this turns off the led
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
client.publish("test", receivedChars); // publish: arduino -> mqtt bus
newData = false;
}
}
Hello thanks for the interesting code , I would like to know if it would be possible to have 2 simultaneous peers at the same time and send data to which peer separately.
gcharles:
Hello thanks for the interesting code , I would like to know if it would be possible to have 2 simultaneous peers at the same time and send data to which peer separately.
I assume you mean 1 master sending to 2 slaves?
It's while since I was familiar with this because the ESP8266 uses too much current in my application. So I don't know the answer to your question.
Re-reading my earlier Replies here I suspect you could give each of the slaves the same MAC address and then they would receive the same message. It should not be difficult to set up a simple test.
You could include an ID code in the message so that the slave would know whether the message was intended for it.
You would need to ensure that only one device transmits at any one time.
can you explain the use of the WIFI_CHANNEL. For example how did you know in the your CONTROLLER code it is WIFI_CHANNEL 4 where it does not seem to be used at ALL in your SLAVE code ?
mars000:
can you explain the use of the WIFI_CHANNEL.
Simple answer is that I can't.
I have forgotten most that I learned while I was working on that code as I have not used it since. And, to be honest, I can't recall if I ever knew the answer to your question.
mars000:
can you explain the use of the WIFI_CHANNEL. For example how did you know in the your CONTROLLER code it is WIFI_CHANNEL 4 where it does not seem to be used at ALL in your SLAVE code ?
To make it easy for people to help you please modify your post and use the code button </>
so your code looks like this and is easy to copy to a text editor. See How to use the Forum
Your code is too long for me to study quickly without copying to my text editor. The text editor shows line numbers, identifies matching brackets and allows me to search for things like all instances of a particular variable or function.
Also please use the AutoFormat tool to indent your code for easier reading.
Hi Robin2!
how can i use you example for remote control?
can you write this example with the code for one button master
that to light the LED on the slave
for example:
for the master - pin buttons - 14
for the slave - pin LED - 2
i use WEMOS D1 mini Pro
lm317t:
Hi Robin2!
how can i use you example for remote control?
I'm sure you can.
It's a long time since I wrote the program and I'm afraid I have forgotten all I had learned at the time so I am not able to provide any more assistance.
Robin2: EXAMPLE PROGRAMS
The following 2 pairs of examples are functionally similar to the first 2 examples in my Simple nRF24L01+ Tutorial
Simple one-way transmission from the controller to slave. I am presenting the slave code first because it is where the mac address is defined and the controller must use that address.
EspnowSlave.ino
Hey Robin, can you please tell me how I can send an array of 2 variables using this code of yours? I tried to make some changes but it doesn't work, please help me
joseben:
Hey Robin, can you please tell me how I can send an array of 2 variables using this code of yours? I tried to make some changes but it doesn't work, please help me
It's a long time since I wrote this so I may not be able to help. However until you post your program so I can see what you have tried I definitely can't.
Robin2:
It's a long time since I wrote this so I may not be able to help. However until you post your program so I can see what you have tried I definitely can't.
...R
It's okay, I finally did manage to send the variables, instead of using an array, I used them as variables in the structure data.Its working just fine.
busco:
How could I make a message from slave_2 return to Master? Thank you
Sorry, as I said in Reply #22 its a long time since I was familiar with this code so I probably can't help. Hopefully somebody else may be able to.
It would be a good idea to post the programs that represent your best attempt and tell us in detail what they actually do and what you want them to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.