marcoschwartz / aREST

A RESTful environment for Arduino
http://aREST.io/
Other
1.2k stars 279 forks source link

rest.function() bug: command is improperly passed to function #308

Open segfaultDelirium opened 2 years ago

segfaultDelirium commented 2 years ago

I am using modified Ethernet example (code below). I use postman to send GET request on following url: http://192.168.0.192/led?command=1 I should be always getting the same result: { "return_value": 1, "id": "008", "name": "dapper_drake", "hardware": "arduino", "connected": true } but 50% of requests have correct return value: 1. and the other half have return value 0 or I get 3000 lines of garbage see here: https://pastebin.com/vY1vcL4t.

Could you direct me to what can be causing this issues?

arduino code:

#include <SPI.h>
#include <Ethernet.h>
#include <aREST.h>
#include <avr/wdt.h>

// 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, 0, 20);

// Ethernet server
EthernetServer server(80);

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

void setup(void)
{
  // Start Serial
  Serial.begin(115200);

  // Function to be exposed
  rest.function("led",ledControl);

  // Give name & ID to the device (ID should be 6 characters long)
  rest.set_id("008");
  rest.set_name("dapper_drake");

  // Start the Ethernet connection and the server
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());

  // Start watchdog
  wdt_enable(WDTO_4S);

}

void loop() {

  // listen for incoming clients
  EthernetClient client = server.available();
  rest.handle(client);
  wdt_reset();
}

// Custom function accessible by the API
int ledControl(String command) {
  Serial.print("passed command: ");
  Serial.println(command);
  // Get state from command
  int state = command.toInt();
  return state;
}

This is serial monitor output:

server is at 192.168.0.192
passed command: 1
passed command: 
passed command: 1
passed command: ! @T@P!1 1
Asar!Aae`t  @oataa`Ran`iae!7 2!.
passed command: 1
passed command: 
passed command: 1
passed command: 
passed command: 1
passed command: 
passed command: 1
passed command: 
passed command: 1