Notifications
Clear all

Need programming help with an esp32 using android bluetooth control

12 Posts
3 Users
0 Likes
1,466 Views
(@cwalker1960)
Member
Joined: 3 years ago
Posts: 9
Topic starter  

I'm currently working on a personal project using an esp32 to control a (cheap ebay chinese version) four relay module. The relay pins pull low to energize each relay I'm using the "bluetooth electrictronics" app from the play store to control the relays. the code I have is working perfectly for what i need it to do but anytime the esp32 has to restart it pulls all the pins I am using low which energizes the relays. I'm not using this in a dangerous situation so there's no chance of hurting anyone (well maybe) but it would be a little more than inconvenient should I have a power failure while sleeping. More on this later. My question is , is there any thing I could add at the beginning of the code that would set all the pins I'm using to high that would still allow the rest of the code to function as it does?  I will post the code as soon as I figure out how and go into more detail about my project. Thanks in advance for any help or foresight someone may have to offer me on this matter.


   
Quote
(@cwalker1960)
Member
Joined: 3 years ago
Posts: 9
Topic starter  

Here is the code I'm using:

 

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
String res = "";

BluetoothSerial SerialBT;

void setup() {
Serial.begin(115200);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(14, OUTPUT);
pinMode(15,OUTPUT);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop()
{
while (!SerialBT.available()); // Wait Until anything is coming from bluetooth client

while (SerialBT.available()) // Read until the bluetooth client is sending.
{
char add = SerialBT.read();
res = res + add;
delay(1);
}

 

// Assigning Actions on particular conditions
if (res == "T")
{
Serial.println("Connection Established!!!");
}
if (res == "LUP")
{
Serial.println("LEFT UP");
digitalWrite(3, LOW);
digitalWrite(15, LOW);
}
if (res == "0")
{
Serial.println("OFF");
digitalWrite(3,HIGH);
digitalWrite(15,HIGH);
}
if (res == "LDN")
{
Serial.println("LEFT DOWN");
digitalWrite(3, LOW);
digitalWrite(14,LOW);
}
if (res == "0")
{
Serial.println("OFF");
digitalWrite(3,HIGH);
digitalWrite(14,HIGH);
}
if (res == "RUP")
{
Serial.println("RIGHT UP");
digitalWrite(5,LOW);
digitalWrite(15,LOW);
}
if (res == "0")
{
Serial.println("OFF");
digitalWrite(5, HIGH);
digitalWrite(15,HIGH);
}
if (res == "RDN")
{
Serial.println("RIGHT DOWN");
digitalWrite(5, LOW);
digitalWrite(14,LOW);
}
if (res == "0")
{
Serial.println("OFF");
digitalWrite(5,HIGH);
digitalWrite(14,HIGH);
}

res = ""; // clearing the string.
}

 

I need the code to set all the pins I'm using to HIGH upon restart regardless of a bluetooth controller being connected


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2531
 

@cwalker1960 

Your output pins mode is set but the pins values are never specified in the setup() section. Try setting them to HIGH in a digitalWrite() there for a start.

 

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@cwalker1960)
Member
Joined: 3 years ago
Posts: 9
Topic starter  

Now for the explanation I promised in the first post, after successfully releasing all the magic smoke I could get out of the circuit board on my sleep number air bed controller I decided it was time to put one of the esp32 boards I had to good use, (you know , something besides amusing myself with an led array that flashes my name in three colors). So inside this air mattress pump is a 110vac air compressor (about 1.5psi max) a left solenoid, a right solenoid and an exhaust solenoid, All 3 solenoids are 110vdc (because why not) and they will not open on 110 vac. I guess that was the reason for the three bridge rectifiers that I first let the smoke out of. Anyhow with some creative wiring, another single but larger bridge rectifier and the four relay module for arduino I was back in business. I'm quite sure my implementation of code to fire any given two relays simultaneously leaves a little to be desired but hey , it works for me, ( way open to better suggestions for this to). The problem only arises when the esp looses power and restarts with all 4 pins low. This leaves both the left and right solenoids open as well as the pump running and the exhaust solenoid valve open. since the exhaust solenoid is capable of dissipating air much quicker than the pump can produce it, it leaves me with very flat mattresses in short order which could lead to some seriously stiff back aches.


   
ReplyQuote
(@cwalker1960)
Member
Joined: 3 years ago
Posts: 9
Topic starter  

 "electrictronics" new word for the day now if I can just find the definition. That's prolly where my problem is.


   
ReplyQuote
(@cwalker1960)
Member
Joined: 3 years ago
Posts: 9
Topic starter  
Posted by: @will

@cwalker1960 

Your output pins mode is set but the pins values are never specified in the setup() section. Try setting them to HIGH in a digitalWrite() there for a start.

 

could you be a little more specific. LOL


   
ReplyQuote
(@cwalker1960)
Member
Joined: 3 years ago
Posts: 9
Topic starter  

like maybe just edit mine so I can load it, That way when I fry all this stuff it'll be your fault


   
ReplyQuote
(@cwalker1960)
Member
Joined: 3 years ago
Posts: 9
Topic starter  

Thanks Will that done it. 

BluetoothSerial SerialBT;

void setup() {
Serial.begin(115200);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(14, OUTPUT);
pinMode(15,OUTPUT);
digitalWrite(3,HIGH);
digitalWrite(5,HIGH);
digitalWrite(14,HIGH);
digitalWrite(15,HIGH);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}


   
ReplyQuote
(@cwalker1960)
Member
Joined: 3 years ago
Posts: 9
Topic starter  

I can appreciate that others don't always share my sense of humor. I'll try to keep it to myself hereout. I do thank you again Will for solving my problem . As you can probably already tell , I usually scour the web looking for some code that does at least similar to what I'm trying to achieve and modify it to the best of my ability to work for me. I wish I knew more about coding and programming because I have many ideas that I would like to see take shape but as I said in my introduction I'm far more mechanically minded than electronics and programming. Will, your help today was one I'll not soon forget, I had tried adding a few things to the code before asking on here and most failed to compile. It was a simple solution and very obvious once you pointed it out to me, it just eluded me until you did. 


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2531
 

@cwalker1960 

The forum is here to help people. If you have an idea or requirement and don't know to embody it in code, then ask here. Probably somebody will be able to answer your question. Be aware that the answer may be "no, you can't do that".

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1680
 

Hi @will and @cwalker1960,

  I haven't tried this with Arduino, so I can't be certain there are no 'unexpected' consequences, but with microcontrollers, it is generally better practice to set the output state BEFORE enabling the output.

As this circuit drives mechanical things (relays), which can only react slowly, it probably doesn't matter, but in another case, it might.

In the case of the code sample a few messages earlier, my recommendation is to put the four 'digitalwrites' BEFORE the four 'pinmodes'.

In addition, make sure each output pin is 'gently' pulled to the 'do nothing' state (high in your example), when the circuit powers up, before the pin is enabled to be an output. This might be achieved internally by a pull up resistor within the microcontroller (read the data sheet carefully) or by using an external resistor. This often happens 'by chance', as microcontroller manufacturers realise an internal pull up will often save a component (per pin) on the board, but it should be checked when designing a circuit to make sure.

..........

Techy Explanation .. Term "GPIO pin" below represents each of the 4 pins in above discussion.

The sequence of events at power up should be as follows (assuming 'high' is the 'do nothing' choice):

  1. Reset pin of microcontroller must be held in 'reset active' state as power is applied.
  2. Power applied to microcontroller - Microcontroller will power up with the pin as an input/do nothing type.
  3. GPIO Pin 'gently' pulled high by resistor (internal or external to microcontroller).
  4. Reset pin is 'released' from 'active reset' ... microcontroller will start 'processing' instructions.
  5. Set GPIO pin 'value' to high ... this prepares the microcontroller GPIO pin circuit, but GPIO pin output state unchanged (This equates to 'digitalwrite high' )
  6. set GPIO pin to output, this enables the 'high' set in the previous step to be 'actively driven' (this equates to 'pinmode out')

The danger of doing steps 5 and 6 in reverse order occurs if the internal state of the GPIO pin driver circuit (sometimes or always) defaults to a 'low'. Then the sequence for the GPIO pin can become:

  • Power up to output 'high' (As per steps 1 to 4, previously)
  • Output driven to low when the GPIO pin output is enabled (Equivalent to step 6, previously)
  • Output driven back to high (Equivalent to step 6, previously)

This, high-low-high pulse sequence will probably only be for a short period, say a few microseconds, which would be too short for a mechanical device like a relay to react to, but could easily trigger an electronic circuit.

----------

I hope this explanation is useful and helpful, possibly saving  a lot of 'debugging' time for someone, especially if they have only limited test gear available.

Best wishes to all.


   
ReplyQuote
(@cwalker1960)
Member
Joined: 3 years ago
Posts: 9
Topic starter  

Many thanks to Will And DaveE for all your help.

This is the final code I loaded to the board. Everything is working as expected. I did change the pins I was originally using as I found I would have to disconnect power to the relay module to be able to upload code using those pins. I'm not completely sure why that is but I don't have to disconnect the relay module using the new pins. and I know that it would never be a good idea to upload code to a board while it is connected to some device that may accidently start up but  1) it's just a pump for an air bed and 2) I can unplug the 110vac power the relays are controlling. I just wanted to be able to upload any new code i may have without having to open the box each time.

 

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
String res = "";

BluetoothSerial SerialBT;

void setup() {
Serial.begin(115200);
digitalWrite(25,HIGH);
digitalWrite(26,HIGH);
digitalWrite(27,HIGH);
digitalWrite(33,HIGH);
pinMode(25, OUTPUT);
pinMode(26, OUTPUT);
pinMode(27, OUTPUT);
pinMode(33,OUTPUT);
digitalWrite(25,HIGH);
digitalWrite(26,HIGH);
digitalWrite(27,HIGH);
digitalWrite(33,HIGH);

SerialBT.begin("MyBed"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop()
{
while (!SerialBT.available()); // Wait Until anything is coming from bluetooth client

while (SerialBT.available()) // Read until the bluetooth client is sending.
{
char add = SerialBT.read();
res = res + add;
delay(1);
}

 

// Assigning Actions on particular conditions
if (res == "T")
{
Serial.println("Connection Established!!!");
}
if (res == "LUP")
{
Serial.println("LEFT UP");
digitalWrite(26, LOW);
digitalWrite(27, LOW);
}
if (res == "0")
{
Serial.println("OFF");
digitalWrite(26,HIGH);
digitalWrite(27,HIGH);
}
if (res == "LDN")
{
Serial.println("LEFT DOWN");
digitalWrite(26, LOW);
digitalWrite(25,LOW);
}
if (res == "0")
{
Serial.println("OFF");
digitalWrite(26,HIGH);
digitalWrite(25,HIGH);
}
if (res == "RUP")
{
Serial.println("RIGHT UP");
digitalWrite(33,LOW);
digitalWrite(27,LOW);
}
if (res == "0")
{
Serial.println("OFF");
digitalWrite(33, HIGH);
digitalWrite(27,HIGH);
}
if (res == "RDN")
{
Serial.println("RIGHT DOWN");
digitalWrite(33, LOW);
digitalWrite(25,LOW);
}
if (res == "0")
{
Serial.println("OFF");
digitalWrite(33,HIGH);
digitalWrite(25,HIGH);
}

res = ""; // clearing the string.
}


   
ReplyQuote