question

masoos avatar image
masoos asked

Arduino to VRM Example

Hi, I'm searching for a basic Arduino code example, but there is nothing yet. very confusing after all these years.....

Here is my code, which does not work...I always get "Fehler beim Abrufen der Daten" means the else path of the HTTP_Code_OK. Can someone please teach me??

Thanks

----------

void loop() {

if(wifiMulti.run() == WL_CONNECTED) {

String url = "https://vrmapi.victronenergy.com/v2/full";
HTTPClient http;
http.addHeader("x-authorization:", "eyJ0eAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjgwY2NhN2VhYjYyNWZlNjVmZmM4YTI1ZTY2OTIxODQ2In0.eyJ1aWQiOiIzNTMyOTgiLCJ0b2tlbl90eXBlIjoiZGVmYXVsdCIsImlzcyI6InZybWFwaS52aWN0cm9uZW5lcmd5LmNvbSIsImF1ZCI6Imh0dHBzOi8vdnJtYXBpLnZpY3Ryb25lbmVyZ3kuY29tLyIsImlhdCI6MTY5MzY1MzkyMCwiZXhwIjoxNjkzNzQwMzIwLCJqdGkiOiI4MGNjYTdlYWI2MjVmZTY1ZmZjOGEyNWU2NjkyMTg0NiJ9.ZSNaayziK_8zlwfGktiHvnNe1WrXSRk3GtvaTvT0x5kqbvXBkHYY6UmuQ6lq6xtz4VIbzl-BEO1bmBf3udMd5km2i-uSXe64KAOoEbXM-fCpmn_B7b64h3AxMH8ApPUm4YUwVeChcaQzfHbMQLrcOtyyn8g4b5eylDLvhLhs8pq3_HnuzS-7aH-SkJttzena1A1NgjJed4An096DoNOzaZF_To7p0F0ocqRCaK9pIllYVOgKovC-rLJvs1j3NX-iIm1tFsQ8CRMxWVm9rZ7RsjOxFwNl2SzR2Imk_5BcceQXVlrLVwvxPVXyIOTPGl-iIbFNlkH5-gUmrfVLVvUQ"); // Ihr Authentifizierungstoken

http.begin(url);

int httpResponseCode = http.GET();

if (httpResponseCode == HTTP_CODE_OK) {

String payload = http.getString();

Serial.println(payload);

} else {

Serial.println("Fehler beim Abrufen der Daten");

}

http.end();


} else {

Serial.println("Verbindung zum WLAN verloren, erneuter Verbindungsversuch...");

}

delay(5000);

}

VRMarduino
1 comment
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

kevgermany avatar image kevgermany ♦♦ commented ·
@masoos

Moved to modifications space.

0 Likes 0 ·
5 Answers
matt1309 avatar image
matt1309 answered ·

Hi @masoos

ChatGPT will be your friend here.

However an easy debug is in your else statement to add Serial.println(httpResponseCode);

This will at least give you the error. It may be due to lack of Content-type header.

ie adding another header ('Content-Type: application/json')

I'm not familiar with Victron API, there may be other required headers also but printing the response code should give you a good idea.

I would test the API call using a tool like CURL then when you know you're getting the data you want translate the curl command into C++/arduino code.


2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

masoos avatar image
masoos answered ·

I'm sorry. I need to inform you, that this code was propsed by ChatGPT. I tried with several http.addHeaders, w/o success.

3 comments
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

matt1309 avatar image matt1309 commented ·

Hi @masoos

What debugging steps have you done?

Are you sure it's your code and not the request itself that's wrong?


Here's the steps I would take to debug:

1. Get a working api request to VRM, that you know works, using curl!

This will confirm if the issues are with your code or with what you're requesting. At the moment it could just be the token/authentication header is wrong. Or the endpoint of https://vrmapi.victronenergy.com/v2/full is wrong.


I would highly recommend using curl for this. Once you've got a curl command working and returning data, turning that into C++ will be easy. (chatGPT would literally do that for you...)



2. As I mentioned before print httpResponseCode in the else statement. Otherwise you have no idea what responseCode you're receiving. This will likely help a lot with what's going wrong with the request.






0 Likes 0 ·
Show more comments
excalibur83 avatar image
excalibur83 answered ·

Hello,

are you sure httpResponseCode should be declared as int (Integer) ?

1 comment
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

masoos avatar image masoos commented ·
yes, result is "401"
0 Likes 0 ·
masoos avatar image
masoos answered ·

I also tried with other endpoint and now auth with email....

-------------------------------

HTTPClient http;

String payload = "{\"username\":\"m@s....\",\"password\":\"123....\"}";

http.sendRequest();

http.post("/v2/auth/login");

http.sendHeader("Content-Type", "application/json");

http.sendHeader("Content-Length", payload.length());

http.beginBody();

http.print(payload);

http.endRequest();

int statusCode = http.responseStatusCode();

String response = http.responseBody();

Serial.print("HTTP-Statuscode: ");

Serial.println(statusCode);

Serial.print("Antwort vom Server: ");

Serial.println(response);

-----------------


does not work either. hmmmmm

3 comments
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

matt1309 avatar image matt1309 commented ·

what response/status code are you getting?

0 Likes 0 ·
masoos avatar image masoos matt1309 commented ·
Again 401....
0 Likes 0 ·
matt1309 avatar image matt1309 masoos commented ·

Hi @masoos


payload.length i think needs to be a string in the header.

Try:

HTTPClient http;
String payload = "{\"username\":\"m@s....\",\"password\":\"123....\"}";

http.begin("https://vrmapi.victronenergy.com/v2/auth/login"); // Set the target URL

http.addHeader("Content-Type", "application/json");
http.addHeader("Content-Length", String(payload.length()));

int statusCode = http.POST(payload); // Send the POST request with the payload

// Print status code and response
Serial.print("HTTP-Statuscode: ");
Serial.println(statusCode);
String response = http.getString();
Serial.println(response);

http.end(); // Close the connection



1 Like 1 ·
masoos avatar image
masoos answered ·

OK, this worked!

Result:

HTTP status code: 200

{"token":"xxxx","idUser":yyyyy,"verification_mode":"password","verification_sent":false}


200 is http okay.
I think I can continue like this now.

Question: what does "verification_sent":false}" mean exactly? Do I have to verify myself with token, or is mail/PW enough? Or to put it another way, when do I have to take tokens instead?

Thank you very much for your efforts

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

Related Resources