Hello,
I'm building a ESP32 Relay control which also reads temperature from the BME280 sensor.
With the SMS sent to a ESP32 GSM module it changes the relay state and also sends the temperature if neccessary.
However I'm not able to control the relay with the ESP32 while running on battery. My pin 32 which controls the relay output on HIGH is about 3.3v. While the USB-C is connected the voltage on a pin is 5v and I could control the relay. Is there somekind code where I could set pin default voltage to a 5v?
Also is there any kind of code to set Pin 0 on board to pull up internally on a battery. I'm using it to switch manually relay on or off. While I'm running on a battery the pin state is LOW. While running USB-C power it is HIGH and changes to LOW while pressing button.
Will add a part of a code which I'm using at the moment.
// I2C for SIM800 (to keep it running when powered from battery)
TwoWire I2CPower = TwoWire(0);
// I2C for BME280 sensor
TwoWire I2CBME = TwoWire(1);
Adafruit_BME280 bme;
// Set serial for debug console (to Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to SIM800 module)
#define SerialAT Serial1
// Define the serial console for debug prints, if needed
//#define DUMP_AT_COMMANDS
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
#define IP5306_ADDR 0x75
#define IP5306_REG_SYS_CTL0 0x00
bool setPowerBoostKeepOn(int en){
Wire.beginTransmission(IP5306_ADDR);
Wire.write(IP5306_REG_SYS_CTL0);
if (en) {
Wire.write(0x37); // Set bit1: 1 enable 0 disable boost keep on
} else {
Wire.write(0x35); // 0x37 is default reg value
}
return Wire.endTransmission() == 0;
}
void printValuesSensor() {
SerialMon.print("Temperature = ");
SerialMon.print(bme.readTemperature());
SerialMon.println(" *C");
SerialMon.print("Pressure = ");
SerialMon.print(bme.readPressure() / 100.0F);
SerialMon.println(" hPa");
SerialMon.print("Approx. Altitude = ");
SerialMon.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
SerialMon.println(" m");
SerialMon.print("Humidity = ");
SerialMon.print(bme.readHumidity());
SerialMon.println(" %");
SerialMon.println();
delay(2000);
}
void setup() {
// Set console baud rate
SerialMon.begin(115200);
pinMode(Relay, OUTPUT);
digitalWrite(Relay, LOW);
/* pinMode(LED13, OUTPUT);
digitalWrite(LED13,LOW);
pinMode(Button, INPUT);
digitalWrite(Button,HIGH);*/
delay(100);
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP);
// Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +" Seconds");
delay(500);
// Keep power when running from battery
Wire.begin(I2C_SDA, I2C_SCL);
Wire.begin(I2C_SDA_2, I2C_SCL_2);
bool isOk = setPowerBoostKeepOn(1);
SerialMon.println(String("IP5306 KeepOn ") + (isOk ? "OK" : "FAIL"));
// Start I2C communication
I2CPower.begin(I2C_SDA, I2C_SCL);
I2CBME.begin(I2C_SDA_2, I2C_SCL_2);
delay(1000);
// Set modem reset, enable, power pins
pinMode(MODEM_PWKEY, OUTPUT);
pinMode(MODEM_RST, OUTPUT);
pinMode(MODEM_POWER_ON, OUTPUT);
digitalWrite(MODEM_PWKEY, LOW);
digitalWrite(MODEM_RST, HIGH);
digitalWrite(MODEM_POWER_ON, HIGH);
// Set GSM module baud rate and UART pins
// SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
SerialAT.begin(9600, SERIAL_8N1, MODEM_RX, MODEM_TX);
delay(3000);
// Restart SIM800 module, it takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println("Initializing modem...");
// use modem.init() if you don't need the complete restart
modem.restart();
delay(3000);
// Unlock your SIM card with a PIN if needed
if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
modem.simUnlock(simPIN);
}
delay(1000);
SerialAT.print("AT+CSQ");
delay(1000);
Serial.print("\nSignal quality=");
SerialMon.print(SerialAT.read());
delay(1000);
/* SerialAT.println("AT+CMGD=1,4\r"); // delete all SMS
Serial.print("\nDeleting all SMS");
delay(1500); */
{
SerialAT.println("AT"); //If everything is Okay it will show "OK" on the serial monitor
delay(5000);
SerialAT.println("AT+CMGD=1,4\r"); // delete all SMS
delay(2500);
Serial.print("\nDeleting all SMS");
delay(2500);
SerialAT.println("AT+CMGF=1"); // Configuring TEXT mode
delay(1000);
Serial.print("\nConfiguring SMS mode");
delay(1000);
SerialAT.println("AT+CNMI=2,2,0,0,0"); //Configure the SIM800L on how to manage the Received SMS... Check the SIM800L AT commands manual
delay(1000);
Serial.print("\nConfiguring SMS receive mode");
delay(2500);
}
if (!bme.begin(0x76, &I2CBME)) {
SerialMon.println("Could not find a valid BME280 sensor, check wiring!");
modem.sendSMS(SMS_TARGET,"System NOT OKAY!") ;
while (1);
}
delay(2500);
// To send an SMS, call modem.sendSMS(SMS_TARGET, smsMessage)
And then changing the state of a relay by SMS.
if(Relay_Change_State_SMS!=-1){
digitalRead(Relay);
if(relay_state==HIGH){
digitalWrite(Relay, LOW);
String RelayMessage = String("Relay turned OFF");
if (modem.sendSMS(SMS_TARGET, RelayMessage)){
SerialMon.println("Sending SMS:");
SerialMon.println(RelayMessage);
SerialAT.println("AT+CMGD=1,4\r"); // delete all SMS
Relay_Change_State_SMS=-1;
smscounter++;
delay(500);
}
}
if(relay_state==LOW){
digitalWrite(Relay, HIGH);
String RelayMessage = String("Relay turned ON");
if( modem.sendSMS(SMS_TARGET, RelayMessage)){
SerialMon.println("Sending SMS:");
SerialMon.println(RelayMessage);
SerialAT.println("AT+CMGD=1,4\r"); // delete all SMS
Relay_Change_State_SMS=-1;
smscounter++;
delay(500);
}
}
And how I change relay state by button. The downside is that while running on a battery the Pin0 is always LOW and then relay state in a loop keeps changing LOW to HIGh and vice versa.
relay_state=digitalRead(Relay);
if(Buttonstate== LOW &&relay_state== HIGH){
digitalWrite(Relay, LOW);
digitalWrite(LED13, LOW);
SerialMon.println("Relay switched OFF");
}
if(Buttonstate== LOW && relay_state== LOW){
digitalWrite(Relay, HIGH);
digitalWrite(LED13, HIGH);
SerialMon.println("Relay switched ON");