Вопрос по ESP-NOW

Sem553

✩✩✩✩✩✩✩
10 Янв 2021
95
9
Здравствуйте, есть вопрос по работе esp-now, а именно как передать переменную int, как нужно изменить код?

Вот он ниже:
C++:
#include <WifiEspNow.h>
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#endif

// The recipient MAC address. It must be modified for each device.
static uint8_t PEER[]{0x9A, 0xCD, 0xAC, 0x32, 0x28, 0x14};
int test = 65;


void
printReceivedMessage(const uint8_t mac[WIFIESPNOW_ALEN], const uint8_t* buf, size_t count,
                     void* arg)

{
  Serial.printf("Message from %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3],
                mac[4], mac[5]);
  for (int i = 0; i < static_cast<int>(count); ++i) {
    Serial.print(static_cast<char>(buf[i]));
  }
  Serial.println();
}

void
setup()
{
  Serial.begin(115200);
  Serial.println();

  WiFi.persistent(false);
  WiFi.mode(WIFI_AP);
  WiFi.disconnect();
  WiFi.softAP("ESPNOW", nullptr, 3);
  WiFi.softAPdisconnect(false);
  // WiFi must be powered on to use ESP-NOW unicast.
  // It could be either AP or STA mode, and does not have to be connected.
  // For best results, ensure both devices are using the same WiFi channel.

  Serial.print("MAC address of this node is ");
  Serial.println(WiFi.softAPmacAddress());

  uint8_t mac[6];
  WiFi.softAPmacAddress(mac);
  Serial.println();
  Serial.println("You can paste the following into the program for the other device:");
  Serial.printf("static uint8_t PEER[]{0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X};\n", mac[0],
                mac[1], mac[2], mac[3], mac[4], mac[5]);
  Serial.println();

  bool ok = WifiEspNow.begin();
  if (!ok) {
    Serial.println("WifiEspNow.begin() failed");
    ESP.restart();
  }

  WifiEspNow.onReceive(printReceivedMessage, nullptr);

  ok = WifiEspNow.addPeer(PEER);
  if (!ok) {
    Serial.println("WifiEspNow.addPeer() failed");
    ESP.restart();
  }
}

void
loop()
{
  char msg[2];
  int len = snprintf(msg, sizeof(msg), "1");
  WifiEspNow.send(PEER, reinterpret_cast<const uint8_t*>(msg), len);
  delay(3000);
}
В етом коде раз в 3 секунды присылает число 1, как сделать что бы приписать переменную int
вверху добавляю int test = 1;
а в конце в код вместо "1" пишу test, но так програма не хочет пишет ошибку

char msg[2];
int len = snprintf(msg, sizeof(msg), test);
WifiEspNow.send(PEER, reinterpret_cast<const uint8_t*>(msg), len);

как сделать верно?

Заранее благодарен!
 

Сотнег

★★★★★★★
15 Янв 2020
4,104
1,434
@Sem553, единица в кавычках - это не число, а текст из одного символа.
String test = "1";