Send RangeSlider values to the device, to trigger a command

dashboard
workflow
#1

I’m trying to create a temperature controller, where I choose the temperature in the RangeSlider and the “Send” button I send the command to the device. I’m getting the RangeSlider value to appear in the workflow in “slider0”, but I’m not sure how to get that value and send it to the device. The structure would be: slider0 = Temperature off. tempC = Temperature in degrees celsius. if slider0 <= tempC {digitalWrite (Relaypin, LOW)} else if {digitalWrite (Relaypin, HIGH)}. The problem is that I do not know what function I put in the code to write this command on the device. Can someone help me?

0 Likes

#2

Hello @Luiz_Carlos_de_Azeve,

Just a couple questions so I can further assist! :grinning:

  1. What kind of device are you using?
  2. Which protocol does the device support?

Thanks,
Julia

0 Likes

#3

Hi, @JuliaKempf

I’m using the ESP8266.

0 Likes

#4

Hi @Luiz_Carlos_de_Azeve,

Writing to a device can be done with a Device Command Output Node. I would recommend replacing your Device: Update Node with a Device Command Node, you can read more about them here. Then, you will need to include some code on the ESP8266 to handle the device command, here is an example:

// Called whenever the device receives a command from the Losant Platform.
void handleCommand(LosantCommand *command) {
  Serial.print("Command received: ");
  Serial.println(command->name);
  if(strcmp(command->name, "turn-on-led") == 0) {
    digitalWrite(LED_PIN, 1);
  }
}

// Subscribe to commands.
device.onCommand(&handleCommand);

The ESP8266 code with the Device Command Node will allow for data to be sent to your device from the workflow.

Hopefully this helps,
Julia

0 Likes

#5

Good Morning @JuliaKempf,

I did what you said, but without success, it looks like the conditional does not get the value of slider0, I tested each one individually putting {{data.temC}}> 20 to send the “off” command to the led and it worked, {data.slider0}}> 20 to turn on the led and did not work, it looks like the conditional does not get the value. I put {{data.tempC}}> {{data.slider0}} to turn on the led, that also did not work. I’m trying to compare the temperature and slider0 values ​​so that depending on the value the conditionals send the commands. I do not know what is happening. I’ve been trying for some time, I thought it was simple, but I realize it’s being very difficult.

0 Likes

#6

Hello @Luiz_Carlos_de_Azeve,

I think I see what could be the problem here, but let me know if this is not where you were heading! It seems like you are trying to get the last device state to compare to your slider value that is coming from the dashboard. If this is the case, you can remove that Device: State Trigger and replace it with a Data: Time Series node. This will allow you to get your last state value and add it to the payload.

Let me know if anything is unclear!
Julia

0 Likes

#7

I was able to work with your tip, :smiley: now I can control the temperature, turning it on and off when I choose a temperature and send value, there is only one problem and this is the last one, for example, the ambient temperature is 30ºC, I temperature to 20ºC, beauty, it works normal, but when the temperature reaches 20ºC the cooling does not turn off automatically, I need to manually regulate again. Is there a way to make it work automatically, as if it were all the time comparing the temperatures and when the temperature stays above 20ºC the cooling turns on automatically and when the temperature reaches 20ºC it automatically shuts off without me having to stay the all the time manually sending the value of the slider to the workflow?

0 Likes

#8

Hi @Luiz_Carlos_de_Azeve,

You will need to see if the cooling feature can be triggered on and off with a command, and if that value is available to you from your device (such as cooling=true). If it is available to you, or you are able to trigger it off and on, it would be easy to add a workflow that would check for you and is able to compare time. You could begin with a Device: State trigger that leads to a conditional to check if the temperature is at (or below) 20 degrees celsius and then check if the cooling is on or not. Then based on this, you could toggle the cooling on or off and send that command to your device.

Hope this helps!
Julia

0 Likes

#9

Good morning Julia, I could not do this. I was thinking, is there any way to assign the value of slider0 to a variable in the device’s algorithm? If so, I can create a comparison structure within the firmware itself. This way, every time the slider0 value is changed, it will be sent to the device, which will be careful to check if the temperature is above or below the value of slider0 and depending on the value of slider0 it will turn on or off the cooling. It is possible? Thank you, a hug.

0 Likes

#10

Help, please. I need to solve this problem.

0 Likes

#11

Hi @Luiz_Carlos_de_Azeve,

I’m going to need a bit more information to help further with this, and apologize for the barrage of questions :smile:

What is being read by the device? What can the device report? What variables are reported by the thermostat?

If possible, could you post your ESP8266 code?

Thanks!
Julia

0 Likes

#12

Hi @JuliaKempf,

The device reads temperature.

Only reports temperature.

The variables are tempC and Humidity,

    // Example testing sketch for various DHT humidity/temperature sensors
    // Written by ladyada, public domain

    // REQUIRES the following Arduino libraries:
    // - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
    // - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor

    #include "DHT.h"

    #define DHTPIN 0     // Digital pin connected to the DHT sensor
    // Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
    // Pin 15 can work but DHT must be disconnected during program upload.

    // Uncomment whatever type you're using!
    //#define DHTTYPE DHT11   // DHT 11
    #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
    //#define DHTTYPE DHT21   // DHT 21 (AM2301)

    // Connect pin 1 (on the left) of the sensor to +5V
    // NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
    // to 3.3V instead of 5V!
    // Connect pin 2 of the sensor to whatever your DHTPIN is
    // Connect pin 4 (on the right) of the sensor to GROUND
    // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

    // Initialize DHT sensor.
    // Note that older versions of this library took an optional third parameter to
    // tweak the timings for faster processors.  This parameter is no longer needed
    // as the current DHT reading algorithm adjusts itself to work on faster procs.
    DHT dht(DHTPIN, DHTTYPE);

    #include <ESP8266WiFi.h>
    #include <Losant.h>
    #define RELAY_PIN 2 //(THIS PIN IS RESPONSABLE TO TURN ON/OFF THE MACHINE) 
    #define REF_PIN 16 //(THIS PIN IS RESPONSABLE TO TURN ON/OFF THE REFRIGERATION)


    // WiFi credentials.
    const char* WIFI_SSID = "xxxxx";
    const char* WIFI_PASS = "xxxxxx";

    // Losant credentials.
    const char* LOSANT_DEVICE_ID = "xxxxxxx";
    const char* LOSANT_ACCESS_KEY = "xxxxxxxxx";
    const char* LOSANT_ACCESS_SECRET = "xxxxxxxxx";



    WiFiClient wifiClient;

    // For an unsecure connection to Losant.
    // WiFiClient wifiClient;

    LosantDevice device(LOSANT_DEVICE_ID);

//TURN ON/OFF THE MACHINE.
    // Called whenever the device receives a command from the Losant platform.
    void handleCommand(LosantCommand *command) {
      Serial.println();
      Serial.print("Command received: ");
      Serial.println(command->name);
      if (strcmp(command->name, "on") == 0) {
        digitalWrite(RELAY_PIN, HIGH);
        Serial.println("Relay on");
      } else if (strcmp(command->name, "off") == 0) {
        digitalWrite(RELAY_PIN, LOW);
        Serial.println("Relay off");
      }
//TURN ON/OFF THE REFRIGERATION
      Serial.println();
      Serial.print("Command received: ");
      Serial.println(command->name);
      if (strcmp(command->name, "onref") == 0) {
        digitalWrite(REF_PIN, HIGH);
        Serial.println("Relay on");
      } else if (strcmp(command->name, "offref") == 0) {
        digitalWrite(REF_PIN, LOW);
        Serial.println("Relay off");
      }

    }

    void connect() {

      // Connect to Wifi.
      Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(WIFI_SSID);

      WiFi.begin(WIFI_SSID, WIFI_PASS);

      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }

      Serial.println("");
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());

      // Connect to Losant.
      Serial.println();
      Serial.print("Connecting to Losant...");

      device.connect(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

      // For an unsecure connection.
      // device.connect(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

      while(!device.connected()) {
        delay(500);
        Serial.print(".");
      }

      Serial.println("Connected!");
    }

    void setup() {
      Serial.begin(115200);
      Serial.println(F("DHTxx test!"));

      dht.begin();

      while(!Serial) { }
      pinMode(RELAY_PIN, OUTPUT);  // default turned off
      digitalWrite(RELAY_PIN, LOW);
      pinMode(REF_PIN, OUTPUT);  // default turned off
      digitalWrite(REF_PIN, LOW);
      

      // Register the command handler to be called when a command is received
      // from the Losant platform.
      device.onCommand(&handleCommand);

      connect();
    }

    void report(double humidity, double tempC, double tempF, double heatIndexC, double heatIndexF) {
      StaticJsonBuffer<500> jsonBuffer;
      JsonObject& root = jsonBuffer.createObject();
      root["humidity"] = humidity;
      root["tempC"] = tempC;
      root["tempF"] = tempF;
      root["heatIndexC"] = heatIndexC;
      root["heatIndexF"] = heatIndexF;
      device.sendState(root);
      Serial.println("Reported!");
    }

    int timeSinceLastRead = 0;
      
    void loop() {
      // Wait a few seconds between measurements.
      
       bool toReconnect = false;

      if(WiFi.status() != WL_CONNECTED) {
        Serial.println("Disconnected from WiFi");
        toReconnect = true;
      }

      if(!device.connected()) {
        Serial.println("Disconnected from Losant");
        Serial.println(device.mqttClient.state());
        toReconnect = true;
      }

      if(toReconnect) {
        connect();
      }

      device.loop();

      // Report every 2 seconds.
      if(timeSinceLastRead > 2000) {
        // Reading temperature or humidity takes about 250 milliseconds!
        // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
        float h = dht.readHumidity();
        // Read temperature as Celsius (the default)
        float t = dht.readTemperature();
        // Read temperature as Fahrenheit (isFahrenheit = true)
        float f = dht.readTemperature(true);

        // Check if any reads failed and exit early (to try again).
        if (isnan(h) || isnan(t) || isnan(f)) {
          Serial.println("Failed to read from DHT sensor!");
          timeSinceLastRead = 0;
          return;
        }

        // Compute heat index in Fahrenheit (the default)
        float hif = dht.computeHeatIndex(f, h);
        // Compute heat index in Celsius (isFahreheit = false)
        float hic = dht.computeHeatIndex(t, h, false);

        Serial.print("Humidity: ");
        Serial.print(h);
        Serial.print(" %\t");
        Serial.print("Temperature: ");
        Serial.print(t);
        Serial.print(" *C ");
        Serial.print(f);
        Serial.print(" *F\t");
        Serial.print("Heat index: ");
        Serial.print(hic);
        Serial.print(" *C ");
        Serial.print(hif);
        Serial.println(" *F");
        report(h, t, f, hic, hif);

        timeSinceLastRead = 0;
      }
      delay(100);
      timeSinceLastRead += 100;
    }
0 Likes

#13

@JuliaKempf, can you help me?

0 Likes

#14

To do this you will need to utilize the Device Command Node and will need to look into Sending Commands to your device. The Device Command Node will allow you to use the slider0 value within the device code, and you can even have a workflow that sends the command only when the slider0 value changes.

Let me know how else I can help!
Julia

0 Likes