// This programm calls Philips Hue JSON service. // The return value is a JSON string. Since PicoC does not support // JSON I use scraping of the string to find the values. // Build RvEsoft 2017 int DEBUG_LEVEL=0; int aantal = 3; // aantal sensor scannen char* host = "192.168.20.30"; int sensort1 = 0; double dLoad; char* p; int i; // ""=niet aanzezig, text= "/api/"apiKey"/sensors/1" char* Sensoraanwezig[4]; Sensoraanwezig[1] = "/api/ATkisLiHdd7/sensors/10"; Sensoraanwezig[2] = NULL; Sensoraanwezig[2] = "/api/ATkisLiHdd7/sensors/17"; Sensoraanwezig[3] = NULL; Sensoraanwezig[3] = "/api/ATkisLiHdd7/sensors/21"; int SensorToAQ[4]; // start AQxx SensorToAQ[1] = 0; SensorToAQ[2] = 3; SensorToAQ[3] = 6; int SensorPress[4]; // start AQxx SensorPress[1] = 0; SensorPress[2] = 0; SensorPress[3] = 0; if (DEBUG_LEVEL > 0)printf("Start HUE watch"); int GetIntValue(char *name, int def, int neg) { int value = def; int pos = strfind(result, name, 0); if(pos > 0) { char *stemp = calloc(1, 10); int lenName = strlen(name); strncpy(stemp, result + pos + lenName + neg, 5); value = atoi(stemp); free(stemp); stemp = 0; } return value; } char *GetIntText(char *name, char *def, int neg) { char *value = def; int pos = strfind(result, name, 0); int pose = strfind(result, "\"type\":\"", 0) - 10; int posl = pose-pos; if(pos > 0) { char *stemp = calloc(1, 10); int lenName = strlen(name); strncpy(stemp, result + pos + lenName + neg, posl); value = stemp; free(stemp); stemp = 0; } return value; } char *GetIntPress(char *name, char *def, int neg) { char *value = def; int pos = strfind(result, name, 0); int pose = pos + 5; int posl = pose-pos; if(pos > 0) { char *stemp = calloc(1, 10); int lenName = strlen(name); strncpy(stemp, result + pos + lenName + neg, posl); value = stemp; free(stemp); stemp = 0; } return value; } /// Main loop. while(TRUE) { for (i = 1; i < 4; i++) { if(Sensoraanwezig[i]!=NULL) { // Sensor 1 char *result = httpget(host, Sensoraanwezig[i]); sleep(30); if(result != 0) { /// Button name char *tempt1 = GetIntText("\"name\":\"", "Not Found", 0); //char *temp="Sensor1"; setoutputtext(i - 1, tempt1); /// Button hit int temp1 = GetIntValue("\"buttonevent\":", 0, 0); if(temp1 < 1) { /// Motion detection hit char *temp2 = GetIntPress("\"presence\":", 0, 0); if (temp2 != "false") { temp1=0; } else { temp1=1; if (DEBUG_LEVEL > 0) printf ("Sensor motion %s ", temp2); } } setoutput(0 + SensorToAQ[i], temp1); /// Button hit at time int pressedH1 = GetIntValue("\"lastupdated\":\"", 12, 11); int pressedM1 = GetIntValue("\"lastupdated\":\"", 59, 14); int pressedS1 = GetIntValue("\"lastupdated\":\"", 59, 17); int pressed1 = (pressedH1 * 10000) + (pressedM1 * 100) + (pressedS1 * 1); setoutput(1 + SensorToAQ[i], pressed1); if(SensorPress[i] != pressed1) { setoutput(2 + SensorToAQ[i], 1); SensorPress[i] = pressed1; if (DEBUG_LEVEL > 0) printf ("Sensor pressed %s ", pressed1); } else { setoutput(2 + SensorToAQ[i], 0); } } free(result); } } sleep(500); }