These are chat archives for esp8266/Arduino

4th
Aug 2017
Me No Dev
@me-no-dev
Aug 04 2017 07:08
PUT means HTTP and HTTP runs single requests: connect, send, read, disconnect. It's not a connection that is open all of the time
paraflou
@paraflou
Aug 04 2017 08:13
And how can i then use async client to make put requests in loop? Calling ::connect every time i want to send request is very slow.
paraflou
@paraflou
Aug 04 2017 08:28
'''

'''

define BASE_HUE_URL "192.168.100.175"

define BASE_HUE_PORT 80

define PUT_HUE_URL "PUT /api/[hue bridge key]/lights/3/state HTTP/1.1\r\nHost: http://192.168.100.175\r\nUser-Agent: ESP8266HTTPClient\r\nContent-Type: application/json\r\nContent-Length: 11/r/n/r/n{\"on\":true}"

static AsyncClient * aClient = NULL;

void setup()
{
Serial.begin(115200);
Serial.setDebugOutput(true);

// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" ----- WiFi Connected -----");
WiFi.macAddress(mac);
Serial.print("MAC: ");
Serial.print(mac[0], HEX);
Serial.print(":");
Serial.print(mac[1], HEX);
Serial.print(":");
Serial.print(mac[2], HEX);
Serial.print(":");
Serial.print(mac[3], HEX);
Serial.print(":");
Serial.print(mac[4], HEX);
Serial.print(":");
Serial.println(mac[5], HEX);

server.onRequestBody( {

if (request->url() == "/spi") {
  printf("On request body data: %s\n", data);
  DynamicJsonBuffer jsonBuffer;
  JsonObject& root = jsonBuffer.parseObject((const char*)data);
  if (root.success()) {
    if (root.containsKey("command")) {
      SPISlave.setData(root["command"].asString());


      digitalWrite(LPC_INT_PIN, 1);
    }
  }
  request->send(200, "text/plain", "end");
}

});
server.begin();

aClient = new AsyncClient();

aClient->onDisconnect( {
Serial.println("############ Async Client Disconnect ############");
c->free();
delete c;
});

aClient->onConnect( {
Serial.println("Async Client Connected");
}, NULL);

aClient->onData( {
Serial.print("\r\nData: ");
Serial.println(len);
uint8_t d = (uint8_t)data;
for (size_t i = 0; i < len; i++)
Serial.write(d[i]);
}, NULL);

if (!aClient->connect(BASE_HUE_URL, BASE_HUE_PORT)) {
Serial.println("Connect Fail\n");
AsyncClient * client = aClient;
aClient = NULL;
delete client;
}
}

void loop()
{
if ((WiFi.status() == WL_CONNECTED)) {

  aClient->write(PUT_HUE_URL);

}
'''

This message was deleted
Sorry for the mess. Above is a cut from my code. The thing i am trying to do is to send put messages to hue brigde all the time. How can i achieve this?