Adding some functions to blynk code not working

Hi guys mabe you can help me. My goal is to take the blynk example firmware wich is working fine with my particle core and just add 2 functions available to be call by ifttt one of em turn the relays on and the other turn them off. For doing this i wanted to mix the original blynk code with the relayshield code but i am having an error when verifying the code.

This is the code i ve modified:

    // This #include statement was automatically added by the Particle IDE.
#include "RelayShield.h"

/**************************************************************
 * 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
 *   Blynk community:            http://community.blynk.cc
 *   Social groups:              http://www.fb.com/blynkapp
 *                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 * 
 * WARNING: It is recommended to use SparkCorePolledTimer library
 *          to make periodic actions (similar to SimpleTimer on Arduino).
 *
 **************************************************************/
//#define BLYNK_DEBUG // Uncomment this to see debug prints
#define BLYNK_PRINT Serial
#include "blynk/blynk.h"

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

// Create an instance of the RelayShield library, so we have something to talk to
RelayShield myRelays;

// Create prototypes of the Spark.functions we'll declare in setup()
int relayOn(String command);
int relayOff(String command);


// Attach a Button widget (mode: Switch) to the Digital pin 7 - and control the built-in blue led.
// Attach a Graph widget to Analog pin 1
// Attach a Gauge widget to Analog pin 2

// No coding required for direct pin operations!

void setup()
{
    Serial.begin(9600);
    delay(5000); // Allow board to settle
    Blynk.begin(auth);

 //.begin() sets up a couple of things and is necessary to use the rest of the functions
    myRelays.begin(2);

    // Use myRelays.begin(2); if you have the square, white RelayShield (from the Core)
    // to use, just add a '2' between the parentheses in the code above.

    // Register Spark.functions and assign them names
    Particle.function("relayOn", relayOn);
    Particle.function("relayOff", relayOff);

}

// Attach a Button widget (mode: Push) to the Virtual pin 1 - and send sweet tweets!
BLYNK_WRITE(V1) {
    if (param.asInt() == 1) { // On button down...
        // Tweeting!
        // Note:
        //   We allow 1 tweet per minute for now.
        //   Twitter doesn't allow identical subsequent messages.
        Blynk.tweet("My Particle project is tweeting using @blynk_app and it’s awesome!\n @Particle #IoT #blynk");
        
        // Pushing notification to the app!
        // Note:
        //   We allow 1 notification per minute for now.
        Blynk.notify("You pressed the button and I know it ;)");
    }
}

// Attach a ZeRGBa widget (mode: Merge) to the Virtual pin 2 - and control the built-in RGB led!
BLYNK_WRITE(V2) {
    int r = param[0].asInt();
    int g = param[1].asInt();
    int b = param[2].asInt();
    if (r > 0 && g > 0 && b > 0) {
        RGB.control(true);
        RGB.color(r, g, b);
    } else {
        RGB.control(false);
    }
}

void loop()
{
    Blynk.run();
    
    if (ModeBtnPressed()) {
        Blynk.notify("Mode button was pressed");
    }
}

// *** Utility functions

bool ModeBtnPressed() {
    if(millis() > 5000) {
        if(BUTTON_GetDebouncedTime(BUTTON1) >= 50) {
            BUTTON_ResetDebouncedState(BUTTON1);
            return 1;
        }
    }
    return 0;
}

int relayOn(String command){
    // Ritual incantation to convert String into Int
    char inputStr[64];
    command.toCharArray(inputStr,64);
    int i = atoi(inputStr);
    
    // Turn the desired relay on
    myRelays.on(i);
    
    // Respond
    return 1;    
}

int relayOff(String command){
    // Ritual incantation to convert String into Int
    char inputStr[64];
    command.toCharArray(inputStr,64);
    int i = atoi(inputStr);
    
    // Turn the desired relay off
    myRelays.off(i);
    
    // Respond
    return 1;    
}

and this is the error shown:

01_particle.cpp: In function 'int relayOn(String)':
01_particle.cpp:119:5: error: 'myRelays' was not declared in this scope
     // Ritual incantation to convert String into Int
     ^

01_particle.cpp: In function 'int relayOff(String)':
01_particle.cpp:132:5: error: 'myRelays' was not declared in this scope
     // Ritual incantation to convert String into Int
     ^

make[2]: *** [../build/target/user/platform-0-l01_particle.o] Error 1
make[1]: *** [user] Error 2
make: *** [main] Error 2
Error: Could not compile. Please review your code.

I see that the compiler is telling me that my functions are not declared but i thought that declaring them on the main code that would be enough. So how can i fix this?

Thanks folks for any advice.

Javier

@javiercviegas, the issue is not that your functions are not declared, it’s that:

01_particle.cpp:132:5: error: 'myRelays' was not declared in this scope

You have this line at the top of the file:

RelayShield myRelays;

but you don’t have a supporting library for the RelayShield object! :wink:

Thanks for answering peekay but i did include the RelayShield at the top. Doesn't that include the library?

@javiercviegas, I missed that! To add the relay shield library did you select that library whiled editing your app and do an “include in app” to attach the library? The reason I ask is that the include statement should read:

#include "RelayShield/RelayShield.h"

No i am having troubles including libraries that way. i just copy paste the include statement from another example. If i use the include in app button then when i verify the code tells me that RelayShield.h was no found. But i will try it that way.

Hmmm now the error is:

32+0 records in
32+0 records out
32 bytes (32 B) copied, 0.000834201 s, 38.4 kB/s
4+0 records in
4+0 records out
4 bytes (4 B) copied, 0.0943698 s, 0.0 kB/s
In file included from blynk/BlynkApi.h:18:0,
                 from blynk/BlynkApiParticle.h:14,
                 from blynk/BlynkParticle.h:16,
                 from blynk/BlynkSimpleParticle.h:14,
                 from blynk/blynk.h:2,
                 from 01_particle.cpp:24:
blynk/BlynkProtocolDefs.h:89:0: warning: "htons" redefined [enabled by default]
         #define htons(x) ( ((x)<<8) | (((x)>>8)&0xFF) )
 ^
In file included from ../hal/src/core/platform_headers.h:23:0,
                 from ./inc/application.h:32,
                 from 01_particle.cpp:2:
../platform/NET/CC3000/CC3000_Host_Driver/socket.h:195:0: note: this is the location of the previous definition
 #define htons(A)     ((((UINT32)(A) & 0xff00) >> 8) | \
 ^
In file included from blynk/BlynkApi.h:18:0,
                 from blynk/BlynkApiParticle.h:14,
                 from blynk/BlynkParticle.h:16,
                 from blynk/BlynkSimpleParticle.h:14,
                 from blynk/blynk.h:2,
                 from 01_particle.cpp:24:
blynk/BlynkProtocolDefs.h:90:0: warning: "htonl" redefined [enabled by default]
         #define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \
 ^
In file included from ../hal/src/core/platform_headers.h:23:0,
                 from ./inc/application.h:32,
                 from 01_particle.cpp:2:
../platform/NET/CC3000/CC3000_Host_Driver/socket.h:187:0: note: this is the location of the previous definition
 #define htonl(A)    ((((UINT32)(A) & 0xff000000) >> 24) | \
 ^
In file included from blynk/BlynkApi.h:18:0,
                 from blynk/BlynkApiParticle.h:14,
                 from blynk/BlynkParticle.h:16,
                 from blynk/BlynkSimpleParticle.h:14,
                 from blynk/blynk.h:2,
                 from 01_particle.cpp:24:
blynk/BlynkProtocolDefs.h:94:0: warning: "ntohs" redefined [enabled by default]
         #define ntohs(x) htons(x)
 ^
In file included from ../hal/src/core/platform_headers.h:23:0,
                 from ./inc/application.h:32,
                 from 01_particle.cpp:2:
../platform/NET/CC3000/CC3000_Host_Driver/socket.h:199:0: note: this is the location of the previous definition
 #define ntohs                   htons
 ^
In file included from blynk/BlynkApi.h:18:0,
                 from blynk/BlynkApiParticle.h:14,
                 from blynk/BlynkParticle.h:16,
                 from blynk/BlynkSimpleParticle.h:14,
                 from blynk/blynk.h:2,
                 from 01_particle.cpp:24:
blynk/BlynkProtocolDefs.h:95:0: warning: "ntohl" redefined [enabled by default]
         #define ntohl(x) htonl(x)
 ^
In file included from ../hal/src/core/platform_headers.h:23:0,
                 from ./inc/application.h:32,
                 from 01_particle.cpp:2:
../platform/NET/CC3000/CC3000_Host_Driver/socket.h:192:0: note: this is the location of the previous definition
 #define ntohl                   htonl
 ^
In file included from blynk/BlynkParticle.h:16:0,
                 from blynk/BlynkSimpleParticle.h:14,
                 from blynk/blynk.h:2,
                 from 01_particle.cpp:24:
blynk/BlynkApiParticle.h:87:6: warning: #warning "analogInputToDigitalPin not defined => Named analog pins will not work" [-Wcpp]
     #warning "analogInputToDigitalPin not defined => Named analog pins will not work"
      ^
01_particle.cpp:25:37: fatal error: RelayShield/RelayShield.h: No such file or directory
  *          to make periodic actions (similar to SimpleTimer on Arduino).
                                     ^

compilation terminated.
make[2]: *** [../build/target/user/platform-0-l01_particle.o] Error 1
make[1]: *** [user] Error 2
make: *** [main] Error 2
Error: Could not compile. Please review your code.

You need to select a target processor and a firmware version. The the looks of the errors, this is not done.

It may be the way you included it. You need to "include in app", then select you app and then it will include it. The library should show up as being part of your app.

Sorry for the noobie questions but how can i select the processor and firmware? i am going to include the library again and see what happened.

@javiercviegas, bottom left hand corner is a circle icon that looks like a gun sight. Hovering your mouse will show “target”. Click on that and choose one of your devices.

1 Like

You were right! I started from scratch and added both libraries the way you suggested and selected the target core and version and now no error was found! Lets see if ifttt see the functions. Thanks peekay!!

1 Like

The docs would be the place to look first :wink:
https://docs.particle.io/guide/getting-started/build/photon/#using-libraries

1 Like