No Real Time control and android aplication working occasionally


#1

Hello, My project have 3 sensors, MQ-2, DHT11, PIR motion sensor, and the dashboard is monitoring the sensors perfectly. I connected relay module and servo motor and started to control them from dashboard but it doesnot seem realtime, after pressing the button it takes more than 20 seconds to perform the function over relay module and servo motor and most of the time it doesnt function as well. My internet speed is also fine more than 5Mbps.


#2

can you post the code you are using.


#3

I apologize for replying late. Actually I was not able to login in community.Here is the code which i am using.

//#define CAYENNE_DEBUG       // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266Shield.h>
#include <DHT.h>
#include <SimpleTimer.h>
#include <Servo.h>

// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

// Set ESP8266 Serial object. In this example we use the Serial1 hardware serial which is available on boards like the Arduino Mega.
#define EspSerial Serial1

ESP8266 wifi(&EspSerial);

#define PIR_SENSOR_PIN 5 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define LED_PIN 11
#define BUZZER_PIN 12
#define DHT_PIN  7
#define SMOKE_SENSOR_PIN A1
#define SOIL_SENSOR_PIN A3
#define RELAY_ACTUATOR_PIN 34
#define SERVO_ACTUATOR_PIN 4


#define PIR_VIRTUAL_CHANNEL V1
#define HUMIDITY_VIRTUAL_CHANNEL V2
#define TEMPERATURE_VIRTUAL_CHANNEL V3
#define SMOKE_VIRTUAL_CHANNEL V4
#define SOIL_VIRTUAL_CHANNEL V5
#define RELAY_VIRTUAL_CHANNEL V6
#define SERVO_VIRTUAL_CHANNEL 7

#define DHTTYPE DHT11
DHT dht(DHT_PIN, DHTTYPE);
Servo s1;


SimpleTimer timer;
int pos = 0;

void setup()
{
  pinMode(LED_PIN, OUTPUT);      // declare LED as output
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(SMOKE_SENSOR_PIN, INPUT);
  pinMode(RELAY_ACTUATOR_PIN, OUTPUT);
  s1.attach(SERVO_ACTUATOR_PIN);
 
  Serial.begin(9600);
  delay(10);
  EspSerial.begin(115200);
  delay(10);

  Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
  timer.setInterval(5000L, transmitData);

  dht.begin();
}

void loop()
{
  Cayenne.loop();
  if (digitalRead(PIR_SENSOR_PIN) == HIGH) {            // check if the input is HIGH
    digitalWrite(LED_PIN, HIGH);  // turn LED ON
    tone(BUZZER_PIN, 4000, 60000 );
    delay(10);
      }
      else
      {
     digitalWrite(LED_PIN, LOW);  // turn LED ON
     tone(BUZZER_PIN, 0, 4000);
     delay(10);
    }
  pirSensor();
  timer.run();

}


CAYENNE_OUT_DEFAULT()
{
  Cayenne.virtualWrite(0, millis());
}

CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
}

int previousState = -1;
int currentState = -1;
unsigned long previousMillis = 0;
void pirSensor()
{
  unsigned long currentMillis = millis();
  // Check sensor data every 250 milliseconds
  if (currentMillis - previousMillis >= 10) {
    // Check the sensor state and send data when it changes.
    currentState = digitalRead(PIR_SENSOR_PIN);
    if (currentState != previousState) {
      Cayenne.virtualWrite(V1, currentState, "motion", "d");
      previousState = currentState;
        previousMillis = currentMillis;
  }
}
}

void transmitData(){
  
  float t = dht.readTemperature();
  Cayenne.celsiusWrite(V2, t); //virtual pin

  float h = dht.readHumidity();
  Cayenne.virtualWrite(V3, h, "rel_hum", "p"); //virtual pin

  Cayenne.virtualWrite(V4, analogRead(SMOKE_SENSOR_PIN), "analog_sensor");
  
  Cayenne.virtualWrite(V5, analogRead(SOIL_SENSOR_PIN), "analog_sensor");
}

CAYENNE_IN(V6)
{
  Cayenne.syncAll();
  // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  if (getValue.asInt() == 0) {
    digitalWrite(RELAY_ACTUATOR_PIN, HIGH);
    delay(1);
  }
  else {
    digitalWrite(RELAY_ACTUATOR_PIN, LOW);
    delay(1);
  }
}

CAYENNE_IN(V7)
{
  // Determine angle to set the servo.
  int position = getValue.asDouble() * 180;
  // Move the servo to the specified position.
  s1.write(position);
}

#4

Sorry for the late reply. Are you still having an issue? I know there were some MQTT server problems around the time you posted. Your code seems fine.