Skip to content

Commit

Permalink
Finalized google sheets integration, NTP sync module, and publish pla…
Browse files Browse the repository at this point in the history
…tform
  • Loading branch information
prototypicalpro committed Jun 13, 2019
1 parent 78c6d4a commit 1229b57
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 72 deletions.
46 changes: 46 additions & 0 deletions examples/GoogleSheetsExample/GoogleSheetsExample.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

#include <Loom.h>

// Fill out the config with your google sheets values
const char* json_config_pretty =
#include "json_config_pretty.h"
;


LoomManager Manager("Manager", "Loom", 1, 1, DeviceType::NODE, Verbosity::V_HIGH, Verbosity::V_LOW);

void setup()
{
pinMode(LED_BUILTIN, OUTPUT); // Set the LED pin mode

Serial.begin(115200);
while(!Serial); // Ensure Serial is ready to go before anything happens in LOOM_DEBUG mode.
delay(1000);

LPrintln("Initialized Serial!\n");

LPrintln("\nConfig:\n", json_config_pretty, "\n");

Manager.parse_config(json_config_pretty);
Manager.print_config();

LPrintln("strlen json_config_pretty ", strlen(json_config_pretty));

LPrintln("\n ** Setup Complete ** ");
}



void loop()
{
Manager.measure();

JsonObject j = Manager.package();

Manager.publish(j);

serializeJsonPretty(j, Serial);
Serial.println();

delay(10000);
}
93 changes: 93 additions & 0 deletions examples/GoogleSheetsExample/json_config_pretty.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"{\
'general':\
{\
'device_name':'Device',\
'family':'Loom',\
'instance_num':1,\
'family_num':0\
},\
'components':[\
{\
'name':'Loom_Analog',\
'parameters':[\
'Analog',\
8,\
12,\
true,\
true,\
true,\
true,\
true,\
true,\
0,\
0,\
0,\
0,\
0,\
0\
]\
},\
{\
'name':'Loom_Digital',\
'parameters':[\
'Digital',\
true,\
true,\
false,\
false,\
false,\
false,\
false,\
false,\
false,\
false,\
false,\
false\
]\
},\
{\
'name':'Loom_Ethernet',\
'parameters':[\
'Ether1',\
[<your-mac-address>],\
[192,168,0,1]\
]\
},\
{\
'name':'Loom_DS3231',\
'parameters':[\
'RTC1',\
12,\
true,\
false,\
6\
]\
},\
{\
'name':'Loom_NTP_Sync',\
'parameters':[\
'NTP1',\
0,\
0\
]\
},\
{\
'name':'Loom_GoogleSheets',\
'parameters':[\
'Goog',\
0,\
'/macros/s/<your-url>/exec',\
'<your-sheet-id',\
'<your-Tab-id>',\
'<your-device-id>'\
]\
},\
{\
'name':'Loom_Interrupt_Manager',\
'parameters':[\
'Interrupt-Manager',\
0\
]\
}\
]\
}"
2 changes: 0 additions & 2 deletions src/InternetPlats/Loom_InternetEthernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ bool Loom_Ethernet_I::is_connected()

/////////////////////////////////////////////////////////////////////
Client& Loom_Ethernet_I::http_request(const char* domain, const char* url, const char* body, const char* verb) {
LPrint("Connecting to: ", domain, "\n");
// if the socket is somehow still open, close it
if (m_client.connected()) m_client.stop();
// * the rainbow connection *
Expand All @@ -113,7 +112,6 @@ Client& Loom_Ethernet_I::http_request(const char* domain, const char* url, const
print_module_label();
LPrint("Writing http: ", domain, "\n");
write_http_request(m_client, domain, url, body, verb);
write_http_request(Serial, domain, url, body, verb);
// gosh that was easy
m_is_connected = true;
// return the client for data reception
Expand Down
67 changes: 32 additions & 35 deletions src/InternetPlats/Loom_Trust_Anchors.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,46 @@ extern "C"
* Certificates are BearSSL br_x509_trust_anchor format. Included certs:
*
* Index: 0
* Label: DigiCert High Assurance EV Root CA
* Subject: C=US,O=DigiCert Inc,OU=www.digicert.com,CN=DigiCert High Assurance EV Root CA
* Domain(s): www.cloudflare.com
* Label: GlobalSign
* Subject: OU=GlobalSign Root CA - R2,O=GlobalSign,CN=GlobalSign
* Domain(s): script.google.com
*/

#define TAs_NUM 1

static const unsigned char TA_DN0[] = {
0x30, 0x6c, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
0x02, 0x55, 0x53, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0a,
0x13, 0x0c, 0x44, 0x69, 0x67, 0x69, 0x43, 0x65, 0x72, 0x74, 0x20, 0x49,
0x6e, 0x63, 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13,
0x10, 0x77, 0x77, 0x77, 0x2e, 0x64, 0x69, 0x67, 0x69, 0x63, 0x65, 0x72,
0x74, 0x2e, 0x63, 0x6f, 0x6d, 0x31, 0x2b, 0x30, 0x29, 0x06, 0x03, 0x55,
0x04, 0x03, 0x13, 0x22, 0x44, 0x69, 0x67, 0x69, 0x43, 0x65, 0x72, 0x74,
0x20, 0x48, 0x69, 0x67, 0x68, 0x20, 0x41, 0x73, 0x73, 0x75, 0x72, 0x61,
0x6e, 0x63, 0x65, 0x20, 0x45, 0x56, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20,
0x43, 0x41,
0x30, 0x4c, 0x31, 0x20, 0x30, 0x1e, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13,
0x17, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x53, 0x69, 0x67, 0x6e, 0x20,
0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x20, 0x2d, 0x20, 0x52, 0x32,
0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x0a, 0x47,
0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x53, 0x69, 0x67, 0x6e, 0x31, 0x13, 0x30,
0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x47, 0x6c, 0x6f, 0x62,
0x61, 0x6c, 0x53, 0x69, 0x67, 0x6e,
};

static const unsigned char TA_RSA_N0[] = {
0xc6, 0xcc, 0xe5, 0x73, 0xe6, 0xfb, 0xd4, 0xbb, 0xe5, 0x2d, 0x2d, 0x32,
0xa6, 0xdf, 0xe5, 0x81, 0x3f, 0xc9, 0xcd, 0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, 0x84, 0x6c, 0xa8, 0xcd, 0x4b, 0xb4, 0xa0, 0x7d, 0x0c, 0x79, 0x4d,
0xf0, 0xb8, 0x2d, 0xcb, 0x21, 0xca, 0xd5, 0x6c, 0x5b, 0x7d, 0xe1, 0xa0,
0x29, 0x84, 0xa1, 0xf9, 0xd3, 0x94, 0x49, 0xcb, 0x24, 0x62, 0x91, 0x20,
0xbc, 0xdd, 0x0b, 0xd5, 0xd9, 0xcc, 0xf9, 0xea, 0x27, 0x0a, 0x2b, 0x73,
0x91, 0xc6, 0x9d, 0x1b, 0xac, 0xc8, 0xcb, 0xe8, 0xe0, 0xa0, 0xf4, 0x2f,
0x90, 0x8b, 0x4d, 0xfb, 0xb0, 0x36, 0x1b, 0xf6, 0x19, 0x7a, 0x85, 0xe0,
0x6d, 0xf2, 0x61, 0x13, 0x88, 0x5c, 0x9f, 0xe0, 0x93, 0x0a, 0x51, 0x97,
0x8a, 0x5a, 0xce, 0xaf, 0xab, 0xd5, 0xf7, 0xaa, 0x09, 0xaa, 0x60, 0xbd,
0xdc, 0xd9, 0x5f, 0xdf, 0x72, 0xa9, 0x60, 0x13, 0x5e, 0x00, 0x01, 0xc9,
0x4a, 0xfa, 0x3f, 0xa4, 0xea, 0x07, 0x03, 0x21, 0x02, 0x8e, 0x82, 0xca,
0x03, 0xc2, 0x9b, 0x8f,
};

static const unsigned char TA_RSA_E0[] = {
Expand Down
24 changes: 14 additions & 10 deletions src/Loom_NTP_Sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,30 @@ void LoomNTPSync::print_state() {
/////////////////////////////////////////////////////////////////////
void LoomNTPSync::measure() {
// if periodic sync is requested and the sync time has passed
if (m_last_error == Error::OK
if (m_last_error == Error::OK
&& m_internet->is_connected()
&& m_next_sync.unixtime() != 0
&& (m_rtc->now() - m_next_sync).totalseconds() >= 0) {
// synchronize the RTC
const DateTime& timeNow = m_sync_rtc();
if (timeNow.unixtime() == 0) {
// try again at the next cycle
return;
}
DateTime timeNow;
// repeat synchronize if this is the first power on
do {
timeNow = m_sync_rtc();
if (timeNow.unixtime() != 0) m_next_sync = DateTime(0);
else delay(100);
} while (m_next_sync.unixtime() == 1);
// set the next sync time
if (m_sync_interval != 0) {
// to n hours from now
m_next_sync = DateTime(timeNow) + TimeSpan(0, m_sync_interval, 0, 0);
}
// to done syncing
else m_next_sync = DateTime(0);
}
else if (m_last_error != Error::OK) {
else {
print_module_label();
LPrint("Could not synchronize RTC due to error ", static_cast<uint8_t>(m_last_error), "\n");
if (m_last_error != Error::OK)
LPrint("Could not synchronize RTC due to error ", static_cast<uint8_t>(m_last_error), "\n");
else if (!(m_internet->is_connected()))
LPrint("Could not synchronize RTC due to lack of internet");
}
}

Expand Down
62 changes: 39 additions & 23 deletions src/PublishPlats/Loom_GoogleSheets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,39 @@ void Loom_GoogleSheets::print_config() {
/////////////////////////////////////////////////////////////////////
bool Loom_GoogleSheets::send_to_internet(const JsonObject json, LoomInternetPlat* plat) {
print_module_label();
// serialize the data, checking for an error
if (!m_serialize_internet_impl(json)) {
LPrint("Buffer overflow during serialize!\n");
return false;
}
else {
LPrint(m_buffer, "\n");
Client& m_cli = plat->http_get_request("script.google.com", m_buffer);
if (!m_cli.connected()) return false;
// discard all oncoming data
const auto start = millis();
while (m_cli.connected()) {
const auto read = m_cli.available();
if(read) m_cli.read(nullptr, read);
// timeout in case connection doesn't close itself
if (millis() - start > 5000) m_cli.stop();
}
// all done!
print_module_label();
LPrint("Published successfully!\n");
return true;
}
}

/////////////////////////////////////////////////////////////////////
bool Loom_GoogleSheets::m_serialize_internet_impl(const JsonObject json) {
// step one: package timestamp
char* m_data_cur = m_data_start;
const JsonObject time_obj = json["timestamp"];
for (const JsonPair i : time_obj) {
const int printed = snprintf(m_data_cur, &(m_buffer[sizeof(m_buffer) - 1]) - m_data_cur,
const int printed = snprintf(m_data_cur, m_buffer_left(m_data_cur),
"%s~%s~", i.key().c_str(), i.value().as<const char*>());
if (printed < 0) {
LPrint("Buffer overflow during timestamp packaging");
return false;
}
if (printed < 0) return false;
// move the pointer foward
m_data_cur = &(m_data_cur[printed]);
}
Expand All @@ -66,33 +89,26 @@ bool Loom_GoogleSheets::send_to_internet(const JsonObject json, LoomInternetPlat
const JsonObject data_vals = i["data"];
for (const auto d : data_vals) {
// serialize the key
const int printed = snprintf(m_data_cur, &(m_buffer[sizeof(m_buffer) - 1]) - m_data_cur,
"%s-%s~", name, d.key().c_str());
if (printed < 0) {
LPrint("Buffer overflow during data packaging");
return false;
}
const int printed = snprintf(m_data_cur, m_buffer_left(m_data_cur),
"%s-%s~", name, d.key().c_str());
if (printed < 0) return false;
// move the pointer foward
m_data_cur = &(m_data_cur[printed]);
// serialize the value
const auto data_tmp = d.value();
const auto size = measureJson(data_tmp);
const auto max_size = &(m_buffer[sizeof(m_buffer) - 1]) - m_data_cur;
const auto json_size = measureJson(data_tmp);
const auto max_json_size = m_buffer_left(m_data_cur);
// no overflow check
if (size + 1 >= max_size) {
LPrint("Buffer overflow during data packaging");
return false;
}
serializeJson(data_tmp, m_data_cur, max_size);
if (json_size + 1 >= max_json_size) return false;
serializeJson(data_tmp, m_data_cur, max_json_size);
// add the trailing tilde
m_data_cur[size] = '~';
m_data_cur[json_size] = '~';
// move the pointer forward
m_data_cur = &(m_data_cur[size + 1]);
m_data_cur = &(m_data_cur[json_size + 1]);
}
}
// step three: replace the last trailing tilde with a null
m_data_cur--;
m_data_cur[0] = '\0';
// step four: send the data!
LPrint("Google sheets data: ", m_buffer, "\n");
}
return true;
}
11 changes: 10 additions & 1 deletion src/PublishPlats/Loom_GoogleSheets.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,19 @@ class Loom_GoogleSheets : public LoomPublishPlat {
bool send_to_internet(const JsonObject json, LoomInternetPlat* plat) override;

private:
// Get the number of bytes left in buffer from a given pointer in buffer
size_t m_buffer_left(const char* buffer_index) const {
const char* end_buffer = &(m_buffer[sizeof(m_buffer) - 1]);
if (buffer_index >= end_buffer) return 0;
return end_buffer - buffer_index;
}
// private utility send function
bool m_serialize_internet_impl(const JsonObject json);

// storage for all those strings we're going to need above
// main string buffer, the front of which stores our URL
// do not edit this directly, use m_url_end instead.
char m_buffer[1024];
char m_buffer[768];
// store a pointer to where we can actually add data
char* m_data_start;
};
Expand Down
Loading

0 comments on commit 1229b57

Please sign in to comment.