12king
August 24, 2022, 11:49pm
1
How to publish string from main loop
This publish and subscribe
#include <UIPEthernet.h>
#include <PubSubClient.h>
int led = 13;
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte server[] = { 192, 168, 12, 19 };
byte ip[] = { 192, 168, 12, 20 };
Char array[] = {"1234};
boolean blink = false;
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
// Callback function
void callback(char* topic, byte* payload, unsigned int length) {
}
void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);
Ethernet.begin(mac, ip);
digitalWrite(led, LOW);
Serial.println( Ethernet.localIP());
if (client.connect(" ")) {
client.publish("output/system/","ready");
client.subscribe("input", "1");
Serial.println("1");
digitalWrite(led, HIGH);
}
}
void loop()
{
client.loop();
}
I want to publish array from main loop. How to publish string value to broker ?
client.publish("output/system/","ready");
What does that line of code publish ?
Do you mean a string (a zero terminated array of chars) or a String (an object of the String library) ?
Please give an example
12king
August 25, 2022, 6:56am
3
String (an object of the String library)
String Value = input
The real question is, why are you using a String in the first place ?
Please post an example sketch
1 Like
12king
August 25, 2022, 7:16am
5
I want to publish String Value = input; in sketch
int count = 0; // count = 0
char input[11]; // character array
char * validTags[] = {"0A006FBE33E9", "0E004560173D"};
void setup()
{
Serial.begin(9600); // begin serial port with baud rate 9600bps
Serial1.begin(9600);
}
void loop()
{
if(Serial1.available())
{
count = 0;
If(Serial1.available() && count < 12) // Read 12 characters and store them in input array
{
input[count] = Serial1.read();
count++;
delay(5);
}
if (count == 12){
count = 0;
String Value = input;
Serial.println(Value); // Print RFID tag number
}
for (int t = 0; t < 2; t++)
{
if (strcmp(validTags[t], input) == 0)
{
Serial.print("Tag ");
}
}
}
}
This code looks familiar
Why do you try to copy the input array to a String ? If you put a '\0' on the end of the input array then you will have a C style string (lowercase s) and can publish that
You will need to make the input array larger to accommodate the extra character but it is already too small to hold the 12 characters that you are putting in it
12king
August 25, 2022, 7:39am
7
My mistake
char input[12];
Change the 12 to 13 then do this
input[count] = Serial1.read();
count++;
input[count] = '\0'; //add the terminating zero
When you have finished reading characters you will have a C style string named input which you can print, publish, whatever
12king
August 25, 2022, 7:48am
9
Actually i don't understand from where to publish in sketch
What exactly do you want to publish and what has to happen before you publish it ?
12king
August 25, 2022, 8:22am
11
I want to publish tag number to broker. System should get tag number before to publish
So once you have got the tag number in the input array, publish it instead of, or as well as printing it. You do not need to convert it or copy it to a String (uppercase S) to do this
1 Like
12king
August 25, 2022, 9:14am
13
Here is my sketch, I checked broker and I don't get tag number, broker is connected
#include <UIPEthernet.h>
#include <PubSubClient.h>
int led = 13;
int count = 0; // count = 0
char input[12]; // character array
char * validTags[] = {"0A006FBE33E9", "0E004560173D"};
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte server[] = { 192, 168, 12, 19 };
byte ip[] = { 192, 168, 12, 20 };
EthernetClient ethClient;
void callback(char* topic, byte* payload, unsigned int length);
PubSubClient client(server, 1883, callback, ethClient);
// Callback function
void callback(char* topic, byte* payload, unsigned int length) {
}
void setup()
{
Serial.begin(9600); // begin serial port with baud rate 9600bps
Serial1.begin(9600);
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);
Ethernet.begin(mac, ip);
digitalWrite(led, LOW);
Serial.println( Ethernet.localIP());
if (client.connect(" ")) {
client.publish("output/system/","ready");
client.subscribe("input", "1");
Serial.println("1");
digitalWrite(led, HIGH);
}
}
void loop()
{
client.loop();
if(Serial1.available())
{
count = 0;
if(Serial1.available() && count < 13) // Read 12 characters and store them in input array
{
input[count] = Serial1.read();
count++;
input[count]= '\0';
client.publish("output/system/","input[count]");
delay(5);
}
if (count == 12){
count = 0;
String Value = input;
Serial.println(Value); // Print RFID tag number
}
for (int t = 0; t < 2; t++)
{
if (strcmp(validTags[t], input) == 0)
{
Serial.print("Tag ");
}
}
}
}
When I said
You are not doing that. Instead you are publishing within the for loop that reads the individual characters of the tag ID and to make it worse you are publishing the character of input that you just set to '\0' which marks the end of the string. ie you are publishing an empty string
Publish input after you have collected all of the characters of the tag ID
As a matter of interest is the "ready" message from setup() ever received by the broker and how are you monitoring what it receives ?
12king
August 27, 2022, 2:01am
15
Why I don't receive value of i in broker.
My broker only shows following output on terminal
output i
I was expecting 2 instead of i. What i need to get value of i ?
#include <UIPEthernet.h>
#include <PubSubClient.h>
int led = 13;
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte server[] = { 192, 168, 12, 19 };
byte ip[] = { 192, 168, 12, 20 };
boolean blink = false;
int i = 2;
EthernetClient ethClient;
void callback(char* topic, byte* payload, unsigned int length);
PubSubClient client(server, 1883, callback, ethClient);
// Callback function
void callback(char* topic, byte* payload, unsigned int length) {
}
void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);
Ethernet.begin(mac, ip);
digitalWrite(led, LOW);
Serial.println( Ethernet.localIP());
if (client.connect(" ")) {
// client.publish("output/system/","ready");
client.publish("output", "i");
Serial.println("1");
digitalWrite(led, HIGH);
}
}
void loop()
{
//client.publish("output","ready");
client.loop();
}
The reason that you get i instead of its value is that you send "i"
client.publish("output", "i");
Publish i instead
1 Like
12king
August 27, 2022, 6:42am
17
Thanx there is a problem when i reset the board then only i am getting the value but i don't get 2 i get arrow symbol
do i have to write reconnect function in code
How exactly are you viewing what you receive ?
As an experiment try
client.publish("output", i + 48);
I know that it looks silly but it may provide a clue as to what is going on
1 Like
12king
August 27, 2022, 6:57am
19
I am looking on my windows terminal
client.publish("output", i + 48);
output (null)
client.publish("output", i + 49);
output (null)
I am still not clear how you are viewing what the MQTT broker receives
What are you using as your MQTT broker ?