Json File, struct, Char Problem

Hi.
I’ve been round in circles for a few hours now, and I think I need help. I have a function that reads in a JSON file and assigns the content to a struct.

This function is a JSON_Functions.h

bool loadJson() {
……
DataStuct.netSSID = NetWork[0].as<const char*>();
DataStuct.netPASS = NetWork[1].as< const char*>();
….
}

This is defended in my main code

struct DataStuct_t {
  const char* netSSID;
  const char* netPASS;
  uint8_t ip1;
  uint8_t ip2;
  uint8_t ip3;
  uint8_t ip4;
  uint8_t sub1;
  uint8_t sub2;
  uint8_t sub3;
  uint8_t sub4;
} ;


Serial.print("SSID: >>" + String(DataStuct.netSSID) + "<< PASS: >>" + String(DataStuct.netPASS) + "<<" );

WiFi.begin(DataStuct.netSSID, DataStuct.netPASS);

The issue I have is if I update the SSID and Pass in the JSON, and then read it in it will not update the DataStuct.netSSID as DataStuct.netPASS as they are const and if I understand correctly once assigned you can not reassign.

I have tried every combination I can think of, I need to come up with a way where if I undated the SSID and Pass I will use those new settings.

The closest I got was changing

const char* netSSID;
 const char* netPASS;

to
char netSSID;
char netPASS;

and 
DataStuct.netSSID = NetWork[0].as<const char*>();
DataStuct.netPASS = NetWork[1].as< const char*>();

To

DataStuct.netSSID = NetWork[0].as<char>();
DataStuct.netPASS = NetWork[1].as<char>();

But when I run I get an error of when calling the WiFi Begin

WiFi.begin(DataStuct.netSSID, DataStuct.netPASS);

Any help would be great

Thanks

Brian

Arduino: 1.8.6 (Windows 7), Board: "WeMos D1 R2 & mini, 80 MHz, 4M (1M SPIFFS), v2 Prebuilt (MSS=536), Disabled, None, 921600"

D:\Aarduino Projects\PixPod v1.0\PixPod\PixPod.ino: In function 'void setup()':

PixPod:369:52: error: call of overloaded 'begin(char&, char&)' is ambiguous

     WiFi.begin(DataStuct.netSSID, DataStuct.netPASS);

                                                    ^

D:\Aarduino Projects\PixPod v1.0\PixPod\PixPod.ino:369:52: note: candidates are:

In file included from C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0\libraries\ESP8266WiFi\src/ESP8266WiFi.h:34:0,

                 from D:\Aarduino Projects\PixPod v1.0\PixPod\PixPod.ino:4:

C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0\libraries\ESP8266WiFi\src/ESP8266WiFiSTA.h:38:21: note: wl_status_t ESP8266WiFiSTAClass::begin(const char*, const char*, int32_t, const uint8_t*, bool) <near match>

         wl_status_t begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);

                     ^

C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0\libraries\ESP8266WiFi\src/ESP8266WiFiSTA.h:38:21: note:   no known conversion for argument 2 from 'char' to 'const char*'

In file included from C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0\libraries\ESP8266WiFi\src/ESP8266WiFi.h:34:0,

                 from D:\Aarduino Projects\PixPod v1.0\PixPod\PixPod.ino:4:

C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0\libraries\ESP8266WiFi\src/ESP8266WiFiSTA.h:39:21: note: wl_status_t ESP8266WiFiSTAClass::begin(char*, char*, int32_t, const uint8_t*, bool) <near match>

         wl_status_t begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);

                     ^

C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0\libraries\ESP8266WiFi\src/ESP8266WiFiSTA.h:39:21: note:   no known conversion for argument 2 from 'char' to 'char*'

exit status 1
call of overloaded 'begin(char&, char&)' is ambiguous

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Why would you change a constant character pointer to a character? JUST drop the const, NOT the *.

PaulS:
Why would you change a constant character pointer to a character? JUST drop the const, NOT the *.

I tried that I changed the struct to “char* netSSID” and “char* netPASS”, however I then can’t seem to assign the value to it when handling the JSON.
The site tells me all the possibilities,

I’ve tried

as()
as()
.as<char*>();

And I get the same error on all of them
error: invalid conversion from ‘ArduinoJson::Internals::JsonVariantAs<char*>::type {aka const char*}’ to ‘char*’ [-fpermissive]

I would have thought that .as<char*>(); should have worked, based on the error message it seems to think that the JsonVariant is a const char*, at the point I’m reading the JSON file (which is just string file) it shouldn’t think this, that’s the point of telling it what var type I what.

In the struct I can’t use string to store them a when I use the WiFi.begin they need to be chars which is a whole lot of bother converting a string to a char

#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson

char netSSID[20];


memset(netSSID, ‘\0’, sizeof(netSSID));
memcpy(netSSID, root[“netSSID”].as<char*>(), sizeof(netSSID));

Flawless.

JMeller:
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson

char netSSID[20];


memset(netSSID, ‘\0’, sizeof(netSSID));
memcpy(netSSID, root[“netSSID”].as<char*>(), sizeof(netSSID));

Flawless.

Telling memcpy() to set 20 characters in netSSID, and then supplying 14 does not make sense. The number of characters to set MUST be based on the string length of the netSSID value in the JSON file (and must NOT be more than the length of the array to set the values in).

// struct field declaration
char *netSSID = NULL;

// struct field initialization
instance.netSSid = strdup(netSSID, root["netSSID"].as<char*>());

is how I would define and value the struct member.

PaulS: Telling memcpy() to set 20 characters in netSSID, and then supplying 14 does not make sense.

I'm attempting to figure out which PaulS I am conversing with in relpy #7 ...

PaulS:
Telling memcpy() to set 20 characters in netSSID, and then supplying 14 does not make sense. The number of characters to set MUST be based on the string length of the netSSID value in the JSON file (and must NOT be more than the length of the array to set the values in).

// struct field declaration

char *netSSID = NULL;

// struct field initialization
instance.netSSid = strdup(netSSID, root[“netSSID”].as<char*>());



is how I would define and value the struct member.

Thanks for your replies guys, sorry for my delay i could find the post again ! I got round the problem by setting the filed in the struct as strings and using WiFi.begin((const char*)local_SSID.c_str(), (const char*)Local_netPASS.c_str());

PS How do you find threads you started on here? I saw your replies last week as they shown up on the unread posts button at the top of the page, but I read them while i was out, and when I came back I they don’t show in the list again as they were clicked, when viewing my profile is doesn’t show threads I started, which mean I can’t find my posts again, which is why is taken me so long to reply