I been struggling with IFTTT and Twilio to be able to send/receive an SMS via Iphone to call functions in the firmware and send back the appropriate responses. I'm reading the forum of previous questions on this but wondering if anyone has had succeed this year? Doesn't sound like a 3rd party SIM is an option. I'm looking for a solution even if it means using a different platform with cellular. (which one?) Would appreciate any feedback on this issue to steer me in the right direction. Thanks!
Particle devices are great at stuff like this. Below is an outline of how to set this up.
Note: this code is not tested and might need some tweaking.
Prerequisites
- Particle Device: Set up and connected to the Particle Cloud.
- Twilio Account: Sign up for a Twilio account and obtain a Twilio phone number capable of sending and receiving SMS.
- Access to Particle Console: For setting up webhooks and integrations.
Step 1: Set Up a Particle Function on Your Device
Create a function in your device's firmware that can be called remotely. This function will process incoming SMS commands.
Firmware Example:
#include <Particle.h>
SYSTEM_MODE(AUTOMATIC);
int handleCommand(String command);
void setup() {
Particle.function("handleCommand", handleCommand);
}
void loop() {
// Your main code here
}
int handleCommand(String command) {
if (command == "status") {
// Perform action, e.g., retrieve sensor data
String response = "All systems operational.";
Particle.publish("sms_response", response);
return 1; // Success
} else {
// Unknown command
String response = "Unknown command received.";
Particle.publish("sms_response", response);
return -1; // Failure
}
}
Step 2: Configure Twilio to Send SMS Commands to Particle
When an SMS is received, Twilio can trigger a Particle function via an HTTP POST request. This is unconfirmed code but would probably get work.
a. Create a Twilio Function to Forward SMS to Particle
-
Log into Twilio Console:
- Navigate to Functions & Assets > Functions.
- Click Create a Function > Blank.
-
Configure the Function:
- Name:
ForwardSMS
- Path:
/forward-sms
- Name:
-
Function Code:
exports.handler = function(context, event, callback) { const axios = require('axios'); const particleAccessToken = 'YOUR_PARTICLE_ACCESS_TOKEN'; const particleDeviceID = 'YOUR_PARTICLE_DEVICE_ID'; const command = event.Body.trim(); const sender = event.From; axios.post(`https://api.particle.io/v1/devices/${particleDeviceID}/handleCommand`, null, { params: { access_token: particleAccessToken, arg: command } }) .then(response => { const twiml = new Twilio.twiml.MessagingResponse(); twiml.message('Command received. Processing...'); callback(null, twiml); }) .catch(error => { console.error(error); const twiml = new Twilio.twiml.MessagingResponse(); twiml.message('Error processing command.'); callback(null, twiml); }); };
- Replace
YOUR_PARTICLE_ACCESS_TOKEN
andYOUR_PARTICLE_DEVICE_ID
with your actual credentials.
- Replace
-
Save and Deploy the Function.
b. Configure Twilio Messaging Webhook
- Navigate to Phone Numbers > Manage > Active Numbers.
- Select your Twilio number.
- Under Messaging, set A Message Comes In to Function, and select the
ForwardSMS
function.
Step 3: Set Up a Particle Webhook to Send Responses Back via Twilio
Your Particle device can publish events that trigger a webhook to send SMS messages back through Twilio.
a. Create a Particle Webhook
-
Log into Particle Console:
- Go to Integrations > New Integration > Twilio Webhook.
-
Configure the Webhook:
- Event Name:
sms_response
- URL:
https://api.twilio.com/2010-04-01/Accounts/YOUR_TWILIO_ACCOUNT_SID/Messages.json
- Request Type:
POST
- Authentication:
- Type: Basic Auth
- Username: Your Twilio Account SID
- Password: Your Twilio Auth Token
- Form Fields:
To
:{{PARTICLE_EVENT_VALUE}}
(Modify if needed)From
: Your Twilio Number (e.g.,+1234567890
)Body
:{{PARTICLE_EVENT_VALUE}}
- Event Name:
-
Advanced Settings:
- Set Webhook Response Template if you need to handle responses.
-
Save the Webhook.
Additional Resources
- Twilio API Documentation: Twilio API
- Particle Functions: Particle Function Documentation
- Particle Integrations with Twilio: Particle-Twilio Integration Guide
Thank you Gpatt1 for the detailed response. After implementing your suggestion I received 'Unknown command received' followed by 'Command received.Processing...' when I send a SMS text of 'status' It would seem to me that Twilio is working however I cannot seem to get the correct response of 'All systems operations' after sending a SMS text of 'status'. Am I missing something? Thanks!
You will have to play around with the specifics of the code for each. Each step has its own complexities.
Specifics of the code for each - functions in the firmware? Not sure I'm following you.
Are you in the United States? And did you go through the A2P10DLC registration process at Twilio? This requires filling out a form and it takes a few weeks to process, and is required to actually send a SMS using any API to a US-based mobile device.
And is there a specific reason you need SMS? Because there are much easier ways to send an receive requests using the Particle cloud and the LTE data service built into the Boron LTE.
Yes I have completed the A2P10DLC registration on the phone number I'm using for Twilio last month.
The specific reason is portability on my cell phone. I have an Android and don't have the Particle IO App. If there is an simpler way than just sending a text to a phone number I would like to look into it, I'm not convince there is anything easier. Is the Boron the best platform to send/receive SMS texts? Thanks!
OK I found a way to receive a SMS text from Twilio to execute any number of functions. Not pretty but works.
Hey something easier would be to send notifications to your phone.
I use Pushbullet for that.
This is a bit dated but might still help you:
Best,
Another option, similar as Pushbullet, is the Pushover service.