Skip to content

Commit

Permalink
Commit ThingSpeak posting
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Nov 28, 2020
1 parent c3be219 commit a3fa552
Show file tree
Hide file tree
Showing 8 changed files with 489 additions and 2 deletions.
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ lib_deps =
https://github.com/lbussy/CircularBuffer.git
https://github.com/lbussy/LCBUrl.git
https://github.com/lbussy/esptelnet.git
ThingSpeak
build_type = release

[env:d1_mini]
Expand Down
12 changes: 10 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void setup()
if (loadConfig())
Log.notice(F("Configuration loaded." CR));
else
Log.error(F("Unable to load cofiguration." CR));
Log.error(F("Unable to load configuration." CR));

pinMode(LED, OUTPUT);
pinMode(RESETWIFI, INPUT_PULLUP);
Expand Down Expand Up @@ -91,7 +91,7 @@ void loop()

// ThingSpeak timer
Ticker tsTimer;
tsTimer.attach(config.brewfather.freq * 60, setDoBFTarget);
tsTimer.attach(config.thingspeak.freq * 60, setDoTSTarget);

while (true)
{
Expand Down Expand Up @@ -129,6 +129,14 @@ void loop()
config.brewfather.update = false;
saveConfig();
}
if (config.thingspeak.update)
{
Log.notice(F("Resetting ThingSpeak frequency timer to %l minutes." CR), config.thingspeak.freq);
bfTimer.detach();
bfTimer.attach(config.thingspeak.freq * 60, setDoTSTarget);
config.thingspeak.update = false;
saveConfig();
}
serialLoop();
maintenanceLoop();
yield();
Expand Down
296 changes: 296 additions & 0 deletions src/thingSpeakWork.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
/* Copyright (C) 2019-2020 Lee C. Bussy (@LBussy)

This file is part of Lee Bussy's Brew Bubbles (brew-bubbles).

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */

#include "thingSpeakWork.h"

/*
Function: createChannel

Summary:
Create a Thingspeak channel

Parameters:
const char *key - Thingspeak account API key
const char *bubName - Name of the current Brew Bubble

Returns:
Channel number if successful, null string on failure

*/

void getNewBBChannel(String &writeKey, const char *key, const char *bubName)
{
String postData;

// Name the Channel
char name[65];
sprintf(name, "%s | %s", API_KEY, bubName);

// Create channel description
char description[129];
sprintf(description, "%s data for %s.", API_KEY, bubName);

// Name channel fields
char tempFormat[1];
if (config.bubble.tempinf)
{
tempFormat[0] = 'F';
}
else
{
tempFormat[0] = 'F';
}

char fieldAmb[11];
sprintf(fieldAmb, "Ambient °%s", tempFormat);

char fieldVes[10];
sprintf(fieldVes, "Vessel °%s", tempFormat);

const char *field[] = {"BPM", fieldAmb, fieldVes};

String retString = "";
int retVal = createChannel(
retString,
key,
name,
sizeof(field) / sizeof(field[0]),
field,
description,
true,
NULL,
"https://www.brewbubbles.com",
NULL);

if (retVal == 200)
{
const size_t capacity = JSON_ARRAY_SIZE(0) + JSON_ARRAY_SIZE(2) + 2 * JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(16) + 360;
DynamicJsonDocument doc(capacity);

DeserializationError err = deserializeJson(doc, retString);

if (err)
{
Log.error(F("Error: deserializeJson() returned: %s."), err.c_str());
return;
}

for (int i = 0; i < 2; i++)
{
if (doc["api_keys"][i]["write_flag"] == true)
{
const char *wf = doc["api_keys"][i]["api_key"];
writeKey = wf;
}
}
return;
}
else
{
Log.error(F("ThingSpeak POST returned value %d." CR), retVal);
}
}

/*
Function: createChannel

Summary:
Create a Thingspeak channel

Parameters:
String &channelJSON - (Required, by Reference) Returns the write key JSON
const char * api_key - (Required) Specify User API Key, which you can find in your profile. This key is different from the channel API keys.
const char * field - Array of field names.
const char * name - (Optional) Name of the channel.
const char * description - (Optional) Specify an ID or name for the client making the request.
const bool * public_flag - (Optional) Whether the channel is public. The default is false.
const char * tags - (Optional) Comma-separated list of tags.
const char * url - (Optional) Web page URL for the channel.
const char * metadata - (Optional) Metadata for the channel, which can include JSON, XML, or any other data.

Returns:
Channel number if successful.
0 if attempt failed

*/

int createChannel(
String &channelJSON,
const char *api_key,
const char *name,
const unsigned long fieldCount,
const char *field[],
const char *description,
const bool public_flag,
const char *tags,
const char *url,
const char *metadata)
{
WiFiClient client;
HTTPClient http;

// Create POST data
String postData;

if (!strlen(api_key))
return ERR_BADAPIKEY;
postData += "api_key=" + String(api_key);

if (strlen(name))
postData += "&name=" + urlencode(String(name));

// Handle field names
String fieldData = "";
for (unsigned int i = 0; i < fieldCount; i++)
{
fieldData += "&";
char buffer[4];
sprintf(buffer, "%d", i + 1);
String inst = buffer;
fieldData += "field" + inst + "=" + urlencode(String(field[i]));
}
if (fieldData.length() == 0)
return ERR_SETFIELD_NOT_CALLED;
postData += fieldData;

if (strlen(description))
postData += "&description=" + urlencode(String(description));

if (public_flag)
postData += "&public_flag=true";

if (tags && strlen(tags))
postData += "&tags=" + urlencode(String(tags));

if (url && strlen(url))
postData += "&url=" + urlencode(String(url));

if (metadata && strlen(metadata))
postData += "&metadata=" + urlencode(String(metadata));

String tsURL = "http://" + String(THINGSPEAK_URL) + ":" + THINGSPEAK_PORT_NUMBER + "/channels.json";
http.begin(client, tsURL); //Specify request destination
http.addHeader("Content-Type", "application/x-www-form-urlencoded");

int httpCode = http.POST(postData);
channelJSON = http.getString();

#ifdef PRINT_DEBUG_MESSAGES
Serial.println(F("DEBUG: Create ThingSpeak Channel results:"));
Serial.println(httpCode); //Print HTTP return code
Serial.println(channelJSON); //Print request response payload
#endif

http.end(); //Close connection
return httpCode;
};

String urldecode(String stringToProcess)
{

String encodedString = "";
char chr;
char code0;
char code1;
for (unsigned int i = 0; i < stringToProcess.length(); i++)
{
chr = stringToProcess.charAt(i);
if (chr == '+')
{
encodedString += ' ';
}
else if (chr == '%')
{
i++;
code0 = stringToProcess.charAt(i);
i++;
code1 = stringToProcess.charAt(i);
chr = (h2int(code0) << 4) | h2int(code1);
encodedString += chr;
}
else
{

encodedString += chr;
}

yield();
}

return encodedString;
}

String urlencode(String stringToProcess)
{
String encodedString = "";
char chr;
char code0;
char code1;
for (unsigned int i = 0; i < stringToProcess.length(); i++)
{
chr = stringToProcess.charAt(i);
if (chr == ' ')
{
encodedString += '+';
}
else if (isalnum(chr))
{
encodedString += chr;
}
else
{
code1 = (chr & 0xf) + '0';
if ((chr & 0xf) > 9)
{
code1 = (chr & 0xf) - 10 + 'A';
}
chr = (chr >> 4) & 0xf;
code0 = chr + '0';
if (chr > 9)
{
code0 = chr - 10 + 'A';
}
encodedString += '%';
encodedString += code0;
encodedString += code1;
}
yield();
}
return encodedString;
}

unsigned char h2int(char chr)
{
if (chr >= '0' && chr <= '9')
{
return ((unsigned char)chr - '0');
}
if (chr >= 'a' && chr <= 'f')
{
return ((unsigned char)chr - 'a' + 10);
}
if (chr >= 'A' && chr <= 'F')
{
return ((unsigned char)chr - 'A' + 10);
}
return (0);
}
Loading

0 comments on commit a3fa552

Please sign in to comment.