/******************************************************************** connect establishes a connection to the Slack RTM API ********************************************************************/ bool ArduinoSlackBot::connect() { // Step 1: Find WebSocket address via RTM API (https://api.slack.com/methods/rtm.start) HTTPClient http; String slackAddr = "https://slack.com/api/rtm.start?token="; slackAddr = String(slackAddr + slackToken); PRINTLN(slackAddr); http.begin(slackAddr.c_str(), slackSSLFingerprint); int httpCode = http.GET(); if (httpCode != HTTP_CODE_OK) { PRINTF("HTTP GET failed with code %d\n", httpCode); return false; } WiFiClient *client = http.getStreamPtr(); client->find("wss:\\/\\/"); String host = client->readStringUntil('\\'); String path = client->readStringUntil('"'); path.replace("\\/", "/"); // Step 2: Open WebSocket connection and register event handler PRINTLN("WebSocket Host=" + host + " Path=" + path); webSocket.beginSSL(host, 443, path, "", ""); webSocket.onEvent(webSocketEvent); return true; }
/* ====================================================================== 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; }
int main(int argc, char *argv[]) { HTTPClient client = HTTPClient(ENDPOINT, 8000); client.GET(); printf("\nPress any key to exit..."); getchar(); return 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; }
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; }
void CustomURL_Plugin::sndGENIOT(const char *line) { char str[140], str2[150]; strcpy(str, PropertyList.readProperty(EE_GENIOT_PATH)); if (strstr(str, "thingspeak")) { strcat(str, "&field2=%d"); sprintf(str2, str, &line[7], millis()/60000L); } else { sprintf(str2, str, &line[7]); } SERIAL_PORT << F("Sending to URL: \"") << str2 << "\"" << endl; HTTPClient http; http.begin(str2); //addHCPIOTHeaders(&http, token); int rc = AT_FW_Plugin::processResponseCodeATFW(&http, http.GET()); //SERIAL_PORT << "Result: " << http.errorToString(rc).c_str(); }
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; }
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(); } } } }
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; }
void AT_FW_Plugin::getTS(const char* line) { HTTPClient http; http.begin(String(HTTP_STR) + atCIPSTART_IP + (line + 4)); processResponseCodeATFW(&http, http.GET()); }