Example #1
0
/* ======================================================================
Function: httpPost
Purpose : Do a http post
Input   : hostname
          port
          url
Output  : true if received 200 OK
Comments: -
====================================================================== */
boolean httpPost(char * host, uint16_t port, char * url)
{
  HTTPClient http;
  bool ret = false;

  unsigned long start = millis();

  // configure traged server and url
  http.begin(host, port, url); 
  //http.begin("http://emoncms.org/input/post.json?node=20&apikey=2f13e4608d411d20354485f72747de7b&json={PAPP:100}");
  //http.begin("emoncms.org", 80, "/input/post.json?node=20&apikey=2f13e4608d411d20354485f72747de7b&json={}"); //HTTP

  Debugf("http%s://%s:%d%s => ", port==443?"s":"", host, port, url);

  // start connection and send HTTP header
  int httpCode = http.GET();
  if(httpCode) {
      // HTTP header has been send and Server response header has been handled
      Debug(httpCode);
      Debug(" ");
      // file found at server
      if(httpCode == 200) {
        String payload = http.getString();
        Debug(payload);
        ret = true;
      }
  } else {
      DebugF("failed!");
  }
  Debugf(" in %d ms\r\n",millis()-start);
  return ret;
}
Example #2
0
int requestConfig(bool save) {
  int ret = -1;

  HTTPClient http;

  // configure url
  sprintf(url, "http://%s:%d/iotconfig?mac=%s", iotSrv, iotPort, macStr);
  http.begin(url); //HTTP

  // start connection and send HTTP header
  int httpCode = http.GET();

  // httpCode will be negative on error
  if(httpCode > 0) {
      // HTTP header has been send and Server response header has been handled
      // file found at server
      if(httpCode == HTTP_CODE_OK) {
          String payload = http.getString();
          int paysize = payload.length() + 1;
          char json[paysize];

          payload.toCharArray(json, paysize);

          StaticJsonBuffer<jsonSize> jsonBuffer;
          JsonObject& root = jsonBuffer.parseObject(json);

          // Test if parsing succeeds.
          if (!root.success()) {
            // parsing failed
            return ret; // bail out
          } else { // parsing successful, save file
            ret = root["cfgversion"];
            if (save==true) {
              File configFile = SPIFFS.open(jsonFile, "w");
              if (!configFile) {
                ret = -1;
              }
              root.printTo(configFile);
              configFile.close();
            }
          }
      }
  } else {
      ret = -1;
  }

  http.end();
  return ret;
}
Example #3
0
String getHTTPFile(String url) {
    HTTPClient http;
    String payload;
  //  Serial << "Retriving HTTP: " << url << endl;
    http.begin(url); //HTTP
    int httpCode = http.GET();
    if(httpCode > 0) {
        if (DEBUG) Serial << F("[HTTP] GET... code:") << httpCode << endl;
        if(httpCode == HTTP_CODE_OK) payload = http.getString();
    } else {
        if (DEBUG) Serial << F("[HTTP] GET... failed, error:") << http.errorToString(httpCode).c_str() << endl;
    }

    http.end();
    return payload;
}
String ICACHE_FLASH_ATTR ThingSpeakClass::getThingSpeak(String talkBackID, String talkApiKey)   //talkback message processing
{  //TalkBack Function from thingspeak
	String pageLength;
	String CommandString = "";
	String HTMLResult;
	bool GoodResult = false;

	DebugPrintln("talkback checking...");
	if (TalkBackEnabled == false) return "";
	if (thingSpeakURL == "") return "";
	

	if (WiFi.status() != WL_CONNECTED) return "";   //check to make sure we are connected...

	String url = "/talkbacks/" + talkBackID + "/commands/execute?api_key=" + talkApiKey;
	
	HTTPClient http;	

	http.begin("http://"+ thingSpeakURL + url);

	int httpCode = http.GET();

	// httpCode will be negative on error
	if (httpCode > 0) {
		// HTTP header has been send and Server response header has been handled
		//DebugPrintln("[HTTP] GET... code: " + String(httpCode));
		// file found at server
		if (httpCode == HTTP_CODE_OK) {
			CommandString  = http.getString();			
		}
	}
	else {
		DebugPrintln("[HTTP] GET... failed, error: " + http.errorToString(httpCode));
	}   
	http.end();

	
	
	CommandString.replace("\n", "");
	if (CommandString!="") DebugPrintln("Got talkback result : " + CommandString);
	return CommandString;
	
}
Example #5
0
void loop() {
  String url = "http://things.ubidots.com/api/v1.6/variables/" ID_VARIABLE "/values";
  http.begin(url);
  http.addHeader("Content-Type", "application/json");
  http.addHeader("X-Auth-Token", AUTH_TOKEN);
  int content_length =0;
  String payload = String("{ \"value\": " + String(millis()/1000) + "}");
  //content_length = payload.length();
  //http.addHeader("Content-Length", String(content_length));
  int httpCode = http.POST(payload);
  if(httpCode > 0) {
    String payload = http.getString();
    Serial.println(payload);
  }
  else {
    Serial.print("[HTTP] failed, error: ");Serial.println(http.errorToString(httpCode).c_str());
  }
  http.end();
  delay(20000);
}
Example #6
0
bool SendMailSms::FinalizeConnection() {
  HTTPClient http;  

  ESP.wdtDisable();
  TRACE2("Open URL: ",m_Data);
  if ( !http.begin(m_Data) ) {
    TRACE2("Error opening URL: ",m_Data);
    ESP.wdtEnable(10000);
    return false;
  }
  int httpCode = http.GET();
  TRACE2("HTTP Code: ",httpCode);
  bool bRvl = false;
  if (httpCode == HTTP_CODE_OK) {
    String payload = http.getString();
    TRACE(payload);
    if (payload.indexOf("{\"success\":true")>=0) bRvl = true;
  }
  http.end();
  ESP.wdtEnable(10000);
  return bRvl; 
}