I have a project in which I have a 5v RF signal pulse (cut to 3.3v using 3.3v regulator) going through a transistor (2n2222) acting as a switch to send power to a 3.3v latching relay.
I also have a ESP8266-12e (Blynk ESP) connected to a Blynk app. When the button on the app is tapped it turns on the D5 pin on the ESP. This send a 3.3v 12mA pulse to the same transistor to activate the 3.3v latching relay.
The RF is coming from a remote sensor that when certain conditions are met send a signal to the main unit I am having problems with.
The Blynk app allows for remote access to the main unit.
When the latching relay is SET by either the RF or Blynk ESP signal it turns off the Blynk ESP, and turns on another ESP8266-12e (Reset ESP). The Reset ESP connects to the internet and sends some info to Thingspeak and then turns on its D5 pin which is connected to the RESET pin on the latching relay. The Reset ESP is then turned off stoping transmission to ThingSpeak, and turning back on the Blynk ESP so the device can be access remotely again.
My problem arrises when I try to use the Blynk ESP to activate the latching relay. If I run it through the 3.3v regulator the first time I tap the Blynk app button the relay tries to engage but fails and the Blynk ESP restarts. After about 10 seconds when I tap the Blynk app button again the relay engages and the Blynk ESP turns off, the Reset ESP starts up and does it thing and then RESETs the relay.
When I use the RF signal in this set up everything thing goes well.
I have tried to rewire the Blynk ESP to bypass the 3.3v regulator since it only put out a 3.3v pulse I thought it didn't have enough power to go through the regulator and effect the transistor. (But then why does it work the second time?)
Anyway when I did this the relay was activated the first time, turning off Blynk ESP and on the Reset ESP.
However now when the ESP activates its D5 pin to RESET the relay it isn't successful and it starts over sometimes looping several times.
I have tried to put a 9K resistor in the Blynk ESP line to the transistor and this seems to have helped. Sometimes the Reset ESP activates the relay the first time and sometimes it takes two to three loops but it always resets.
I am at a loss as to why this is happening and need some creative problem solving and guidance to figure this out.
I am posting the code for the Blynk ESP and the Reset ESP and the schematic for the main unit. I have included some of the main wiring routes and some power consumption/output on the schematic. If you need more information I will try to get it.
Looking up the power out from the AD1 pin on the HT12D is 1.6mA @5vdc.
THIS IS THE CODE FOR THE RESET ESP
#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
strcpy(apiKey,customAPIKey.getValue());
strcpy(channelKey,customAPIKey2.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
Serial.print(addr,'A');
//Write string to eeprom
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);
}
void loop() {
digitalWrite(D5,LOW);
itt = 0;
Serial.println("input");
Serial.print(itt);
delay(1000);
WiFiManager wifiManager;
if (WiFi.status() == WL_DISCONNECTED) {
wifiManager.autoConnect("FloWT1");
delay(30000);}
if (WiFi.status() == WL_CONNECTED) { Serial.println("Connected");
delay(1000);
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(1000);
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(1000);
client.stop();
delay(7000);
digitalWrite(D5,HIGH);
Serial.println("d5HIGH");
delay(200);
digitalWrite(D5,LOW);
Serial.println("Waiting 20 secs");
// thingspeak needs at least a 15 sec delay between updates
// 20 seconds to be safe
}
}
THIS IS THE CODE FOR THE BLYNK ESP
#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();
}
AND THIS IS THE SCHEMATIC
I forgot the 9K resistor on the green dotted line.
I am thinking of turning off the WiFi on Reset ESP before I turn on D5 pin but don't know if that will help.
Anyway thanks for everybody's help.