Hello all - I am trying to implement IP Control of a Sony TV. The Sony rest IP is well documented and using their nice example web page I was able to code web pages to POST a request and control the TV as intended.
All my attempts to send the POST request to the TV -- using essentially identical code as that in the web page -- using an Arduino wifi board have failed - except one. Aa a debugging exercise, if I use the Arduino web server to create an html page that runs a java script onload event, and then open that web page, the TV does what it supposed to do.
I am trying to understand the difference between a browser JS XMLHttpRequest and an Arduino ArduinoHttpClient request.
The Arduino code [snipet] that I just can not get to work to decrease volume -1:
#include <SPI.h>
#include <WiFiNINA.h>
#include <ArduinoHttpClient.h>
WiFiServer server(80);
int port = 80;
char serverAddress[] = "192.168.1.20";
WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
//---
//STANDARD WIFI AND CLIENT CONNECT CODE
//-------
String postData = "'{\"method\":\"setAudioVolume\",\"version\":\"1.0\",\"id\":1,\"params\":[{\"target\":\"speaker\",\"volume\":\"-1\"}]}'";
//Following alternative gives the same result
//String postData = "'{""method"":""setAudioVolume"",""version"":""1.0"",""id"":1,""params"":[{""target"":""speaker"",""volume"":""-1""}]}'";
client.beginRequest();
client.post("/sony/audio");
client.sendHeader("X-Auth-PSK:", "1234");
client.sendHeader("HOST:", "192.168.1.26"); //wifi address. excluding results in a JSON format error returned
client.sendHeader("Content-Type:", "application/json; charset=UTF-8");
client.beginBody();
client.print(postData);
client.endRequest();
int statusCode = client.responseStatusCode();
String response = client.responseBody();
Serial.print("POST Status code: ");
Serial.println(statusCode);
Serial.print("POST Response: ");
Serial.println(response);
The return messages from the TV are
POST Status code: 400
POST Response: <html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
The Arudino code [snipet] that works (when I open the web page) by calling the send() function when the document body loads. Note, all other code besides the snipets are identical:
client.println("<script>");
client.println("function send(){");
client.println("var xhr = new XMLHttpRequest();");
client.println("xhr.open('POST', 'http://192.168.1.20/sony/audio');");
client.println("xhr.setRequestHeader('X-Auth-PSK', '1234');");
client.println("xhr.send('{\"method\":\"setAudioVolume\",\"version\":\"1.0\",\"id\":1,\"params\":[{\"target\":\"speaker\",\"volume\":\"-1\"}]}');");
client.println("}");
client.println("</script>");
client.println("<body onload=\"send()\">");
client.println("Hello world");
client.println("</body>");
I have run out of ideas on what to change or google next so am posting here. I have sure learned a lot in this project, and feel I am so close....
Cheers