Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

How to use http Put-Methode to Control Philips Hue Light

Goal: Switch on/off my Philips Hue Light. Control it through my Philips Hue Hub with Address http://192.168.0.55 where the lamp represent as an Ressource.
All I have to do is to send a PUT Message with Message Body {"on":true} on the Ressource “http://192.168.0.55/api/User/lights/5/state

Suppose “http_client.c” is a good start Code for this job. But have no idea without better example.
Can anyone give an example or guide me to a starting point.
Thanks a lot!


    /*
     * Copyright (c) 2014 Cesanta Software Limited
     * All rights reserved
     *
     * This program fetches HTTP URLs.
     */

    #include "mongoose.h"

    static int s_exit_flag = 0;
    static int s_show_headers = 0;


    static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
      struct http_message *hm = (struct http_message *) ev_data;

      switch (ev) {
        case MG_EV_CONNECT:
          if (*(int *) ev_data != 0) {
            fprintf(stderr, "connect() failed: %s\n", strerror(*(int *) ev_data));
            s_exit_flag = 1;
          }
          break;
        case MG_EV_HTTP_REPLY:
          nc->flags |= MG_F_CLOSE_IMMEDIATELY;
          if (s_show_headers) {
            fwrite(hm->message.p, 1, hm->message.len, stdout);
          } else {
            fwrite(hm->body.p, 1, hm->body.len, stdout);
          }
          putchar('\n');
          s_exit_flag = 1;
          break;
        case MG_EV_CLOSE:
          if(s_exit_flag == 0) {
            printf("Server closed connection\n");
            s_exit_flag = 1;
          }
          break;
        default:
          break;
      }
    }

    int main(int argc, char *argv[]) {
      struct mg_mgr mgr;
      int i;
      mg_mgr_init(&mgr, NULL);

      mg_connect_http(&mgr, ev_handler, "http://192.168.0.55/api/7z6oa96P89NDATuCz3kzBqLqWpX04oXmQ-JGrA3P/lights/5", NULL, NULL);

      while (s_exit_flag == 0) {
        mg_mgr_poll(&mgr, 1000);
      }
      mg_mgr_free(&mgr);

      return 0;
    }
Sign In or Register to comment.