does anyone know how to convert a char to uint16_t*?
I would attempt something like this:
char c;
uint16_t val = *reinterpret_cast<uint16_t*>(&c);
Try it out though. It's my first day programming C++
Why do you want to do that? It doesn't make sense to turn an 8-bit character into a 16-bit pointer to unsigned integer, especially if the character happens to be an odd number - all sorts of mayhem would ensue.
But I suppose you could do this:
char c;
uint16_t *ip;
.
.
ip = (uint16_t *)c;
Pete
Agree with el_supremo... it makes little sense so feels like a XY problem (You want to do X, and you think Y is the best way of doing so and thus instead of asking help for X, you ask guidance for Y)
What’s your use case?
Adafruit_MQTT_Client mqtt(&client, uintServer, uintPort, uintUsername, uintKey); // Setup Adafruit IO
Adafruit_MQTT_Publish gpg = Adafruit_MQTT_Publish(&mqtt, uintUsername "/feeds/salty.grains-per-gallon");
Adafruit_MQTT_Publish ppm = Adafruit_MQTT_Publish(&mqtt, uintUsername "/feeds/salty.parts-per-million");
Adafruit_MQTT_Publish alerts = Adafruit_MQTT_Publish(&mqtt, uintUsername "/feeds/salty.alerts");
Adafruit_MQTT_Subscribe iqOnOffButton = Adafruit_MQTT_Subscribe(&mqtt, uintUsername "/feeds/salty.wateriq");
I need to pass this Adafruit IO command 4 uint16_t* variables. I get the values to pass to this command in char form. I think I need to convert them from chars to uint16_t* variables.
A char is a data
A uint16_t * is a pointer to unsigned integral data
So it makes no sense
I need to pass this Adafruit IO command 4 uint16_t* variables.
Where do you see that?
What is the format of the thing you receive in char ? What does it look like?
you probably need to pass is a pointer to the data of this is to call the constructor
class Adafruit_MQTT {
public:
Adafruit_MQTT(const char *server,
uint16_t port,
const char *cid,
const char *user,
const char *pass);
Are you looking at proving info for the const char *
?
How would I go about passing the char to the port argument, then?
Adafruit_MQTT_Publish gpg = Adafruit_MQTT_Publish(&mqtt, aioUsername, "/feeds/salty.grains-per-gallon");
Also, when I try to pass this the username variable, I get this error message.
error: invalid conversion from 'const char*' to 'uint8_t {aka unsigned char}' [-fpermissive]
Again What is the format of the thing you receive in char ? What does it look like?
The port variable is 4 numbers long. The username variable is 5 to 20 letters long.
I get the values to pass to this command in char form
Let's try one more time. Where do you get those characters from? The Serial port? A file on an SD card?
POST YOUR CODE. ALL OF IT.
Pete
This is C++, you get the nice syntax for casts:
uint16_t(x)
Again show what you receive? Like is it a long cstring on one line, comma separated elements ?
If so just parse it to have pointers to the start of each substring by adding ‘\0’ where necessary - strtok() can help for this
Since my code is over 900 lines long, I have posted the parts relating to the problem here.
#include <FS.h> // Hardware Management
#include <ArduinoJson.h>
char aioServer[40];
char aioPort[6];
char aioUsername[15];
char aioKey[40];
char apiKey[40];
// JSON Addresses:
//config.json
//aioServer
//aioPort
//aioUsername
//aioKey
//apiKey
void MQTT_connect();
void setup() {
if (SPIFFS.begin()) {
Serial.print("Reading saved credentials...");
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
Serial.print("");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
Serial.print("");
size_t size = configFile.size();
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()) {
Serial.println(" done!");
strcpy(aioServer, json["aioServer"]);
strcpy(aioPort, json["aioPort"]);
strcpy(aioUsername, json["aioUsername"]);
strcpy(aioKey, json["aioKey"]);
strcpy(apiKey, json["apiKey"]);
} else {
Serial.println(" failed");
}
}
} else {
Serial.println(" done!");
}
} else {
Serial.println(" failed");
}
Serial.println(" "); // Connect to WiFi access point.
Serial.println("Connecting to WiFi");
if (eeGetInt(48) < 100) {
deviceId = random(100,1000);
eeWriteInt(48, deviceId);
} else {
deviceId = eeGetInt(48);
}
String deviceBuffer = "SALTY-";
deviceBuffer += String(deviceId);
WiFiManagerParameter custom_aio_server("Adafruit IO Server", "Adafruit IO Server", aioServer, 40);
WiFiManagerParameter custom_aio_port("Adafruit IO Port", "Adafruit IO Port", aioPort, 6);
WiFiManagerParameter custom_aio_username("Adafruit IO Username", "Adafruit IO Username", aioUsername, 15);
WiFiManagerParameter custom_aio_key("Adafruit IO Key", "Adafruit IO Key", aioKey, 32);
WiFiManagerParameter custom_api_key("Google API Key", "Google API Key", apiKey, 39);
wifiManager.addParameter(&custom_aio_server);
wifiManager.addParameter(&custom_aio_port);
wifiManager.addParameter(&custom_aio_username);
wifiManager.addParameter(&custom_aio_key);
wifiManager.addParameter(&custom_api_key);
wifiManager.autoConnect(deviceBuffer.c_str());
wifiManager.setConfigPortalTimeout(180);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
Serial.println(F("WiFi connected! "));
Serial.print(F("IP address: ")); Serial.println(WiFi.localIP());
Serial.println(" ");
bool updateFileSystem = false;
if (strlen(custom_aio_server.getValue()) > 2) {
strcpy(aioServer, custom_aio_server.getValue());
json["aioServer"] = aioServer;
bool updateFileSystem = true;
} else if (strlen(aioServer) < 2) {
strcpy(aioServer, "io.adafruit.com");
json["aioServer"] = aioServer;
bool updateFileSystem = true;
}
if (strlen(custom_aio_port.getValue()) > 2) {
strcpy(aioPort, custom_aio_port.getValue());
json["aioPort"] = aioPort;
bool updateFileSystem = true;
} else if (strlen(aioPort) < 2) {
strcpy(aioPort, "8883");
json["aioPort"] = aioPort;
bool updateFileSystem = true;
}
if (strlen(custom_aio_username.getValue()) > 2) {
strcpy(aioUsername, custom_aio_username.getValue());
json["aioUsername"] = aioUsername;
bool updateFileSystem = true;
}
if (strlen(custom_aio_key.getValue()) > 2) {
strcpy(aioKey, custom_aio_key.getValue());
json["aioKey"] = aioKey;
bool updateFileSystem = true;
}
if (strlen(custom_api_key.getValue()) > 2) {
strcpy(apiKey, custom_api_key.getValue());
json["apiKey"] = apiKey;
bool updateFileSystem = true;
} else if (strlen(apiKey) < 2) {
strcpy(apiKey, "AIzaSyDrmCNZtBlV5F1O-J5Kwla9P2cXcBUJGQc");
json["apiKey"] = apiKey;
bool updateFileSystem = true;
}
if (updateFileSystem) {
Serial.print("Saving updated credentials...");
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println(" failed");
Serial.println(" ");
} else {
Serial.println(" sent!");
Serial.println(" ");
}
json.printTo(configFile);
configFile.close();
}
}
Adafruit_MQTT_Client mqtt(&client, aioServer, aioPort, aioUsername, aioKey); // Setup Adafruit IO
Adafruit_MQTT_Publish gpg = Adafruit_MQTT_Publish(&mqtt, aioUsername, "/feeds/salty.grains-per-gallon");
Adafruit_MQTT_Publish ppm = Adafruit_MQTT_Publish(&mqtt, aioUsername, "/feeds/salty.parts-per-million");
Adafruit_MQTT_Publish alerts = Adafruit_MQTT_Publish(&mqtt, aioUsername, "/feeds/salty.alerts");
Adafruit_MQTT_Subscribe iqOnOffButton = Adafruit_MQTT_Subscribe(&mqtt, aioUsername, "/feeds/salty.wateriq");
void loop() {
}
Re: msg #7
The Adafruit_MQTT_Publish function/method is declared like this in the library:
Adafruit_MQTT_Publish::Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver,const char *feed, uint8_t q)
so if you call it with this:
Adafruit_MQTT_Publish gpg = Adafruit_MQTT_Publish(&mqtt, aioUsername, "/feeds/salty.grains-per-gallon");
you are trying to give it "/feeds/salty.grains-per-gallon" as the "uint8_t q" argument. That is why the error message says "invalid conversion from 'const char*' to 'uint8_t".
I've never used MQTT etc. so I have no idea what should be there.
Pete
I tried giving it a uint8_t variable I defined (for testing) and I got this error message
invalid conversion from 'uint8_t' to 'const char*'
I'm so confused.
A pointer in memory is NOT a 8 bit random integral...
Do you understand the code you posted ? what parts did you add?
I have tried using a pointer, and I get
invalid conversion from 'const char*' to 'uint8_t* {aka unsigned char*}' [-fpermissive]
You need to understand exactly what the library is expecting for that argument. Just sticking a random variable in there isn't going to get you anywhere.
Pete
Do you know what type of variable that argument takes?