Skip to content

Issue with multiple Custom functions #290

@shockagent

Description

@shockagent

I made a very simple sketch and I defined 2 functions to use with aREST.
I use an Arduino UNO with Ethernet shield.
I use VSCode with PlatformIO.

The first function is working as expected, only if I don't call it with a query string, BUT if I pass it a query string, the second function is called instead.
The second function doesn't read params I pass to it: the params string is just empty.

Source code:
`
#include <SPI.h>
#include <SD.h>
#include <Ethernet.h>
#include <aREST.h>
#include <avr/wdt.h>

#define PORT 1977

// Enter a MAC address for your controller below.
byte mac[] = {0x90, 0xA2, 0xDA, 0x0E, 0xFE, 0x40};

// IP address in case DHCP fails
IPAddress ip(192, 168, 1, 77);

// Ethernet server
EthernetServer server(PORT);

// Create aREST instance
aREST rest = aREST();

// Variables to be exposed to the API
int ev1, ev2, ev3;
boolean sd;

// Declare functions to be exposed to the API
int StartSD(String);
int AFunction(String);

void setup(void)
{
// Start Serial
Serial.begin(9600);
while (!Serial)
{
;
}

pinMode(10, OUTPUT);

// Init variables and expose them to REST API
sd = false;
ev1 = HIGH;
ev2 = HIGH;
ev3 = HIGH;

// rest.variable("humidity", &humidity);
rest.variable("ev1", &ev1);
rest.variable("ev2", &ev2);
rest.variable("ev3", &ev3);
rest.variable("sd", &sd);

// Function to be exposed
char init[] = "init";
char prova[] = "prova";
rest.function(init, StartSD);
rest.function(prova, AFunction);

// Give name & ID to the device (ID should be 6 characters long)
static const char name[] = "ev";
rest.set_id("008");
rest.set_name(name);

// Start the Ethernet connection and the server
Ethernet.begin(mac, ip);

server.begin();
Serial.print("server is at ");
Serial.print(Ethernet.localIP());
Serial.print(":");
Serial.println(String(PORT));

// Start watchdog
wdt_enable(WDTO_4S);
}

void loop()
{

// listen for incoming clients
EthernetClient client = server.available();
if (client)
{
Serial.print("client connected ");
Serial.println(client.remoteIP());
rest.handle(client);
}
wdt_reset();
}

// Custom function accessible by the API
int StartSD(String params)
{
Serial.print("Parameters: ");
Serial.println(params);

if (!sd)
{
if (SD.begin(4))
{
sd = true;
Serial.println("Init SD: OK");
return 1;
}
else
{
sd = false;
Serial.println("Init SD: error");
return 0;
}
}
else
{
Serial.println("Init SD: already init");
return 1;
}
}

int AFunction(String params)
{
Serial.print("Function Params: ");
Serial.println(params);
return 1;
}
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions