Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Compilation error: missing terminating" character?
Whats wrong in this

What I have tried:

Remove " this added " this but doesn't happen anything
Posted
Updated 25-Feb-23 7:56am

1 solution

The following works for me
C++
#include <cstdio>
using namespace std;
#define DEVICE_LABEL "foo"

int main()
{
  char topic[0x100];
  sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
}

The possible culprit is (your) DEVICE_LABEL. How is it defined?
 
Share this answer
 
v2
Comments
751-Atharva Vanjole 25-Feb-23 14:17pm    
#include <wifi.h>
#include <wifiudp.h>
#include <pubsubclient.h>
#include <ntpclient.h>


#define WIFISSID "xxxxxxxxxxxxx" // Enter WifiSSID here
#define PASSWORD "xxxxxxx" // Enter password here
#define TOKEN "xxxxxxxxxxxxxxxxxxxxxxxx" // Ubidots' TOKEN
#define MQTT_CLIENT_NAME "xxxxxxxxx" // MQTT client Name
// * Define Constants
#define VARIABLE_LABEL "xxxxxxxxxxx" // ubidots variable label
#define DEVICE_LABEL "xxxxxxxxxxxxxxxxxxxx" // ubidots device label


#define SENSORPIN A0 // Set the A0 as SENSORPIN

char mqttBroker[] = "industrial.api.ubidots.com";
char payload[10000];
char topic[150];
// Space to store values to send
char str_sensor[10];
char str_millis[20];
double epochseconds = 0;
double epochmilliseconds = 0;
double current_millis = 0;
double current_millis_at_sensordata = 0;
double timestampp = 0;
int j = 0;
/****************************************
Auxiliar Functions
****************************************/
WiFiClient ubidots;
PubSubClient client(ubidots);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");

void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
Serial.write(payload, length);
Serial.println(topic);
}

void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");

// Attemp to connect
if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) {
Serial.println("Connected");
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
}

/****************************************
Main Functions
****************************************/
void setup() {
Serial.begin(115200);
WiFi.begin(WIFISSID, PASSWORD);
// Assign the pin as INPUT
pinMode(SENSORPIN, INPUT);

Serial.println();
Serial.print("Waiting for WiFi...");

while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}

Serial.println("");
Serial.println("WiFi Connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
timeClient.begin();
client.setServer(mqttBroker, 1883);
client.setCallback(callback);
timeClient.update();
epochseconds = timeClient.getEpochTime();
epochmilliseconds = epochseconds * 1000;
Serial.print("epochmilliseconds=");
Serial.println(epochmilliseconds);
current_millis = millis();
Serial.print("current_millis=");
Serial.println(current_millis);
}

void loop() {
if (!client.connected()) {
reconnect();
j = 0;
}
//sprintf(payload, "%s", "{\"ECG_Sensor_data\": [{\"value\":1234, \"timestamp\": 1595972075},{\"value\":1111, \"timestamp\": 1595971075},{\"value\":2222, \"timestamp\": 1595970075}]}");
j = j + 1;
Serial.print("j=");
Serial.println(j);
sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\": [", VARIABLE_LABEL); // Adds the variable label
for (int i = 1; i <= 3; i++)
{
float sensor = analogRead(SENSORPIN);
dtostrf(sensor, 4, 2, str_sensor);
sprintf(payload, "%s{\"value\":", payload); // Adds the value
sprintf(payload, "%s %s,", payload, str_sensor); // Adds the value
current_millis_at_sensordata = millis();
timestampp = epochmilliseconds + (current_millis_at_sensordata - current_millis);
dtostrf(timestampp, 10, 0, str_millis);
sprintf(payload, "%s \"timestamp\": %s},", payload, str_millis); // Adds the value
delay(150);
}

float sensor = analogRead(SENSORPIN);
dtostrf(sensor, 4, 2, str_sensor);
current_millis_at_sensordata = millis();
timestampp = epochmilliseconds + (current_millis_at_sensordata - current_millis);
CPallini 25-Feb-23 17:14pm    
Are you sure about the error message?
The following code compiles fine in the Arduino IDE
#define VARIABLE_LABEL "xxxxxxxxxxx" // ubidots variable label
#define DEVICE_LABEL "xxxxxxxxxxxxxxxxxxxx" // ubidots device label

char topic[150];
void setup() {
Serial.begin(115200);
delay(500);
}

void loop() {
sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900