0

i was programming my node mcu i modified the code and got a complination error but when i compiled the original code i got no error modified code

#include "arduino_secrets.h"
#include "thingProperties.h"

// define the GPIO connected with Relays and switches
#define RelayPin1 5  //D1
#define RelayPin2 4  //D2
#define RelayPin3 14 //D5
#define RelayPin4 12 //D6

#define RelayPin5 2  //d4
#define RelayPin6 0   //D3 
#define RelayPin7 13  //D7
#define RelayPin8 15   //d8

#define wifiLed   16   //D0

int toggleState_1 = 0; //Define integer to remember the toggle state for relay 1
int toggleState_2 = 0; //Define integer to remember the toggle state for relay 2
int toggleState_3 = 0; //Define integer to remember the toggle state for relay 3
int toggleState_4 = 0; //Define integer to remember the toggle state for relay 4
int toggleState_5 = 0; //Define integer to remember the toggle state for relay 5
int toggleState_6 = 0; //Define integer to remember the toggle state for relay 6
int toggleState_7 = 0; //Define integer to remember the toggle state for relay 7
int toggleState_8 = 0; //Define integer to remember the toggle state for relay 8

void relayOnOff(int relay) {

  switch (relay) {
    case 1:
      if (toggleState_1 == 0) {
        digitalWrite(RelayPin1, HIGH); // turn on relay 1
        toggleState_1 = 1;
        Serial.println("Device1 ON");
      }
      else {
        digitalWrite(RelayPin1, LOW); // turn off relay 1
        toggleState_1 = 0;
        Serial.println("Device1 OFF");
      }
      delay(100);
      break;
    case 2:
      if (toggleState_2 == 0) {
        digitalWrite(RelayPin2, HIGH); // turn on relay 2
        toggleState_2 = 1;
        Serial.println("Device2 ON");
      }
      else {
        digitalWrite(RelayPin2, LOW); // turn off relay 2
        toggleState_2 = 0;
        Serial.println("Device2 OFF");
      }
      delay(100);
      break;
    case 3:
      if (toggleState_3 == 0) {
        digitalWrite(RelayPin3, HIGH); // turn on relay 3
        toggleState_3 = 1;
        Serial.println("Device3 ON");
      } else {
        digitalWrite(RelayPin3, LOW); // turn off relay 3
        toggleState_3 = 0;
        Serial.println("Device3 OFF");
      }
      delay(100);
      break;
     case 4:
      if (toggleState_4 == 0) {
        digitalWrite(RelayPin4, HIGH); // turn on relay 3
        toggleState_4 = 1;
        Serial.println("Device4 ON");
      } else {
        digitalWrite(RelayPin4, LOW); // turn off relay 3
        toggleState_4 = 0;
        Serial.println("Device4 OFF");
      }
      delay(100);
      break;
     case 5:
      if (toggleState_5 == 0) {
        digitalWrite(RelayPin5, HIGH); // turn on relay 3
        toggleState_5 = 1;
        Serial.println("Device5 ON");
      } else {
        digitalWrite(RelayPin5, LOW); // turn off relay 3
        toggleState_5 = 0;
        Serial.println("Device5 OFF");
      }
      delay(100);
      break;
     case 6:
      if (toggleState_6 == 0) {
        digitalWrite(RelayPin6, HIGH); // turn on relay 3
        toggleState_6 = 1;
        Serial.println("Device6 ON");
      } else {
        digitalWrite(RelayPin6, LOW); // turn off relay 3
        toggleState_6 = 0;
        Serial.println("Device6 OFF");
      }
      delay(100);
      break;
     case 7:
      if (toggleState_7 == 0) {
        digitalWrite(RelayPin7, HIGH); // turn on relay 3
        toggleState_7 = 1;
        Serial.println("Device7 ON");
      } else {
        digitalWrite(RelayPin7, LOW); // turn off relay 3
        toggleState_7 = 0;
        Serial.println("Device7 OFF");
      }
      delay(100);
      break;
    case 8:
      if (toggleState_8 == 0) {
        digitalWrite(RelayPin8, HIGH); // turn on relay 4
        toggleState_8 = 1;
        Serial.println("Device8 ON");
      }
      else {
        digitalWrite(RelayPin8, LOW); // turn off relay 4
        toggleState_8 = 0;
        Serial.println("Device8 OFF");
      }
      delay(100);
      break;
    default : break;
  }
}

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();

  pinMode(RelayPin1, OUTPUT);
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);
  pinMode(RelayPin5, OUTPUT);
  pinMode(RelayPin6, OUTPUT);
  pinMode(RelayPin7, OUTPUT);
  pinMode(RelayPin8, OUTPUT);

  pinMode(wifiLed, OUTPUT);

  digitalWrite(RelayPin1, LOW);
  digitalWrite(RelayPin2, LOW);
  digitalWrite(RelayPin3, LOW);
  digitalWrite(RelayPin4, LOW);
  digitalWrite(RelayPin5, LOW);
  digitalWrite(RelayPin6, LOW);
  digitalWrite(RelayPin7, LOW);
  digitalWrite(RelayPin8, LOW);

  digitalWrite(wifiLed, HIGH);  //Turn ON WiFi LED
}

void loop() {
  ArduinoCloud.update();
  

  if (WiFi.status() != WL_CONNECTED)
  {
    digitalWrite(wifiLed, HIGH); //Turn ON WiFi LED
  }
  else{
    digitalWrite(wifiLed, LOW); //Turn OFF WiFi LED
  }
}

the original code

#include "arduino_secrets.h"
#include "thingProperties.h"

// define the GPIO connected with Relays and switches
#define RelayPin1 5  //D1
#define RelayPin2 4  //D2
#define RelayPin3 14 //D5
#define RelayPin4 12 //D6

#define SwitchPin1 10  //SD3
#define SwitchPin2 0   //D3 
#define SwitchPin3 13  //D7
#define SwitchPin4 3   //RX

#define wifiLed   16   //D0

int toggleState_1 = 0; //Define integer to remember the toggle state for relay 1
int toggleState_2 = 0; //Define integer to remember the toggle state for relay 2
int toggleState_3 = 0; //Define integer to remember the toggle state for relay 3
int toggleState_4 = 0; //Define integer to remember the toggle state for relay 4

void relayOnOff(int relay) {

  switch (relay) {
    case 1:
      if (toggleState_1 == 0) {
        digitalWrite(RelayPin1, HIGH); // turn on relay 1
        toggleState_1 = 1;
        Serial.println("Device1 ON");
      }
      else {
        digitalWrite(RelayPin1, LOW); // turn off relay 1
        toggleState_1 = 0;
        Serial.println("Device1 OFF");
      }
      delay(100);
      break;
    case 2:
      if (toggleState_2 == 0) {
        digitalWrite(RelayPin2, HIGH); // turn on relay 2
        toggleState_2 = 1;
        Serial.println("Device2 ON");
      }
      else {
        digitalWrite(RelayPin2, LOW); // turn off relay 2
        toggleState_2 = 0;
        Serial.println("Device2 OFF");
      }
      delay(100);
      break;
    case 3:
      if (toggleState_3 == 0) {
        digitalWrite(RelayPin3, HIGH); // turn on relay 3
        toggleState_3 = 1;
        Serial.println("Device3 ON");
      } else {
        digitalWrite(RelayPin3, LOW); // turn off relay 3
        toggleState_3 = 0;
        Serial.println("Device3 OFF");
      }
      delay(100);
      break;
    case 4:
      if (toggleState_4 == 0) {
        digitalWrite(RelayPin4, HIGH); // turn on relay 4
        toggleState_4 = 1;
        Serial.println("Device4 ON");
      }
      else {
        digitalWrite(RelayPin4, LOW); // turn off relay 4
        toggleState_4 = 0;
        Serial.println("Device4 OFF");
      }
      delay(100);
      break;
    default : break;
  }
}

void manual_control() {
  //Manual Switch Control
  if (digitalRead(SwitchPin1) == LOW) {
    delay(200);
    relayOnOff(1);
  }
  else if (digitalRead(SwitchPin2) == LOW) {
    delay(200);
    relayOnOff(2);
  }
  else if (digitalRead(SwitchPin3) == LOW) {
    delay(200);
    relayOnOff(3);
  }
  else if (digitalRead(SwitchPin4) == LOW) {
    delay(200);
    relayOnOff(4);
  }
}

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();

  pinMode(RelayPin1, OUTPUT);
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);

  pinMode(wifiLed, OUTPUT);

  pinMode(SwitchPin1, INPUT_PULLUP);
  pinMode(SwitchPin2, INPUT_PULLUP);
  pinMode(SwitchPin3, INPUT_PULLUP);
  pinMode(SwitchPin4, INPUT_PULLUP);

  //During Starting all Relays should TURN OFF
  digitalWrite(RelayPin1, HIGH);
  digitalWrite(RelayPin2, HIGH);
  digitalWrite(RelayPin3, HIGH);
  digitalWrite(RelayPin4, HIGH);

  digitalWrite(wifiLed, HIGH);  //Turn OFF WiFi LED
}

void loop() {
  ArduinoCloud.update();
  
  manual_control(); //Control relays manually

  if (WiFi.status() != WL_CONNECTED)
  {
    digitalWrite(wifiLed, HIGH); //Turn OFF WiFi LED
  }
  else{
    digitalWrite(wifiLed, LOW); //Turn ON WiFi LED
  }
}

void onSwitch1Change() {
  if (switch1 == 1)
  {
    digitalWrite(RelayPin1, HIGH);
    Serial.println("Device1 ON");
    toggleState_1 = 1;
  }
  else
  {
    digitalWrite(RelayPin1, LOW);
    Serial.println("Device1 OFF");
    toggleState_1 = 0;
  }
}

void onSwitch2Change() {
  if (switch2 == 1)
  {
    digitalWrite(RelayPin2, HIGH);
    Serial.println("Device2 ON");
    toggleState_2 = 1;
  }
  else
  {
    digitalWrite(RelayPin2, LOW);
    Serial.println("Device2 OFF");
    toggleState_2 = 0;
  }
}

void onSwitch3Change() {
  if (switch3 == 1)
  {
    digitalWrite(RelayPin3, HIGH);
    Serial.println("Device2 ON");
    toggleState_3 = 1;
  }
  else
  {
    digitalWrite(RelayPin3, LOW);
    Serial.println("Device3 OFF");
    toggleState_3 = 0;
  }
}

void onSwitch4Change() {
  if (switch4 == 1)
  {
    digitalWrite(RelayPin4, HIGH);
    Serial.println("Device4 ON");
    toggleState_4 = 1;
  }
  else
  {
    digitalWrite(RelayPin4, LOW);
    Serial.println("Device4 OFF");
    toggleState_4 = 0;
  }
}

i have included the arduino_secrets and thingproperties in my both pieces of code the difference is that i wanted 8 relay pins to be configured instead of 4 pins please ask if any thing is needed from me

this was the exact error

C:\Users\akki kisu\Documents\Arduino\libraries\ArduinoIoTCloud\src\property\Property.cpp:25:19: note: '#pragma message: No RTC available on this architecture - ArduinoIoTCloud will not keep track of local change timestamps .'
   25 |   #pragma message "No RTC available on this architecture - ArduinoIoTCloud will not keep track of local change timestamps ."
      |                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
c:/users/akki kisu/appdata/local/arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld.exe: sketch\Code_NodeMCU_Alexa_ArduinoIotCloud_4Relays.ino.cpp.o:(.text._Z14initPropertiesv+0x1c): undefined reference to `_Z15onSwitch1Changev'
c:/users/akki kisu/appdata/local/arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld.exe: sketch\Code_NodeMCU_Alexa_ArduinoIotCloud_4Relays.ino.cpp.o:(.text._Z14initPropertiesv+0x34): undefined reference to `_Z15onSwitch2Changev'
c:/users/akki kisu/appdata/local/arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld.exe: sketch\Code_NodeMCU_Alexa_ArduinoIotCloud_4Relays.ino.cpp.o:(.text._Z14initPropertiesv+0x40): undefined reference to `_Z15onSwitch3Changev'
c:/users/akki kisu/appdata/local/arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld.exe: sketch\Code_NodeMCU_Alexa_ArduinoIotCloud_4Relays.ino.cpp.o:(.text._Z14initPropertiesv+0x4c): undefined reference to `_Z15onSwitch4Changev'
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).
7
  • 3
    And... what is the "error" you got?
    – Majenko
    Oct 21, 2021 at 15:52
  • you can reduce the code by almost 90% if you use arrays to hold pin numbers and pin state ... if (toggleState[relay] == 0) { digitalWrite(RelayPin[relay], HIGH);
    – jsotola
    Oct 21, 2021 at 17:11
  • 2
    "Hey please help me with this error. Though I won't tell you what error it is." We are not magical. Please provide the full error output including the line where the error happened
    – chrisl
    Oct 21, 2021 at 17:30
  • did you remove the variables in the thing definition on Arduino IoT cloud? or you get error "Undefined reference to onSwitch1Change"?
    – Juraj
    Oct 21, 2021 at 19:12
  • What is the difference between your modified code and the original code? Look at it, and you are at least one step nearer to the reason. Oct 22, 2021 at 7:04

1 Answer 1

0

In Arduino IoT cloud the variables synced between the Cloud and the sketch are defined in the definition of the (IoT) thing. The variables are reflected into the thingProperties.h file which is maintained by the Cloud editor. The on<variable_name> functions in the sketch handle the synchronization of these variables. They are declared in the thingProperties.h

You removed the onSwitchChange functions, but you didn't remove the switchChange variables in the definition of the Thing. The thingProperties.h still contains declarations of onSwitchChange functions, but the linker can't find the definition of the function because you removed it from the sketch.

The errors are like this one:

undefined reference to `_Z15onSwitch1Changev'

To solve this, remove the switchChange variables from the thing in the Cloud UI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.