0

I'm trying to send a status message in JSON format, to a Google sheet using IFTTT and WebHook.

  1. IFTTT recipe works OK while posting values to all of its 3 cells to Google sheet (String and char variables).

  2. JSON message is not too long ( tried with a very short message ).

  3. JSON is created using ArduinoJSON library, and get posted as a MQTT message as expected.

  4. 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: close\r\nContent-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);
}
  • please indicate the point of failure ... it is unclear from your description where the failure is occuring – jsotola Jul 18 at 20:58
  • Please provide relevant source code, settings or other information, as well as error messages or other logs or traces. – jcaron Jul 18 at 21:43
  • @jsotola see edit 1 – Guy . D Jul 19 at 12:12
  • Can you provide details of what happens, I.e. all output logs and errors? Can you also show the code that calls those functions? You seem to be encoding JSON inside JSON? Also you should never compose JSON yourself by concatenation, always use existing JSON serialisation functions from libraries like you do in your second function. – jcaron Jul 19 at 12:51
  • @jcaron 1) what happens ? nothing get posted when to post with that String involved. 2) calling this function: see edit 2 ( output was placed inside function 1) 3) JSON inside JSON inside function_1 was copied from a given example – Guy . D Jul 19 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.