Jump to content
  • 0

Modify global variable using REST API with HTTP.PUT


Go to solution Solved by parper,

Question

Hello,

I'm trying to modify a already define global variable using the REST API for the HC2.

This is done from a ESP2866 (nodeMCU-isch) board.

 

I can do a http.get towards the HC" and I get the correct JSON information.

When I do http.put I seem not to be able to set the variable.

My message headers contains a basic auth, and I have payload that looks like this

{"value":"On"}

The address looks like this http://1.2.3.4/api/globalVariables/thevariable

 

According to the httpclient i'm using i seem to get return code 200 but no result and the variable is not changed.

 

Using the hc2ip/docs link I can test this out with the correct payload and it works fine.

However I'm logged in as an Admin then.

Trying to change the user from my ESP2866 I just got that the HC" banned me for 30 min.

 

So is there a problem using normal users when trying to modify a global variable ? Do I need to be admin ?

Or is there something else I need to check ?

 

Regards P

Link to post
Share on other sites

15 answers to this question

Recommended Posts

  • 0

Hello, 

I have checked that and that is not the problem, length variable that is.

How ever I can see that normal users in Fibaro HC2 only has read access to the global variables.

This is probably the cause of this.

 

I do not want to login with my admin account so how do I give access to the global variables from a normal user ?

Anyone know ?

Link to post
Share on other sites
  • 0

Ok I got the solution:

 

Since 4.530 of HC2(can't tell if this is the case on earlier versions).

1. To read global variables you can use any of the registered HC2 users.

2. To write you need to be administrator. I needed to use my whole email to the super user and password. Then I could set the global variables.

       [email protected]/password when generating the basic auth secret.

 

So this is a working pseudo code package structure:

 

PUT /api/globalVariables/myvar HTTP/1.1
Authorization: Basic thiscontainsthehashedsecretstring
Content-Length: 14(is calculated depending on the payload)
Host: 192.168.xx.xx:80
Content-Type: application/json

{"value":"tothevalueyouwant"}

Link to post
Share on other sites
  • 0

Hi Parper,

 

Good to see you have solved this. I'm still struggeling a bit to get my lolin32 sending variable values to my HC2.

Do you sent the Username/Password in the URL? If you add it before the IP like HTTP:[email protected]:[email protected]:80/api/globalVariables/yourvariable it doesn't seem to work with the email adress.

 

Or do you add the authentication in the header? Can you share an example?

 

Thanks,

 

Erik

Link to post
Share on other sites
  • 0

Thanks for the reply. Can you please share an example.

This is what I have now in my arduino sketch: 

 

http.addHeader(" {\"Content-Type\" : \"application/json\"",
               " \"login\": \"admin username\"",
               "\"password\": \"admin password\"}");

 

This results in a 401 response code....

Edited by EK
Link to post
Share on other sites
  • 0

@EK

String doCall(String sceneId, String param) {

  int x = D1;

  String url = String("http://192.168.1.36:80/api/scenes/") + sceneId + String("/action/start");
  String payload = "<empty>";

  //Serial.println("CALL BEGIN");

  HTTPClient http;
  http.begin(url);
  http.addHeader("Content-Type", "application/json");
  http.setAuthorization("[email protected]", "Micro$oft");
  int httpCode = http.POST(param);
  //Serial.println(httpCode);
  if (httpCode > 0) {
    // HTTP header has been send and Server response header has been handled
    // file found at server
    if (httpCode == HTTP_CODE_OK) {
      payload = http.getString();
    }
    else {
      payload = String(httpCode);
    }
  }
  else {
    payload = "ERROR: " + String(http.errorToString(httpCode).c_str());
  }
  http.end();
  //Serial.println("CALL DONE");

  return payload;
}

 

Link to post
Share on other sites
  • 0

Thanks for the code. I had all but

http.setAuthorization ("[email protected]", "Micro$oft");

But when I insert it (with the right credentials of course) it returns an error:


HTTP_Put_temp_and_hum:71:1: error: stray '\357' in program
 http.setAuthorization("[email protected]", "Micro$oft");
 ^

 

Without this line, I get no errors in the script. any thoughts? Am I using the wrong library?

(now using #include <WEMOS_SHT3X.h>
SHT3X sht30(0x45);
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>)

 

Link to post
Share on other sites
  • 0

#include "ESP8266WiFi.h"
#include <ESP8266HTTPClient.h>
#include <ESP8266mDNS.h>
 

Edited by 10der
Link to post
Share on other sites
  • 0
On 6/8/2019 at 8:49 PM, 10der said:

@EK

String doCall(String sceneId, String param) {

  int x = D1;

  String url = String("http://192.168.1.36:80/api/scenes/") + sceneId + String("/action/start");
  String payload = "<empty>";

  //Serial.println("CALL BEGIN");

  HTTPClient http;
  http.begin(url);
  http.addHeader("Content-Type", "application/json");
  http.setAuthorization("[email protected]", "Micro$oft");
  int httpCode = http.POST(param);
  //Serial.println(httpCode);
  if (httpCode > 0) {
    // HTTP header has been send and Server response header has been handled
    // file found at server
    if (httpCode == HTTP_CODE_OK) {
      payload = http.getString();
    }
    else {
      payload = String(httpCode);
    }
  }
  else {
    payload = "ERROR: " + String(http.errorToString(httpCode).c_str());
  }
  http.end();
  //Serial.println("CALL DONE");

  return payload;
}

 

 

Hi @10der

 

Thanks for sharing the above

What does the 'param' represent in the function call above

 

I want to start scene  269  so I use 

doCall(269) but not sure what else to pass in the function call

 

I tried leaving it blank but it wouldn't compile

full sketch attached

 

 

 

 

Sound_Detect.txt.ino

Edited by AutoFrank
Link to post
Share on other sites
  • 0
51 minutes ago, AutoFrank said:

 

Hi @10der

 

Thanks for sharing the above

What does the 'param' represent in the function call above

 

I want to start scene  269  so I use 

doCall(269) but not sure what else to pass in the function call

 

I tried leaving it blank but it wouldn't compile

full sketch attached

 

 

 

 

Sound_Detect.txt.ino

 

All sorted  - thanks 

Link to post
Share on other sites
  • 0
10 hours ago, AutoFrank said:

 

All sorted  - thanks 

param - it's json.

 

doCall("94", sendCommand("RF-ID", "CONNECTED"));
String sendCommand(String command, String value)
{
  String json = "{\"args\":[\"" + command + "\", \"" + value + "\"]}";
  return json;
}

after this trick you can get any data in your scene 

Link to post
Share on other sites
  • 0

This is what works for me when updating global variables using a wemos.

 

Some other example used a POST but that didn't work. PUT does work

 

http.begin("http://IP-address:80/api/globalVariables/Temp_Kelder");
          http.setAuthorization("Username", "passwd");	//admin credentials

int httpResponseCode = http.PUT("{\"value\": \"" +String(sht30.cTemp) +"\"}");   //Send the actual Put request

   if(httpResponseCode>0){
 
    String response = http.getString();                       //Get the response to the request
    Serial.print("HTTP Put Response Code: ");
    Serial.println(httpResponseCode);   //Print return code
    Serial.println(response);           //Print request answer
 
   }
   else{
 
    Serial.print("Error on sending PUT: ");
    Serial.println(httpResponseCode);
 
   }

        http.end();

 

  • Thanks 1
Link to post
Share on other sites
  • 0
7 minutes ago, EK said:

This is what works for me when updating global variables using a wemos.

 

Some other example used a POST but that didn't work. PUT does work

 

http.begin("http://IP-address:80/api/globalVariables/Temp_Kelder");
          http.setAuthorization("Username", "passwd");	//admin credentials

int httpResponseCode = http.PUT("{\"value\": \"" +String(sht30.cTemp) +"\"}");   //Send the actual Put request

   if(httpResponseCode>0){
 
    String response = http.getString();                       //Get the response to the request
    Serial.print("HTTP Put Response Code: ");
    Serial.println(httpResponseCode);   //Print return code
    Serial.println(response);           //Print request answer
 
   }
   else{
 
    Serial.print("Error on sending PUT: ");
    Serial.println(httpResponseCode);
 
   }

        http.end();

 

 

Thanks @EK

Link to post
Share on other sites
  • 0

This is far too complicated ... Is it  so that we have to make fibaro to poll outside system with http (because https does not wok either) ,  or open fibaro web interface to internet via NAT just to do the update manually.

 

Not fun at all :-(

 

 

 

 

 

Edited by Markoe
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...