Exemplo n.º 1
0
bool WebServiceFSM::putJson(JsonObject& msg) {
  // .printTo stupidly writes a '\0' in its last position, so any destination
  // printed to must ask for one more than the strlen of the JSON.  At the
  // other end of the connection, Node.js will choke on PUT data if you throw a
  // null byte at it.  So, the FILE* must be as large as the JSON string, not
  // counting the null byte.
  // I place the primary blame on ArduinoJson for this one.  Honte, Benoit!
  char to_server[MAX_MSG_SIZE];
  msg.printTo(to_server, 1+msg.measureLength());
  auto fout = fmemopen(to_server, msg.measureLength(), "r");

  curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
  curl_easy_setopt(curl, CURLOPT_READDATA, fout);

  char from_server[MAX_MSG_SIZE];
  auto fin = fmemopen(from_server, MAX_MSG_SIZE, "w");
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, fin);

  res = curl_easy_perform(curl);
  fclose(fin);
  fclose(fout);
  // Turn off PUT mode
  curl_easy_setopt(curl, CURLOPT_UPLOAD, 0);

  // Check for errors
  if(res != CURLE_OK) {
    fprintf(stderr, "WebQ: Failed to access %s: %s\n", "localhost",
        curl_easy_strerror(res));
  }
  else {
    fprintf(stderr, "WebQ: Connection successful, received '%s'\n", from_server);
  }

  return res == CURLE_OK;
}
Exemplo n.º 2
0
bool WebServiceFSM::postJson(JsonObject& msg) {
  // .printTo stupidly writes a '\0' in its last position, so any destination
  // printed to must ask for one more than the strlen of the JSON.
  char to_server[MAX_MSG_SIZE];
  msg.printTo(to_server, 1+msg.measureLength());
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, to_server);

  char from_server[MAX_MSG_SIZE];
  auto fin = fmemopen(from_server, MAX_MSG_SIZE, "w");

  curl_easy_setopt(curl, CURLOPT_WRITEDATA, fin);

  res = curl_easy_perform(curl);
  fclose(fin);

  // Check for errors
  if(res != CURLE_OK) {
    fprintf(stderr, "WebQ: Failed to access %s: %s\n", "localhost",
        curl_easy_strerror(res));
  }
  else {
    fprintf(stderr, "WebQ: Connection successful, received '%s'\n", from_server);
  }

  return res == CURLE_OK;
}
Exemplo n.º 3
0
String IFTTTMaker::sendTriggerEventWithData(String eventName, JsonObject& payload) {
	String response="";
  bool finishedHeaders = false;
  bool currentLineIsBlank = true;
	long now;
	bool avail;
	// Connect with IFTTT
	if (client->connect(HOST, SSL_PORT)) {
		//Serial.println(".... connected to server");
		String a="";
		char c;
		int ch_count=0;
		client->print("POST /trigger/"+eventName+"/with/key/"+_key);
    client->println(" HTTP/1.1");
    // Host header
    client->print("Host:"); client->println(HOST);
    // JSON content type
    client->println("Content-Type: application/json");
    // Content length
    int length = payload.measureLength();
    client->print("Content-Length:"); client->println(length);
    // End of headers
    client->println();
    // POST message body
    String out;
    payload.printTo(out);
    //Serial.println(out);
    client->println(out);

    now=millis();
		avail=false;
    //Serial.println("starting timer");
		while (millis()-now<1500) {
			while (client->available()) {
				char c = client->read();
				//Serial.print(c);
        response = response + c;
			}
		}
	}

  //Serial.println("response");
  //Serial.println(response);
  return response;
}