0

Activate pin on feed a certain value
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Activate pin on feed a certain value

by gbenna on Wed Dec 23, 2020 3:54 pm

I am working with adafruit and MQTT using Arduino IDE. I am posting values on the Adafruit dashboard from an esp8266-01 and I can see they are getting posted correctly.
I am subscribing to one of the feeds on an esp8266-12e and I am getting the correct values when they are updated.
My problem is that I want to activate a pin on the 12e when the value of the feed is 50.
I have tried this but I am not getting the pin to activate.

Code: Select all | TOGGLE FULL SIZE
Adafruit_MQTT_Subscribe trip2 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/trip");
Adafruit_MQTT_Subscribe errors = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/errors");
Adafruit_MQTT_Subscribe throttle = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/throttle");

Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(5000))) {
    if (subscription == &trip2) {
      Serial.print(F("Got onoff: "));
      Serial.println((char *)trip2.lastread);
      if ((char *)trip2.lastread == 50) {
        digitalWrite(D5, HIGH);
      }
    } else if(subscription == &errors) {
      Serial.print(F("ERROR: "));
      Serial.println((char *)errors.lastread);
    } else if(subscription == &throttle) {
      Serial.println((char *)throttle.lastread);
    }
  }


If I just put
Code: Select all | TOGGLE FULL SIZE
 if (subscription == &trip2) {
      Serial.print(F("Got onoff: "));
      Serial.println((char *)trip2.lastread);
     
        digitalWrite(D5, HIGH);
   
    }


the pin is activated and an LED comes on but not with the condition.
What am I doing wrong?
Please HELP.

gbenna
 
Posts: 6
Joined: Mon Sep 30, 2019 5:03 pm

Re: Activate pin on feed a certain value

by gbenna on Thu Dec 24, 2020 12:35 pm

I figured this out. Instead of using

if((char *)trip2.lastread == "0"){
digitalWrite(D5,HIGH); }

I changed it from a char to an int with

uint16_t sliderval = atoi((char *)trip2.lastread);

and then used my condition.

if (sliderval == 0){
digitalWrite(D5,HIGH); }

and now it works just fine.

gbenna
 
Posts: 6
Joined: Mon Sep 30, 2019 5:03 pm

Please be positive and constructive with your questions and comments.