Sending arduino sensor data to Cayenne using POST request


#1
#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h" 
#define DHTPIN 2 
DHT dht;
const int sensor_pin = A1; 
unsigned int ldr;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 
EthernetClient client;
String writeAPIKey = "";
String channelID = "";
void setup()
{
    Serial.begin(9600);
    Ethernet.begin(mac,ip);
    if (Ethernet.begin(mac) == 0) {
    Serial.println(F("Unable to configure Ethernet using DHCP"));
    for (;;);
   }
  Serial.println("Ethernet configured via DHCP");
  delay(10000);
  dht.setup(DHTPIN);
}
void loop() 
{
  delay(1000);
  float moisture_percentage; 
  int sensor_analog;
  sensor_analog = analogRead(sensor_pin); 
  moisture_percentage = ( 100 - ((sensor_analog/1023.00) * 100) ); 
  float t = dht.getTemperature(); 
  float h = dht.getHumidity(); 
  ldr=analogRead(A0);

ThingSpeakUpdate(“field1=”+String(moisture_percentage)+"&field2="+String(t)+"&field3="+String(h)+"&field4="+String(ldr));

}

void ThingSpeakUpdate(String tsData)
{
    Serial.println("Date string: " + tsData);

   Serial.println("...Connecting to Thingspeak");

    if(client.connect("api.thingspeak.com", 80))
    {
        Serial.println("...Connection succesful, updating datastreams");

        client.print("POST /update HTTP/1.1\n");
        client.print("Host: api.thingspeak.com\n");
        client.print("Connection: close\n");
        client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
        client.print("Content-Type: application/x-www-form-urlencoded\n");
        client.print("Content-Length: ");
        client.print(tsData.length());
        client.print("\n\n");

        client.println(tsData); //the ""ln" is important here.

        delay(200);

        Serial.println("Thingspeak update sent.");
    }
    else{
        Serial.println("Unable to connect to Thingspeak.");
    }

    if(!client.connected()){
        client.stop();
    }
    client.flush();
    client.stop();
}

I have used the above code to send the arduino sensor data to Thingspeak using the POST request. I’m unaware of how to do the same thing in Cayenne. Please help me to modify the above code to send data to Cayenne using POST request


#2

Just use Arduino cayenne mqtt library


#3

No, I specifically want to follow this method. This is the project which has been assigned to me. Kindly help me.


#4

Sure, use the Cayenne Rest API , you can extract some good info from Tutorial Using Postman with Cayenne API
or Cayenne API tutorial for beginners beginners with ai2/thunkable