ESP8266 relay (one) on Adafruit

Hi there,

i found this code on internet…when i upload it (through CH340) on esp-01 and then put esp-01 on ESP8266 then in adafruit with ON/OFF i can turn on/off only led on esp-01 but relay’s LED is constantly blue and no reaction…where is the problem?

thanks

/***************************************************
This is the code for the project DIY Sonoff using ESP8266

Original code is from
Adafruit MQTT Library ESP8266 Example
Code edited by
Sachin Soni
http://www.techiesms.com
****************************************************/
#include <ESP8266WiFi.h>
#include “Adafruit_MQTT.h”
#include “Adafruit_MQTT_Client.h”
#define relay 2

/************************* WiFi Access Point *********************************/

#define WLAN_SSID “…your SSID…”
#define WLAN_PASS “…your password…”

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER “io.adafruit.com
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME “…your AIO username (see https://accounts.adafruit.com)…”
#define AIO_KEY “…your AIO key…”

/************ Global State (you don’t need to change this!) ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or… use WiFiFlientSecure for SSL
//WiFiClientSecure client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

/****************************** Feeds ***************************************/

// Setup a feed called ‘photocell’ for publishing.
// Notice MQTT paths for AIO follow the form: /feeds/
//Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME “/feeds/photocell”);

// Setup a feed called ‘onoff’ for subscribing to changes.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME “/feeds/Sonoff_1”);

/*************************** Sketch Code ************************************/

// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();

void setup() {
Serial.begin(115200);
delay(10);

Serial.println(F(“Adafruit MQTT demo”));

// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);

WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();

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

pinMode(relay,OUTPUT);

// Setup MQTT subscription for onoff feed.
mqtt.subscribe(&onoffbutton);
}

uint32_t x=0;

void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();

// this is our ‘wait for incoming subscription packets’ busy subloop
// try to spend your time here

Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(1))) {
if (subscription == &onoffbutton) {
Serial.print(F("Got: "));
Serial.println((char *)onoffbutton.lastread);
uint16_t state = atoi((char *)onoffbutton.lastread);
digitalWrite(relay,state);
}
}

// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
/*
if(! mqtt.ping()) {
mqtt.disconnect();
}
*/
}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;

// Stop if already connected.
if (mqtt.connected()) {
return;
}

Serial.print("Connecting to MQTT… ");

uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println(“Retrying MQTT connection in 5 seconds…”);
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries–;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println(“MQTT Connected!”);
}

Well, it appears to half work if the esp8266 builtin led responds as required. Show your wiring diagram. The problem could also be there.

i am using CH340 to transfer code on esp-01 and then put it back on esp8266

i found on net other codes where is

define relay D2 instead #define relay 2 like it is in my code

but when i put D2 in line a got error 'D2' was not declared in this scope

do not know where is the problem

If the original code compiled and the led functioned as expected, then leave the code alone. You have already been asked to show a wiring diagram. Even an picture of a hand drawn diagram is OK.

wiring diagram is 5v on input and nothing else…esp-01 goes on esp8266
for programming using CH340
thats it…no arduino uno or any other board

neznamnista: wiring diagram is 5v on input and nothing else...esp-01 goes on esp8266 for programming using CH340 thats it...no arduino uno or any other board

Didn't you mention previously a relay ?

yes esp8266 relay with 1ch + esp-01 + CH 340

You have to be much clearer about the hardware components you are using. Include a wiring diagram (schematic) and links to the main parts. You have not made it clear how the relay (module) is connected to the esp01 or power supply.

and now just for @6v6gt who doesn’t understand what i am talking about

pictures of devices i have

1st picture esp8266 relay 1ch with esp-01

2nd picture ch 340 usb programmer

ch340usbprogrammer.jpg

OK. It is a combined module consisting of an ESP01, a relay, a relay driver (transistor) and a voltage regulator similar to this:

https://www.ebay.de/itm/ESP-01-01S-WIFI-WLAN-Relais-Board-Erweiterung-Shield-Arduino-ESP8266/253998592986

|500x436

It appears your sketch assumes GPIO2 is the pin used to drive the relay part. Here is a pinout of the ESP01. However, looking at the pictures, it appears that the relay is controlled by GPIO0.

You are powering the device with 5 volts ?

The next thing is to write a very simple sketch which just attempts to switch the relay on for a few seconds, then switch it off, just to see if the device itself works. Try both GIPO2 and GPIO0.

yes 5 volts input

then i used this code to try if relay is working and yes, its working, but dont know how to control it through Adafruit

#include <ESP8266WiFi.h>

const char* ssid = “YOUR SSID”; // fill in here your router or wifi SSID
const char* password = “YOUR PASSWORD”; // fill in here your router or wifi password
#define RELAY 0 // relay connected to GPIO0
WiFiServer server(80);

void setup()
{
Serial.begin(115200); // must be same baudrate with the Serial Monitor

pinMode(RELAY,OUTPUT);
digitalWrite(RELAY, LOW);

// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

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

// Start the server
server.begin();
Serial.println(“Server started”);

// Print the IP address
Serial.print(“Use this URL to connect: “);
Serial.print(“https://192.168.0.178/”);
Serial.print(WiFi.localIP());
Serial.println(”/”);

}

void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client)
{
return;
}

// Wait until the client sends some data
Serial.println(“new client”);
while(!client.available())
{
delay(1);
}

// Read the first line of the request
String request = client.readStringUntil(’\r’);
Serial.println(request);
client.flush();

// Match the request
int value = LOW;
if (request.indexOf("/RELAY=ON") != -1)
{
Serial.println(“RELAY=ON”);
digitalWrite(RELAY,LOW);
value = LOW;
}
if (request.indexOf("/RELAY=OFF") != -1)
{
Serial.println(“RELAY=OFF”);
digitalWrite(RELAY,HIGH);
value = HIGH;
}

// Return the response
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println(""); // this is a must
client.println("");
client.println("");
client.println(“ESP8266 RELAY Control”);
client.print("Relay is now: ");

if(value == HIGH)
{
client.print(“OFF”);
}
else
{
client.print(“ON”);
}
client.println("

“);
client.println(“Turn <a href=”/RELAY=OFF”>OFF RELAY
“);
client.println(“Turn <a href=”/RELAY=ON”>ON RELAY
“);
client.println(”");

delay(1);
Serial.println(“Client disonnected”);
Serial.println("");
}

Please use code tags when include sections of code in this forum so the code appears in a window.

If you mean that you want the first sketch which you posted to work with the relay, then I guess you have to change this statement:

#define relay 2

to:

#define relay 0

neznamnista: i found on net other codes where is

define relay D2 instead #define relay 2 like it is in my code

but when i put D2 in line a got error 'D2' was not declared in this scope

do not know where is the problem

The ESP8266 core matches Arduino pin numbers to the ESP8266 board's GPIO numbers. But some ESP8266 boards chose to print pin labels on the silkscreen of the boards that don't match the GPIO pin numbers (because they wanted the pin numbering to be consecutive). To deal with this, the variant files for those boards provide a series of macros with the D*n* name format to map the pin labels to GPIO numbers. For example, the WeMos D1 Mini's variant file has this:

static const uint8_t D0   = 16;
static const uint8_t D1   = 5;
static const uint8_t D2   = 4;
static const uint8_t D3   = 0;
static const uint8_t D4   = 2;
static const uint8_t D5   = 14;
static const uint8_t D6   = 12;
static const uint8_t D7   = 13;
static const uint8_t D8   = 15;

So you can see that the pin marked D0 on the WeMos D1 Mini is connected to GPIO16, and so on. This is confusing because the official Arduino cores just use integer literals for digital pin numbers, not D*n* pin names. To make things more confusing, not all ESP8266 boards have D*n* pin names defined. You are probably using the "Generic ESP8266 Module", which doesn't have D*n* pin names defined.

yes using Generic ESP8266 module and when i define D2 it tells me an error

‘D2’ was not declared in this scope

what i need to add to code that is working to communicate to adafruit

#include <ESP8266WiFi.h>
 
const char* ssid = "YOUR SSID"; // fill in here your router or wifi SSID
const char* password = "YOUR PASSWORD"; // fill in here your router or wifi password
 #define RELAY 0 // relay connected to  GPIO0
WiFiServer server(80);
 
void setup()
{
  Serial.begin(115200); // must be same baudrate with the Serial Monitor
 
  pinMode(RELAY,OUTPUT);
  digitalWrite(RELAY, LOW);
 
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Start the server
  server.begin();
  Serial.println("Server started");
 
  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("https://192.168.0.178/");
  Serial.print(WiFi.localIP());
  Serial.println("/");
 
}
 
void loop()
{
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client)
  {
    return;
  }
 
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available())
  {
    delay(1);
  }

  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
 
  // Match the request
  int value = LOW;
  if (request.indexOf("/RELAY=ON") != -1) 
  {
    Serial.println("RELAY=ON");
    digitalWrite(RELAY,LOW);
    value = LOW;
  }
  if (request.indexOf("/RELAY=OFF") != -1) 
  {
    Serial.println("RELAY=OFF");
    digitalWrite(RELAY,HIGH);
    value = HIGH;
  }
 
  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  this is a must
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.println("<head><title>ESP8266 RELAY Control</title></head>");
  client.print("Relay is now: ");
 
  if(value == HIGH)
  {
    client.print("OFF");
  }
  else
  {
    client.print("ON");
  }
  client.println("

");
  client.println("Turn <a href=\"/RELAY=OFF\">OFF</a> RELAY
");
  client.println("Turn <a href=\"/RELAY=ON\">ON</a> RELAY
");
    client.println("</html>");
 
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
}

You have said that the code in your first post appears to work (powers a led) but does not operate the relay.
Is that the code that you are using to “communicate to adafruit” ?

Have you tried the change suggested in post #11 in this thread to use the relay with that code ?

yes correct

but i also sent you in previous post code that turns on/off relay and maybe is easier to build it to work from adafruit