Skip to content

Commit

Permalink
Wed Mar 15 11:25:13 CDT 2017
Browse files Browse the repository at this point in the history
  • Loading branch information
kolban committed Mar 15, 2017
1 parent c65d370 commit bf8649b
Show file tree
Hide file tree
Showing 16 changed files with 346 additions and 227 deletions.
51 changes: 51 additions & 0 deletions cpp_utils/IFTTT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* IFTTT.cpp
*
* Created on: Mar 14, 2017
* Author: kolban
*/
#if defined(ESP_HAVE_CURL)
#include "IFTTT.h"
#include <cJSON.h>


/**
* @brief Construct an IFTTT maker client using the supplied key.
*/
IFTTT::IFTTT(std::string key) {
m_key = key;
m_restClient.addHeader("Content-Type", "application/json");
m_restClient.setVerbose(true);
} // IFTTT


IFTTT::~IFTTT() {
} // ~IFTTT


/**
* @brief Trigger a maker event at IFTTT.
*
* @param [in] event The event type to send.
* @param [in] value1 The value of value1.
* @param [in] value2 The value of value2.
* @param [in] value3 The value of value3.
*/
void IFTTT::trigger(
std::string event,
std::string value1,
std::string value2,
std::string value3) {
m_restClient.setURL("https://maker.ifttt.com/trigger/" + event + "/with/key/" + m_key);
cJSON *root;
root = cJSON_CreateObject();

cJSON_AddStringToObject(root, "value1", value1.c_str());
cJSON_AddStringToObject(root, "value2", value2.c_str());
cJSON_AddStringToObject(root, "value3", value3.c_str());

m_restClient.post(std::string(cJSON_Print(root)));

cJSON_Delete(root);
} // trigger
#endif // ESP_HAVE_CURL
23 changes: 23 additions & 0 deletions cpp_utils/IFTTT.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* IFTTT.h
*
* Created on: Mar 14, 2017
* Author: kolban
*/

#ifndef MAIN_IFTTT_H_
#define MAIN_IFTTT_H_

#include "RESTClient.h"

class IFTTT {
public:
IFTTT(std::string key);
virtual ~IFTTT();
void trigger(std::string event, std::string value1 = "", std::string value2 = "", std::string value3 = "");
private:
RESTClient m_restClient;
std::string m_key;
};

#endif /* MAIN_IFTTT_H_ */
4 changes: 4 additions & 0 deletions cpp_utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#CPP Utils
This directory contains a wealth of C++ classes that have been found useful when working in C++ in conjunction
with the ESP-IDF. The classes have been documented using `doxygen` so one can run a doxygen processor over them
to create the user guides and programming references.
166 changes: 166 additions & 0 deletions cpp_utils/RESTClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* RESTClient.cpp
*
* Created on: Mar 12, 2017
* Author: kolban
*/
#if defined(ESP_HAVE_CURL)

#define _GLIBCXX_USE_C99 // Needed for std::string inclusion.


#include <curl/curl.h>
#include <esp_log.h>
#include <string>

#include "RESTClient.h"

static char tag[] = "RESTClient";


RESTClient::RESTClient() {
m_curlHandle = curl_easy_init();
m_timings = new RESTTimings(this);
} // RESTClient


RESTClient::~RESTClient() {
::curl_easy_cleanup(m_curlHandle);
curl_slist_free_all(m_headers);
delete m_timings;
} // ~RESTClient


/**
* @brief Perform an HTTP GET request.
*/
void RESTClient::get() {
prepForCall();
::curl_easy_setopt(m_curlHandle, CURLOPT_HTTPGET, 1);
int rc = ::curl_easy_perform(m_curlHandle);
if (rc != CURLE_OK) {
ESP_LOGE(tag, "get(): %s", getErrorMessage().c_str());
}
} // get


/**
* @brief Perform an HTTP POST request.
*
* @param [in] body The body of the payload to send with the post request.
*
*/
void RESTClient::post(std::string body) {
prepForCall();
::curl_easy_setopt(m_curlHandle, CURLOPT_POSTFIELDS, body.c_str());
int rc = ::curl_easy_perform(m_curlHandle);
if (rc != CURLE_OK) {
ESP_LOGE(tag, "post(): %s", getErrorMessage().c_str());
}
} // post


/**
* @brief Get the last error message.
*/
std::string RESTClient::getErrorMessage() {
std::string errMsg(m_errbuf);
return errMsg;
} // getErrorMessage


/**
* @brief Callback function to handle the data received.
*
* This is a callback function architected by libcurl to be called when data is received.
* We append the data to an accumulating buffer.
*
* @param [in] buffer A buffer of records.
* @param [in] size The size of a record.
* @param [in] nmemb The number of records of unit `size`.
* @param [in] userp A pointer to the RESTClient class instance.
*
* @return The number of bytes of data processed.
*/
size_t RESTClient::handleData(void *buffer, size_t size, size_t nmemb, void *userp) {
//printf("handleData: size: %d, num: %d\n", size, nmemb);
RESTClient *pClient = (RESTClient *)userp;
pClient->m_response.append((const char *)buffer, size*nmemb);
return size * nmemb;
} // handleData


/**
* @brief Add a header to the list of headers.
*
* For example:
*
* @code{.cpp}
* client.addHeader("Content-Type", "application/json");
* @endcode
*
* @param [in] name The name of the header to be added.
* @param [in] value The value of the header to be added.
*/
void RESTClient::addHeader(std::string name, std::string value) {
std::string headerString = name + ": " + value;
m_headers = curl_slist_append(m_headers, headerString.c_str());
} // addHeader


/**
* @brief Prepare for a call using a reset handle.
*/
void RESTClient::prepForCall() {
::curl_easy_reset(m_curlHandle);

if (m_verbose) {
::curl_easy_setopt(m_curlHandle, CURLOPT_VERBOSE, 1L);
} else {
::curl_easy_setopt(m_curlHandle, CURLOPT_VERBOSE, 0L);
}

::curl_easy_setopt(m_curlHandle, CURLOPT_ERRORBUFFER, m_errbuf);
::curl_easy_setopt(m_curlHandle, CURLOPT_SSL_VERIFYPEER, 0L);
::curl_easy_setopt(m_curlHandle, CURLOPT_CAINFO, nullptr);
::curl_easy_setopt(m_curlHandle, CURLOPT_URL, m_url.c_str());
::curl_easy_setopt(m_curlHandle, CURLOPT_HTTPHEADER, m_headers);
::curl_easy_setopt(m_curlHandle, CURLOPT_WRITEFUNCTION, handleData);
::curl_easy_setopt(m_curlHandle, CURLOPT_WRITEDATA, this);
m_response = "";
} // prepForCall


RESTTimings::RESTTimings(RESTClient *client) {
this->client = client;
}


/**
* @brief Refresh the timings information.
*/
void RESTTimings::refresh() {
::curl_easy_getinfo(client->m_curlHandle, CURLINFO_STARTTRANSFER_TIME, &m_starttransfer);
::curl_easy_getinfo(client->m_curlHandle, CURLINFO_NAMELOOKUP_TIME, &m_namelookup);
::curl_easy_getinfo(client->m_curlHandle, CURLINFO_CONNECT_TIME, &m_connect);
::curl_easy_getinfo(client->m_curlHandle, CURLINFO_APPCONNECT_TIME, &m_appconnect);
::curl_easy_getinfo(client->m_curlHandle, CURLINFO_PRETRANSFER_TIME, &m_pretransfer);
::curl_easy_getinfo(client->m_curlHandle, CURLINFO_TOTAL_TIME, &m_total);
} // refresh


/**
* @brief Return the timings information as a string.
*
* @return The timings information.
*/
std::string RESTTimings::toString() {
std::string ret = "Start Transfer: " + std::to_string(m_starttransfer) + \
"\nName lookup: " + std::to_string(m_namelookup) + \
"\nConnect: " + std::to_string(m_connect) + \
"\nApp Connect: " + std::to_string(m_appconnect) + \
"\nPre Transfer: " + std::to_string(m_pretransfer) + \
"\nTotal: " + std::to_string(m_total);
return ret;
} // toString
#endif // ESP_HAVE_CURL
80 changes: 80 additions & 0 deletions cpp_utils/RESTClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* RESTClient.h
*
* Created on: Mar 12, 2017
* Author: kolban
*/

#ifndef MAIN_RESTCLIENT_H_
#define MAIN_RESTCLIENT_H_
#include <string>
#include <curl/curl.h>
class RESTClient;

class RESTTimings {
public:
RESTTimings(RESTClient *client);
void refresh();
std::string toString();
private:
double m_namelookup = 0;
double m_connect = 0;
double m_appconnect = 0;
double m_pretransfer = 0;
double m_starttransfer = 0;
double m_total = 0;
RESTClient *client = nullptr;
};

class RESTClient {
public:
RESTClient();
virtual ~RESTClient();
void addHeader(std::string name, std::string value);
void get();
std::string getErrorMessage();
std::string getResponse() {
return m_response;
}

RESTTimings *getTimings() {
return m_timings;
}

void post(std::string body);

/**
* @brief Set the URL for the target.
*
* @param [in] url The target for HTTP request.
*
*/
void setURL(std::string url) {
m_url = url;
};

/**
* @brief Set the verbosity flag.
*
* @param [in] The value of the verbosity.
*/
void setVerbose(bool value) {
m_verbose = value;
};



private:
CURL *m_curlHandle;
std::string m_url;
char m_errbuf[CURL_ERROR_SIZE];
struct curl_slist *m_headers = nullptr;
bool m_verbose = false;
friend class RESTTimings;
RESTTimings *m_timings;
std::string m_response;
static size_t handleData(void *buffer, size_t size, size_t nmemb, void *userp);
void prepForCall();
};

#endif /* MAIN_RESTCLIENT_H_ */
1 change: 0 additions & 1 deletion cpp_utils/WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ void WiFi::connectAP(std::string ssid, std::string password){
}



ESP_ERROR_CHECK( esp_event_loop_init(wifiEventHandler->getEventHandler(), wifiEventHandler));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(::esp_wifi_init(&cfg));
Expand Down
2 changes: 1 addition & 1 deletion cpp_utils/WiFiEventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ esp_err_t WiFiEventHandler::eventHandler(void *ctx, system_event_t *event) {
printf("Found a next handler\n");
rc = eventHandler(pWiFiEventHandler->nextHandler, event);
} else {
printf("NOT Found a next handler\n");
//printf("NOT Found a next handler\n");
}
return rc;
}
Expand Down
5 changes: 4 additions & 1 deletion cpp_utils/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
# src/ directory, compile them and link them into lib(subdirectory_name).a
# in the build directory. This behaviour is entirely configurable,
# please read the ESP-IDF documents if you need to do this.
COMPONENT_ADD_INCLUDEDIRS=.
COMPONENT_ADD_INCLUDEDIRS=.

## Uncomment the following line if we have an implementation of libcurl available to us.
#CXXFLAGS+=-DESP_HAVE_CURL
4 changes: 4 additions & 0 deletions hardware/accelerometers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#Accelerometers
An accelerometer detects acceleration and in some cases gyro scopic movement.

See also the C++ class for MPU-6050 support.
2 changes: 2 additions & 0 deletions hardware/neopixels/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Moved
The class supporting the WS2812 pixels has moved to the `cpp_utils` folder.
Loading

0 comments on commit bf8649b

Please sign in to comment.