Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By cschmol
#35269 Hello,

I want to use my esp8266-12 (with adapter board) to control lighting in my house.
I have therefore etched a PCB that includes a socket for the esp8266, a 74hc595 shift register (useful to provide 5V needed by the Relay board), an AMS1117 power supply and 2 buttons for RESET and flash programming.

On output pins of the shift register, I connect an 8-channel Relay board (including transistors, 74hc595 just provides 0/5V, relay board provides the current).

Everything works fine when I have no light attached.
But when I connect a light, it might go on once, and then the esp8266 will stop working with rst cause 4.
I have already added 1000uF to the 5V power supply, and 1uF across VCC and GND on esp8266, but I still always get that reset.
The power supply should not be the problem, it should provide more than 2A @5V.

Plese find here the schematic (not 100% accurate, I made some minor changes to errors I discovered after etching) and the source code.

Code: Select all#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino

//needed for library
#include <EEPROM.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* mqtt_server = "m20.cloudmqtt.com";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
char mask = 0;

const int _SER = 5;
const int _SCL = 14;
const int _SCK = 16;
const int _RCK = 12;
const int G   = 13;

void write_sr(char mask) {
   int i;
   digitalWrite(_RCK, 0);
   digitalWrite(_SCK, 0);

   for(i=0; i<8; i++) {
      digitalWrite(_SER, mask & (1<<i));
      digitalWrite(_SCK, 1);
      digitalWrite(_SCK, 0);
   }
   digitalWrite(_RCK, 1);
   digitalWrite(_RCK, 0);
}

void setup() {
    pinMode(_SER, OUTPUT);
    pinMode(_SCL, OUTPUT);
    pinMode(_SCK, OUTPUT);
    pinMode(_RCK, OUTPUT);
    pinMode(G, OUTPUT);

    digitalWrite(_SCL, 1);
    digitalWrite(G, 0);

    // put your setup code here, to run once:
    Serial.begin(115200);

    //WiFiManager
    //Local intialization. Once its business is done, there is no need to keep it around
    WiFiManager wifi;
    //reset saved settings
    //wifi.resetSettings();
   
    //set custom ip for portal
    //wifi.setAPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));

    //fetches ssid and pass from eeprom and tries to connect
    //if it does not connect it starts an access point with the specified name
    //here  "AutoConnectAP"
    //and goes into a blocking loop awaiting configuration
    wifi.autoConnect("AutoConnectAP");
    //or use this for auto generated name ESP + ChipID
    //wifi.autoConnect();

   
    //if you get here you have connected to the WiFi
    Serial.println("connected...yeey :)");

    client.setServer(mqtt_server, 13840);
    client.setCallback(callback);

    write_sr(0);
}

void callback(char* topic, byte* payload, unsigned int length) {
  //print the received message and topic
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  String t(topic);
  //Serial.print("Constructed a String object with the contents: ");
  //Serial.println(t);

  int pos = int(t.charAt(t.length()-1)) - '0';
  int v = ((char)payload[0] == '1') ? 1 : 0;

  Serial.print("Accessing port ");
  Serial.print(pos);
  Serial.print(" and setting to ");
  Serial.println(v);

  if(v==0) {
    mask |= (1<<pos);
    //mask = 255;
  } else {
    mask &= ~(1<<pos);
    //mask = 0;
  }

   write_sr(mask);

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client", "-----------", "-----------------")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outTopic", "hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
      client.subscribe("/node0/set/#");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}


void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  return;

  /*write_sr(255);
  Serial.println("ON");
  delay(1000);
  write_sr(0);
  Serial.println("OFF");
  delay(1000);*/
}


Image

Thank you for helping me find the error :)
User avatar
By Barnabybear
#35293 Hi, you problem has been reported a few times. SSRs appear to work fine (even mm away from the ESP), relays with contacts cause problems when switching mains voltages.
My best guess as to why is that AC is sinusoidal, so the point at which it can be switched is anything from 0V to +or- nominal voltage * 1.414. So with 240V the switching could take place at any point between -340V and +340V. However with an SSR, it is on until the signal is removed and the voltage crosses the zero point. As it never disconnects with any voltage present on the contacts (ok SSRs don’t have contacts but you know what I mean) there is no ark and no RF produced.
Your options:
A nice metal box (biscuit tin) to put the relays in, make sure it is earthed to the mains earthing or this will just defeat the point and you may die.
Use SSRs.
If you can’t do any of the above, but it’s a bit over the top.
Use an opto coupled device to detect the zero crossing point of the mains. Combine the ESP output to the shift register output enable, with the zero crossing detection (logic – and, or nand – whatever works) to ensure the relay is switched with the lowest possible voltage on its contacts. Depending on the switching time of your relays you may not be able to switch on the zero cross you detected and you may have to introduce a delay to switch on the next zero cross.
It’s a bit of a long way round, we used to do it years ago to keep RF to a minimum, it had a bonus that it increased the life of the contacts.
User avatar
By cschmol
#35302 Now that you mention it, I was able to find tons of results on the web related to esp8266 and relays :)
As of the proposed solutions:
1. SSR's are more expensive than relays (although the click would be gone :) )
2. Putting only the relays in a box should work, I am going to try that out when I have a metallic box.
3. I am going to do some research about the zero-crossing circuit, as i would need a small inexpensive one (I plan on deploying those modules everywhere in the house); maybe you know a fitting one?

One more solution has come to my mind (that is very specific to my application):
Write the software so that it deals with the constant resetting; the shift register is not resetting, and would hold the correct state of the lights, and in home automation, a small occasional delay when switching lights would be acceptable.
Here, mqtt's retain feature comes in handy, the module could fetch the current states of the lights once it is started again after reset.

In other forums, it was suggested to cut the reset pin off, as it works as antenna and picks up the before-mentioned switching RF frequencies.

Thank you for your fast reply, that helped me find the source of the problem :D
User avatar
By Barnabybear
#35305 Hi, Glad that helped - I work mostly with the ESP8266-01 which has the Vcc, Reset and CH_PD pins next to each other so I just solder them all together on the header pins and haven't had a problem since. Try shielding the relays with tin foil, just wrap the body of the relay BUT make sure you have 6mm gap between any pins and the foil. Anti-static shipping materials work aswell BUT again you need 6mm gap as they are conductive.
These are listed as 5V but I haven't had one yet thet doesn't switch fully at 3.3v. Only 2A but my house is full of LED bulbs, the only danger I have is that I don't have enough load the hold the SSR.
http://www.ebay.co.uk/itm/311379468003? ... EBIDX%3AIT