在 Arduino 上通过 MQTT 将 int 数组作为字符串发布
Publish array of int as string over MQTT on Arduino
作为新手,这让我抓狂了好几个小时:
我有:
int relayStates[] = {0,0,1,1,0,1,0,0};
稍后在我的代码中,我想使用 PubSubClient MQTT 将状态发布为 char[] 为:
00110100
client.publish(topic,<here char[]>);
我尝试了所有我能想到的转换,但没有任何效果。
有人可以帮助我吗?
此致,
奥斯卡
让我们以手动方式进行操作,以便您了解如何从 int 数组构建字符串
定义一个足够大的全局字符数组:
char textToSend [9] = {'[=10=]'}; // takes 8 chars and a terminator
这里我们从 relayStates[]
和 "convert" 复制到 char(适用于所有个位数)
textToSend [0] = '[=11=]'; // we reset the char array
for(int i = 0; i < 8; i++) {
if (relayStates[i] == 0) textToSend [i] = '0'; // SINGLE quote as it is a char
if (relayStates[i] == 1) textToSend [i] = '1'; // SINGLE quote as it is a char
}
textToSend [8] = '[=11=]'; // we terminate the char array
client.publish(topic, textToSend); // we transfer the array content to MQTT
该方法透明,内存效率高,可用于稳定的生产环境。
您可以添加的改进:
- 从int数组size获取for循环的结束值
- 使其与 0 和 1 以外的整数一起使用 - 使用辅助数组进行转换
char numBuffer [9] = {'[=13=]'}; // takes 8 chars and a terminator for converting ints
itoa(relayStates[i], numBuffer,10); // converts an int to a base 10 dec char array
strcat(textToSend, numBuffer);
很抱歉迟到这个话题的问题。我尝试了发布一个整数数组(0 和 1 除外)的解决方案。但是数组里面只写了一个int。
for (int i = 0; i <= 2; i++) {
for (int j = 0; j <= 2; j++) {
digitalWrite(adressPins[j], bitRead(i, j));
delayMicroseconds(50);
}
feuchteRoh[i] = analogRead(33);
}
for (int i = 0; i <= 2; i++) {
trockenheit[i] = map(feuchteRoh[i], rohTief, rohHoch, 0, 100);
}
for (int i = 0; i <= 2; i++) {
feuchtigkeit[i] = 100 - trockenheit[i];
Serial.println(feuchtigkeit[i]);
}
for (int i = 0; i <= 2; i++) {
itoa(feuchtigkeit[i], numBuffer, 10);
}
Serial.println(numBuffer);
msg[0] = '[=10=]';
strcat(msg, numBuffer);
msg[10] = '[=10=]';
client.publish("ESP32/ Test", msg);
变量名是德语,希望这不是问题。
感谢您的帮助。
作为新手,这让我抓狂了好几个小时:
我有:
int relayStates[] = {0,0,1,1,0,1,0,0};
稍后在我的代码中,我想使用 PubSubClient MQTT 将状态发布为 char[] 为:
00110100
client.publish(topic,<here char[]>);
我尝试了所有我能想到的转换,但没有任何效果。 有人可以帮助我吗?
此致, 奥斯卡
让我们以手动方式进行操作,以便您了解如何从 int 数组构建字符串 定义一个足够大的全局字符数组:
char textToSend [9] = {'[=10=]'}; // takes 8 chars and a terminator
这里我们从 relayStates[]
和 "convert" 复制到 char(适用于所有个位数)
textToSend [0] = '[=11=]'; // we reset the char array
for(int i = 0; i < 8; i++) {
if (relayStates[i] == 0) textToSend [i] = '0'; // SINGLE quote as it is a char
if (relayStates[i] == 1) textToSend [i] = '1'; // SINGLE quote as it is a char
}
textToSend [8] = '[=11=]'; // we terminate the char array
client.publish(topic, textToSend); // we transfer the array content to MQTT
该方法透明,内存效率高,可用于稳定的生产环境。
您可以添加的改进:
- 从int数组size获取for循环的结束值
- 使其与 0 和 1 以外的整数一起使用 - 使用辅助数组进行转换
char numBuffer [9] = {'[=13=]'}; // takes 8 chars and a terminator for converting ints
itoa(relayStates[i], numBuffer,10); // converts an int to a base 10 dec char array
strcat(textToSend, numBuffer);
很抱歉迟到这个话题的问题。我尝试了发布一个整数数组(0 和 1 除外)的解决方案。但是数组里面只写了一个int。
for (int i = 0; i <= 2; i++) {
for (int j = 0; j <= 2; j++) {
digitalWrite(adressPins[j], bitRead(i, j));
delayMicroseconds(50);
}
feuchteRoh[i] = analogRead(33);
}
for (int i = 0; i <= 2; i++) {
trockenheit[i] = map(feuchteRoh[i], rohTief, rohHoch, 0, 100);
}
for (int i = 0; i <= 2; i++) {
feuchtigkeit[i] = 100 - trockenheit[i];
Serial.println(feuchtigkeit[i]);
}
for (int i = 0; i <= 2; i++) {
itoa(feuchtigkeit[i], numBuffer, 10);
}
Serial.println(numBuffer);
msg[0] = '[=10=]';
strcat(msg, numBuffer);
msg[10] = '[=10=]';
client.publish("ESP32/ Test", msg);
变量名是德语,希望这不是问题。
感谢您的帮助。