June 18, 2024

Project Discription

I build control panels for various props for a local haunted house, Milburn's Haunted Manor. Because of issues with either misbehaving guests or some other problem I was asked to develop a system, with multiple inputs, which would alert the staff if there is a problem (in a specific room) in the haunted house. The inital thought was a bunch of on/off switches, that would light up a led on a status panel. The bundle of wires that would require seemed a bit troublesome.

After some thought I remembered how Particle.io's Wi-Fi based controllers could be set up to send out a predetermined text message at the press of a button, (or some other event). It had been about three years since I have played with it, but I dusted off a Particle Argon and loaded up some code I had previously written to see if it still worked. For a short answer, it did not.

For the text messaging side I use Twillio, an awesome company that I recommend, also, it plays well with Particle.io products.. There is small charge for their texting service. To give you an example, while testing these five control boxes I sent 204 text messages, it cost a little over a dollar, so not breaking the bank. However, To get back to the reason it did not work, apparently, since I used the service last, the supplied phone numbers must be registered to an approved Campaign under your Brand. This happened in 2023. Numbers not tregistered, will be blocked from texting. To registered is easy, it does take a couple of days for all the forms to be reviewed and your ability to send out text messages turned on. Please plan ahead.

The General Idea and a Few Specs

The structure used for the haunted house is pretty large. There is a concern the 3.3 vdc signal from the Photon 2 might degrade if the distance from the Photon 2 to the push button switch is too great. We decided two control systems, called boxes here on out, for each structure (there are two structures). The push buttons would be put in each room to give the actor in the room the ability to quickly send a text to the staff, notifying they have a problem. Sometimes guests get a little carried away, grabbing actors, being distructive or maybe something not working correctly.

Each box will have twelve inputs. I used the following pins on the Photon 2 for inputs: D2, D3, D4, D5, D10, D11, D12, D13, D14, D15, D16, D17. The Photon 2 dataheet recommends not to use pins D6, D7 and D8, these are boot mode pins if the button is pushed on start up it could change the way the Photon 2 boots up. These three pins can be used for regular GPIO pins once boot up is complete. I use pin D6 to control a LED to say the program is running on the external of the box.

Each of the boxes took around four to five hours to assemble and test. A lot of it was tedious work. With twelve inputs per box and five boxes (one spare), made for sixty duplicate wirings. If you click on the picture you can see a larger image that will give you a better idea of all the wiring. A lot of soldering and heat shrink tube. But in the end, it came out pretty good.

Particle Photon 2

These little controllers are great. You can set them up in the field and access them via the Particle Web IDE to make changes the program. No more driving forty minutes, dragging a laptop into a dark corner to access the controller to make an adjustment to the way things are running. I do highly suggesting purchasing the external antenna, it will increase your Wi-Fi range.

If you go to Particle's Website for the Photon 2 it can give you a better idea of what this little wonder can do. It is an IoT platform, which means you can use webhooks to do various tasks, like sending texts, you can send data over the internet, share data without running wires. You do need a good Wi-Fi signal, but that is pretty easy these days with hotspots.

I pair the Photon 2 with the Feather Screw Terminal by NCD.io. This makes wiring and hooking up power a breeze. I comes with screw terminal to easily access GPIO pins. There is an integrated I2C port to hook up sensors if needed. It is very solidly built, with the screw terminals soldered very tightly to the board. Sometimes you feel like you are about ready to twist off the screw terminal, but not here.

Both companies offer other excellent products to greatly enhance your projects.

Wiring Things

I use this standard wiring setup for every switch on my microcontroller projects. A 10k ohm pull-down resistor is used on the input pins to ground. It ensures the voltage between the VCC and the microcontroller input pin is actively controlled when the switch is open. The resistor is to prevent a short by limiting the current flow.

I normally solder all this together. This time I used a terminal strip I recently found on Amazon. It really made wiring easy. It has an A and B side, I ran GND to the A side and 3.3 vdc to the B side. You can see it in the picture above. I feel it made for a very organized and nice looking box. The thing only costs $5 each, finding them available might be another story. Both times I bought all the seller had in stock (6 of them...).

I enclosed it all in a waterproof plastic junction box, IP67, again from Amazon. It measures: 8.7" x 6.7" x 4.3", plenty of room to work in. The real nice feature is the removable back plate. Of the five enclosures I purchased the first one was a lot better quality. The square plastic holes were well drilled and accepted a small zip-tie (again, I bought the only one the seller had). The other four needed slight modifications. But I kept a drill on hand with a step bit, and enlarged the holes as need. They all cost around $15 a piece, but you can find them costing a lot more too, so be careful. In the end, it is a solid project.

I am looking forward to how far we can run a wire carrying 3.3 vdc without the resistance in the wire degrading the voltage below usable. I tested 100 feet and still was able to measure 3.3 volts. The excitement of the challenge.

Particle IDE

I do not want to get too indepth on how to claim the Photon 2 and set up your account on Particle.io. It's not hard and there are already YouTube videos and tons of other help on that one.

Before you set up your Webhooks you'll need to have your Twilio account set up. As I said before, it will take a couple of days for them to review why you want to text people. If you are planning on doing something like this, then do that early on. They will supply you with a user name and a password, both over 20 characters long. They tell you to keep it secret, and I highly recommend you don't share, unless you with to be hacked.

Writing your code is really no different than an Arduino UNO. But the beauty is you can flash it over Wi-Fi. There are a lot of examples available. Find one that is close to what you want to do and modify it to meet your needs.

The Code Makes Things Play Together

If you click on the picture to the left you'll get a PDF version of the code for just one push button input. I felt posting the code for all twelve inputs was a bit much. If you need more input pins just and add additional lines, "int buttonPin2", and so on.

Here is what the code does:

STARTUP(WiFi.selectAntenna(ANT_EXTERNAL)); - tells the Photon 2 to use the external antenna.

int buttonPin1 = 2; - Sets the button 1 as pin 2. Again I use pins D2, D3, D4, D5, D10, D11, D12, D13, D14, D15, D16, D17 for all twelve of my inputs.

int ledPin = 6; - Is to turn on a LED external to the box to show people the program is active. It takes awhile for the Photon 2 to boot up, if you read the docs it flashes a blue light when it is running, but you can't see that when the enclosure is closed.

int buttonState1 = LOW; - Sets the button state to low at startup. Probably not needed, but why not.

String body1 = "Emergency Text - Room 01"; - This is the message that will be sent out over the webhook. You can set this to what you want, this is just what I used for testing.

pinMode(buttonPin1, INPUT); - Sets the pin to an INPUT pin.

pinMode(ledPin, OUTPUT); - Sets the pin controlling the LED to an output.

digitalWrite(ledPin, HIGH); - At the start of the LOOP with the program running, the external LED is turned on.

buttonState1 = digitalRead(buttonPin1); - Checks to see what state the first pin is at, HIGH or LOW. If the button is pushed it will have 3.3v applied to it, and be HIGH. If the button is not pushed it will be pulled-down to LOW with the 10K ohm reisistor to ground.

if (buttonState1 == HIGH) { - If button is pressed, it runs the little sub-loop, if not the program will continue on to the next step past the sub-loop.

Particle.publish("tim_sms", body1, PRIVATE); - This tells the Photon to push out the contents inside the parentheses. "tim_sms" is the name of the webhook that will be activated. "body1" is what will be sent. "PRIVATE" makes it only the group you assigned can see it. There are a lot of rules associated with Publish, please see the Particle Docs.

delay(1000); - Finally, a delay of one second. Play with this time delay to get what works for you. Do not make it longer than needed, it stops the program during the time span of the delay. If someone else pushes a button during that delay the Photon 2 will not detect it.

That is the easy part of it...

Setting up the Webhooks

The first time I set this up it was fairly easy. When I went to add the second webhook, it became more of a challenge. I'll try to give you some advice, and I might have to change that advice as I play with this more. I'm not an expert, but I got it to work.

If not already, log onto the Particle.io page. On the bar menu on the left side of the screen, select "INTEGRATIONS". On the right hand side click on the "+ADD NEW INTEGRATION". Another window will open and select Twilio.

To the right is a Step by Step Guide. It should be current where this page could be out of date.

I found it easier to work with the CUSTOM TEMPLATE than the standard fill in the form that pops up. If you scroll to the bottom of the page "Configure you Twillio Integration" You will have the option of selecting "Go to advanced view". Scroll back to the top and select "Custom Template" It will allow you to enter code instead of filling out the form. In one incidence, it was the only way I could fix an issue I was having. HERE is the code I have for my webhook. You will need the secret USERNAME and PASSWORD from Twilio. The name you use in the "event" line will need to be the same one you use in your "Particle.publish" line of the code you put into the Photon 2. Twilio will assign you a phone number the texts will be from, this goes in the "FROM" line, the phone number you wish to received the text goes in the "TO" line. Make sure you have the "+" sign in front of the numbers.

Back on the list of Integrations, make sure the one you just made is ENABLED.

If you click on the new Integration you just made, it will give you an option to test it. That should be it. Good Luck!