Tengo un ESP8266-12e (ESP1) que está conectado a Blynk con un botón momentáneo. Cuando se activa el botón hace que el pin D5 HIGH que pasa a través de un transistor y alimenta el lado de un relé de enclavamiento. Cuando esto sucede, el relé apaga el ESP1 y enciende otro ESP8266-12e (ESP2). ESP2 se ejecuta durante unos 15 segundos de conexión a ThingSpeak y, a continuación, ajuste el pin D5 a HIGH activando el lado de reinicio del relé de enclavamiento. Esta vez el relé apaga el ESP2 y enciende el ESP1. El lado de ajuste del relé de enclavamiento también se puede activar a través de una señal de RF que va a través del transistor. Cuando el RF activa el relé con una señal de .5sec todo funciona bien. El ESP1 se apaga, el ESP2 se enciende y pasa por su código y luego activa el relé. Después de eso todo funciona como debería. Sin embargo cuando pulso el botón Blynk asociado con el ESP1 no se apaga y enciende el ESP2, sino que el led azul del ESP1 parpadea, el relé de enganche no se activa y el ESP1 permanece encendido. Cuando después de unos 10 segundos vuelvo a tocar el botón Blynk. Entonces se enciende el ESP2 y se apaga el ESP1. El ESP va a través de su código y luego restablece el relé.
Sé que cuando el ESP2 se apaga no está terminando su código por lo que el pin D5 sigue en HIGH cuando se apaga. ¿Es esto lo que está causando que el ESP1 se reinicie cuando se supone que se apague y el ESP2 se encienda? ¿Es posible que deba poner un pequeño condensador entre vcc del ESP2 y tierra para que cuando el relé de enclavamiento se apaga todavía hay suficiente energía para que el ESP2 terminar su código y gire el pin D5 BAJA? Pensaría que al estar apagado arrancaría todo desde cero. Estoy publicando mi código para ambos ESPs y un esquema.
ESP1 (código del botón Blynk)
#define BLYNK_PRINT Serial
//NodeMCU 1.0 (ESP-12E Module constant connect)
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//NEW AUTOCONNECT
#include <FS.h> //https://github.com/esp8266/Arduino
#include <EEPROM.h>
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <ArduinoJson.h>
//NEW AUTOCONNECT
const byte numChars = 32;
char receivedChars[numChars];
char Password[36]="";
char apiKey[32]="";
char apiKey2[32]="";
char channelKey[16];
char channelKey2[16];
String channelKey21= "&";
char auth[] = "";
byte pinState = LOW;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
WiFiClient client;
//NEW AUTOCONNECT
char defaultHost[100] = ""; //Thing Speak IP address (sometime the web address causes issues with ESP's :/
const byte wifiResetPin = 13;
int interruptPinDebounce = 0;
long debouncing_time = 1000;
volatile unsigned long wifiResetLastMillis = 0;
bool shouldSaveConfig = false;
int address = 0;
byte value;
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;}
void handleWifiReset(){
if(millis()<wifiResetLastMillis){
wifiResetLastMillis = millis();
}
if((millis() - wifiResetLastMillis)>= debouncing_time){
Serial.println("Clearing WiFi data resetting");
WiFiManager wifiManager;
wifiManager.resetSettings();
SPIFFS.format();
ESP.reset();
delay(1000);
}
wifiResetLastMillis = millis();
}
int addr = 0;
//END NEW AUTOCONNECT
void setup() {
Serial.begin(115200);
pinMode(D5,OUTPUT);
digitalWrite(5,LOW);
//NEW AUTOCONNECT
WiFiManager wifiManager;
// put your setup code here, to run once:
pinMode(wifiResetPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(wifiResetPin), handleWifiReset,FALLING);
WiFiManagerParameter customAPIKey("apiKey", "Authorization Code", apiKey, 32);
wifiManager.addParameter(&customAPIKey);
wifiManager.autoConnect("FloWT2");
Serial.println("Connected");
strcpy(apiKey, customAPIKey.getValue());
if (shouldSaveConfig) {
Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["defaultHost"] = defaultHost;
json["apiKey"] = apiKey;
Serial.println("API");
Serial.println("CHANNEL");
Serial.print(apiKey);
Serial.print(channelKey);
String(apiKey2) = String(apiKey);
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
json.printTo(configFile);
json.printTo(Serial);
delay(1000);
configFile.close();
//end save
}
Serial.println("local ip");
Serial.println(WiFi.localIP());
strcpy(apiKey,customAPIKey.getValue());
String( apiKey2) = String (apiKey);
Serial.println("apiKEY");
Serial.print(apiKey);
EEPROM.begin(512); //Initialize EEPROM
EEPROM.write(addr, 'A'); //Write character A
addr++; //Increment address
EEPROM.write(addr, 'B'); //Write character A
addr++; //Increment address
EEPROM.write(addr, 'C'); //Write character A
addr++; //Increment address
EEPROM.write(addr, 'D'); //Write character A
String www = apiKey2;
Serial.println("thisiswww");
Serial.print (www);
for(int i=0;i<www.length();i++) //loop upto string lenght www.length() returns length of string
{
EEPROM.write(0x0F+i,www[i]); //Write one by one with starting address of 0x0F
}
EEPROM.commit();
WiFiClient client;
EEPROM.begin(512);
Serial.println("WHAT"); //Goto next line, as ESP sends some garbage when you reset it
Serial.print(char(EEPROM.read(addr))); //Read from address 0x00
addr++; //Increment address
Serial.print(char(EEPROM.read(addr))); //Read from address 0x01
addr++; //Increment address
Serial.println(char(EEPROM.read(addr))); //Read from address 0x02
addr++; //Increment address
Serial.println(char(EEPROM.read(addr))); //Read from address 0x03
addr++; //Increment address
Serial.println(char(EEPROM.read(addr))); //Read from address 0x04
//Read string from eeprom
String uuu;
for(int i=0;i<32;i++)
{
uuu = uuu + char(EEPROM.read(0x0F+i)); //Read one by one with starting address of 0x0F
}
Serial.println("this");
Serial.print(uuu); //Print the text on serial monitor
uuu.toCharArray(auth,33);
Serial.println("auth");
Serial.print(auth);
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run();
}
Código ESP2
#include <ESP8266WiFi.h>
//NEW AUTOCONNECT
#include <FS.h> //https://github.com/esp8266/Arduino
#include <EEPROM.h>
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <ArduinoJson.h>
//END NEW AUTOCONNECT
//String apiKey = "8RXVFOW83KRZHSNL"; pool filler
//const char* ssid = "Gary's Wi-Fi Network";
//const char* password = "Homenetwork";
const char* server = "api.thingspeak.com";
//NEW AUTOCONNECT
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
char Password[36]="";
char apiKey[16]="";
char apiKey2[16]="";
char channelKey[16];
String channelKey2= "&";
//END NEW AUTOCONNECT
//DHTesp dht;
//DHTesp dht2;
//DHTesp dht3;
WiFiClient client;
//NEW AUTOCONNECT
char defaultHost[100] = ""; //Thing Speak IP address (sometime the web address causes issues with ESP's :/
long itt = 500;
const byte wifiResetPin = 13;
int interruptPinDebounce = 0;
long debouncing_time = 1000;
volatile unsigned long wifiResetLastMillis = 0;
bool shouldSaveConfig = false;
int address = 0;
byte value;
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;}
void handleWifiReset(){
if(millis()<wifiResetLastMillis){
wifiResetLastMillis = millis();
}
if((millis() - wifiResetLastMillis)>= debouncing_time){
Serial.println("Clearing WiFi data resetting");
WiFiManager wifiManager;
wifiManager.resetSettings();
SPIFFS.format();
ESP.reset();
delay(1000);
}
wifiResetLastMillis = millis();
}
int addr = 0;
//END NEW AUTOCONNECT
void setup() {
pinMode(D5,OUTPUT);
digitalWrite(D5,LOW);
//NEW AUTOCONNECT
WiFiManager wifiManager;
// put your setup code here, to run once:
pinMode(wifiResetPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(wifiResetPin), handleWifiReset,FALLING);
WiFiManagerParameter customAPIKey("apiKey", "ThingSpeakWriteAPI", apiKey, 16);
WiFiManagerParameter customAPIKey2("channelKey", "ThingSpeakChannel Number", channelKey, 16);
//END NEW STUFF
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
//WiFiManager wifiManager;
//NEW STUFF START
//wifiManager.setSaveConfigCallback(saveConfigCallback);
wifiManager.addParameter(&customAPIKey);
wifiManager.addParameter(&customAPIKey2);
//END NEW STUFF
//reset saved settings
//wifiManager.resetSettings();
//set custom ip for portal
//wifiManager.setAPStaticIPConfig(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
wifiManager.autoConnect("FloWT1");
Serial.println("Connected");
//NEW STUFF START
strcpy(apiKey, customAPIKey.getValue());
strcpy(channelKey, customAPIKey2.getValue());
String chan = String(channelKey);
if (shouldSaveConfig) {
Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["defaultHost"] = defaultHost;
json["apiKey"] = apiKey;
Serial.println("API");
Serial.println("CHANNEL");
Serial.print(apiKey);
Serial.print(channelKey);
String(apiKey2) = String(apiKey);
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
json.printTo(configFile);
json.printTo(Serial);
delay(1000);
configFile.close();
//end save
}
Serial.println("local ip");
Serial.println(WiFi.localIP());
//END NEW STUFF
//or use this for auto generated name ESP + ChipID
//wifiManager.autoConnect();
//Serial.println("WriteApi");
//Serial.println(apiKey);
//if you get here you have connected to the WiFi
//Serial.println("K)");
//save the custom parameters to FS
strcpy(apiKey,customAPIKey.getValue());
strcpy(channelKey,customAPIKey2.getValue());
String( apiKey2) = String (apiKey);
Serial.println("apiKEY");
Serial.print(apiKey);
EEPROM.begin(512); //Initialize EEPROM
// write appropriate byte of the EEPROM.
// these values will remain there when the board is
// turned off.
EEPROM.write(addr, 'A'); //Write character A
addr++; //Increment address
EEPROM.write(addr, 'B'); //Write character A
addr++; //Increment address
EEPROM.write(addr, 'C'); //Write character A
Serial.print(addr,'A');
//Write string to eeprom
/*String uuu = channelKey;
for(int i=0;i<uuu.length();i++) //loop upto string lenght uuu.length() returns length of string
{
EEPROM.write(0x0F+i,uuu[i]); //Write one by one with starting address of 0x0F
}*/
String uuu = channelKey;
String www = apiKey2 + uuu;
Serial.print (www);
for(int i=0;i<www.length();i++) //loop upto string lenght www.length() returns length of string
{
EEPROM.write(0x0F+i,www[i]); //Write one by one with starting address of 0x0F
}
EEPROM.commit();
//Read string from eeprom
Serial.begin(115200);
// dht.setup(D3, DHTesp::DHT11);
//dht2.setup(D6, DHTesp::DHT11);
//dht3.setup(D7, DHTesp::DHT11);
// WiFi.begin(ssid, password);
//Serial.println();
//Serial.println();
//Serial.print("Connecting to ");
//Serial.println(ssid);
// WiFi.begin(ssid, password);
//while (WiFi.status() != WL_CONNECTED)
//{
//delay(500);
//Serial.print(".");
//}
//Serial.println("");
//Serial.println("WiFi connected");
}
void loop() {
digitalWrite(D5,LOW);
itt = 0;
Serial.println("input");
Serial.print(itt);
// digitalWrite(D1,HIGH);
delay(1000);
WiFiManager wifiManager;
if (WiFi.status() == WL_DISCONNECTED) {
wifiManager.autoConnect("FloWT1");}
delay(3000);
if (WiFi.status() == WL_CONNECTED) { Serial.println("Connected");
delay(3000);
WiFiClient client;
char defaultHost[100] = "api.thingspeak.com";
EEPROM.begin(512);
Serial.println("WHAT"); //Goto next line, as ESP sends some garbage when you reset it
Serial.print(char(EEPROM.read(addr))); //Read from address 0x00
addr++; //Increment address
Serial.print(char(EEPROM.read(addr))); //Read from address 0x01
addr++; //Increment address
Serial.println(char(EEPROM.read(addr))); //Read from address 0x02
addr++; //Increment address
Serial.println(char(EEPROM.read(addr))); //Read from address 0x03
//Read string from eeprom
String www;
//Here we dont know how many bytes to read it is better practice to use some terminating character
//Lets do it manually www.circuits4you.com total length is 20 characters
for(int i=0;i<16;i++)
{
www = www + char(EEPROM.read(0x0F+i)); //Read one by one with starting address of 0x0F
}
String uuu;
for(int i=31;i<32;i++)
{uuu = uuu + char(EEPROM.read(0x0+i));
}
Serial.println("this");
Serial.print(www); //Print the text on serial monitor
Serial.println("that");
Serial.print(uuu);
String one = "&";
String two = one + "field";
String three = two + uuu;
String four = three + "=";
channelKey2 = four;
delay(3000);
delay(5000);
Serial.print(channelKey2);
if (client.connect(server, 80)) {
Serial.println("hi");
String postStr = apiKey;
Serial.print( postStr);
postStr += channelKey2;
postStr += String(itt);
Serial.print(postStr);
//postStr += "&field4=";
//postStr += String(f2);
//postStr += "&field5=";
//postStr += String(f3);
postStr += "\r\n\r\n";
Serial.println("NOW");
Serial.print(www);
Serial.println("THEN");
Serial.print(uuu);
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+String (www) + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n\n");
client.print(postStr);
Serial.println("Sending data to Thingspeak");
}
delay(2000);
digitalWrite(D5,HIGH);
Serial.println("d5HIGH");
delay(500);
client.stop();
Serial.println("Waiting 20 secs");
// thingspeak needs at least a 15 sec delay between updates
// 20 seconds to be safe
}
}
Si colocar los condensadores ayudaría, de qué tamaño deberían ser. El ESP-12e utiliza alrededor de 100mA a 3,3v.
Con el ejemplo que se sugirió existe la posibilidad de poner el condensador antes del regulador de tensión. En mi caso el regulador de voltaje está antes del relé que corta la energía a los ESPs. Así que el condensador no sería capaz de suministrar la energía a los ESP.
- ¿Resolverá el problema un condensador?
-
¿Los condensadores almacenan tensión o corriente? Si es tensión entonces necesito un condensador de 3,3v de ? uF. Si sólo almacenan corriente, ¿qué tamaño necesitaría? Hice un experimento y encontré la primera vez que los incendios ESP1 hay una pequeña cantidad de corriente que va a la ESP2 para una fracción de segundo. Supongo que podría probar un condensador a la ESP2 y ver. ¿No quiero quemarlo? Tal vez un 25v a 1000 uf. ¿Es demasiado grande? No soy un EE y necesito un poco de orientación. Gracias por la sugerencia.