forked from nkolban/esp32-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kolban
committed
Mar 15, 2017
1 parent
c65d370
commit bf8649b
Showing
16 changed files
with
346 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.