Sprintf 'char' to 'const char*'

Hey guys I'm having some trouble getting this sprintf to work.

I got this code that connects to a hotspot, when connected, it sends its MAC address, then the hotspots send data: parameter1 / parameter2.

I find the / and I find the last char of parameter 1 and the first char of parameter 2.
With those, I want to make a char array. For this example, it should be 1p

But I'm having problems making the char array, I keep getting :
invalid conversion from 'char' to 'char*'

I tried multiple things but with no succes.

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

// Set WiFi credentials
#define WIFI_SSID "Little Edian Hotspot"

// UDP
WiFiUDP UDP;
#define UDP_PORT 4210
char incomingPacket[255];  // buffer for incoming packets
const char seperator = '/';
uint8_t wantedpos;

void setup() {
  // Setup serial port
  Serial.begin(115200);
  Serial.println();

  // Begin WiFi
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID);


  // Connecting to WiFi...
  Serial.print("Connecting to ");
  Serial.print(WIFI_SSID);
  // Loop continuously while WiFi is not connected
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(100);
    Serial.print(".");
  }

  // Connected to WiFi
  Serial.println();
  Serial.print("Connected! IP address: ");
  Serial.println(WiFi.localIP());

  // Begin UDP port
  UDP.begin(UDP_PORT);
  Serial.print("Opening UDP port ");
  Serial.println(UDP_PORT);

  UDP.beginPacket(UDP.remoteIP(), UDP_PORT);
  UDP.write(WiFi.macAddress().c_str());
  UDP.endPacket();

  Serial.println(WiFi.macAddress().c_str());
}

void loop() {

  int packetSize = UDP.parsePacket();
  if (packetSize)
  {
    // receive incoming UDP packets
    Serial.printf("Received %d bytes from %s, port %d\n", packetSize, UDP.remoteIP().toString().c_str(), UDP.remotePort());
    int len = UDP.read(incomingPacket, 255);
    if (len > 0)
    {
      incomingPacket[len] = 0;
    }
    Serial.printf("UDP packet contents: %s\n", incomingPacket);

    for (int i = 0; i < strlen(incomingPacket); i++) {
      if (seperator == incomingPacket[i]) {
        wantedpos = i;
        break;
      }
    }
    if (wantedpos != 0) {
      char buffer[255];
      sprintf("%c%c", incomingPacket[wantedpos - 2], incomingPacket[wantedpos + 2]);
      Serial.println(buffer);
      Serial.println(incomingPacket[wantedpos - 2]);
      Serial.println(incomingPacket[wantedpos + 2]);      
//      UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
//      UDP.write(buffer);
//      UDP.endPacket();
      wantedpos = 0;
    }
  }
}

What is the first parameter of the sprintf() function ?
If you don't know, then don't think about the sketch, and ask yourself where the data is going.

1 Like

What happens when you change the line above to (note the cast on the conversion string):

      sprintf( (char *) "%c%c", incomingPacket[wantedpos - 2], incomingPacket[wantedpos + 2]);

It will still be wrong. The first argument to sprintf() is a pointer to the buffer where the created string will be stored, NOT the conversion string. Be sure you've actually created the buffer space before calling sprintf(), don't just give it a random (or null) pointer.

1 Like