(SOLVED): Expected primary-expression before 'const'

Hello,

I am trying to send one UDP packet but get stuck on concatenate between different data type and don’t know how to solve it.

The code:

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

WiFiUDP Udp;
const char* UdpIPaddress  = "192.168.0.240";
unsigned int UdpPort = 4210;  

const char* Udpmessage  = "Message";
int a=0;
float Temp=29.25;

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

void loop() {
  if(a==0){
    a = a + 1;
    const char* sendmessage = const char*(a) + "=" + const char*(Temp) + "=" + Udpmessage;
    UdpSend(sendmessage, UdpIPaddress, UdpPort);
  }
}

void UdpSend(const char* message, const char * ipaddress, int port){
      Serial.println("Send message: ");
      Udp.beginPacket(ipaddress, port);
      Udp.write(message);
      Udp.endPacket();
}

Arduino IDE compiler error: expected primary-expression before ‘const’

Please help…

wieb:

WiFiUDP Udp;

const char* UdpIPaddress  = "192.168.0.240";
.....
unsigned int UdpPort = 4210; 
const char* Udpmessage  = "Message";
.....

I see 2 const in your code. I bet the cpmpiler stops on the first.
So, there is an error in the line before : WiFiUDP Udp;
Check spelling

This code is very confusion to me… but it looks you are trying to do type-casting which fails. try something like this:

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

WiFiUDP Udp;
const char* UdpIPaddress  = "192.168.0.240";
unsigned int UdpPort = 4210; 
char buf[40];

const char* Udpmessage  = "Message";
int a=0;
float Temp=29.25;

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

void loop() {
 if(a==0){   // WHY check on zero ??? 
   a = a + 1;
   sprintf(buf, "%d=%f=%s", a,Temp, Udpmessage);
   UdpSend(buf, UdpIPaddress, UdpPort);
 }
}

void UdpSend(const char* message, const char * ipaddress, int port){
     Serial.println("Send message: ");
     Udp.beginPacket(ipaddress, port);
     Udp.write(message);
     Udp.endPacket();
}

demkat1:
I see 2 const in your code. I bet the cpmpiler stops on the first.
So, there is an error in the line before : WiFiUDP Udp;
Check spelling

Compiler stops in this line:

const char* sendmessage = const char*(a) + "=" + const char*(Temp) + "=" + Udpmessage;

Already double & triple check the spelling but not found any spelling mistake.

paulvha:
// WHY check on zero ???

I am trying to send only one UDP packet, that is why check on zero.

Or maybe you have a better solution? I am just a newbie, still lack of knowledge.

Thanks a lot PAULVHA for the guidance, because of your code now it works perfectly... :slight_smile: :slight_smile: :slight_smile:

wieb:
Compiler stops in this line:

const char* sendmessage = const char*(a) + "=" + const char*(Temp) + "=" + Udpmessage;

Already double & triple check the spelling but not found any spelling mistake.

Oops, that is a 3rd one const.

I didn’t look in “loop” because you cannot assign - change values in a const.

Perhaps this is the reason of the error