/********************************************************************
  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;
}
Example #2
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;
}