Code Question - Trying to cast Strings as Char Arrays for network credentials

Hello Arduino Forum members,

I am working on a project using an Arduino UNO R4 WiFi where it should connect to a wireless local area network in order to send webhook signals to in IFTTT protocol. The WiFi credentials are entered through an Arduino cloud dashboard, and there are 2 cloud variables that pass the credentials into the code. (see attached)

The issue I am having is in the 'thingProperties.h' file, which is auto generated by Arduino IOT cloud, but I am editing it so that the network credentials can be changed in the desired way, and not by changing the constants in the auto generated file.

As per my understanding, the ArduinoIoTPreferredConnection() function takes 2 char array parameters to use as the SSID and passkey. Because of this, I am attempting to convert my cloud variables (ssid & password) which are strings into char arrays using the strcpy() function. When I attempt to verify this code, I get a very familiar error message:

expected constructor, destructor, or type conversion before '(' token

This error arises for both lines where I call the strcpy() function. I understand that this error often is the compiler not understanding some sort of syntax error that has occurred in the code (i.e. missing ; on line before), however I cannot find any. Please help me resolve this error. Thank you in advance.

Arduino IoT Cloud Dashboard

thingProperties.h

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

#include <iostream>
#include <cstring>
#include <string.h> 


String password;
String ssid;

CloudCounter flow_abnormal;


const int passLength = password.length(); 
const int ssidLength = ssid.length(); 

// declaring character arrays (+1 for null terminator) 
char* PASS = new char[passLength + 1]; 
char* SSID = new char[ssidLength + 1]; 

// copying the contents of the strings to char arrays 

strcpy(PASS, password.c_str()); 
strcpy(SSID, ssid.c_str()); 
//error occurs here ^

void initProperties(){

  ArduinoCloud.addProperty(password, READWRITE, ON_CHANGE);
  ArduinoCloud.addProperty(ssid, READWRITE, ON_CHANGE);
  ArduinoCloud.addProperty(flow_abnormal, READWRITE, ON_CHANGE);

}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID,PASS);

Please disregard the references to a variable called flow_abnormal, it is irrelevant to the error in question.

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

#include <iostream>
#include <cstring>
#include <string.h>

String password;
String ssid;
const char* PASS = password.c_str();
const char* SSID = ssid.c_str();

CloudCounter flow_abnormal;

void initProperties() {
  ArduinoCloud.addProperty(password, READWRITE, ON_CHANGE);
  ArduinoCloud.addProperty(ssid, READWRITE, ON_CHANGE);
  ArduinoCloud.addProperty(flow_abnormal, READWRITE, ON_CHANGE);
}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

First, the last line of that code makes no sense. It's not inside a function, it's not a function call, it's not a function definition, it's not a function prototype. It is a compiler error waiting to happen.

Second, if it were a proper function call, why bother creating the SSID and PASS variables at all? Just do it on the fly:

   ArduinoIoTPreferredConnection(ssid.c_str(), password.c_str());
2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.