void ICACHE_FLASH_ATTR ThingSpeakClass::SendThingSpeakValues()
{

	//  if (ThingEnabled) DebugPrintln("thingspeak enabled"); else DebugPrintln("thingspeak disabled");                   
	if (ThingEnabled == false) return;
	if (thingSpeakURL == "") return;
	DebugPrintln(thingSpeakURL);

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

	String postStr = "api_key=" + thingWriteKey;
	if (HMGlobal.hmPitTemp != "U")  postStr += "&field1=" + HMGlobal.hmPitTemp;
	if (HMGlobal.hmFood1 != "U") postStr += "&field2=" + HMGlobal.hmFood1;
	if (HMGlobal.hmFood2 != "U") postStr += "&field3=" + HMGlobal.hmFood2;
	if (HMGlobal.hmAmbient != "U") postStr += "&field4=" + HMGlobal.hmAmbient;
	if (HMGlobal.hmFanMovAvg != "U") postStr += "&field5=" + HMGlobal.hmFanMovAvg;
	if (HMGlobal.hmFan != "U") postStr += "&field6=" + HMGlobal.hmFan;
	if (HMGlobal.hmSetPoint != "U") postStr += "&field7=" + HMGlobal.hmSetPoint;
	if (HMGlobal.hmLidOpenCountdown != "U") postStr += "&field8=" + HMGlobal.hmLidOpenCountdown;

	if (inAlarm)  //if alarm was triggered we send on next msg
	{
		postStr += "&status=" + MyWebServer.urlencode(MyWebServer.CurTimeString() + " " + AlarmInfo);
		AlarmInfo = "";
		inAlarm = false;
	}

		
		HTTPClient http;
		
		DebugPrintln("http://" + thingSpeakURL + "/update");
		http.begin("http://" + thingSpeakURL + "/update");

		int httpCode = http.POST(postStr);

		// httpCode will be negative on error
		if (httpCode > 0) {
			if (httpCode == HTTP_CODE_OK) {				
			}
		}
		else {
			DebugPrintln("[HTTP] POST... failed, error: " + http.errorToString(httpCode));
		}
		http.end();
		
		
	DebugPrintln("sending thingspeak stuffs");

}
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; 
}
void TimeClient::updateTime() {
  HTTPClient http;

  String url = "http://www.google.com/";
  const char * headerkeys[] = {"Date"} ;
  size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*);
  // Based on Arduino core BasicHttpClient.ino example
  // https://github.com/esp8266/Arduino/blob/1588b45a8a15e4d3f1b42f052fc41590e9bec0bb/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino
  // configure http client and url
  http.begin(url); //HTTP
  http.collectHeaders(headerkeys,headerkeyssize);
  // 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
	  USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
	  // file found at server
	  if ((httpCode == HTTP_CODE_OK)|| (httpCode == HTTP_CODE_FOUND)) {
		String payload = http.header("Date");
		USE_SERIAL.println(payload);
		http.end();
        payload.toUpperCase();
        // example:
      	// date: Thu, 19 Nov 2015 20:25:40 GMT
      	if (payload!=NULL) {
          Serial.println(payload.substring(17, 19) + ":" + payload.substring(20, 22) + ":" +payload.substring(23, 25));
          int parsedHours = payload.substring(17, 19).toInt();
          int parsedMinutes = payload.substring(20, 22).toInt();
          int parsedSeconds = payload.substring(23, 25).toInt();
          Serial.println(String(parsedHours) + ":" + String(parsedMinutes) + ":" + String(parsedSeconds));
          localEpoc = (parsedHours * 60 * 60 + parsedMinutes * 60 + parsedSeconds);
          Serial.println(localEpoc);
          localMillisAtUpdate = millis();
        }
      }
  }
}
Example #8
0
int uploadFile(const char* _filename, const char* _fileurl) { // upload new file to fs by downloading from a remote server, rather than reflash the entire spiffs
  int ret = false;
  HTTPClient http;
  //const char* fileUrl = "http://mypi3/iot/index.html";
  //const char* fileName = "/test2.html";

  if (hasSerial) Serial.printf("url %s\n", _fileurl);
  if (hasSerial) Serial.printf("file %s\n", _filename);

  http.begin(_fileurl); // init http client

  // 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) {
      if (hasSerial) Serial.printf("HTTP client http status %d\n", httpCode);

      // get lenght of document (is -1 when Server sends no Content-Length header)
      int len = http.getSize();
      int paysize = len;

      if (hasSerial) Serial.printf("HTTP content size %d bytes\n", paysize);

      // create buffer for read
      uint8_t buff[128] = { 0 };

      // get tcp stream
      WiFiClient * stream = http.getStreamPtr();

      // create or recreate file on spiffs
      File configFile = SPIFFS.open(_filename, "w");
      if (!configFile) {
        if (hasSerial) Serial.printf("Failed to open %s for write.\n",_filename);
        return ret;
      }
      if (hasSerial) Serial.println("File open, write start.");

      // read all data from server
      while(http.connected() && (len > 0 || len == -1)) {
        // get available data size
        size_t size = stream->available();

        if (size) {
              // read up to 128 byte
              int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));

              // write it to Serial
              configFile.write(buff, c);

              if(len > 0) {
                  len -= c;
              }
        }
      } // EoF or http connection closed
      configFile.close();
      http.end();
      if (hasSerial) Serial.println("File closed, write complete.");
      return paysize;
    } else {
      if (hasSerial) Serial.printf("HTTP client http error %d\n", httpCode);
      return httpCode;
    }
  } else {
    if (hasSerial) Serial.printf("HTTP client http error %d\n", httpCode);
    return httpCode;
  }

  return 0;
}