ESP8266 Witty Cloud with low-level trigger relay, Always ON

Hi All,
I’m trying out a simple thing. Using this ESP8266 Witty Wifi Cloud board to control a 1CH low level trigger relay, using Thinger app.
Here’s the code I wrote for the same:

#include <ESP8266WiFi.h>
#include <ThingerESP8266.h>    //THINGER.IO library

// Your WiFi credentials.
const char ssid[] = "xxxxxxxxxx";
const char pass[] = "yyyyyyyyy";
int led = 13;
int relay = 5;

ThingerESP8266 thing("myUser", "abcdefg", "xxxxyyyy");

void setup()
{
  // Debug console
  Serial.begin(9600);

  thing.add_wifi(ssid, pass);

  pinMode(led, OUTPUT);
  thing["LED"] << [](pson & in) {
    if (in.is_empty()) {
      in = (bool) digitalRead(led);
    }
    else {
      digitalWrite(led, in ? HIGH : LOW);
    }
  };

  pinMode(relay, OUTPUT);
  thing["RELAY"] << [](pson & in) {
    if (in.is_empty()) in = (bool) digitalRead(relay);
    else digitalWrite(relay, in ? HIGH : LOW);
  };

  // LDR resource
  thing["LUX"] >> outputValue(analogRead(A0));
}

void loop()
{
  thing.handle();
}

With this code, I can control the the LED On/Off, and I can see the value of LDR everytime I refresh on the mobile app. So I know the connection is proper. But the relay stays ON all the time, and doesn’t switch Off/On when switching from the App. (I’ve connected the relay VCC and GND to an external USB power, and the IN pin to the GPIO5 on the ESP8266 Witty Wifi board.)
Any ideas, on how to get the relay to work?

Update: Saw the documentation and modified the relay code as follows:

  pinMode(relay, OUTPUT);
  thing["RELAY"] << invertedDigitalPin(relay);

But no difference. The relay still stays ON. :frowning:

Remember that the ESP8266 works at 3.3v, are you using a logic level shifter? Take present that maybe the high level at 3.3V it isn´t enough to activate high level at 5v.

I think that sometime I did it with nodemcu, i mean connect relays directly to nodemcu pin, but it blow that particular pin, but with a logic level shifter it works flawlessly.

Yes, please, share information/picture about your relay. You will need to use a relay with a transistor as switch, or a relay with an optocoupler. The ESP8266 pins cannot drive enough current to activate the solenoid of a relay.

Best!

Ahh I see. I think you’re both right. This is the relay I’m using:

Odd, I’m using a NodeMCU at 3.3v and activating a similarly specked relay from a pin with zero problems.

pinMode(RELAYPIN1, OUTPUT);
digitalWrite(RELAYPIN1, HIGH);
thing[“relay1”] << invertedDigitalPin(RELAYPIN1);