How can I control two boards in one sketch?

Hi. I'm looking for help with trying to figure out how to combine two separate boards nodemcu and esp32 4-relay board in one sketch for use via blynk. can anyone tell me how to? I keep getting an error saying "Compilation error: ESP8266WiFi.h: No such file or directory"~ a conflict error. because the sketch needs to include both for esp8266 and esp32, but for some reason it cant.
Here's my code:

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest
  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.
    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app
  Blynk library is licensed under MIT license
  This example code is in public domain.
 *************************************************************
  This example runs directly on ESP32 chip.
  Note: This requires ESP32 support package:
    https://github.com/espressif/arduino-esp32
  Please be sure to select the right ESP32 module
  in the Tools -> Board menu!
  Change WiFi ssid, pass, and Blynk auth token to run :)
  Feel free to apply it to any other example. It's simple!
 *************************************************************/

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

#define RELAY_PIN_1 21
#define RELAY_PIN_2 19
#define RELAY_PIN_3 18
#define RELAY_PIN_4 5
#define LED_PIN     25


/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID "TMPL2XKwz0HTx"
#define BLYNK_TEMPLATE_NAME "ESP32"
#define BLYNK_AUTH_TOKEN "StMNnw7Ijl0rL10slDDHAiScSSknvkLq"

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = BLYNK_AUTH_TOKEN;

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

const int dcMotor1 = D1;
int slider1;
const int inPut1 = D0;
int mappedValue1=0;
const int dcMotor2 = D3;
int slider2;
const int inPut2 = D2;
int mappedValue2=0;

BLYNK_WRITE(V0)
{
    if (param.asInt() == 1) {
        digitalWrite(RELAY_PIN_1, HIGH);
        Blynk.logEvent("relay_state", "RELAY_1 ON");//Sending Events
    } else {
        digitalWrite(RELAY_PIN_1,  LOW);
        Blynk.logEvent("relay_state", "RELAY_1 OFF");//Sending Events
    }
}

BLYNK_WRITE(V1)
{
    if (param.asInt() == 1) {
        digitalWrite(RELAY_PIN_2, HIGH);
        Blynk.logEvent("relay_state", "RELAY_2 ON");//Sending Events
    } else {
        digitalWrite(RELAY_PIN_2,  LOW);
        Blynk.logEvent("relay_state", "RELAY_2 OFF");//Sending Events
    }
}

BLYNK_WRITE(V2)
{
    if (param.asInt() == 1) {
        digitalWrite(RELAY_PIN_3, HIGH);
        Blynk.logEvent("relay_state", "RELAY_3 ON");//Sending Events
    } else {
        digitalWrite(RELAY_PIN_3, LOW);
        Blynk.logEvent("relay_state", "RELAY_3 OFF");//Sending Events
    }
}

BLYNK_WRITE(V3)
{
    if (param.asInt() == 1) {
        digitalWrite(RELAY_PIN_4, HIGH );
        Blynk.logEvent("relay_state", "RELAY_4 ON");//Sending Events
    } else {
        digitalWrite(RELAY_PIN_4, LOW);
        Blynk.logEvent("relay_state", "RELAY_4 OFF");//Sending Events
    }
}

//Syncing the output state with the app at startup
BLYNK_CONNECTED()
{
    Blynk.syncVirtual(V0);  // will cause BLYNK_WRITE(V0) to be executed
    Blynk.syncVirtual(V1);  // will cause BLYNK_WRITE(V1) to be executed
    Blynk.syncVirtual(V2);  // will cause BLYNK_WRITE(V2) to be executed
    Blynk.syncVirtual(V3);  // will cause BLYNK_WRITE(V3) to be executed
}

void setup()
{
    // Debug console
    Serial.begin(115200);
    pinMode(RELAY_PIN_1, OUTPUT);
    pinMode(RELAY_PIN_2, OUTPUT);
    pinMode(RELAY_PIN_3, OUTPUT);
    pinMode(RELAY_PIN_4, OUTPUT);
    pinMode(LED_PIN, OUTPUT);
    delay(100);

    Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
    pinMode(dcMotor1,OUTPUT);
    pinMode(inPut1, OUTPUT);
    digitalWrite(inPut1, HIGH);
    pinMode(dcMotor2,OUTPUT);
    pinMode(inPut2, OUTPUT);
    digitalWrite(inPut2, HIGH);
}
void loop()
{
    Blynk.run();

}  // Read value from slider and set PWM duty cycle
BLYNK_WRITE(V4){
  slider1 = param.asInt(); 
  mappedValue1 = map(slider1,0,1023,0,255);
  //generate PWM signal
  analogWrite(dcMotor1, mappedValue1);
  
}
BLYNK_WRITE(V5){
  slider2 = param.asInt(); 
  mappedValue2 = map(slider2,0,1023,0,255);
  //generate PWM signal
  analogWrite(dcMotor2, mappedValue2);
}

  

The BLYNK_WRITE(V4) and BLYNK_WRITE(V5) is being used by the nodemcu as sliders for pc fan speed control at 1023 variable.
Please help. Thanks. :slight_smile:

Your topic does not indicate a problem with IDE 1.x and hence has been moved to a more suitable location on the forum.


Your question i not quite clear to me.

You have a sketch for a ESP8266 and you have a sketch for a ESP32 and you want to combine both in one sketch that you now want to run on one of those boards? If so, what will be the board where the new sketch needs to running on?

1 Like

You will nead to learn ifdef and ifndef to have one program code be compilable on multiple platforms. Very good programmers do this for all their programs that will be used on other devices.

1 Like

Hi @twistex_iot. The "esp8266" boards platform is configured so that a macro named ARDUINO_ARCH_ESP8266 is defined when you compile a sketch while a board of that platform is selected in Arduino IDE. Likewise, the "esp32" platform defines a macro named ARDUINO_ARCH_ESP32 when you compile a sketch while a board of that platform is selected.

This means you can use the conditional preprocessor directives to compile sections of code in your sketch only when one board or the other is selected:

#if defined(ARDUINO_ARCH_ESP8266)
// ESP8266-specific code here
#elif defined(ARDUINO_ARCH_ESP32)
// ESP32-specific code here
#else
#error This sketch is not compatible with the selected board.
#endif

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.