I am trying to read the response from a HTTP post request into a PICOC program. So far the tcpstream posts the appropriate JSON data from the miniserver to the remote server. I can see the remote server returning the json data. How do I read this response into the PICOC program?
Thanks in advance.
PICOC Code (IP addresses removed):
#define BUFF_SIZE 40000 #define RD_BLOCK_SIZE 128 char* ReadTemperatures() { STREAM* pTcpStream = stream_create("/dev/tcp/x.x.x.x/4242",0,0); // create tcp stream char* pTcpCmd = "POST /GetStat HTTP/1.1\r\nHost: x.x.x.x\r\nContent-Length: 11\r\nAccept: '*/*\r\nUser-Agent: LoxLIVE [en]\r\nContent-Type: application/json; charset=utf-8\r\n\r\n{\"INFO\":0}"; char* res; printf("S: %d", strlen(pTcpCmd)); stream_write(pTcpStream,pTcpCmd,strlen(pTcpCmd)); // write to output buffer stream_flush(pTcpStream); // flush output buffer char szBuffer[BUFF_SIZE]; char szTmpBuffer[RD_BLOCK_SIZE]; int nCnt; int nBytesReceived = 0; int i = 0; printf("Stream Opened"); do { nCnt = stream_read(pTcpStream,szTmpBuffer,RD_BLOCK_SIZE,4000); nCnt = stream_read(pTcpStream,szTmpBuffer,RD_BLOCK_SIZE,4000); printf("Received: %s", szTmpBuffer); if(nCnt > 0) strncpy((char*)szBuffer+i*RD_BLOCK_SIZE,szTmpBuffer,nCnt); nBytesReceived += nCnt; i++; } while (nCnt > 0); szBuffer[nBytesReceived] = 0; stream_close(pTcpStream); printf("Data received: %s", szBuffer); return szBuffer; } void ProcessInput(char* data) { //[{"device": "kitchen", "CURRENT_TEMPERATURE": 16.5}, {"device": "office", "CURRENT_TEMPERATURE": 19.0}] char device[30]; float temp; char* pS = data; int pos = 0; while(*pS) { p = strstrskip(pS,"\"device:\" "); if (p != NULL) { //get device name pos = strfind(p,",", pos); strncpy(device,p+1,pos-1); //get temp reading p = strstrskip(p,"\"CURRENT_TEMPERATURE\": "); if (p != NULL) { pos = strfind(p,",", pos); temp = batof(p + (pos - 1)); setio(device,temp); } ps += pos; } else { pos = (strlen(pS) + 1); } pS += pos; } } while(TRUE) { printf("Reading Temperatures"); char* res = ReadTemperatures(); //ProcessInput(res); sleeps(10); }
Kommentar