Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arduino as client #434

Open
atrent opened this issue Nov 5, 2019 · 18 comments
Open

Arduino as client #434

atrent opened this issue Nov 5, 2019 · 18 comments
Assignees

Comments

@atrent
Copy link

atrent commented Nov 5, 2019

Hi, do I understand correctly that, with the current Firmata lib for Arduino, it is not possible to use an Arduino as "client"? (e.g., having two Arduinos/ESP8266, one slave of the other)

If so, is there any development effort undergoing?

If I'm wrong (as I hope) can you point me towards the right doc/example?

Thank you

@soundanalogous
Copy link
Member

You will have to write an application for one Arduino, but yes the other can use Firmata since the Arduino Firmata library is written for both cases, however there are no example I'm aware of that use Firmata in this way since most user's motivation with Firmata is to control an Arduino using an application written in a language other than C/C++.

@soundanalogous
Copy link
Member

You can also use the Firmata library for communication without using the StandardFirmata sketch (StandardFirmata is just an example use of the library after all).

@atrent
Copy link
Author

atrent commented Nov 6, 2019

Could this be an example "FirmataBlink"? (sort of)

#include <Firmata.h>

void setup() {
    Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
    //Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
    //Firmata.attach(SET_PIN_MODE, setPinModeCallback);
    Firmata.begin(57600); // TODO cambiare seriale
}

void loop() {
    // FirmataBlink (D13 is on port 1?)
    Firmata.sendDigitalPort(1,255); // HIGH (all pins on port 1)
    sleep(1000);
    Firmata.sendDigitalPort(1,0); // LOW (all pins on port 1)
    sleep(1000);
}

This would be the client code (on the "master" Arduino) while the other Arduino would be flashed with a StandardFirmata.

@zfields
Copy link
Contributor

zfields commented Nov 6, 2019

That's not unreasonable, but your implementation uses the legacy Firmata.h header and implementation. A while back, I wrote the Firmata Marshaller and Parser to give more fine grain control for client libraries, and in fact, it is the implementation invoked by Firmata.h

What you are attempting to do, can be made much more simple by utilizing the FirmataMarshaller directly.

#include <FirmataMarshaller.h>

firmata::FirmataMarshaller marshaller;

void setup() {
  Serial.begin(57600);
  marshaller.begin(Serial);
  marshaller.sendPinMode(LED_BUILTIN, OUTPUT);
}

void loop () {
  marshaller.sendDigital(LED_BUILTIN, HIGH);
  sleep(1000);
  marshaller.sendDigital(LED_BUILTIN, LOW);
  sleep(1000);
}

EDIT: Added firmata namespace to pseudo-code.

@zfields zfields self-assigned this Nov 6, 2019
@atrent
Copy link
Author

atrent commented Nov 6, 2019

Thanks! I'll try it.
And you're still implying that on the slave Arduino a StandardFirmata sketch is running?

@zfields
Copy link
Contributor

zfields commented Nov 6, 2019

Yes, that is correct. Just to clarify terms, we consider what you're calling "slave" as the host, and "master" as a client of the host.

@atrent
Copy link
Author

atrent commented Nov 6, 2019

Yes I am calling:

  • master/client an Arduino instead of a standard use case PC/Raspberry/etc.
  • slave/server/host another Arduino as in the standard use case

@umar14
Copy link

umar14 commented Nov 14, 2019

@zfields

#include <FirmataMarshaller.h>

FirmataMarshaller marshaller;

void setup() {
  Serial.begin(57600);
  marshaller.begin(Serial);
  marshaller.sendPinMode(LED_BUILTIN, OUTPUT);
}

void loop () {
  marshaller.sendDigital(LED_BUILTIN, HIGH);
  sleep(1000);
  marshaller.sendDigital(LED_BUILTIN, LOW);
  sleep(1000);
}

Hello, I am trying to accomplish a similar thing. I tried compiling your code but it gives error.
'FirmataMarshaller' does not name a type

I am using the built-in firmata library in arduino ide (v2.5.8). Do I need to change something?

@atrent
Copy link
Author

atrent commented Nov 14, 2019

I think that was sample code to give an idea, the real code should be something like:

#include <FirmataMarshaller.h>

firmata::FirmataMarshaller marshaller;

void setup() {
    Serial.begin(57600);
    marshaller.begin(Serial);
    marshaller.sendPinMode(LED_BUILTIN, OUTPUT);
}

void loop () {
    marshaller.sendDigital(LED_BUILTIN, HIGH);
    delay(1000);
    marshaller.sendDigital(LED_BUILTIN, LOW);
    delay(1000);
}

@umar14
Copy link

umar14 commented Nov 14, 2019

@atrent Thank you so much for the quick reply. I am trying to control arduino mega using esp8266 board and mqtt over WiFi. This just made everything a piece of cake! 😀

@atrent
Copy link
Author

atrent commented Nov 14, 2019

I still have to try a setup using ESP8266 with an Arduino slave, did you already try? Did you succeed? Can you post here your setup and final code? Thanks

@umar14
Copy link

umar14 commented Nov 14, 2019

Yes I did. Tried it just now and it works flawlessly.

I am using NodeMCU board as firmata master/client and flashed the above code you just mentioned.

However, one slight change. When flashing to NodeMCU or any other esp8266 board, you would need to change the BUILTIN_LED variable to the actual pin number of the slave (Arduino uno in my case). You specifically have to mention pin 13 and not write BUILTIN_IN.

I flashed the FirmataStandard.ino (present in Arduino IDE) example file on Arduino slave. No changes needed.

Connected NodeMCU TX pin to Arduino uno RX pin and GND to GND.

That's all. The LED on the slave started blinking. Thank you!

@atrent
Copy link
Author

atrent commented Nov 14, 2019

Yep, the LED_BUILTIN problem (not always mapped correctly on ESP8266 boards) is known. Thanks again

@zfields
Copy link
Contributor

zfields commented Nov 14, 2019

@atrent Can this issue be considered resolved?

Also, thank you for fixing up my pseudo-code!

@atrent
Copy link
Author

atrent commented Nov 14, 2019

I think it would be great to include an example in the lib package, then resolved ;)

@umar14
Copy link

umar14 commented Nov 14, 2019

An example with callbacks please...

@zfields
Copy link
Contributor

zfields commented Nov 14, 2019

Loud and clear. The best "example" is probably Firmata.h, but I know what you guys are asking for and I'll see if I can't whip something up.

@atrent
Copy link
Author

atrent commented Nov 14, 2019

Yep, an example in the "examples" directory, such as a "RemoteBlink.ino" sketch (to be coupled to StandardFirmata...)
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants