Help a cloud programming newb

Alright, I’m getting a bit overwhelmed and I’m hoping maybe someone can help.

I have several particle photons that I wrote the code for (or a lot of the code, anyway). I have a couple that run automated gates with a simple relay. They also publish events using their device name in the event when they change state. They also listen for a general event so I can ask with other devices for them to report their current state. Then I have another device that can control the gates by publishing commands for specific gates. It also listens for state changes in case some OTHER device changes them (ie. the particle iOS app, IFTTT, etc) and sets some LEDs so I can quickly look at a thing to know the gate status. It even asks for the state of the gates when it boots, too. All this works fine between devices and was pretty trivial to get working thanks to the Particle functions.

Okay, great. I can control my gates with IFTTT. I can see status and control gates with a Photon connected to a couple buttons and an LCD. Now what I really want is some kind of web interface so I could do gate scheduling. IFTTT actually works fine for regular scheduling, but really sucks if I just want to do something like “open the main gate from 1pm to 3pm this coming Saturday only.” Anyone got any sample code available for something like that? I’m not even sure the best way to do it. I know the basics of setting up an AWS instance with a web server or something, but I haven’t ever done any Javascript programming (I’m a better IT guy than programmer, but strangely when I do program I’m a better C programmer than anything else, which doesn’t help much when it comes to web things).

Any pointers to something with source available that does something even remotely similar to this? I’ve done some searching and found this handy guide to the Particle Javascript library:

https://docs.idew.org/internet-of-things-project/references-for-wiring-and-coding/particle-cloud

But that’s still a long ways from there to an app like I’m talking about.

–Donnie

When you talk of scheduled opening/closing times, do you really want to have that schedule stored remotely?
Would it not be more practical to just send the schedule to the individual device, have it stored there and let it do its thing (even offline)?

When opting for the later approach you could have a look at this library
https://build.particle.io/libs/TimeAlarms/0.0.3/tab/example/TimeAlarmsExample.ino

With that you’d only need to implement a Particle.function() to receive a formated schedule entry, which parses the individual time and cycle fields, stores them to EEPROM and sets the respective TimeAlarm

That’s definitely a reasonable suggestion. I was hoping to leave the code on the gate controller as minimal as possible for reliability reasons, but I don’t think this goes too far.

That still leaves me needing a user interface to send and edit the schedule, though.

—Donnie

Hi Donnie,

and when you say that you need an UI to send and edit the schedule, do you think the Particle console would be enough for that?

https://console.particle.io/devices

This is in line with what @ScruffR suggested in the post above:

EDIT: or you wanted an UI so other people than you to be able to set that schedule?

Cheers

2 Likes

I wasn’t quite thinking that rudimentary, but it would be a simple starting point.

But in order to pick a solution the context wants to be established and then one can hone in on a viable solution.

Blynk would be a simple approach for a mobile app.
There are also lots of approaches to issue a Particle.function() request without the Particle JS API although I’d suggest using that (it’s not that complicated :wink: )

@djb_rh, how would you rate your JS skillset?

1 Like

My JS skill set is nonexistent. That said, if I found any code somewhat close, I’m sure I could learn. Without a starting point, I’m less likely to figure it out quickly enough to keep my interest.

But yes, I would like an interface I could make secure with its own user/pass that I could then give to my family to use to control the gate. I’ve thought about a Blynk app instead, and that may be the better way to go. I’ve got a couple other ideas outside of this gate (I use Photons for a few things outside of my gates) that could use something like Blynk, but haven’t bubbled them up the priority list yet.

Anyway, good discussion so far. Much appreciated.

In my ideal world, there would be an iCloud integration to the Apple Calendar app. Then I could just create a shared calendar with repeating events for the scheduled opening and then I could modify those as needed (and share the calendar with my family so they could do the same). But I don’t think there’s any way to do that on the Apple side, so that’s a non-starter.

—Donnie

Hi @djb_rh

Seems you are in good hands here, so I will just add my 2 cents worth :slight_smile: But check with @gusgonnet, as he is really good at implementing UI.

BLYNK is is easy enough to get going and have two sets of timers, Simple and Advance. This will let you run some schedules, but you will have to implement a schedule per gate as each event will trigger a pin (or multiple pins I suppose might be possible). I used Blynk for the very purpose of scheduling running times for pumps due to its easy implementation.

Referring to the image below and pertaining to operational timers:

  1. MANUAL - Manually turn load on/off - on demand

  2. SCHEDULE - First timer. Using simple timer as no “day of the week” is required. You can set on/off times.

  3. POWER SAVING - Second timer schedule. In your case this can be used for Gate 2. I a using it for reduce run times. You can set on/off times.

  4. MASTER ON/OFF - Bypasses the entire system and shuts off all timers when set to HIBERNATE

Having said this and IF I understand correctly, your requirement is somewhat more complex. It seems you are looking to implement a schedule per device as well as having the option to manually override this schedule by setting another temporary schedule, not only on demand and not necessarily recurring. I suppose this too is possible using another Timer instance called i.e “Manual override” that can be utilised for this purpose??

Hope this helps.
Friedl.

1 Like

To be clear, each gate has its own Photon. They are in physically different places and thus this is required. They run the same exact code, but they do use their own device name when they publish events and to know what events to listen for so that they can do different things at different times.

I could have made things a lot simpler by just hard coding a name into each one and flashing them with slightly different code, but that just seemed so much less elegant.

—Donnie

Hi @djb_rh -

I assumed as much yes :slight_smile: Sticking to BLYNK as UI;

This will be possible from a single app utilising the Device Selector (or Blynk Tiles) widget in combination with some timers. If you have a look at some Youtube video’s on BLYNK it might be more clear.

Depending on how frequently these schedules change, I would most certainly look into the possibility of scheduling opening/closing via a calendar. Currently though, this is outside of my capabilities so I hope someone else can assist if this is direction you chose follow.

IF the schedules do not change often and IF you are happy with a mobile UI , I think BLYNK might be a workable solution for you.

Regards,
Friedl.

So how tough is it to mix in custom code with the Blynk firmware? I started using the Photon by buying some ncd.io boards with relays built in, and they are on I/O extender chips, so they’ll need additional code. And then I’d need to also add in my event stuff to continue working with my other things.

—Donnie

HI Donnie -

Hmmm… It should be fairly simple. If your Photon is triggering the relays now, you have done most of the hard work :slight_smile:

I would suggest watching the very brief tutorial videos just so you can familiar with how to connect the UI to a device. If you tried to implement and are having some problems, I can try to assist but would need to see some code. You can. also have a look at this project I did, you can easily see how I implemented the BLYNK code triggering a relay. Basically the exact thing you want to do, I am just switching on pumps as apposed to opening gates :slight_smile:

EDIT: Just to agree with @ScruffR and @gusgonnet… If you are capable of doing so, I agree with having the schedule stored locally on the device. As mentioned, it will allow the device to run even when offline. I have not been able to get this done and touch wood, so far I have not been let down by having the scheduled stored in the UI.

Regards,
Friedl.

1 Like

You’re absolutely right that storing the schedule on the device is the most reliable way to do this.

It’s funny…when I bought the NCD board I stumbled across it because it had IFTTT support and I had no idea about how easy it was to program the Photon. Originally I didn’t have any intention of doing that. But I was already doing some Arduino stuff by the time I got it and decided due to the built in WiFi I’d try a Photon in place of something I was doing with an Arduino. Then I found out how nice the environment was and how good the Photon was and started getting ideas for enhancing my gate setup.

Using IFTTT for the regular scheduling has been more robust than I thought it would be, tbh. It’s just a lot harder than I’d like to do something like “don’t close the gate at 6pm today even though IFTTT is going to tell you to.” I’ve intended to code something to do that, but every solution I come up with seems like it would make the entire system less reliable.

So yeah, building scheduling into the firmware instead of using IFTTT probably makes sense. I use a Photon to drive an actual clock in a different project and was surprised in early versions of the code that there’s very little need to re-sync the time with the network as these keep time VERY well.

—Donnie

So I think the “CronAlarms” library may be the way to go for local scheduling on the Photon. Should be relatively easy to pass cron strings via cloud events. It hit me that Unix cron was exactly the type functionally I wanted so I searched for “Arduino cron” and bam, found that. That generally works better than Particle searches but works just the same in the end.

But that still leaves the user interface. Shooting for the moon, it would be interesting to hack something like this:

to generate the proper strings when new events were created or deleted and send them. Might be above my pay grade, though. But to accomplish my full goal, I do need some kind of app or web UI that can do it. Still thinking and googling.

—Donnie

2 Likes

Hi @djb_rh -

Glad you found what you are looking for.

With regards to UI, it is no easy feat unfortunately, unless you scale up to large numbers and don’t mind paying up :slight_smile: Quick question, can your calendar nog be the UI, scheduling an “event” for each gate? I Also suppose this depends on how many gates you have. The user experience might be just fine for 10 or so gates, but once you reach more, it might become troublesome to do on a calendar.

Depending on the scale of the project, I would opt for the simplest (as few 3rd parties as possible) and most easy to use experience for the user. If you project is large enough to justify some expenses, I would suggest having a chat with @gusgonnet about custom apps designed to your requirement :slight_smile:

Regards,
Friedl.

1 Like

Reliability is not a matter of feature or code line count. It’s only governed by the amount of thought you put into planning and reliable coding practices.

2 Likes

The more I look and think about this, the more I think the right answer is a lightweight CalDAV server hacked to generate the proper cron strings when changes to a calendar are made and then publish them as Particle events that my gate controller would pick up. Then I could use iCalendar as the UI.

Definitely above my pay grade, however. Would definitely consider paying someone to implement something like this, though.

–Donnie

If you want to write an app that can call Particle functions and read Particle variables, you might want to look at our project: https://github.com/TeamPracticalProjects/MIT-App-Inventor-Particle-Photon-test. It shows you how to code up apps in MIT App Inventor 2 that can talk to your Particle devices. It also includes a setup template that makes it easy to select the device that you want the app to talk to. Right now, MIT App Inventor 2 only supports Android, but iOS support is supposed to happen “Real Soon Now”.

Once you get the hang of it, creating these apps is very simple. They work much better than IFTTT (which tends to have variable delays and sometimes looses events altogether) and you have a lot more programmability than with Blynk.

Interesting. Currently, iOS support would be a must.

I’d still need to do something like add CronAlarms to my code and then use App Inventor to set it, but I forgot to look into app making platforms like that.

Is there a good way to keep up with App Inventor news so something would ping me when the iOS version drops?

–Donnie

The App Inventor Community site is where I would go for the latest information:

Hi Donnie.

I’ll provide some rough pointers on how I’d likely go about version1:

  • A schedule like you talk about is just a table of off/on events. On at 1pm with a full date stamp, Off at another datestamp.
  • I like using Firebase for simple database operations with great javascript libraries. Your data structure for this would essentially be a list of documents, each document having a timestamp and an action and possibly a gate ID and a completed flag.
  • You then need something to periodically query the Firebase database. CRON job yeah? :slight_smile: Pipedream have free services that let you run a simple nodejs script and then take an action. https://docs.pipedream.com/workflows/steps/triggers/#cron-scheduler
    https://pipedream.com/@tod/cron-scheduler-workflow-p_mkC5B1/readme
    So you’d query the firebase database for the list of actions. Loop through actions and see if any are at or behind current datestamp and have not been completed. Run the action as a particle web api call to your gateway device. Flag it as completed (later on when it’s in production you can delete the action document after logging it for posterity).

That’s how I’d do it. As to front end, there are a number of fairly easy javascript/angular frameworks to patch something together. I often use Ionic as you can make it into a mobile app, but your needs may be a lot simpler.
Good luck!