Coding an ESP8266 to the use the Bond API

I am a newbie. Last week I couldn’t spell API but after much reading and searching I have made real progress. But today I have to admit I am stuck. I hope this forum - and this category - is appropriate for my question.

The good news: I am able to control my fireplace (on/off, flame size, fan speed) using the Bond API commands. I can execute the HTTP code successfully using either a windows cmd line interface (with curl), or by importing the raw command into Postman. So I am confident I have the right Bond API syntax and logic.

The bad news: My objective is to use an ESP8266 (Arduino) with additional logic to control my fireplace. That’s where I am stuck. I have reviewed a lot of code - but I can’t find examples of an ESP8266 interfacing with a Bond. So I modified a simple example that does an HTTP PUT call and tried to substitute my BOND API commands. Unfortunately I keep getting json errors.

I concede that this is more of a coding issue of an API call within an ESP8266, as opposed to a BOND API issue (especially since I know that my commands work in Postman). Nonetheless, after struggling for some time, I hope someone on this forum might be able to help me over this hurdle.

Here is one of my commands that works successfully in Postman:
curl -X PUT -H “Content-Type: application/json” -H "BOND-Token: fd****1" -i http://192.168.../v2/devices/7d/actions/TurnFpFanOff -d “{}”

Following is the esp8266 code that I have modified to use the above command. It returns a json parse error {"_error_id":23,"_error_msg":“json parse error”}

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

void setup()
{
Serial.begin(115200); //Serial connection
WiFi.begin(“my sid”, “my password”); //WiFi connection

while (WiFi.status() != WL_CONNECTED)
{ //Wait for the WiFI connection completion
delay(500);
Serial.println(“Waiting for connection”);
}

HTTPClient http; //Declare object of class HTTPClient
http.begin(“http://192.168../v2/devices/7d/actions/TurnFpFanOff -d “{}”");
http.addHeader(“Content-Type”, “application/json”); //Specify content-type header
http.addHeader(“BOND-Token”, "fd
*******1”);

int httpCode = http.PUT(“Message from ESP8266”); //Send the request
String payload = http.getString(); //Get the response payload

Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
http.end(); //Close connection
}

void loop()
{
}

Thank you very much for your assistance.

I think you’re asking this question on the wrong forum; you just need help making an HTTP request with ESP8266.

For one thing, this line is wrong. The “-d” option for curl specifies the body to include. You cannot include it like this.

http.begin(“http://192.168…/v2/devices/7d/actions/TurnFpFanOff -d “{}””);

Instead, it should be:

http.begin("http://192.168../v2/devices/7d/actions/TurnFpFanOff");

Finally, I think this line:

int httpCode = http.PUT(“Message from ESP8266”); //Send the request

Should be (I think):

int httpCode = http.PUT("{}"); //Send the request

You should also rigorously check your code. In the code you’ve shared, a lot of your quotes are using “smartquotes”. These all need to be translated to simple quotes. The compiler doesn’t understand smartquotes.

1 Like

Thank you for your timely response. I appreciate your help. I agree with you that it’s more of an HTTP request issue than a BOND API issue. Nonetheless I tried this forum as I thought someone might have done exactly what I was trying to do.

If I may, I have one observation after removing the -d"{}" as you suggested. I tried this modified line in Postman and it failed. In Postman (at least), I need to include the -d"{}". That’s why I assumed I would have to include it in my HTTP PUT call on the ESP8266.

I will experiment with the http.PUT("{}") next.

Thank you again for your help.

If I may, I have one observation after removing the -d"{}" as you suggested. I tried this modified line in Postman and it failed. In Postman (at least), I need to include the -d"{}". That’s why I assumed I would have to include it in my HTTP PUT call on the ESP8266.

In Postman, you’ll need to use the “-d” but not in ESP8266.

“-d” is a curl command which references the body. Postman is just interpreting the curl command line switch for you. In order to port your HTTP request, you need to reformat those command line switches into ESP8266-compatible language. I’m sharing another example of how to run a curl request with PHP. I translated the curl command using an online utility for you.

Essentially, you need to translate any HTTP request properly no matter how you use it.

Thanks very much dxdc for your quick follow-up reply. I will carefully review the example you provided me and apply it to my requirements.

Hi. I “believe” I have resolved the HTML/ json syntax errors, as I am now getting an HTTP Return Code of 200. I have modified my ESP8266 sketch as follows:

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

void setup()
{
   Serial.begin(115200); //Serial connection
   WiFi.begin("my sid", "my password"); //WiFi connection

   while (WiFi.status() != WL_CONNECTED)
   { //Wait for the WiFI connection completion
      delay(500);
      Serial.println("Waiting for connection");
   }

   HTTPClient http; //Declare object of class HTTPClient
   http.begin("http://192.168.1.**/v2");

   http.addHeader("Content-Type", "application/json");
   int httpResponseCode = http.PUT("{\"BOND-Token\":\"fd*************1\",\"devices\":\"7*****d\",\"actions\":\"SetFpFan\",\"argument\":\"1\"}");
   
   Serial.print("HTTP Response code: ");
   Serial.println(httpResponseCode);

   String payload = http.getString(); //Get the response payload

   Serial.println(payload); //Print request response payload
   http.end(); //Close connection
}

void loop()
{
}

The resulting output is:
12:07:40.226 → Waiting for connection
12:07:40.733 → Waiting for connection
12:07:41.226 → Waiting for connection
12:07:41.410 → HTTP Response code: 200

Even though the HTTP Response code is 200, the fireplace does not respond. (Recall, the equivalent command in Postman works properly and does control the fireplace).

Following the HTTP Response code is approximately 300 lines of the Request Response Payload from BOND. (As a result, I “believe” that I am reaching my BOND device). It appears to me that the Payload is the full BOND Documentation - and not specific to my request.

Sample of a few lines of the payload:

1 2:07:41.593 →
12:07:41.593 →
12:07:41.593 →
12:07:41.593 → * {
12:07:41.593 → margin: 0 auto;
12:07:41.593 → width: 100%;
12:07:41.593 → display: block;
12:07:41.593 → text-align: center;
12:07:41.593 → font-family: sans-serif;
12:07:41.593 → transition: all .75s
12:07:41.593 → }
12:07:41.593 →
12:07:41.593 → body {

12:07:41.778 →
Everything Smart…
12:07:41.778 →

12:07:41.778 →




12:07:41.778 →
Connecting to BOND product…


12:07:41.966 →

12:07:41.966 →
The following three fields are mandatory


12:07:42.763 → de(“token”).style.display = “none”;
12:07:42.763 → de(“m”).style.display = “none”;
12:07:42.763 → get_token();
12:07:42.763 → advanced();
12:07:42.763 → static_ip();
12:07:42.763 → setInterval(function() {
12:07:42.763 → get_token();
12:07:42.763 → get_sys_wifi_sta();
12:07:42.763 → }, 2000)
12:07:42.763 →
12:07:42.763 →
12:07:42.763 →

I appear to be at the point where I am now struggling with communicating properly with the BOND so that it will in turn control my fireplace. Perhaps you can tell me what I am doing wrong. I seem to be very close - your help is very appreciated. Thanks

Hi @canquest,

Looks like you need to still make the modifications that @dxdc suggested:

and


Here’s what’s happening:

You’re currently using the url http://192.168.1.**/v2 but it should be http://192.168.1.**/v2/devices/7d/actions/TurnFpFanOff.

If you query http://192.168.1.**/v2/ (note the trailing slash), you will get the root topic of the Bond API. However, without the trailing slash, you will get the WebGUI (which is exactly what you’re seeing.)

Thanks very much Merck! I’ll make those changes and see what happens. Much appreciated.

That worked (for the TurnFpFanOff command)! Thanks very much @dxdc and @merck for your guidance. I knew I was close - I just needed that little extra help to get it to work.

1 Like

(This may well be more of an http coding issue, as opposed to an BOND API issue, but I want to make sure that I am using the API correctly)

As mentioned above, BOND API commands that don’t require arguments work well from an esp8266.
However, most BOND API commands do require arguments. For example, in order to turn the fireplace fan on, I need to pass an argument. Normally on an esp8266, this would be accomplished by appending ?argument= to the url.
For example,
http.begin(“http://192.168.1.74/v2/devices/76ad588d/actions/SetFpFan?argument=1”);
But I am having no success with this.
I have also tried replacing argument with fpfan_speed (which I found in the BOND API Documentation), which was also not successful.
If someone can advise me how I can pass an argument to control the fireplace fan, that would be wonderful. Thanks very much in advance.

I am going to assume you will modify the empty PUT dxdc mentioned above to something like this:

http.begin("http://192.168.1.74/v2/devices/76ad588d/actions/SetFpFan");

int httpCode = http.PUT("\"argument\": 1}");

The escape for putting double quotes around argument varies between platforms - but " is a common way to say that in many environments.

YES! That worked! Thank you very much @residualimages, @merck and @dxdc. I realize that my problem was mostly due to coding - so thank you for going the extra mile and helping me out.

Now I can combine the power of my esp8266 with the great BOND API interface to create unlimited customized integrations between my microcomputer and my fireplace and my ceiling fans.

Thank you all so very much for your help.