Add a variable in a HTML request?

Hello everyone !

I'm triggering a webhook to make.com from my Arduino MKR 1010. With WifiNina librairie it sends a ssl request and it triggers perfectly the webhook.

I would like to send a variable to be able to use it later. In my case the température of a sensor. It should look like that : https://hook.eu1.make.com/{mykey}?variable but I don't know the synthax of adding the varible at the end in my case.

He is the isolate code of the html request:

Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  
  if (client.connect(server, 443)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    client.println("GET /mbvbogobiXXXXXXXXXXwu6oz HTTP/1.1");
    client.println("Host: hook.eu1.make.com");
    client.println("Connection: close");
    client.println();
    
     // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting from server.");
    client.stop();

    // do nothing forevermore:
  }
  

}

And here is my entire code:

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled 2"
  https://create.arduino.cc/cloud/things/b632f186-68af-4a8b-be8e-8684bbd3bc6f 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  CloudTemperatureSensor temp_fermenter1;
  CloudTemperatureSensor temp_TAL;
  int tAL_temp_alert;
  bool tAL_alert_status;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#include <SPI.h>
#include <WiFiNINA.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is connected to GPI2
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

DeviceAddress sensor1 = { 0x28, 0xCA, 0x6, 0x49, 0xF6, 0x55, 0x3C, 0x68 };
DeviceAddress sensor2 = { 0x28, 0x3E, 0x6E, 0x49, 0xF6, 0x1F, 0x3C, 0x95 };


///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_OPTIONAL_PASS;    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key index number (needed only for WEP)

int temp_time_mark = millis(); //variable to store a millis
int temp_check_time = 1000; // Number of millisecond betzeen two temp check




int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "hook.eu1.make.com";   

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiSSLClient client;

void setup() {
  

  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  
  
  
  
  

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 5 seconds for connection:
    delay(5000);
  }
  Serial.println("Connected to WiFi");
  printWiFiStatus();

  
  
  
}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  
   if(millis()- temp_time_mark>=temp_check_time){
    
    
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  
  Serial.print("Sensor 1(*C): ");
  Serial.println(sensors.getTempC(sensor1)); 
 
  temp_TAL = sensors.getTempC(sensor1);
  Serial.println("Température d'alerte: "); 
  Serial.println(tAL_temp_alert); 
  
  temp_fermenter1 = sensors.getTempC(sensor2);
  Serial.print("Sensor 2(*C): ");
  Serial.println(sensors.getTempC(sensor2)); 
 
  
  if (temp_TAL >= tAL_temp_alert){
    tAL_alert_status = false;
    Serial.print("temp_TAL: ");
    Serial.println(temp_TAL);
    Serial.print("Envoi de la notification Push...");
    push_notif();
  }
  else{
    tAL_alert_status = true;
  }
    
    
    temp_time_mark=millis(); //reseting the time mark
   }
  
  
  

  
}


void printWiFiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void push_notif(){
  // if there are incoming bytes available
  // from the server, read them and print them:
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

 
  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  
  if (client.connect(server, 443)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    client.println("GET /mbvbogobiXXXXXXXXwu6oz HTTP/1.1");
    client.println("Host: hook.eu1.make.com");
    client.println("Connection: close");
    client.println();
    
     // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting from server.");
    client.stop();

    // do nothing forevermore:
  }
  

}

}


/*
  Since TALTempAlert is READ_WRITE variable, onTALTempAlertChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onTALTempAlertChange()  {
  // Add your code here to act upon TALTempAlert change
}

/*
  Since TALAlertStatus is READ_WRITE variable, onTALAlertStatusChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onTALAlertStatusChange()  {

}



This is where you build the URL (edited to hide your personal key)

you need to do

    client.println("GET /mbvbogobiXXXXXXXXXXwu6oz?temp=42 HTTP/1.1");

for example if you wanted to pass 42 for the temp attribute in the URL

you need to make sure of course that your web hook expects such a request and of course you need to build the URL dynamically to inject the real value

Hi thanks for your answer. The problem with this code is the URL will always send 42. And I want to send the value of the variable "temp" in your example.

yes, as I wrote

As you are on a MKR 1010 there are limited risks of using the String class so you would do

String getRequest = "GET /mbvbogobiXXXXXXXXXXwu6oz?temp=";
getRequest += String(tempValue, 2); // if tempValue is a float and you want 2 digits
getRequest +=  " HTTP/1.1";
client.println(getRequest);

otherwise with a cString you could use sprintf() and a buffer of the appropriate size

That was it thank yo uvery much !

here is the working code for people that would need it:

void push_notif(){
  // if there are incoming bytes available
  // from the server, read them and print them:
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

 
  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  
  if (client.connect(server, 443)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    String getRequest = "GET /mbvbxxxxxxhiddenxxxxxozc?temp_TAL=";
    getRequest += String(temp_TAL, 2); // if tempValue is a float and you want 2 digits
    getRequest +=  " HTTP/1.1";
    Serial.println("Le getRequest est: ");
    Serial.println(getRequest);
    client.println(getRequest);
    
    client.println("Host: hook.eu1.make.com");
    client.println("Connection: close");
    client.println();
    
    /* requette html sans variable
    
    client.println("GET /mbxxxxxhiddenxxxxxx HTTP/1.1");
    client.println("Host: hook.eu1.make.com");
    client.println("Connection: close");
    client.println();
    */
    
     // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting from server.");
    client.stop();

    // do nothing forevermore:
  }
  

glad it helped. have fun

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.