Agricultural Monitor
3. Deploy a Function
3 / 8

In this step, we're going to purchase a Twilio phone number and set up a servleress function to retrieve humidity results from our sensor.

What is a Twilio Phone Number?

Twilio allows you to instantly provision national, mobile, and toll-free phone numbers in more than 70 countries usable with developer-friendly APIs. This Blueprint uses a phone number when sending text messages to notify stakeholders of intrusions.

Visit the phone numbers documentation to learn more about provisioning and best practices.

Purchase a Twilio Phone Number

You will need a Twilio phone number to send SMS alerts.

  • Visit the Buy a Number section of the Console
  • Enter an area code of your choice in the input field
  • Check at least SMS from the Capabilities checkbox
  • Click Search


You should see a list of available numbers to purchase.

  • Select a number of your choice
  • Click Buy This Number

Make note of your number; we will use it in the next section.

What is Twilio Functions?

Functions is a serverless development environment; it will host the server side code and website required to communicate with the Particle Electron.

Visit our Glossary to learn more about serverless architectures.

We’re going to use a short function to handle incoming readings from our device in the field and send a message when conditions are too dry.

Add a Function

  • Visit our repository and find the code in the ‘Runtime’ directory.
  • In another tab, open the Runtime - Functions Console.
  • Hit the big red plus (‘+’) button to create a new function. Add a Function
  • Select ‘Blank’ and ‘Create’
  • In path, enter ‘moisture’, and use a ‘FUNCTION NAME’ of ‘Moisture’.
  • Paste the code from the repository into the CODE field. Add a Moisture Function
  • ‘Save’ the function.

Set Environment Variables

Functions provides the ability to abstract variables from your code with environment variables. Our function today requires you to set three environment variables.

  • Visit the Configure Pane in Functions.
  • Create a variable SECRET_API_KEY. In the device code, we are using ‘top-secret’ (no quotes). Also see the discussion below on security.
  • Create a variable YOUR_PHONE_NUMBER. Enter a phone number to text with warnings in E.164 format.
  • Create a variable TWILIO_NUMBER. Populate it with the number you purchased in the above step, again in E.164 format.

Production Security and Revocable Keys

In production, you need a revocable key or certificate for individual device authentication. As data can be extracted from physical devices, you can use a key server or pre-assigned certificates and revoke them if they are lost or stolen.

When writing code, best practice dictates limiting your allowed scope to only the permissions you’d like to grant to a device. For example, today’s code will only permit a numeric reading sent via SMS through a template.

Retrieve Your Function Path

  • Go back to your ‘Moisture’ Function.
  • ‘Save’ it again to ensure it is picking up the new environment variables.
  • Click the ‘Copy’ button at the end of ‘Path’ at the top of the screen, and save the URL somewhere to use in the next step.