8000 Wed Mar 15 11:25:13 CDT 2017 · esp32vn/esp32-snippets@bf8649b · GitHub
[go: up one dir, main page]

Skip to content

Commit bf8649b

Browse files
author
kolban
committed
Wed Mar 15 11:25:13 CDT 2017
1 parent c65d370 commit bf8649b

File tree

16 files changed

+346
-227
lines changed

16 files changed

+346
-227
lines changed

cpp_utils/IFTTT.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* IFTTT.cpp
3+
*
4+
* Created on: Mar 14, 2017
5+
* Author: kolban
6+
*/
7+
#if defined(ESP_HAVE_CURL)
8+
#include "IFTTT.h"
9+
#include <cJSON.h>
10+
11+
12+
/**
13+
* @brief Construct an IFTTT maker client using the supplied key.
14+
*/
15+
IFTTT::IFTTT(std::string key) {
16+
m_key = key;
17+
m_restClient.addHeader("Content-Type", "application/json");
18+
m_restClient.setVerbose(true);
19+
} // IFTTT
20+
21+
22+
IFTTT::~IFTTT() {
23+
} // ~IFTTT
24+
25+
26+
/**
27+
* @brief Trigger a maker event at IFTTT.
28+
*
29+
* @param [in] event The event type to send.
30+
* @param [in] value1 The value of value1.
31+
* @param [in] value2 The value of value2.
32+
* @param [in] value3 The value of value3.
33+
*/
34+
void IFTTT::trigger(
35+
std::string event,
36+
std::string value1,
37+
std::string value2,
38+
std::string value3) {
39+
m_restClient.setURL("https://maker.ifttt.com/trigger/" + event + "/with/key/" + m_key);
40+
cJSON *root;
41+
root = cJSON_CreateObject();
42+
43+
cJSON_AddStringToObject(root, "value1", value1.c_str());
44+
cJSON_AddStringToObject(root, "value2", value2.c_str());
45+
cJSON_AddStringToObject(root, "value3", value3.c_str());
46+
47+
m_restClient.post(std::string(cJSON_Print(root)));
48+
49+
cJSON_Delete(root);
50+
} // trigger
51+
#endif // ESP_HAVE_CURL

cpp_utils/IFTTT.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* IFTTT.h
3+
*
4+
* Created on: Mar 14, 2017
5+
* Author: kolban
6+
*/
7+
8+
#ifndef MAIN_IFTTT_H_
9+
#define MAIN_IFTTT_H_
10+
11+
#include "RESTClient.h"
12+
13+
class IFTTT {
14+
public:
15+
IFTTT(std::string key);
16+
virtual ~IFTTT();
17+
void trigger(std::string event, std::string value1 = "", std::string value2 = "", std::string value3 = "");
18+
private:
19+
RESTClient m_restClient;
20+
std::string m_key;
21+
};
22+
23+
#endif /* MAIN_IFTTT_H_ */

cpp_utils/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#CPP Utils
2+
This directory contains a wealth of C++ classes that have been found useful when working in C++ in conjunction
3+
with the ESP-IDF. The classes have been documented using `doxygen` so one can run a doxygen processor over them
4+
to create the user guides and programming references.

cpp_utils/RESTClient.cpp

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* RESTClient.cpp
3+
*
4+
* Created on: Mar 12, 2017
5+
* Author: kolban
6+
*/
7+
#if defined(ESP_HAVE_CURL)
8+
9+
#define _GLIBCXX_USE_C99 // Needed for std::string inclusion.
10+
11+
12+
#include <curl/curl.h>
13+
#include <esp_log.h>
14+
#include <string>
15+
16+
#include "RESTClient.h"
17+
18+
static char tag[] = "RESTClient";
19+
20+
21+
RESTClient::RESTClient() {
22+
m_curlHandle = curl_easy_init();
23+
m_timings = new RESTTimings(this);
24+
} // RESTClient
25+
26+
27+
RESTClient::~RESTClient() {
28+
::curl_easy_cleanup(m_curlHandle);
29+
curl_slist_free_all(m_headers);
30+
delete m_timings;
31+
} // ~RESTClient
32+
33+
34+
/**
35+
* @brief Perform an HTTP GET request.
36+
*/
37+
void RESTClient::get() {
38+
prepForCall();
39+
::curl_easy_setopt(m_curlHandle, CURLOPT_HTTPGET, 1);
40+
int rc = ::curl_easy_perform(m_curlHandle);
41+
if (rc != CURLE_OK) {
42+
ESP_LOGE(tag, "get(): %s", getErrorMessage().c_str());
43+
}
44+
} // get
45+
46+
47+
/**
48+
* @brief Perform an HTTP POST request.
49+
*
50+
* @param [in] body The body of the payload to send with the post request.
51+
*
52+
*/
53+
void RESTClient::post(std::string body) {
54+
prepForCall();
55+
::curl_easy_setopt(m_curlHandle, CURLOPT_POSTFIELDS, body.c_str());
56+
int rc = ::curl_easy_perform(m_curlHandle);
57+
if (rc != CURLE_OK) {
58+
ESP_LOGE(tag, "post(): %s", getErrorMessage().c_str());
59+
}
60+
} // post
61+
62+
63+
/**
64+
* @brief Get the last error message.
65+
*/
66+
std::string RESTClient::getErrorMessage() {
67+
std::string errMsg(m_errbuf);
68+
return errMsg;
69+
} // getErrorMessage
70+
71+
72+
/**
73+
* @brief Callback function to handle the data received.
74+
*
75+
* This is a callback function architected by libcurl to be called when data is received.
76+
* We append the data to an accumulating buffer.
77+
*
78+
* @param [in] buffer A buffer of records.
79+
* @param [in] size The size of a record.
80+
* @param [in] nmemb The number of records of unit `size`.
81+
* @param [in] userp A pointer to the RESTClient class instance.
82+
*
83+
* @return The number of bytes of data processed.
84+
*/
85+
size_t RESTClient::handleData(void *buffer, size_t size, size_t nmemb, void *userp) {
86+
//printf("handleData: size: %d, num: %d\n", size, nmemb);
87+
RESTClient *pClient = (RESTClient *)userp;
F987
88+
pClient->m_response.append((const char *)buffer, size*nmemb);
89+
return size * nmemb;
90+
} // handleData
91+
92+
93+
/**
94+
* @brief Add a header to the list of headers.
95+
*
96+
* For example:
97+
*
98+
* @code{.cpp}
99+
* client.addHeader("Content-Type", "application/json");
100+
* @endcode
101+
*
102+
* @param [in] name The name of the header to be added.
103+
* @param [in] value The value of the header to be added.
104+
*/
105+
void RESTClient::addHeader(std::string name, std::string value) {
106+
std::string headerString = name + ": " + value;
107+
m_headers = curl_slist_append(m_headers, headerString.c_str());
108+
} // addHeader
109+
110+
111+
/**
112+
* @brief Prepare for a call using a reset handle.
113+
*/
114+
void RESTClient::prepForCall() {
115+
::curl_easy_reset(m_curlHandle);
116+
117+
if (m_verbose) {
118+
::curl_easy_setopt(m_curlHandle, CURLOPT_VERBOSE, 1L);
119+
} else {
120+
::curl_easy_setopt(m_curlHandle, CURLOPT_VERBOSE, 0L);
121+
}
122+
123+
::curl_easy_setopt(m_curlHandle, CURLOPT_ERRORBUFFER, m_errbuf);
124+
::curl_easy_setopt(m_curlHandle, CURLOPT_SSL_VERIFYPEER, 0L);
125+
::curl_easy_setopt(m_curlHandle, CURLOPT_CAINFO, nullptr);
126+
::curl_easy_setopt(m_curlHandle, CURLOPT_URL, m_url.c_str());
127+
::curl_easy_setopt(m_curlHandle, CURLOPT_HTTPHEADER, m_headers);
128+
::curl_easy_setopt(m_curlHandle, CURLOPT_WRITEFUNCTION, handleData);
129+
::curl_easy_setopt(m_curlHandle, CURLOPT_WRITEDATA, this);
130+
m_response = "";
131+
} // prepForCall
132+
133+
134+
RESTTimings::RESTTimings(RESTClient *client) {
135+
this->client = client;
136+
}
137+
138+
139+
/**
140+
* @brief Refresh the timings information.
141+
*/
142+
void RESTTimings::refresh() {
143+
::curl_easy_getinfo(client->m_curlHandle, CURLINFO_STARTTRANSFER_TIME, &m_starttransfer);
144+
::curl_easy_getinfo(client->m_curlHandle, CURLINFO_NAMELOOKUP_TIME, &m_namelookup);
145+
::curl_easy_getinfo(client->m_curlHandle, CURLINFO_CONNECT_TIME, &m_connect);
146+
::curl_easy_getinfo(client->m_curlHandle, CURLINFO_APPCONNECT_TIME, &m_appconnect);
147+
::curl_easy_getinfo(client->m_curlHandle, CURLINFO_PRETRANSFER_TIME, &m_pretransfer);
148+
::curl_easy_getinfo(client->m_curlHandle, CURLINFO_TOTAL_TIME, &m_total);
149+
} // refresh
150+
151+
152+
/**
153+
* @brief Return the timings information as a string.
154+
*
155+
* @return The timings information.
156+
*/
157+
std::string RESTTimings::toString() {
158+
std::string ret = "Start Transfer: " + std::to_string(m_starttransfer) + \
159+
"\nName lookup: " + std::to_string(m_namelookup) + \
160+
"\nConnect: " + std::to_string(m_connect) + \
161+
"\nApp Connect: " + std::to_string(m_appconnect) + \
162+
"\nPre Transfer: " + std::to_string(m_pretransfer) + \
163+
"\nTotal: " + std::to_string(m_total);
164+
return ret;
165+
} // toString
166+
#endif // ESP_HAVE_CURL

cpp_utils/RESTClient.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* RESTClient.h
3+
*
4+
* Created on: Mar 12, 2017
5+
* Author: kolban
6+
*/
7+
8+
#ifndef MAIN_RESTCLIENT_H_
9+
#define MAIN_RESTCLIENT_H_
10+
#include <string>
11+
#include <curl/curl.h>
12+
class RESTClient;
13+
14+
class RESTTimings {
15+
public:
16+
RESTTimings(RESTClient *client);
17+
void refresh();
18+
std::string toString();
19+
private:
20+
double m_namelookup = 0;
21+
double m_connect = 0;
22+
double m_appconnect = 0;
23+
double m_pretransfer = 0;
24+
double m_starttransfer = 0;
25+
double m_total = 0;
26+
RESTClient *client = nullptr;
27+
};
28+
29+
class RESTClient {
30+
public:
31+
RESTClient();
32+
virtual ~RESTClient();
33+
void addHeader(std::string name, std::string value);
34+
void get();
35+
std::string getErrorMessage();
36+
std::string getResponse() {
37+
return m_response;
38+
}
39+
40+
RESTTimings *getTimings() {
41+
return m_timings;
42+
}
43+
44+
void post(std::string body);
45+
46+
/**
47+
* @brief Set the URL for the target.
48+
*
49+
* @param [in] url The target for HTTP request.
50+
*
51+
*/
52+
void setURL(std::string url) {
53+
m_url = url;
54+
};
55+
56+
/**
57+
* @brief Set the verbosity flag.
58+
*
59+
* @param [in] The value of the verbosity.
60+
*/
61+
void setVerbose(bool value) {
62+
m_verbose = value;
63+
};
64+
65+
66+
67+
private:
68+
CURL *m_curlHandle;
69+
std::string m_url;
70+
char m_errbuf[CURL_ERROR_SIZE];
71+
struct curl_slist *m_headers = nullptr;
72+
bool m_verbose = false;
73+
friend class RESTTimings;
74+
RESTTimings *m_timings;
75+
std::string m_response;
76+
static size_t handleData(void *buffer, size_t size, size_t nmemb, void *userp);
77+
void prepForCall();
78+
};
79+
80+
#endif /* MAIN_RESTCLIENT_H_ */

cpp_utils/WiFi.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ void WiFi::connectAP(std::string ssid, std::string password){
8787
}
8888

8989

90-
9190
ESP_ERROR_CHECK( esp_event_loop_init(wifiEventHandler->getEventHandler(), wifiEventHandler));
9291
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
9392
ESP_ERROR_CHECK(::esp_wifi_init(&cfg));

cpp_utils/WiFiEventHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ esp_err_t WiFiEventHandler::eventHandler(void *ctx, system_event_t *event) {
5656
printf("Found a next handler\n");
5757
rc = eventHandler(pWiFiEventHandler->nextHandler, event);
5858
} else {
59-
printf("NOT Found a next handler\n");
59+
//printf("NOT Found a next handler\n");
6060
}
6161
return rc;
6262
}

cpp_utils/component.mk

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55
# src/ directory, compile them and link them into lib(subdirectory_name).a
66
# in the build directory. This behaviour is entirely configurable,
77
# please read the ESP-IDF documents if you need to do this.
8-
COMPONENT_ADD_INCLUDEDIRS=.
8+
COMPONENT_ADD_INCLUDEDIRS=.
9+
10+
## Uncomment the following line if we have an implementation of libcurl available to us.
11+
#CXXFLAGS+=-DESP_HAVE_CURL

hardware/accelerometers/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#Accelerometers
2+
An accelerometer detects acceleration and in some cases gyro scopic movement.
3+
4+
See also the C++ class for MPU-6050 support.

hardware/neopixels/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Moved
2+
The class supporting the WS2812 pixels has moved to the `cpp_utils` folder.

0 commit comments

Comments
 (0)
0