Hi,
After I upload my code, and perform a hard reset all my devices turn on. How do I set my esp32 power on defaults so that nothing turns on? Example; I have a 5 volt fan and a mp3 player that turn on when I power up the esp32 module. I do not want them on upon power up.
Thanks
Do you have the 5 volt fan and mp3 player wired to power on when power is applied? If so, then to get what you want you'll need to connect them into the circuit differently. Based upon how/when you want the devices to be energized..
How about a wiring diagram and a better explanation of what's going on and what you want to go on. How are you wanting to control the 5V fan and the MP3 player?
@bmellen6384, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project
See About the Installation & Troubleshooting category.
Hi Idahowalker,
Thank for reaching out to me. So what happens is when I plug in a fresh board with no code, there is no power applied to any of my devices. But then after I upload firmware and reset it, everything turns on as soon as there is power applied. I would like them not to turn on during powerup or a reset. Once I connect to internet, I'll read data from firebase and decide weather to turn on/off devices.
Below is a drawing and the code. Let me know your thoughts. You'll see in the code that I am connecting to firebase to send and receive data, and based on firebase data, the device will turn fan, mp3, and fire, on/off. All that is actually working pretty good. I just need the device to turn on with no power applied to fire, mp3, fan.
Board:
#include <esp_log.h>
#include
#include
#include <sys/time.h>
#include <BLEDevice.h>
#include <BLEAdvertisedDevice.h>
#include <BLEClient.h>
#include <BLEScan.h>
#include <BLEUtils.h>
#include <sdkconfig.h>
#include <WiFi.h>
#include <WebServer.h>
#include <WiFiManager.h> //GitHub - TheBrunez/WIFIMANAGER-ESP32: wifimanager-esp32
#include <SerialMP3Player.h> //Download SerialMP3Player(Jiang).zip
#include <FirebaseESP32.h> //GitHub - mobizt/HTTPClientESP32Ex: Custom version of ESP32 HTTPClient Library.
#include <FS.h> //this needs to be first, or it all crashes and burns...
#include <SPIFFS.h>
#include <Arduino.h>
#include "esp_system.h"
#include "BLEDevice.h"
#include "BLEScan.h"
#include <ArduinoJson.h>
#include <Time.h>
#include <TimeLib.h> //GitHub - PaulStoffregen/Time: Time library for Arduino
#include <BlynkSimpleEsp32.h> //GitHub - blynkkk/blynk-library: Blynk library for embedded hardware. Works with Arduino, ESP8266, Raspberry Pi, Intel Edison/Galileo, LinkIt ONE, Particle Core/Photon, Energia, ARM mbed, etc.
#define BLYNK_PRINT Serial // Defines the object that is used for printing
#define BLYNK_DEBUG // Optional, this enables more detailed prints
#define BLYNK_NO_BUILTIN // Disable built-in analog & digital pin operations
#define FIREBASE_HOST "fireplace-d81eb.firebaseio.com" //Do not include https:// in FIREBASE_HOST
#define FIREBASE_AUTH "HWzDSrP8WYfsYWrJAnluflxqwr4nPtVIUiZylv2k"
#define DEVICE_ID "Remote_Fireplace_1"
#define FAN_CTL 26
#define MP3_PWR_CTL 27
#define WIFI_STAT 13
#define FIRE_CTL 23
#define FAN_ON digitalWrite(FAN_CTL, HIGH);
#define FAN_OFF digitalWrite(FAN_CTL, LOW);
#define MP3_PWR_ON digitalWrite(MP3_PWR_CTL, HIGH);
#define MP3_PWR_OFF digitalWrite(MP3_PWR_CTL, LOW);
#define FIRE_ON digitalWrite(FIRE_CTL, HIGH);
#define FIRE_OFF digitalWrite(FIRE_CTL, LOW);
#define WIFI_LED_ON digitalWrite(WIFI_STAT, HIGH);
#define WIFI_LED_OFF digitalWrite(WIFI_STAT, LOW);
// ------------------------------------- Function Definition ---------------------------------------
// Pin Configuration of ESP32
void PIN_Init() {
pinMode(FAN_CTL, OUTPUT); // Initialize the FAN_CTL pin as an output
pinMode(MP3_PWR_CTL, OUTPUT); // Initialize the MP3_PWR_CTL pin as an output
pinMode(WIFI_STAT, OUTPUT); // Initialize the WIFI_STAT pin as an output
pinMode(FIRE_CTL, OUTPUT); // Initialize the FIRE_CTL pin as an output
}
// --------------------------------------------------------------------------------------------------
BlynkTimer timer_;
SerialMP3Player mp3;
WiFiManager wifiManager;
FirebaseData firebaseData;
static boolean connected = false, didItRun = false;
uint8_t* pData;
float temp;
float humi;
int n=0;
unsigned long startTime;
String path = "/Fireplace/" + String(DEVICE_ID);
int app_change, app_fan, app_fire, app_mp3, app_temp, app_volume;
int device_fan, device_fire, device_mp3;
int ble_check_cnt;
/* Defined in FreeRTOSConfig.h. */
extern void vConfigureTimerForRunTimeStats( void );
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() vConfigureTimerForRunTimeStats()
TaskHandle_t BLEtaskHandle = NULL;
extern "C" {
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
}
//#include <PubSubClient.h>
#define MQTT_SECURE false
#define UPDATE_TIME 60000
#define UPDATE_LIMIT 180000
static BLEUUID advertisedUUID("0000180f-0000-1000-8000-00805f9b34fb");
//static BLEUUID advertisedUUID("226c0000-6476-4566-7562-66734470666d");
// The remote service we wish to connect to.
static BLEUUID serviceUUID("226c0000-6476-4566-7562-66734470666d");
//static BLEUUID serviceUUID("180f-0000-1000-8000-00805f9b34fb");
// The characteristic of the remote service we are interested in.
static BLEUUID charUUID("226caa55-6476-4566-7562-66734470666d");
static BLEAddress* pServerAddresses[20];
static BLEAddress pServerAddress;
//BLEClient pClient;
static boolean doConnect = false;
static boolean connected_ = false;
//BLERemoteCharacteristic* pRemoteCharacteristic;
static int count=0;
static int current=0;
static bool scanning = true;
static double temperature;
static double humidity;
static unsigned long lastUpdate = 0;
//RTC retained values
RTC_NOINIT_ATTR float temp_rtc = 0.0;
RTC_NOINIT_ATTR float humi_rtc = 0.0;
RTC_NOINIT_ATTR static uint8_t FAN_CTL_ret = 0;
RTC_NOINIT_ATTR static uint8_t FIRE_CTL_ret = 0;
RTC_NOINIT_ATTR static uint8_t MP3_PWR_CTL_ret = 0;
//flag for saving data
bool shouldSaveConfig = false;
hw_timer_t * timer = NULL;
volatile SemaphoreHandle_t timerSemaphore;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
volatile uint32_t isrCounter = 0;
volatile uint32_t lastIsrAt = 0;
CODE TAGS!
Relays.
An opto isolated relay control board would do the trick.
When power is applied, the uController gets powered up and then, through code, decides which device it wants to power on or off.
// --------------------------------------------------------------------------------------------------
// Existing Firebase Path Check
void firebase_path_check(){
Serial.println("------------------------------------");
Serial.println("Path exist test...");
if (Firebase.pathExist(firebaseData, path))
{
Serial.println("Path " + path + " exists: Device is not first time to run!");
}
else
{
Serial.println("Path " + path + " is not exist, this is the first time that the device is running!");
}
Serial.println("------------------------------------");
Serial.println();
}
// --------------------------------------------------------------------------------------------------
// User Defined Firebase setInt Function
static void firebase_device_set(String str, int integer){
if (Firebase.setInt(firebaseData, path + str, (int) integer))
{
Serial.println("SET PASSED");
Serial.println("PATH: " + firebaseData.dataPath());
Serial.println("TYPE: " + firebaseData.dataType());
Serial.print("VALUE: ");
if (firebaseData.dataType() == "int")
Serial.println(firebaseData.intData());
else if (firebaseData.dataType() == "float")
Serial.println(firebaseData.floatData(), 5);
else if (firebaseData.dataType() == "double")
Serial.println(firebaseData.doubleData(), 9);
else if (firebaseData.dataType() == "boolean")
Serial.println(firebaseData.boolData() == 1 ? "true" : "false");
else if (firebaseData.dataType() == "string")
Serial.println(firebaseData.stringData());
else if (firebaseData.dataType() == "json")
//Serial.println(firebaseData.jsonData());
Serial.println("------------------------------------");
Serial.println();
}
else
{
Serial.println("SET FAILED");
Serial.println("REASON: " + firebaseData.errorReason());
Serial.println("------------------------------------");
Serial.println();
}
}
// --------------------------------------------------------------------------------------------------
// User Defined Firebase getInt Function
static int firebase_device_get(String str){
if (Firebase.getInt(firebaseData, path + str))
{
Serial.println("GET PASSED");
Serial.println("PATH: " + firebaseData.dataPath());
Serial.println("TYPE: " + firebaseData.dataType());
Serial.print("VALUE: ");
Serial.println(firebaseData.intData());
Serial.println("------------------------------------");
Serial.println();
return firebaseData.intData();
}
else
{
Serial.println("GET FAILED");
Serial.println("REASON: " + firebaseData.errorReason());
Serial.println("------------------------------------");
Serial.println();
return 9;
}
}
static void issue_update(){
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
delay(100);
firebase_path_check();
Serial.printf("Free heap(): %d\n", xPortGetFreeHeapSize() );
delay(100);
Get_Device_Value();
Serial.printf("Free heap(): %d\n", xPortGetFreeHeapSize() );
delay(100);
Set_Device();
Temp_Value_Update();
Serial.printf("Free heap(): %d\n", xPortGetFreeHeapSize() );
}
// --------------------------------------------------------------------------------------------------
// Device status update to Firebase
static void Device_Status_Update(){
Serial.println("Sending Device Status to Firebase!");
firebase_device_set("/Device/Fan_status", device_fan);
firebase_device_set("/Device/Fire_status", device_fire);
firebase_device_set("/Device/MP3_status", device_mp3);
}
// --------------------------------------------------------------------------------------------------
// Send temperature & humidity value to Firebase
static void Temp_Value_Update(){
Serial.println("Sending temperature and humidity value to Firebase!");
firebase_device_set("/Device/Temp_value", temp_rtc);
firebase_device_set("/Device/Humi_value", humi_rtc);
}
// --------------------------------------------------------------------------------------------------
// Get device setting value from Firebase
static void Get_Device_Value(){
Serial.println("Get device setting value from Firebase!");
temp = firebase_device_get("/Device/Temp_value");
app_mp3 = firebase_device_get("/App/mp3_status");
app_fan = firebase_device_get("/App/fan_status");
app_fire = firebase_device_get("/App/fire_status");
app_temp = firebase_device_get("/App/standard_temp");
app_volume = firebase_device_get("/App/volume_value");
firebase_device_get("/App/CMD_FAN");
}
// --------------------------------------------------------------------------------------------------
// Set device
static void Set_Device(){
Serial.println("Device Setting!");
// ------- FAN Setting ---------
if (app_fan == 1){
FAN_CTL_ret = 1;
Serial.printf(" \n\n FanCtl: %d \n\n", app_fan);
FAN_ON;
device_fan = 1;
}else if (app_fan == 0){
FAN_OFF;
FAN_CTL_ret = 0;
Serial.printf(" \n\n FanCtl: %d \n\n", app_fan);
device_fan = 0;
}
// ------- MP3 Setting ---------
if (app_mp3 == 1){
MP3_PWR_CTL_ret = 1;
MP3_PWR_ON;
delay(500);
mp3.play(1,app_volume);
device_mp3 = 1;
}else if (app_mp3 == 0){
MP3_PWR_CTL_ret = 0;
mp3.stop();
delay(100);
MP3_PWR_OFF;
device_mp3 = 0;
}
// ------- FIRE Setting ---------
if ((app_fire == 1) && (app_temp + 2 > temp)){
FIRE_ON;
device_fire = 1;
}else if ((app_fire == 0) || (app_temp + 2 <= temp)){
FIRE_OFF;
device_fire = 0;
}
delay(100);
Device_Status_Update();
firebase_device_set("/Set_changed", 0);
}
void IRAM_ATTR onTimer(){
// Increment the counter and set the time of ISR
portENTER_CRITICAL_ISR(&timerMux);
isrCounter++;
lastIsrAt = millis();
if(!didItRun){
Serial.println("--ISR Restart--");
ESP.restart();
}
didItRun = false;
portEXIT_CRITICAL_ISR(&timerMux);
// Give a semaphore that we can check in the loop
xSemaphoreGiveFromISR(timerSemaphore, NULL);
// It is safe to use digitalRead/Write here if you want to toggle an output
}
void IRAM_ATTR resetModule() {
ets_printf("REBOOT\n");
esp_restart();
}
class MyClientCallback : public BLEClientCallbacks {
void onConnect(BLEClient* pclient) {
Serial.println("on connect");
}
void onDisconnect(BLEClient* pclient) {
connected_ = false;
scanning = true;
Serial.println("onDisconnect");
}
};
// ####################################################
// ############### BLE FUNCTIONS ######################
// ####################################################
static void notifyCallback(
BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData,
size_t length,
bool isNotify) {
Serial.print("Notify callback for characteristic ");
Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
Serial.print(" of data length ");
Serial.println(length);
String data = String((char *)pData);
temperature = data.substring(2, 6).toDouble();
humidity = data.substring(9, 13).toDouble();
Serial.println(pBLERemoteCharacteristic->toString().c_str());
Serial.printf("Temp: %.1f / Humid: %.1f\n", temperature, humidity);
temp_rtc = temperature;
humi_rtc = humidity;
didItRun = true;
lastUpdate = millis();
// Disconnect from BT
Serial.printf("Free heap(): %d\n", xPortGetFreeHeapSize() );
if (xPortGetFreeHeapSize() <= 10000){
Serial.println("----CONTROLLED----RESTART------");
ESP.restart();
//resetModule();
}
connected_ = false;
}
bool connectToServer(BLEAddress *pAddress) {
if (pAddress->toString() == "4c:65:a8:d4:c3:5d") {
current++;
return false;
}
Serial.print("Forming a connection to ");
Serial.println(pAddress->toString().c_str());
BLEClient* pClient;
BLERemoteCharacteristic* pRemoteCharacteristic;
pClient = BLEDevice::createClient();
pClient->setClientCallbacks(new MyClientCallback());
Serial.println(" - Created client");
vTaskDelay(200);
// Connect to the remove BLE Server.
pClient->connect(*pAddress);
Serial.println(" - Connected to server");
vTaskDelay(200);
// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
vTaskDelay(200);
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID.toString().c_str());
return false;
}
Serial.println(" - Found our service");
How about putting the code you posted into code tags?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
