[SOLVED] Data is not sent using interrupts

Hello everyone, I am working on a project in which I have deployed multiple sensors.
As those sensors trigger critical alarms, so I am handling them using interrupts.
But the problem is that the data is not to ubidots when I am using interrupts.
When I am sending data to ubidots without interrupts, it gives me the delay of 8-10 seconds, which is unacceptable…

My code is

#include<SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include "UbidotsMicroESP8266.h"


WiFiClient client;

//2nd sensor
String idvariable1 = "58ac711176254260e2fxxxxxx"; //heater temp
String token = "u94kZPbWwFPUlzexgQh1j0H2xxxxx";
String idvariable2 = "58ac70ff76254260exxxxx"; 

volatile int value2=5;
volatile int value1=1;


volatile int DIGITAL_SENSOR_PIN= 12;
volatile int LEDPIN= 15;     // The onboard LED
volatile int buttonState = 0;
volatile int buttonState1 = 0;
volatile int buttonState2 = 0;


//first sensor

volatile int switch_state; /* Holds the last digital value */
volatile int LightAnalogValue; /* Holds the last analog value */
volatile int sensorPin =14; // select the input pin for the LDR
volatile int sensorValue = 0; // variable to store the value coming from the sensor
volatile int led = 4; // Output pin for LED
volatile int buzzer = 4  ; // Output pin for Buzzer

//3rd sensor

volatile int LED = 5; // Use the onboard Uno LED
volatile int isObstaclePin = 13;  // This is our input pin
volatile int isObstacle = HIGH;  // HIGH MEANS NO OBSTACLE



void setup() {
Serial.begin(74800);

  WiFi.disconnect();
   WiFi.begin("ProjectIOT","B0BFDB81");
  while ((!(WiFi.status() == WL_CONNECTED))){
    delay(300);

  }
Serial.println("Wifi connected");

   attachInterrupt(13, pin_intru, CHANGE);
   attachInterrupt(12, pin_LDR, CHANGE);
   attachInterrupt(14, pin_fire, CHANGE);
   


  
// declare the ledPin and buzzer as an OUTPUT:
pinMode(led, OUTPUT);
pinMode(buzzer,OUTPUT);



//2nd sensor
 pinMode(LEDPIN, OUTPUT);
 



//3rd sensor

 pinMode(LED, OUTPUT);
  pinMode(isObstaclePin, INPUT);



}

void loop() {
  // put your main code here, to run repeatedly:



}



void pin_intru() 
{
  buttonState = digitalRead(isObstaclePin);
  buttonState= !buttonState;
  digitalWrite(LED, buttonState); 
   delay(1000); 
  Serial.println("OBSTACLE!!, OBSTACLE!!");
intruder();
}



void intruder(void)
{
  
   String var1 = "{\"value\": " + String(value1)+"}";
int      num1 = var1.length();
    
      if(client.connect("things.ubidots.com", 80))
     {
        Serial.println("connected to ubidots");
        delay(100);
        client.println("POST /api/v1.6/variables/"+idvariable1+"/values HTTP/1.1");
        client.println("Content-Type: application/json");
        client.println("Content-Length: "+String(num1));
        Serial.println(String(num1));
        client.println("X-Auth-Token: "+token);

        client.println("Host: things.ubidots.com\n");
        client.print(var1);
        Serial.print(var1+"\n");  
  }
  
  
  
  
  
  }


void pin_LDR()
{
buttonState1 = digitalRead(DIGITAL_SENSOR_PIN);
  digitalWrite(LEDPIN, buttonState1); 
   //delay(3000); 
  Serial.println("Bulb enlightened");
  
  
}

void pin_fire()
{
  buttonState2 = digitalRead(sensorPin);
  buttonState2= !buttonState2;
  digitalWrite(led, buttonState2);
   delay(1000);  
   Serial.println("Fire Detected");
    
   fire(); 

} 


void fire(void)
{
 String var = "{\"value\": " + String(value2)+"}";
   int   num = var.length();
  
   if (client.connect("things.ubidots.com", 80)) 
      {
        Serial.println("connected to ubidots");
       // delay(100);
        client.println("POST /api/v1.6/variables/"+idvariable2+"/values HTTP/1.1");
        client.println("Content-Type: application/json");
        client.println("Content-Length: "+String(num));
     //   Serial.println(String(num));
        client.println("X-Auth-Token: "+token);
        client.println("Host: things.ubidots.com\n");
        client.print(var);
        Serial.print(var+"\n");
  }
}


Hey @Rehanbabar17,

You should construct only one request if you want to send data faster (this depends of your device too), to print line by line in your client object has a computational cost and requires a wait time, so you should construct a Char array or a String with your payload and send it in one request. Keep in mind that if you construct a String type payload you must guarantee that your device should have enough memory available in your routine in order to avoid future crashes.

Here’s a sample request by constructing a Char which follow the one that you provided to us before:

  char* data = (char *) malloc(sizeof(char) * 700);
  
  sprintf(data, "POST /api/v1.6/variables/%s", idvariable1);
  sprintf(data, "%s/values HTTP/1.1\r\nContent-Type: application/json\r\n", data);
  sprintf(data, "%sContent-Length:%s\r\n", data, String(num1));
  sprintf(data, "%sX-Auth-Token: %s\r\nHost: things.ubidots.com\n%s", data, token, var1);
  
  if (client.connect("things.ubidots.com", 80)) {
        Serial.println(F("Connected to Ubidots"));
        client.println(data);
  } 

Alternatively you can use MQTT instead of HTTP which is faster and can give you best results.

Regards,
Maria C.

Thankyou so much Maria, I am doing my project using MQTT now and it is way better than HTTP…