├── keywords.txt ├── library.properties ├── README.md ├── LICENSE └── src ├── ESP32_TwitterAPI.h └── ESP32_TwitterAPI.cpp /keywords.txt: -------------------------------------------------------------------------------- 1 | ESP32_TwitterAPI KEYWORD1 2 | utf16_to_utf8 KEYWORD2 3 | TwitterAPI_Key_Set KEYWORD2 4 | TwitterAPI_begin KEYWORD2 5 | TrendTweet_Get KEYWORD2 6 | TrendTweet_HTTP_Request KEYWORD2 7 | URLEncode KEYWORD2 8 | UTF16toUTF8 KEYWORD2 -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=ESP32_TwitterAPI 2 | version=1.1 3 | author=mgo-tec 4 | maintainer=mgo-tec <...@...> 5 | sentence=This is the library that can ESP32. 6 | paragraph=This is the library that can wESP32. 7 | category=Communication 8 | url=https://www.mgo-tec.com 9 | architectures=esp32 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP32_TwitterAPI library 2 | This Library for using Twitter API with Arduino-ESP32( SPI ). 3 | Beta ver 1.1   4 | 5 | Separately, the Time library is required. 6 | http://playground.arduino.cc/code/time 7 | 8 | # Change log 9 | (1.1) 10 | Fix to get Tweet without setting root CA certificate of Twitter APIs.   11 | 12 | (1.0) 13 | New Uploaded. 14 | 15 | 【更新履歴】(Japanese) 16 | (1.1) 17 | Twitter APIs のルートCA証明書を設定しなくてもツイートを取得できるようにしました。 18 | 19 | My Blog: 20 | https://www.mgo-tec.com -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | mgo-tec modified for code created by chaeplin for ESP32. 2 | 3 | Reference: https://gist.github.com/chaeplin/32dd002ddc5fe92d026055130a519b72 4 | Reference: https://github.com/igrr/axtls-8266/blob/master/crypto/hmac.c 5 | 6 | hmac.c License axTLS 1.4.9 Copyright (c) 2007-2016, Cameron Rich 7 | ssl_hmac_sha1 function modified by mogotec for mbedtls. 8 | 9 | axTLS linsence imported. 10 | 11 | axTLS uses a BSD style license: 12 | 13 | Copyright (c) 2008, Cameron Rich All rights reserved. 14 | 15 | Redistribution and use in source and binary forms, with or without 16 | modification, are permitted provided that the following conditions are met: 17 | 18 | Redistributions of source code must retain the above copyright notice, this 19 | list of conditions and the following disclaimer. Redistributions in binary 20 | form must reproduce the above copyright notice, this list of conditions and 21 | the following disclaimer in the documentation and/or other materials 22 | provided with the distribution. Neither the name of the axTLS Project nor 23 | the names of its contributors may be used to endorse or promote products 24 | derived from this software without specific prior written permission. 25 | 26 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 27 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR 30 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 32 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 33 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 36 | DAMAGE. -------------------------------------------------------------------------------- /src/ESP32_TwitterAPI.h: -------------------------------------------------------------------------------- 1 | /* 2 | ESP32_TwitterAPI.h - for Arduino core for the ESP32 ( Use SPI library ). 3 | Beta version 1.1 4 | 5 | mgo-tec modified for code created by chaeplin for ESP32. 6 | 7 | Reference: https://gist.github.com/chaeplin/32dd002ddc5fe92d026055130a519b72 8 | Reference: https://github.com/igrr/axtls-8266/blob/master/crypto/hmac.c 9 | hmac.c License axTLS 1.4.9 Copyright (c) 2007-2016, Cameron Rich 10 | ssl_hmac_sha1 function modified by mogotec for mbedtls. 11 | 12 | axTLS linsence imported. 13 | 14 | axTLS uses a BSD style license: 15 | 16 | Copyright (c) 2008, Cameron Rich All rights reserved. 17 | 18 | Redistribution and use in source and binary forms, with or without 19 | modification, are permitted provided that the following conditions are met: 20 | 21 | Redistributions of source code must retain the above copyright notice, this 22 | list of conditions and the following disclaimer. Redistributions in binary 23 | form must reproduce the above copyright notice, this list of conditions and 24 | the following disclaimer in the documentation and/or other materials 25 | provided with the distribution. Neither the name of the axTLS Project nor 26 | the names of its contributors may be used to endorse or promote products 27 | derived from this software without specific prior written permission. 28 | 29 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 30 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR 33 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 36 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 39 | DAMAGE. 40 | */ 41 | 42 | #ifndef _ESP32_TWITTERAPI_H_INCLUDED 43 | #define _ESP32_TWITTERAPI_H_INCLUDED 44 | 45 | #include 46 | #include 47 | #include 48 | //Time library -> http://playground.arduino.cc/code/time 49 | #include "TimeLib.h" 50 | 51 | #include 52 | #include "mbedtls/sha1.h" 53 | 54 | //xtensa-esp32 library (UTF16 to UTF8 converter) 55 | #include 56 | #include 57 | #include 58 | #include 59 | 60 | std::string utf16_to_utf8(std::u16string const& src); 61 | 62 | class ESP32_TwitterAPI 63 | { 64 | private: 65 | 66 | #define SHA1_SIZE 20 67 | const char *_Consumer_key; 68 | const char *_Consumer_secret; 69 | const char *_Access_token; 70 | const char *_Access_secret; 71 | 72 | const char *_Base_host; 73 | const char *_Base_URL; 74 | const char *_Base_URI; 75 | 76 | String _Woeid; //WOEID 77 | 78 | const int httpsPort = 443; 79 | 80 | const char *_Key_http_method = "GET"; 81 | const char *_Key_consumer_key = "oauth_consumer_key"; 82 | const char *_Key_nonce = "oauth_nonce"; 83 | const char *_Key_signature_method = "oauth_signature_method"; 84 | const char *_Key_timestamp = "oauth_timestamp"; 85 | const char *_Key_token = "oauth_token"; 86 | const char *_Key_version = "oauth_version"; 87 | const char *_Key_status = "status"; 88 | const char *_Key_signature = "oauth_signature"; 89 | const char *_Value_signature_method = "HMAC-SHA1"; 90 | const char *_Value_version = "1.0"; 91 | 92 | public: 93 | ESP32_TwitterAPI(); 94 | 95 | void TwitterAPI_Key_Set(const char *consu_key, const char *consu_sec, const char *ac_tok, const char *ac_sec); 96 | void TwitterAPI_begin(const char *b_host); 97 | bool TrendTweet_Get(const char *ca, const char *base_URL, const char *base_URI, String woeID, uint8_t Max_data, String Utf8_Str[]); 98 | bool TrendTweet_HTTP_Request(const char *RootCA, String OAuth_header, String status_all, uint8_t max_data, String Utf8_Str[]); 99 | String make_parameter_str(String status_all, uint32_t value_nonce, uint32_t value_timestamp); 100 | String make_sign_base_str(String parameter_str); 101 | String make_signature(const char* secret_one, const char* secret_two, String sign_base_str); 102 | String make_OAuth_header(String oauth_signature, uint32_t value_nonce, uint32_t value_timestamp); 103 | String URLEncode(const char* msg); 104 | void ssl_hmac_sha1(uint8_t *msg, int length, const uint8_t *key, int key_len, unsigned char *digest); 105 | String UTF16toUTF8(String str); 106 | 107 | }; 108 | 109 | #endif -------------------------------------------------------------------------------- /src/ESP32_TwitterAPI.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ESP32_TwitterAPI.cpp - for Arduino core for the ESP32 ( Use SPI library ). 3 | Beta version 1.1 4 | mgo-tec modified for code created by chaeplin for ESP32. 5 | 6 | Reference: https://gist.github.com/chaeplin/32dd002ddc5fe92d026055130a519b72 7 | Reference: https://github.com/igrr/axtls-8266/blob/master/crypto/hmac.c 8 | hmac.c License axTLS 1.4.9 Copyright (c) 2007-2016, Cameron Rich 9 | ssl_hmac_sha1 function modified by mogotec for mbedtls. 10 | 11 | axTLS linsence imported. 12 | 13 | axTLS uses a BSD style license: 14 | 15 | Copyright (c) 2008, Cameron Rich All rights reserved. 16 | 17 | Redistribution and use in source and binary forms, with or without 18 | modification, are permitted provided that the following conditions are met: 19 | 20 | Redistributions of source code must retain the above copyright notice, this 21 | list of conditions and the following disclaimer. Redistributions in binary 22 | form must reproduce the above copyright notice, this list of conditions and 23 | the following disclaimer in the documentation and/or other materials 24 | provided with the distribution. Neither the name of the axTLS Project nor 25 | the names of its contributors may be used to endorse or promote products 26 | derived from this software without specific prior written permission. 27 | 28 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 29 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR 32 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 34 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 35 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 38 | DAMAGE. 39 | */ 40 | 41 | #include "ESP32_TwitterAPI.h" 42 | 43 | ESP32_TwitterAPI::ESP32_TwitterAPI(){} 44 | 45 | //******************************************** 46 | void ESP32_TwitterAPI::TwitterAPI_Key_Set(const char *consu_key, const char *consu_sec, const char *ac_tok, const char *ac_sec){ 47 | _Consumer_key = consu_key; 48 | _Consumer_secret = consu_sec; 49 | _Access_token = ac_tok; 50 | _Access_secret = ac_sec; 51 | } 52 | //******************************************** 53 | void ESP32_TwitterAPI::TwitterAPI_begin(const char *b_host){ 54 | _Base_host = b_host; 55 | } 56 | //**********ツイート取得************************ 57 | bool ESP32_TwitterAPI::TrendTweet_Get(const char *ca, const char *base_URL, const char *base_URI, String woeID, uint8_t Max_data, String Utf8_Str[]) { 58 | _Base_URL = base_URL; 59 | _Base_URI = base_URI; 60 | _Woeid = woeID; 61 | 62 | uint32_t value_timestamp = now(); 63 | uint32_t value_nonce = 1111111111 + value_timestamp; 64 | 65 | Serial.println(F("--------------------------")); 66 | String status_all = ""; 67 | String parameter_str = make_parameter_str(status_all, value_nonce, value_timestamp); 68 | String sign_base_str = make_sign_base_str(parameter_str); 69 | String oauth_signature = make_signature(_Consumer_secret, _Access_secret, sign_base_str); 70 | String OAuth_header = make_OAuth_header(oauth_signature, value_nonce, value_timestamp); 71 | 72 | Serial.print(F("OAuth_header = ")); 73 | Serial.println(OAuth_header); 74 | 75 | bool connection = TrendTweet_HTTP_Request(ca, OAuth_header, status_all, Max_data, Utf8_Str); 76 | if(connection == false){ 77 | Utf8_Str[0] = "Twitter API connection failed"; 78 | } 79 | return connection; 80 | } 81 | //*************** HTTP GET Request*************************************** 82 | bool ESP32_TwitterAPI::TrendTweet_HTTP_Request(const char *RootCA, String OAuth_header, String status_all, uint8_t max_data, String Utf8_Str[]){ 83 | bool connection = false; 84 | String unicode_str[max_data]; 85 | WiFiClientSecure client; 86 | 87 | if( strlen( RootCA ) > 0 ) client.setCACert(RootCA); 88 | 89 | if (client.connect(_Base_host, httpsPort)) { 90 | Serial.print(_Base_host); Serial.print(F("-------------")); 91 | Serial.println(F("connected")); 92 | 93 | String str01 = String(_Key_http_method) + " " + String(_Base_URI) + "?id=" + String(_Woeid) + " HTTP/1.1\r\n"; 94 | str01 += "Accept-Charset: UTF-8\r\n"; 95 | str01 += "Accept-Language: ja,en\r\n"; 96 | String str02 = "Authorization: " + OAuth_header + "\r\n"; 97 | str02 += "Connection: close\r\n"; 98 | str02 += "Content-Length: 0\r\n"; 99 | str02 += "Content-Type: application/x-www-form-urlencoded\r\n"; 100 | str02 += "Host: " + String(_Base_host) + "\r\n\r\n"; 101 | 102 | client.print( str01 ); 103 | client.print( str02 ); 104 | 105 | Serial.println(F("-------------------- HTTP GET Request Send")); 106 | Serial.print( str01 ); 107 | Serial.print( str02 ); 108 | 109 | String res_str = ""; 110 | String name_str = ""; 111 | 112 | uint16_t from, to; 113 | uint8_t n_cnt = 0; 114 | String name_begin_str = "\"name\":\""; 115 | int16_t name_begin_flag = 0; 116 | 117 | Serial.println(F("--------------------HTTP Response")); 118 | 119 | while(client.connected()){ 120 | while (client.available()) { 121 | res_str = client.readStringUntil('\n'); 122 | Serial.println(res_str); 123 | if(res_str.indexOf("\r") <= 2){ 124 | Serial.println(F("-------------JSON GET ALL------------")); 125 | while(client.connected()){ 126 | while(client.available()){ 127 | res_str = client.readStringUntil(','); 128 | name_begin_flag = res_str.indexOf(name_begin_str); 129 | 130 | if( name_begin_flag >= 0){ 131 | from = name_begin_flag + name_begin_str.length(); 132 | to = res_str.length() - 1; 133 | name_str = res_str.substring(from,to) + '\0'; 134 | Serial.println(name_str); 135 | name_str.replace("#", ""); //ハッシュタグ消去 136 | 137 | if(n_cnt < max_data){ 138 | unicode_str[n_cnt] = name_str; 139 | } 140 | name_str = ""; 141 | n_cnt++; 142 | res_str = ""; 143 | } 144 | } 145 | } 146 | } 147 | } 148 | } 149 | client.flush(); 150 | delay(10); 151 | client.stop(); 152 | delay(10); 153 | Serial.println(F("--------------------Client Stop")); 154 | connection = true; 155 | }else { 156 | // if you didn't get a connection to the server2: 157 | Serial.println(F("connection failed")); 158 | connection = false; 159 | } 160 | 161 | if(connection == true){ 162 | int i; 163 | Serial.println(F("----------GET Twitter Trends Unicode ( UTF16 )-------------")); 164 | for(i=0; i> 4]; 291 | encodedMsg += hex[*msg & 0xf]; 292 | } 293 | msg++; 294 | } 295 | return encodedMsg; 296 | } 297 | //************************************************* 298 | void ESP32_TwitterAPI::ssl_hmac_sha1(uint8_t *msg, int length, const uint8_t *key, int key_len, unsigned char *digest) { 299 | mbedtls_sha1_context context; 300 | uint8_t k_ipad[64] = {0}; 301 | uint8_t k_opad[64] = {0}; 302 | int i; 303 | 304 | memcpy(k_ipad, key, key_len); 305 | memcpy(k_opad, key, key_len); 306 | 307 | for (i = 0; i < 64; i++) 308 | { 309 | k_ipad[i] ^= 0x36; 310 | k_opad[i] ^= 0x5c; 311 | } 312 | 313 | mbedtls_sha1_starts(&context); 314 | mbedtls_sha1_update(&context, k_ipad, 64); 315 | mbedtls_sha1_update(&context, msg, length); 316 | mbedtls_sha1_finish(&context, digest); 317 | mbedtls_sha1_starts(&context); 318 | mbedtls_sha1_update(&context, k_opad, 64); 319 | mbedtls_sha1_update(&context, digest, SHA1_SIZE); 320 | mbedtls_sha1_finish(&context, digest); 321 | } 322 | //********** Unicode ( UTF16 ) to UTF-8 convert ******************************** 323 | String ESP32_TwitterAPI::UTF16toUTF8(String str){ 324 | str.replace("\\u","\\"); 325 | str += '\0'; 326 | uint16_t len = str.length(); 327 | char16_t utf16code[len]; 328 | 329 | int i=0; 330 | String str4 = ""; 331 | for(int j=0; j UTF8 cnvert ****************************************** 361 | std::string utf16_to_utf8(std::u16string const& src){ 362 | std::wstring_convert, char16_t> converter; 363 | return converter.to_bytes(src); 364 | } --------------------------------------------------------------------------------