ESP 32 Programming Problem

I am using ESP 32 with 5 Volt realays. By all relays turn on when i switch on the board and after 1 or 2 mins esp 32 its self. I want to turn off all relays in start. The error message is as under.

Connectingifi: Office

Brownout detector was triggered

ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:952
load:0x40078000,len:6084
load:0x40080000,len:7936
entry 0x40080310

alexa_WITH_8_REALAY_WORKING.ino (8.14 KB)

"Brownout detector was triggered" means the voltage to the ESP32 dropped below some minimum value, causing a reset. This is a feature designed to improve reliability. When the microcontroller is run at below the rated voltage, it could lock up, so it's better to reset in this case.

The cause of the low voltage may be that the relay coils in addition to the ESP32 are drawing more current than your power supply can provide.

My relays are on when i power up the Esp. How can i turn my relay off when start up the esp.

Please post your full sketch. If possible, you should always post code directly in the forum thread as text using code tags (</> button on the toolbar). This will make it easy for anyone to look at it, which will increase the likelihood of you getting help. If the sketch is longer than the forum will allow then it's OK to add it as an attachment. After clicking the "Reply" button, you will see an "Attachments and other settings" link.

Please always do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

When your code requires a library that's not included with the Arduino IDE please post a link (using the chain links icon on the toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries in the Arduino IDE or Libraries > Library Manager in the Arduino Web Editor) then say so and state the full name of the library.

1 Like

My all relay are on when I power on the ESP how i power off relay in start up.

int device1 = 19;
int device2 = 18;   
int device3 = 5;  
int device4 = 17;     
int device5 = 16;     
int device6 = 4;    
int device7 = 2;     
int device8 = 15;
  
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <WebSocketsClient.h> // get it from https://github.com/Links2004/arduinoWebSockets/releases 
#include <ArduinoJson.h> // get it from https://arduinojson.org/ or install via Arduino library manager
#include <StreamString.h>

WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
WiFiClient client;

#define MyApiKey "34af406e-6244-4663-997c-34b5f56ddb8a" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
#define MySSID "Office" // TODO: Change to your Wifi network SSID
#define MyWifiPassword "Shift321" // TODO: Change to your Wifi network password

#define HEARTBEAT_INTERVAL 300000 // 5 Minutes

uint64_t heartbeatTimestamp = 0;
bool isConnected = false;

void setPowerStateOnServer(String deviceId, String value);
void setTargetTemperatureOnServer(String deviceId, String value, String scale);

// deviceId is the ID assgined to your smart-home-device in sinric.com dashboard. Copy it from dashboard and paste it here

void turnOn(String deviceId) {
 if (deviceId == "5c35ed05010d8c32b5565580") // Device ID of device 1
 { 
 Serial.print("Turnevice id: ");
 Serial.println(deviceId);
 digitalWrite(device1, LOW);
 } 
 else if (deviceId == "5c35ed93010d8c32b5565588") // Device ID of device 2
 { 
 Serial.print("Turnevice id: ");
 Serial.println(deviceId);
 digitalWrite(device2, LOW);
 }
 else if (deviceId == "5c35ee0e010d8c32b556558b") // Device ID of device 3
 { 
 Serial.print("Turnevice id: ");
 Serial.println(deviceId);
 digitalWrite(device3, LOW);
 } 
 else if (deviceId == "5c35ee59010d8c32b556558d") // Device ID of device 4
 { 
 Serial.print("Turnevice id: ");
 Serial.println(deviceId);
 digitalWrite(device4, LOW);
 }
 else if (deviceId == "5c36e5b304018c4adb252073") // Device ID of device 5
 { 
 Serial.print("Turnevice id: ");
 Serial.println(deviceId);
 digitalWrite(device5, LOW);
 }
 else if (deviceId == "5c36e5be04018c4adb252075") // Device ID of device 6
 { 
 Serial.print("Turnevice id: ");
 Serial.println(deviceId);
 digitalWrite(device6, LOW);
 }
 else if (deviceId == "5c36e5c804018c4adb252077") // Device ID of device 7
 { 
 Serial.print("Turnevice id: ");
 Serial.println(deviceId);
 digitalWrite(device7, LOW);
 }
 else if (deviceId == "5c36e5d204018c4adb252079") // Device ID of device 8
 { 
 Serial.print("Turnevice id: ");
 Serial.println(deviceId);
 digitalWrite(device8, LOW);
 }
 else 
 {
 Serial.print("Turnor unknown device id: ");
 Serial.println(deviceId); 
 } 
}

void turnOff(String deviceId) {
 if (deviceId == "5c35ed05010d8c32b5565580") // Device ID of device 1
 { 
 Serial.print("TurnDevice ID: ");
 Serial.println(deviceId);
 digitalWrite(device1, HIGH);
 }
 else if (deviceId == "5c35ed93010d8c32b5565588") // Device ID of device 2
 { 
 Serial.print("TurnDevice ID: ");
 Serial.println(deviceId);
 digitalWrite(device2, HIGH);
 }
 else if (deviceId == "5c35ee0e010d8c32b556558b") // Device ID of device 3
 { 
 Serial.print("TurnDevice ID: ");
 Serial.println(deviceId);
 digitalWrite(device3, HIGH);
 }
 else if (deviceId == "5c35ee59010d8c32b556558d") // Device ID of device 4
 { 
 Serial.print("TurnDevice ID: ");
 Serial.println(deviceId);
 digitalWrite(device4, HIGH);
 }
  else if (deviceId == "5c36e5b304018c4adb252073") // Device ID of device 5
 { 
 Serial.print("TurnDevice ID: ");
 Serial.println(deviceId);
 digitalWrite(device5, HIGH);
 }
  else if (deviceId == "5c36e5be04018c4adb252075") // Device ID of device 6
 { 
 Serial.print("TurnDevice ID: ");
 Serial.println(deviceId);
 digitalWrite(device6, HIGH);
 }
  else if (deviceId == "5c36e5c804018c4adb252077") // Device ID of device 7
 { 
 Serial.print("TurnDevice ID: ");
 Serial.println(deviceId);
 digitalWrite(device7, HIGH);
 }
  else if (deviceId == "5c36e5d204018c4adb252079") // Device ID of device 8
 { 
 Serial.print("TurnDevice ID: ");
 Serial.println(deviceId);
 digitalWrite(device8, HIGH);
 }
 else 
 {
 Serial.print("Turnfor unknown device id: ");
 Serial.println(deviceId); 
 }
}

void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
 switch(type) {
 case WStype_DISCONNECTED:
 isConnected = false; 
 Serial.printf("[WSc] Webservice disconnected from sinric.com!\n");
 break;
 case WStype_CONNECTED: {
 isConnected = true;
 Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload);
 Serial.printf("Waiting for commands from sinric.com ...\n"); 
 }
 break;
 case WStype_TEXT: {
 Serial.printf("[WSc] get text: %s\n", payload);
 // Example payloads

// For Switch or Light device types
 // {"deviceId": xxxx, "action": "setPowerState", value: "ON"} // https://developer.amazon.com/docs/device-apis/alexa-powercontroller.html

// For Light device type
 // Look at the light example in github
 
 DynamicJsonBuffer jsonBuffer;
 JsonObject& json = jsonBuffer.parseObject((char*)payload); 
 String deviceId = json ["deviceId"]; 
 String action = json ["action"];
 
 if(action == "setPowerState") { // Switch or Light
 String value = json ["value"];
 if(value == "ON") {
 turnOn(deviceId);
 } else {
 turnOff(deviceId);
 }
 }
 else if (action == "SetTargetTemperature") {
 String deviceId = json ["deviceId"]; 
 String action = json ["action"];
 String value = json ["value"];
 }
 else if (action == "test") {
 Serial.println("[WSc] received test command from sinric.com");
 }
 }
 break;
 case WStype_BIN:
 Serial.printf("[WSc] get binary length: %u\n", length);
 break;
 }
}

void setup() 
{
 pinMode (device1, OUTPUT);
 pinMode (device2, OUTPUT);
 pinMode (device3, OUTPUT);
 pinMode (device4, OUTPUT);
 pinMode (device5, OUTPUT);
 pinMode (device6, OUTPUT);
 pinMode (device7, OUTPUT);
 pinMode (device8, OUTPUT);




Serial.begin(115200);
 
 WiFiMulti.addAP(MySSID, MyWifiPassword);
 Serial.println();
 Serial.print("Connectingifi: ");
 Serial.println(MySSID);

// Waiting for Wifi connect
 while(WiFiMulti.run() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
 }
 if(WiFiMulti.run() == WL_CONNECTED) {
 Serial.println("");
 Serial.print("WiFiected. ");
 Serial.print("IPess: ");
 Serial.println(WiFi.localIP());
 }

// server address, port and URL
 webSocket.begin("iot.sinric.com",80,"/");

// event handler
 webSocket.onEvent(webSocketEvent);
 webSocket.setAuthorization("apikey", MyApiKey);
 
 // try again every 5000ms if connection has failed
 webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
}

void loop() {
 
 webSocket.loop();
 
 if(isConnected) {
 uint64_t now = millis();
 
 // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
 if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
 heartbeatTimestamp = now;
 webSocket.sendTXT("H"); 
 }
 } 
}

// If you are going to use a push button to on/off the switch manually, use this function to update the status on the server
// so it will reflect on Alexa app.
// eg: setPowerStateOnServer("deviceid", "ON")
void setPowerStateOnServer(String deviceId, String value) {
 DynamicJsonBuffer jsonBuffer;
 JsonObject& root = jsonBuffer.createObject();
 root["deviceId"] = deviceId;
 root["action"] = "setPowerState";
 root["value"] = value;
 StreamString databuf;
 root.printTo(databuf);
 
 webSocket.sendTXT(databuf);
}

//eg: setPowerStateOnServer("deviceid", "CELSIUS", "25.0")
void setTargetTemperatureOnServer(String deviceId, String value, String scale) {
 DynamicJsonBuffer jsonBuffer;
 JsonObject& root = jsonBuffer.createObject();
 root["action"] = "SetTargetTemperature";
 root["deviceId"] = deviceId;
 
 JsonObject& valueObj = root.createNestedObject("value");
 JsonObject& targetSetpoint = valueObj.createNestedObject("targetSetpoint");
 targetSetpoint["value"] = value;
 targetSetpoint["scale"] = scale;
 
 StreamString databuf;
 root.printTo(databuf);
 
 webSocket.sendTXT(databuf);
}

I guess your relays are device1 to device8 ?

Then in the setup, after the lines

void setup() 
{
 pinMode (device1, OUTPUT);
 pinMode (device2, OUTPUT);
 pinMode (device3, OUTPUT);
 pinMode (device4, OUTPUT);
 pinMode (device5, OUTPUT);
 pinMode (device6, OUTPUT);
 pinMode (device7, OUTPUT);
 pinMode (device8, OUTPUT);

add the lines

digitalWrite(device1, LOW);
digitalWrite(device2, LOW);
digitalWrite(device3, LOW);
digitalWrite(device4, LOW);
digitalWrite(device5, LOW);
digitalWrite(device6, LOW);
digitalWrite(device7, LOW);
digitalWrite(device8, LOW);

I have added the lines but nothing happened. Its stays on . I want all my relay in off status when turn on the esp32

void setup() 
{
 pinMode(device1, OUTPUT);
 digitalWrite(device1, LOW);
 pinMode(device2, OUTPUT);
 digitalWrite(device2, LOW);
 pinMode(device3, OUTPUT);
 digitalWrite(device3, LOW);
 pinMode(device4, OUTPUT);
 digitalWrite(device4, LOW);
 pinMode(device5, OUTPUT);
 digitalWrite(device5, LOW);
 pinMode(device6, OUTPUT);
 digitalWrite(device6, LOW);
 pinMode(device7, OUTPUT);
 digitalWrite(device7, LOW);
 pinMode(device8, OUTPUT);
 digitalWrite(device8, LOW);

The common relay modules are active LOW. So if you're using one of those, you need to use digitalWrite(device1, HIGH);

Thanks it works now. Now i have to deal with the power. I had connected esp with the USB cable of 5 V. When I had turn on all my relays . All the relays turn on and then The board reset it self. Any solution for this. And it keep on resetting again and again. Any solution for this.

It's not a bad idea to use a separate power supply for the relay coils. That will help to isolate the ESP32 from the higher voltage you're probably switching with them. The common relay modules are set up for this. There is typically a jumper marked "JD-VCC" you can remove and then power the coils from that.

Talha909:
Thanks it works now. Now i have to deal with the power. I had connected esp with the USB cable of 5 V. When I had turn on all my relays . All the relays turn on and then The board reset it self.

Are you powering the relay from sub as well ? 8 relays ? be careful not to blow your USB port (probably has an over-current protection anyway)

Thanks all the guys. I plug it with high power adapter and its work great.

One last thing. I have connected the ESP with the powder adapter and give power to relay from Vin Pin and Ground pin. Every thing is working fine. If i try to upload the code while connection vin pin or ground pin. its gave me error. When i unplug these pins then i am able to upload the code. This is the good way or there is any way o upload the code while plugging all the pins.

It's not clear you're asking. Please provide more details about your circuit connections.

I connect ESP and relay module. below the pin detail. Now I power plug usb and relays works fine with withe alexa. If i want to change the SSID and Password. I have to upload the code again. Then i try to upload the while connected all the cables its gives me the error message. But when i unplug the wire 5v and GNG from the relay. Then i can upload the code easily. Please tell me how can I upload the code while connected all the wires.

Relay Vcc connted to V5 of Esp
Relay GND connected to GND of ESP
Relay 1 Pin Connected to ESP Pin 19
Relay 2 Pin Connected to ESP Pin 18
Relay 3 Pin Connected to ESP Pin 5
Relay 4 Pin Connected to ESP Pin 17
Relay 5 Pin Connected to ESP Pin 16
Relay 6 Pin Connected to ESP Pin4
Relay 7 Pin Connected to ESP Pin 2
Relay 8 Pin Connected to ESP Pin 15

while connected all the cables its gives me the error message.

please tell us the error msg.
according to

GPIO2 must also be either left unconnected/floating, or driven Low, in order to enter the serial bootloader.

so changing the relay connection on pin 2 may have to change.

yes you are right. I have converted the pin 2 to 23 and its ok now.