Internet of Things Asked on August 23, 2021
I’m trying to send a status message in JSON format, to a Google sheet using IFTTT and WebHook.
IFTTT recipe works OK while posting values to all of its 3 cells to Google sheet (String
and char
variables).
JSON message is not too long ( tried with a very short message ).
JSON is created using ArduinoJSON
library, and get posted as a MQTT message as expected.
converting the String
variable ( holds the JSON message ) into char
, didn’t send it either.
I’m guessing it has to be with sending a JSON
format using IFTT + Webhooks.
Am I right ? How can I overcome this ?
Guy
EDIT 1
Explanation: makeIFTTTRequest
gets a string as a parameter (checked – and work OK ) to send it to IFTTT.
create_wakeStatus
generates that string in a JSON format (also checked – and sent as MQTT )
function 1: sending to IFTTT
template <typename T1, typename T2, typename T3>
void makeIFTTTRequest(T1 val1, T2 val2, T3 val3)
{
Serial.print("Connecting to ");
Serial.print(server);
WiFiClient client;
int retries = 5;
while (!!!client.connect(server, 80) && (retries-- > 0))
{
Serial.print(".");
}
Serial.println();
if (!!!client.connected())
{
Serial.println("Failed to connect...");
}
Serial.print("Request resource: ");
Serial.println(resource);
String jsonObject = String("{"value1":"") + val1 + "","value2":"" + val2 + "","value3":"" + val3 + ""}";
client.println(String("POST ") + resource + " HTTP/1.1");
client.println(String("Host: ") + server);
client.println("Connection: closernContent-Type: application/json");
client.print("Content-Length: ");
client.println(jsonObject.length());
client.println();
client.println(jsonObject);
int timeout = 5 * 10; // 5 seconds
while (!!!client.available() && (timeout-- > 0))
{
delay(100);
}
if (!!!client.available())
{
Serial.println("No response...");
}
while (client.available())
{
Serial.write(client.read());
}
Serial.println("nclosing connection");
client.stop();
}
function 2:
String create_wakeStatus()
{
StaticJsonDocument<300> doc;
// Constansts
doc["deviceName"] = go2sleep.WakeStatus.name;
doc["bootCount"] = go2sleep.WakeStatus.bootCount;
doc["forcedAwake"] = go2sleep.WakeStatus.awake_duration;
// Per wake cycle
doc["sleepDuration"] = go2sleep.sleepduration;
doc["Wake"] = go2sleep.WakeStatus.wake_clock;
doc["SleepStart"] = go2sleep.WakeStatus.startsleep_clock;
doc["nextWake"] = go2sleep.WakeStatus.nextwake_clock;
doc["RTCdrift"] = go2sleep.WakeStatus.drift_err;
doc["WakeErr"] = go2sleep.WakeStatus.wake_err;
String output;
serializeJson(doc, output);
return output;
}
**EDIT 2 **
function 3:
void postWake()
{
char a[250];
String output = create_wakeStatus();
output.toCharArray(a, output.length() + 1);
// makeIFTTTRequest(output,"A","B"); once tried as String and once as char ... both failed
// makeIFTTTRequest(a,"blabla","blabla");
iot.pub_ext(a, true);
}
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP