Combine two project into one for ESP8266 / Lan with wan

Hello, I have already written a code to be able to control my relays in a local network. Now I want to combine my code with the Blink project code that is use online blynk servers and be able to control my relays both with Blink servers and by entering my local address. my local sketch is :

/* Create a WiFi access point and provide a web server on it. */

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>


#define RELAY 0 // relay connected to  GPIO0
#define RELAY2 2

const char *ssid = "***********"; //Enter your WIFI ssid 
const char *password = "**********"; //Enter your WIFI password 


/* Set these to your desired credentials. */

String stat_str = "--";
String stat_str2 = "--";

ESP8266WebServer server(80);


   connected to this access point to see it.
*/
void handleRoot() { 
  server.send(200, "text/html", "<html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"></head><body bgcolor=\"#cfde2c\"><div style='text-align:center; height:96%; border:10px groove blue; border-radius: 5px;'><strong></strong><h1><i><a style=color:#ff0005; font-size:830%>Shayan Ro0m</a></i></h1></strong><br><br><form action=\"/RELAY_on\" method=\"get\" id=\"form1\"></form><button type=\"submit\" form=\"form1\" value=\"On\"style=\"color:green; background-color:white; font-size:190%; width:230px; margin-left:20px;\" onclick=\'this.form.submit();'>MAHTABI_ON</button><br><br><form action=\"/RELAY_off\" method=\"get\" id=\"form2\"></form><button type=\"submit\" form=\"form2\" value=\"Off\"style=\"color:red; background-color:white; font-size:190%; width:230px; margin-left:20px;\" onclick=\'this.form.submit();'>MAHTABI_OFF</button><h2><a style=color:#ffffff; font-size:330%> MAHTABI_STAT: "+stat_str+"</a><h2><h2><h2><form action=\"/RELAY_on2\" method=\"get\" id=\"form3\"></form><button type=\"submit\" form=\"form3\" value=\"On\"style=\"color:green; background-color:white; font-size:130%; width:210px; margin-left:20px;\" onclick=\'this.form.submit();'>LOSR_ON</button><br><br><form action=\"/RELAY_off2\" method=\"get\" id=\"form4\"></form><button type=\"submit\" form=\"form4\" value=\"Off\"style=\"color:red; background-color:white; font-size:130%; width:210px; margin-left:20px;\" onclick=\'this.form.submit();'>LOSR_OFF</button></h2><a style=color:#ffffff; font-size:830%>LOSR_STAT: "+stat_str2+"</a></h2></body></html>"); 
} 

void handleSave() { 
  if (server.arg("pass") != "") { 
    Serial.println(server.arg("pass")); 
  } 
} 

// Set your Static IP address
IPAddress local_IP(192, 168, 1, 9);
// Set your Gateway IP address
IPAddress gateway(192, 168, 1, 1);

IPAddress subnet(255, 255, 0, 0);



void setup() {

    pinMode(RELAY, OUTPUT); 
  digitalWrite(RELAY, LOW);
  
    pinMode(RELAY2, OUTPUT); 
  digitalWrite(RELAY2, LOW);
  



// Configures static IP address
if (!WiFi.config(local_IP, gateway, subnet)) {
  Serial.println("STA Failed to configure");
}
  
  
  EEPROM.begin(4096);
  int stat = EEPROM.read(0);
  digitalWrite(RELAY, stat);
  if(stat==0)stat_str="OFF";
  if(stat==1)stat_str="ON";

    EEPROM.begin(4096);
  int stat2 = EEPROM.read(0);
  digitalWrite(RELAY2, stat2);
  if(stat2==0)stat_str2="OFF";
  if(stat2==1)stat_str2="ON";
  
  delay(80000); 
  Serial.begin(115200); 
  Serial.println(); 
  
  Serial.println("stat: ");
  Serial.println(stat);
  
  Serial.println("stat2: ");
  Serial.println(stat2);
  
  Serial.print("Configuring access point..."); 
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) { 
    delay(500); 
    Serial.print("."); 
  }



  Serial.println(""); 
  Serial.println("WiFi connected"); 
  Serial.println("IP address: "); 
  Serial.println(WiFi.localIP()); 
  
  server.on("/", handleRoot);
  server.on ("/save", handleSave); 
  server.begin();
  Serial.println("HTTP server started");
  
    server.on("/RELAY_on", []() { 
    digitalWrite(RELAY, 1); 
    Serial.println("on"); 

    EEPROM.write(0,1);//addr,data
    EEPROM.commit();
    stat_str="ON";
    
    handleRoot(); 
  }); 
  server.on("/RELAY_off", []() { 
    digitalWrite(RELAY, 0); 
    Serial.println("off"); 

    EEPROM.write(0,0);//addr,data
    EEPROM.commit();
    stat_str="OFF";
    
    handleRoot(); 
  }); 

//////////////////////////////////////2

    server.on("/RELAY_on2", []() { 
    digitalWrite(RELAY2, 1); 
    Serial.println("on"); 

    EEPROM.write(0,1);//addr,data
    EEPROM.commit();
    stat_str2="ON";
    
    handleRoot(); 
  }); 
  server.on("/RELAY_off2", []() { 
    digitalWrite(RELAY2, 0); 
    Serial.println("off"); 

    EEPROM.write(0,0);//addr,data
    EEPROM.commit();
    stat_str2="OFF";
    
    handleRoot(); 
  }); 
  
}

void loop() {
  server.handleClient();
}

and this is my secend code (blynk online server) :

/*************************************************************

  This is a simple demo of sending and receiving some data.
  Be sure to check out other examples!
 *************************************************************/

// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "TMPLlo69-8WE"
#define BLYNK_DEVICE_NAME "google assisstant"
#define BLYNK_AUTH_TOKEN "*********************"
#define RELAY1 0 // relay connected to  GPIO0
#define RELAY2 2


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "***********";
char pass[] = "************";

BlynkTimer timer;

// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
  
  // Set incoming value from pin V0 to a variable
  int value = param.asInt();

  // Update state
  Blynk.virtualWrite(V1, value);
}

// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
  // Change Web Link Button message to "Congratulations!"
  Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
  Blynk.setProperty(V3, "onImageUrl",  "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
  Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}

// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, millis() / 1000);
}

void setup()
{
  // Debug console
  Serial.begin(115200);
  
  pinMode(RELAY2, OUTPUT); 
  pinMode(RELAY1, OUTPUT); 
  digitalWrite(RELAY1, LOW);
  digitalWrite(RELAY2, LOW);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192, 168, 1, 9), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}

void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

is it possible ?

How are your coding skills? Is this actually your code, that you’ve written yourseld…

or is it code that you’ve obtained and that you don’t really understand?

Pete.

i edited that code and customized for myself. i just want use my relays offline and online . is it possible ?

It should be possible, although I’ve never tried it.

As you are managing your own WiFi connection in the first sketch you’ll need stop using Blynk.begin in the combined sketch and instead use Blynk.confin(auth) and Blynk.connect(timeout_milis).

You’ll also want to do a Blynk.connected() test in the void loop, and only run Blynk.run() if this evaluates to true.

Pete.

1 Like