"i"
is not a character, it’s a character array that basically decays to a pointer to the first element.
You almost certainly want 'i'
.
Alternatively, you may actually want a lookup based on more than a single character, in which case you should be using "i"
but the type in that case is const char *
rather than just char
, both when defining c
and in the base::lookup()
method.
However, if that were the case, I’d give serious thought to using the C++ std::string
type rather than const char *
. It may not be necessary, but using C++ strings may make your life a lot easier, depending on how much you want to manipulate the actual values.
"i"
is not a character, it’s a character array that basically decays to a pointer to the first element.
You almost certainly want 'i'
.
Alternatively, you may actually want a lookup based on more than a single character, in which case you should be using "i"
but the type in that case is const char *
rather than just char
, both when defining c
and in the base::lookup()
method.
However, if that were the case, I’d give serious thought to using the C++ std::string
type rather than const char *
. It may not be necessary, but using C++ strings may make your life a lot easier, depending on how much you want to manipulate the actual values.
I’m trying to upload the code from here to work with an ESP8266. So I copy/pasted the code to the Arduino IDE and I get this error:
C:Program Files (x86)ArduinolibrariesWiFisrc/WiFi.h:79:9: error: initializing argument 1 of ‘int WiFiClass::begin(char*, const char*)’ [-fpermissive]
int begin(char* ssid, const char *passphrase);
^exit status 1
invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
Can anyone help me with this?
per1234
3,9832 gold badges20 silver badges40 bronze badges
asked Jun 21, 2017 at 19:50
This begin()
method expects a modifiable character array as its first argument. That’s what you should provide:
char ssid[] = "YOUR_SSID"; // this is changed
const char* password = "YOUR_PASSWORD"; // this is fine
[...]
WiFi.begin(ssid, password);
answered Jun 21, 2017 at 20:00
Edgar BonetEdgar Bonet
40.6k4 gold badges36 silver badges74 bronze badges
4
The location where you are calling the function begin, has as first parameter a parameter of type const char* instead of char* … remove the const from this argument type.
Probably you have something like
const char* s = ....
...
...begin(s, ...)
Change class s to
char* s = ...
answered Jun 21, 2017 at 19:52
Michel KeijzersMichel Keijzers
12.8k7 gold badges37 silver badges56 bronze badges
2
ArduinoJson returns keys and values as const char*
.
If you try to put these values into a char*
, the compiler will issue an error (or a warning) like the following:
invalid conversion from 'ArduinoJson::Internals::JsonVariantAs<char*>::type {aka const char*}' to 'char*' [-fpermissive]
This happens with any of the following expression:
char* sensor = root["sensor"];
char* sensor = root["sensor"].as<char*>();
// in a function whose return type is char*
return root["sensor"].as<char*>();
The solution is to replace char*
by const char*
const char* sensor = root["sensor"];
const char* sensor = root["sensor"].as<char*>();
// change the return type of the function
return root["sensor"].as<char*>();
See also:
- API Reference: JsonVariant::as<T>()
Offline
Зарегистрирован: 11.01.2019
Дано:
Пример из стандартной библиотеки WiFi.h, в котором в самом начале объявляются две переменные:
// Please input the SSID and password of WiFi const char* ssid = " "; // const char* password = " "; //
Я, как честный Буратино, вставляю в кавычки имя своей Wi-Fi сети, а затем — PublickKey, пароль то бишь…
Пускаю на компиляцию — здрасьте-нате:
invalid conversion from 'const char*' to 'char*' [-fpermissive]
Указывают на строку с инициализацией Wi-Fi модуля:
WiFi.begin(ssid, password);
…и подчеркивают переменную ‘password’.
Но мы же умные! Мы же знаем, что в библиотеке надо посмотреть, как именно объявлены типы данных для этой процедуры! Смотрю:
int WiFiClass::begin(char* ssid, const char *passphrase){}
И вот тут я уже понимаю, что ничего не понимаю! А разве в самом начале, еще перед всеми Void, не было человеческим языком С++ объявлено, что переменная ‘password’ имеет именно тот тип, который нужен процедуре?