#define RESPONSE_SIZE 40000
#define READ_BUFFER_SIZE 1024
#define OUTPUT_SUCCESS 0
#define OUTPUT_POWER_ON 1
#define OUTPUT_VOLUME 2
#define OUTPUT_COUNTER 11
#define OUTPUT_DEBUG 12
int Counter = 0;
while(true)
{
if(++Counter == 100)
Counter = 0;
setoutput(OUTPUT_COUNTER, Counter);
setoutput(OUTPUT_DEBUG, 0);
//sleep(100);
setoutput(OUTPUT_DEBUG, 1);
if(getinput(0) == 0)
{
setoutput(OUTPUT_SUCCESS, 0);
continue;
}
setoutput(OUTPUT_DEBUG, 2);
//Open Stream (Modify IP Address)
STREAM* pTcpStream = stream_create("/dev/tcp/192.168.178.4/80",0,0);
setoutput(OUTPUT_DEBUG, 3);
//Check if Stream is open
if(pTcpStream == NULL)
{
setoutput(OUTPUT_SUCCESS, 0);
return;
}
setoutput(OUTPUT_DEBUG, 4);
char* pCmd = "POST /YamahaRemoteControl/ctrl HTTP/1.1\r\nContent-Type: text/xml; charset=UTF-8\r\nContent-Length: 131\r\n\r\n";
char* pPost_Data = "GetParam";
stream_write(pTcpStream,pCmd,strlen(pCmd));
setoutput(OUTPUT_DEBUG, 5);
stream_write(pTcpStream,pPost_Data,strlen(pPost_Data));
setoutput(OUTPUT_DEBUG, 6);
stream_flush(pTcpStream);
setoutput(OUTPUT_DEBUG, 7);
char Response[RESPONSE_SIZE];
char Read_Buffer[READ_BUFFER_SIZE];
int nCnt;
int nBytesReceived = 0;
memset(Response, 0, RESPONSE_SIZE);
do
{
setoutput(OUTPUT_DEBUG, 8);
nCnt = stream_read(pTcpStream,Read_Buffer, READ_BUFFER_SIZE, 200);
setoutput(OUTPUT_DEBUG, 9);
if (nCnt + nBytesReceived > RESPONSE_SIZE)
{
nBytesReceived = -1;
break;
}
else if(nCnt > 0)
{
strncpy((char*)Response + nBytesReceived, Read_Buffer, nCnt);
nBytesReceived += nCnt;
}
}
while (nCnt > 0);
setoutput(OUTPUT_DEBUG, 10);
stream_close(pTcpStream);
setoutput(OUTPUT_DEBUG, 11);
char *pSearch;
//Search Power State
pSearch = strstrskip(Response, "");
if(strncmp(pSearch, "On", 2) == 0)
setoutput(OUTPUT_POWER_ON, 1);
else
setoutput(OUTPUT_POWER_ON, 0);
setoutput(OUTPUT_DEBUG, 12);
pSearch = strstrskip(Response, "");
float Volume = atof(pSearch) / 10.0f;
setoutput(OUTPUT_VOLUME, Volume);
setoutput(OUTPUT_DEBUG, 13);
//Request was successfull
setoutput(OUTPUT_SUCCESS, 1);
setoutput(OUTPUT_DEBUG, 14);
}