Ping Zapier with Arduino on ESP32-C3

Updated on 6 November 2023
dev board ESP32-C3-DevKitM-1
chip ESP32-C3-MINI-1-N4
features esp32c3 ping webhook send zapier https tls secure post
This tutorial is more than 1 year old. If the steps below do not work, then please check the latest versions and the documentations of the individual tools used.

Before starting

Dependancies

Ensure the following dependancies are downloaded and available:

Pre-requisites

Try these simpler or similar examples:

Buy the components

Code

Download code ping-zapier-arduino-esp32c3.ino
#include <WiFiClientSecure.h>
#include "Secret.h"
#define LED 3  // or LED_BUILTIN for on-board LED

WiFiClientSecure client;

void setup() {
  Serial.begin(115200);
  delay(1000);

  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);

  Serial.print("Attempting to connect to SSID: ");
  Serial.println(ssid);

  WiFi.useStaticBuffers(true);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("Connected to WiFi");

  client.setCACert(zapier_root_ca);
  sendZapierWebhook();
  blink(5);
}

void loop() {}

void sendZapierWebhook() {
  if (!client.connect(server, 443)) {
    Serial.println("Connection failed!");
  } else {
    Serial.println("Connected to Zapier");
    String data = "data1=67&data2=testing";  // Amend the data accordingly
    String request = "POST " + url + " HTTP/1.1\r\n";
    request += "Host: " + String(host) + "\r\n";
    request += "Content-Type: application/x-www-form-urlencoded\r\n";
    request += "Content-Length: " + String(data.length()) + "\r\n\r\n";
    request += data;

    Serial.println("Sending request to Zapier:");
    Serial.println(request);

    client.print(request);

    // Wait for the response until 5 seconds
    unsigned long startTime = millis();
    while (client.connected() && millis() - startTime < 5000) {
      if (client.available()) {
        char c = client.read();
        Serial.print(c);
      }
    }

    // Close the connection
    client.stop();
    Serial.println("Request sent to Zapier");
  }
}

void blink(int times) {
  for (int i = 0; i < times; i++) {
    digitalWrite(LED, HIGH);
    delay(1000);

    digitalWrite(LED, LOW);
    delay(1000);
    Serial.print("Blink ");
    Serial.println(i + 1);
  }
}

Makefile

BOARD?=esp32:esp32:esp32c3
PORT?=/dev/cu.usbserial-*
BUILD=build
## Pass in V=--verbose to output more information than default

.PHONY: help default lint compile upload clean

default: clean lint compile upload

help: ## Show help message
	@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n  make \033[36m\033[0m\n"} /^[$$()% 0-9a-zA-Z_-]+:.*?##/ { printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

lint: ## Lint code using cpplint
	cpplint --extensions=ino --filter=-legal/copyright,-runtime/int *.ino

compile: ## Compile code and create the firmware binary
	arduino-cli compile $(V) --fqbn $(BOARD) --output-dir $(BUILD) ./

upload: ## Upload the firmware to the board
	arduino-cli upload $(V) --fqbn $(BOARD) --port $(PORT) --input-dir $(BUILD)

clean: ## Remove all built files
	rm -rf build

Prototype

A photo of the actual setup.

Ping Zapier with Arduino on ESP32-C3 prototype

Schematic

Wire up the hardware accordingly

Ping Zapier with Arduino on ESP32-C3 schematic

Serial console

Serial output from the firmware.

Ping Zapier with Arduino on ESP32-C3 serial console

Description

This example shows to do to an HTTPS POST with some data to Zapier webhook, which will then create a new row in Google Sheets.

Create a Zap in Zapier

  1. Create a new Google Spreadsheet to connect to Zapier
  2. Create step 1 to Catch Hook in Webhooks by Zapier
  3. Note the Zapier webhook URL
  4. Create step 2 to Create Spreadsheet row in Google Sheets with data fields such as Timestamp with zap_meta_human_now
  5. Test with curl HTTPS POST command

     $ curl -X POST -d "data1=67&data2=testing" "https://hooks.zapier.com/hooks/catch/[ZAPIER_URL]"
    

  6. Check that the Google Spreadsheet has a new row with the data posted

Download Zapier’s certificate with Google Chrome Browser

  1. Click the padlock 🔒 symbol beside the URL
  2. Click certificate is valid
  3. Click the details of the certificate
  4. Click the root certificate and export
  5. Download the certificate
  6. Open the certificate in a text editor
  7. Add " at the start of every line and \n" \ at the end of every line. Last line is \n". Create a const char variable and add to the file Secret.h.

     const char* zapier_root_ca= \
     "-----BEGIN CERTIFICATE-----\n" \
     "MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF\n" \
     "ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6\n" \
     "b24gUm9vdCBDQSAxMB4XDTE1MDUyNjAwMDAwMFoXDTM4MDExNzAwMDAwMFowOTEL\n" \
     "MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv\n" \
     "b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj\n" \
     "ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM\n" \
     "9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw\n" \
     "IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6\n" \
     "VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L\n" \
     "93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm\n" \
     "jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC\n" \
     "AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA\n" \
     "A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI\n" \
     "U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs\n" \
     "N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv\n" \
     "o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU\n" \
     "5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy\n" \
     "rqXRfboQnoZsG4q5WTP468SQvvG5\n" \
     "-----END CERTIFICATE-----\n";
    

References