Send commands to more than one particle core

So is it possible to send the same command or even multiple commands to more than one particle with separate device ID’s. Say I have 3 relay boards I wish to control the same relay on all 3 boards. Each one has a separate Device ID and the same Access token.
1, How can I send the same command to multiple Particle Cores?
2. How can I send different commands at the same time to multiple Particle Cores?

You could have all the cores subscribed to the same event and have the same handler function.

Having multiple options in the event payload is one way to do this. Like if the payload was 1:1:0, the first and second relay would turn on but the third would turn off.

1 Like

How is that possible?

<form action="https://api.particle.io/v1/devices/38002500134734343231xxxx/led?access_token=4d1ec86df976e0a159ec72f80f09da841e9fxxxxx" method="POST">
   
<input type="submit" name="args" value="Complete!">
<input type="submit" name="args" value="Aluminum">
<input type="submit" name="args" value="Paper">
<input type="submit" name="args" value="Glass">  
<input type="submit" name="args" value="General">

There are 11 floors. Each one has a chute door. A selector touch screen on one floor allows the person to select from either metal, paper, glass or general which rotates a turret in the basement. I have hardwire now but want to go wireless so I was building a relay controller which tells the other floors to lock their doors when a door on the top floor is open and while a selection session is being made. I need to send at the time a command is sent locally to the relay to release the door on the floor I’m on while locking the subsequent doors on all the floors below. So I’d have to send different commands to floors below and an open command on the current floor I’m on. I hope this clarifies what I’m trying to do. Sorry if it’s too
long.

Then the particle code

int ledToggle(String command) {
    if (command=="Complete!") {
        digitalWrite(LED, HIGH);
        digitalWrite(relay1, LOW);
        digitalWrite(relay2, LOW);
        digitalWrite(relay3, LOW);
        digitalWrite(relay4, LOW);
        digitalWrite(LED, HIGH);
        delay(200);
        digitalWrite(LED, LOW);;
        return 0;
    }
    return -1;
}

Not quite, at least for me. Is one relay board controlling the doors on all 11 floors? Your question indicates that you want to send commands to multiple Particle devices, so how many of those are there? One for each floor?

I’m sorry… I can see how that would be confusing. There are 12 photons controlling a 4 channel relay board. Each photon has a separate device ID but uses the same access token. Each floor has one 4 channel relay board controlling the door actuator, 2 lights and a fan
Let’s say I’m on floor 10. I have one floor above me and 11 floors below me. If I choose to throw away plastic, I select plastic and my door actuator unlocks. All the floors above and below me need to stay locked. So I send a command to each of them to let those floors know the chute is busy and a red LED actuates and Green LED turns off. A command is sent to the 12th photon which controls a stepper motor driver which rotates the turret into position to allow the chute to distribute the discarded plastic into the plastic bin. Then once the plastic has hit the bin, an all clear is sent to all floors the chute is reopened and ready for more discarded garbage. (A light turns on and the touch screen is notified it’s ready)
I was just thinking about making a C# program and running it on a PC if I can’t do it in HTML. Seems like it might be a whole lot easier with an API. The GUI on the touch screen on each floor is a mobile application which uses HTML.

There are 12 photons controlling 12 4 channel relay boards…

So, you can control all 12 Photons with one published event as @nrobinson2000 suggested. Each Photon should “know” which floor it is on, so you could send something like “0:0:0:0:0:0:0:0:0:0:1:0” to open the door on the 10th floor while locking all the others. The Photon on the first floor would take the first number to decide what to do, the one on the second floor, the second number, etc. All 12 Photons would subscribe to that same event.

2 Likes

That’s genius. But now i’m confused on how to get 12 different photons to take a command from the same event because each of them have a different device ID. So when the HTML Post method which contains the device ID sends, so far I’ve only been able to do one device at a time. How do you send to multiple devices with different device ID’s in the method action = post ?

1 Like

Particle publications don’t take a device ID. A publication is like a radio broadcast, and any device that “listens to that station” (i.e. subscribes to that event) will get the message. I’m not an HTML guy, so I don’t know the syntax, but it shouldn’t require a device ID. For instance, on the CLI, you just do “particle publish eventName someData --private”, and that could be heard by all your devices (if they subscribe to “eventName”)

I’ll have to think about that. I see what you’re saying but maybe my development history has led me to believe that each device listens to a command from the device ID and access token so the web page can have access to the device. I was thinking it was some sort of security. I have an example of it here
http://gigilife.com/Recycle/index2.html
which I simply tell the relays to do different things from one command. It also has a problem with the return value which sends me to a blank page. I’ve seen no other code that shows me any other way to accomplish it. I’ll do some research on the particle publishing evenName arg.

This sounds like you’re calling a Particle.function rather than publishing an event. A function call, does go to one particular device ID. As I said, I don’t know how to do a publish in HTML or C#, but for reference, setting up the POST for a publish in Swift looks like this,

let url = URL(string: "https://api.particle.io/v1/devices/events")
        var postRequest = URLRequest(url: url!, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 10)
        postRequest.httpMethod = "POST"
        let bodyData = "name=\(name)&data=\(data)&private=true&ttl=\(60)&access_token=\(accessToken!)"
        postRequest.httpBody = bodyData.data(using: String.Encoding.utf8)

As you can see, there is no deviceID in that POST, only the access token.

I see. So publishing to an event can send to more than one at a time and I’m just calling one function with html. Thank you. Last question and I’ll stop bothering you. Do you know of any research documents on working with events I can read. Thank you for all your help.

There’s the documentation on publish/subscribe to begin with.
This has helped me in the beginning: Tutorial: Getting Started with Spark.publish()

Using javascript, you don’t get that return page, much cleaner. Why bother with such an elaborate interface for something seemingly so simple? You could just have some physical buttons, or a dial to select the type of waste.

1 Like

@Anthony, I’m late to the discussion, but could you elaborate on why you’d need a HTML request at all?
If you have a dedicated Photon at each of the chutes, why not let the devices do the publishing without any need for a secondary “server”?

Your Photons can read buttons (as mentioned by @Moors7) or even touch screens if you wish and do all the stuff self-contained.
If you need or just wish you can have a secondary machine monitor/log the actions, but that would just be a passive observer.

BTW, the tutorial @Moors7 linked was put together before the company was renamed to Particle, so whenever you read Spark.publish() or Spark.subscribe() it’s now Particle.publish() and Particle.subscribe().

And the prime source for first input on Particle features would be the official documentation
https://docs.particle.io/reference/firmware/photon/#particle-publish-
https://docs.particle.io/reference/firmware/photon/#particle-subscribe-
https://docs.particle.io/guide/getting-started/examples/photon/#the-buddy-system-publish-and-subscribe

For your particular use-case, you could try to re-think the whole approach in terms of what message you publish and how the recipients react to it.
From what I understood, the initiating device doesn’t really need to tell each other device what to do, but rather only tell the crowd what is happening to it and the others should “conclude” what that means to them. It appears that it actually is irrelevant which floor demands exclusiv access to any particular chute and consequently which floors have to be blocked, so no need to communicate that either.
e.g. with four chutes and someone activating chute #3 at any floor the initiating device (“DeviceX”) would just need to Particle.publish("unlockChute_DeviceX", "3", PRIVATE) and all other devices would lock chute #3 (although I’d think locked should be the default state anyway) while the initiating DeviceX would wait for its own event to return and unlock the chute if the event it received was actually its own (and not e.g. “DeviceY”). This way it makes sure that there was no “simultaneous” unlock request for #3 from any other device.
The device controlling the turret also knows what to do when it has to expect anything from chute #3 - when it’s done it could Particle.publish("lockAll", "3", PRIVATE) as dedicated lock event or I’d just reuse the existing logic with Particle.publish("unlockChute_none", "3", PRIVATE).

2 Likes

Moors7, We currently have buttons in place which are archaic. We’re trying to upgrade the facility in Dubai with touch screens and wireless transactions which will hook to our Qrimp server to notify us when each of the bins are getting full. We have a sonic sensor above each bin in the basement which tells us how full it’s getting. Once it gets to a certain point, it sends an SMS to the waste company to let them know they need emptying. On the wall will be a touch pad (Android based application) which is going to send commands to each floor. I’ve used buttons before and there have been times a floating pin will read high even though I’ve used a resistor to gnd and it sends out anyway. It doesn’t happen all the time but it is intermittent. For this reason I chose to go direct command to control option. User select option and event transpires. Not reading pin state but simply sending pin state commands high or low seemed more bulletproof. I have purchased some of the newer mesh but they won’t be ready until September so I’m just going ahead with the design using the Photons and the next building I’ll use the mesh since they want the building completed by next month.

ScruffR, I like that . Thanks for the links. I was with Spark when it was spark in the beginning doing gunshot detection device notifications in your early years. Then I went to work for surgeons doing PIC Microcontroller work and got out of Arduino all together for about 2 years and now I’ve been asked to put a round peg in a square hole. I’m used to communicating to only one Core at a time. I’ve never sent to multiple cores at once. I didn’t even know if it was possible. I have to hand this project off to developers in India and their preference is HTML to import into their phone application. The goal of the building is to make the system accessible by mobile app, Each tenant will receive a tag which identifies their trash as their possession. This will help ensure that each tenant doesn’t select the wrong item group in the menu option and then throw something totally different down the chute. (We know whose trash belongs to which tenant) So once the relay boards function in the field, I go to Abu dhabi to install and test the system out. I’m nervous to say the least because there are 8 more buildings larger than this one they want to install the system. I’m just the first guy low down on the totem pole that recommended they go with WiFi and more particularly with Particle since I’ve had good luck with it and it doesn’t crash like the ESP8266 does all the time. Thanks for the links. I’ll get to reading.

1 Like