Use Siri and Arduino to Speak Sensor Readings

Voice control is all the rage. In this tutorial, we'll show you how to add Siri to your Arduino project to fetch current sensor readings.

Disclosure: Some of the links in this post are affiliate links. This means that, at zero cost to you, Learn Robotics will earn an affiliate commission if you click through the link and finalize a purchase. Learn Robotics is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a way for websites to earn advertising revenues by advertising and linking to Amazon.com.

Table of Contents

Wouldn’t it be nice to ask Siri about your Arduino device and have it tell you current sensor readings?

If you answered yes, then you’ll want to stick around for this tutorial. If not, then I guess you can stop reading. Click here for a random article!

The purpose of this project is to use Siri to get information from your Arduino device. If you have a sensor that’s collecting and publishing readings, you can ask Siri for a real-time update.

This feature is especially useful if your IoT device is remote, out in the field, or you don’t feel like getting up to go look at it!

You can use this project as a starting point for getting into Voice Control or as an example of how to add Siri to an existing Arduino IoT project.

All you need is an iPhone (or iOS device running 12.0 or later) and the Shortcuts app. If you are a beginner to Arduino, IoT, and coding, I highly recommend checking out our Beginner Tutorials first.

This project is very involved and can get confusing fast, especially if you don’t have prior experience.

When’s the last time you learned something new?

We have an online beginner Arduino course, to help you with the hurdle of learning Arduino from scratch. If personal and professional development matters to you, enroll in the course, work through the modules, and earn a certificate.

It’s a great way to keep track of your credentials while learning useful concepts in electronics. Learn More.

There are two parts to this project that need to be configured: the Arduino device and the iOS shortcut.

Let’s dive into each of these in more detail.

Part 1. Wire the NodeMCU and Configure Device Software

For this project, we will use a NodeMCU (ESP8266) controller; however, you can use whatever Arduino-compatible and Internet-enabled Wifi chip that you like.

You’ll also need a DHT11 or DHT22 temperature and humidity sensor, which is what we’ll use to collect readings.

Lastly, I’ve opted to add a 16x2 LCD, connected via I2C. You don’t have to use an LCD; you can use the Arduino IDE Serial Monitor for debugging and testing.

However, if you want to use the device without being tethered to the computer, an LCD comes in handy.

[amazon box=”B07GPBBY7F,B019K5X53O” template=”list”]

Once you have your components, it’s time to wire up the test device.

While you can use this tutorial for practically any Arduino application, I think an applied example makes the configuration easier to understand.

Wire DHT11 Sensor to NodeMCU with LCD I2C

1. Collect & print readings from the DHT11 Sensor

To use the DHT11 sensor, you’ll need to install and import the DHT library in the Arduino IDE. Then, use the following configuration within your sketch.

//global config for DHT11
#define DHTPIN 0 //D3 is GPIO-0 on NodeMCU
#define DHTTYPE DHT11 //use DHT22 if you're using that sensor
DHT dht(DHTPIN, DHTTYPE); //configures the DHT object with the correct pin and type

After that, initialize the DHT11 sensor in setup().

dht.begin();

Next, grab a reading from the sensor by using the following methods:

int temp = int(dht.readTemperature(true)); //get temp in degF
int hum = int(dht.readHumidity()); //get hum in %

I chose to cast these to integers to make data easier to work with on both the LCD and on dweet. You can opt to work with floats if your application calls for a smaller resolution.

Tip: If your DHT11 sensor is reading NaN (not a number), then try supplying it with a 5V input source. The NodeMCU only supplies 3.3Vout, which can lead to funky readings with some sensors.

1a. Print readings to the Serial Monitor

Now that you have readings, it’s time to print them to the Serial Monitor. This is helpful for verifying that you have accurate and appropriate readings.

And, it’s also a way to check to see if Siri is reciting the correct information in later tests.

Printing sensor data is very straightforward. First, initialize the Serial Monitor in the setup() method. The NodeMCU runs at 115200 baud.

Serial.begin(115200);

Make sure when you open the Serial Monitor that 115200 is selected in the bottom right corner.

Then, print the values out. I recommend adding the print lines wherever you collect readings from the sensor. That way you’re printing out the most up-to-date information.

Serial.print("Temperature:");
Serial.print(temp); //variable name for temperature
Serial.println("F"); //units

Serial.print("Humidity:");
Serial.print(hum); //variable name for humidity
Serial.println("%"); //units

Or you can be fancy and print both values on one line.

Serial.print("Temperature:");
Serial.print(temp); //variable name for temperature
Serial.print("F | "); //units
Serial.print("Humidity:");
Serial.print(hum); //variable name for humidity
Serial.println("%"); //units

You can adjust these lines to best match your application.

1b. Print readings to an LCD using I2C

In addition to printing to the Serial Monitor, you may want to add a local LCD to the device. That way you don’t have to be tethered to the computer, but you can still see the current readings.

print DHT11 sensor readings to LCD using I2c

For this, you’ll need to install and import the LCD I2C library in the Arduino IDE. Be sure to follow the wiring diagram if you plan on adding an LCD to your project.

Then, add the LCD configuration code, as follows.

//LCD config
int lcdColumns = 16; //num columns
int lcdRows = 2; //num rows
LiquidCrystal_I2C lcd(0x27,lcdColumns, lcdRows); //configures your LCD type

Next, initialize the LCD in setup().

lcd.init();
lcd.backlight();

Then, print out data as we did for the Serial Monitor.

//print temp
lcd.setCursor(0,0);
lcd.print("Temp:");
lcd.setCursor(6,0);
lcd.print(tempF);
lcd.setCursor(8,0);
lcd.print("F");
//print humidity
lcd.setCursor(0,1);
lcd.print("Hum:");
lcd.setCursor(5,1);
lcd.print(hum);
lcd.setCursor(7,1);
lcd.print("%");

Again, you can customize this to your liking or for your application.

2. Send readings to dweet.io (or another webhook service)

Now that you have readings from the DHT11 sensor, you can POST them to dweet.io. First, connect your NodeMCU to your network, and then follow along with the following steps.

For this section, we will need to first create the HTTP String Request, and then publish it to dweet.

2a. Create an HTTP String Request for Arduino + dweet.io

There are two ways to publish data to dweet.io from a device: 1) append a query string to the webhook URL or 2) send JSON data in the body of the request.

For this project, we’ll append the data to the webhook URL. You’ll need to set up a few arrays to store the variable names and data that you want to send.

//variables as an array (for multiple metrics)
String arrayVariableNames[] = {"Temperature","Humidity"};
int arrayVariableValues[2];
//tells the number of arguments inside each array
int numberVariables = sizeof(arrayVariableValues) / sizeof(arrayVariableValues[0]);

First, create a method to create the query string. We’ll call this method, getDweetString(), and return a String.

Next, create a string with the dweet URL for your thing.

String dweetHttpGet = "GET /dweet/for";
dweetHttpGet = dweetHttpGet + String(thingName) + "?"; //substitutes the thing name (global var)

Then, iterate through the arrays to get the names and values for each reading.

int i=0;
for (i = 0; i < (numberVariables); i++) {
    if (i == numberVariables - 1) {
        //the lastone doesnt have a "&" at the end
        dweetHttpGet = dweetHttpGet + String(arrayVariableNames[i]) + "=" + 
        String(arrayVariableValues[i]);
    }
    else{
        dweetHttpGet = dweetHttpGet + String(arrayVariableNames[i]) + "=" + 
        String(arrayVariableValues[i]) + "&";
    }
}

Lastly, append the headers and return the HTTP String request.

dweetHttpGet = dweetHttpGet + " HTTP/1.1\r\n" +
"Host: " +
host +
"\r\n" +
"Connection: close\r\n\r\n";
return dweetHttpGet;//this is our freshly made http string request

2b. Send the request to dweet.io

Now that we have the String request, we can POST it to dweet using a second method, sendDweet().

First, collect readings for temperature and humidity. Store this value to the temperature and humidity array variables, respectively.

arrayVariableNames[0] = "Temperature";
arrayVariableValues[0] = tempF;

arrayVariableNames[1] = "Humidity";
arrayVariableValues[1] = hum;

Next, connect to dweet.io and print the HTTP String request from step 3a to the WiFi client.

//connect to dweet.io
WiFiClient client;
const int httpPort = 80;

if (!client.connect(host, httpPort)) { //can't connect
    Serial.println("connection failed");
    return;
}
client.print(getDweetString()); //connected, print dweet string to client
delay(10); //wait...
while (client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print(line);
}

Lastly, add a delay of at least 1 second so that we don’t spam the system.

delay(1000);

I’ve added in some conditional logic that only sends data to dweet if the temperature has changed +/- 1 degree Fahrenheit or +/-1% humidity. There’s no reason to send data to dweet continuously if it’s the same number.

Once you have this setup, you can call the sendDweet() method in loop() and upload the code to your board.

Open up dweet.io and go to the thing’s page. It’ll be in the format https://dweet.io/follow/thing-name.

You should see it update with the current Temperature and Humidity data. Verify this works before moving to the next step.

Publish data to dweet.io using NodeMCU and DHT11 sensor

Check the local data readings from the Arduino Serial Monitor (or LCD) with the numbers displayed on dweet.io to confirm that things are working properly.

Part 2: Configure iPhone Shortcuts to fetch JSON data with Siri

In this section, we will set up a shortcut that fetches our sensor data and uses Siri to vocalize it. As I mentioned, you will need an iOS device running 12.0 or later and the Shortcuts app.

[amazon box=”B07XSQT4Q3″ template=”list”]

3. How to create a Shortcut on iOS

First, launch the Shortcuts app and create a Shortcut.

Next, add the seven apps and actions in the same order that’s listed below.

Create a shortcut to receive data from Arduino using Siri

1. URL: The URL for this project will be the dweet.io request webhook with your thing name at the end.

https://dweet.io:443/get/latest/dweet/for/thing-name

2. Network – Get Contents of URL: Substitute the URL into the Network app. Expand the “Show More” option and be sure that the method is set to GET.

3. Scripting: Next, add a “Get Value for” action block. This is how we will parse the JSON data hosted on the dweet.io request URL.

The value is “Value” and the Key is with.1.content.MYVAR1 in “Contents of URL”. MYVAR1 is the name of the data. Make sure to match the capitalization on dweet.io. For example, if we wanted to get the Temperature value, we would reference the Key, with.1.content.Temperature.

Note: The format of your Key might vary slightly if you don’t use dweet.io or your key names are different. This is just an example of how you can obtain a value from a JSON using the Shortcuts app.

4. Variables: Then, add a Set variable action. We will set the variable “Temp” to the Dictionary value from the previous step.

5. Scripting: Repeat this process for the next variable you want to fetch data for. Add a “Get Value for” action block. The value is “Value” and the Key is with.1.content.Humidity in “Contents of URL”.

6. Variables: Next, add a Set variable action. Use the name “Hum” and the value is “Dictionary Value from Step 5.”

7. Documents: Finally, we will have Siri speak the data we retrieved. Choose a Speak action and type in a phrase. I used “The Temperature is” Variable-Temp degrees Fahrenheit, and it’s currently Variable-Hum % Humidity. Make sure that Variable-Temp and Variable-Hum are the magic variables from Steps 4 and 6, respectively.

4. Configure the Shortcut and Test it out

Be sure to give the Shortcut a command phrase. Go to the top right corner in the shortcut and click the three dots. Then type in a phrase.

This phrase will be what you say to Siri to get the information, so make sure it’s memorable and not too long. I used the phrase “Get my device status.” Then press done.

It’s finally time to test out the project!

Make sure your NodeMCU is powered, connected to the Internet, and sending Temperature and Humidity data to dweet.io.

Then, say “Hey, Siri, Get my device status.”

Siri should respond within a few seconds.

“The Temperature is 76 degrees Fahrenheit and it’s currently 66% Humidity.”

Verify that the data Siri says matches what’s shown on dweet.io. If so, congrats, your device is working.

If not, go back and check to make sure you have the device wired and programmed correctly, and that the Shortcut is properly configured.

Wrap Up: How to add Siri to Arduino

Adding Siri to Arduino to recite sensor data is a cool feature any developer, hacker, or maker would want to add to their project. With the Shortcuts app on iOS 12.0 or later, you can easily add voice control to a multitude of systems including Arduino.

In this tutorial, we explored a simple prototype using a NodeMCU and a DHT11 sensor. Then we configured Siri to fetch temperature and humidity data from our device and speak it out loud.

[amazon box=”B07GPBBY7F” template=”list”]

If you’re looking for a way to add voice controls to your custom IoT or home automation system, then this example should get you well on your way to fetching information with just a few words.

Once you’re able to fetch data from an Arduino sensor, you may want to send data to Arduino to control LEDs, motors, and other outputs. Combine both of these features, and you can pretty much do whatever you’d like using Siri and Arduino.

If you enjoyed this tutorial, or if it helped you in any way, consider sending me a coffee and keep me fueled for the next project!

Questions? Drop them in the comments below, and be sure to let me know how you used this tutorial!

Related Articles

2 Responses

  1. Thanks for this great guide 🙂
    I want to get both temp and humidity in one shortcut, but i am only able to get only one.
    I am able get one value when i add script “Get value for xx in content of url” , but when i try to add a new script to get a the second value it says “Get value for XX in temp” it wont let me change temp to Content of URL”

    The only workaround i found is to ask again for the content of url and to the whole thing again in the shortcut, but that means that i have to query dweet two times in one shortcut
    Any ideas what i am doing wrong?

    1. Hi Jimmy, glad you dropped by. Unfortunately without seeing your shortcut it’s tough to say. I recommend reviewing Part 3 and make sure that you’ve configured the apps and actions as listed.

      If you’re using Dweet, you’ll need to make sure that you’re sending both the Temp and Hum readings to the same Dweet Thing for this to work. The idea is to use key-value pairs within a dictionary so that you only need one Shortcut.
      Good luck ~Liz from Learn Robotics

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Invest in Your Future Tech Career & Get Matched with an Expert Robotics Mentor

Connect with your Advisor and select the right Learn Robotics Program to boost your Tech Career

Wait,

Learn Robotics Online
— Get 2 months free!

Exclusive Limited Offer for Serious Beginners Ready to take their Hobby to Engineering Internships, $100k+ Careers, and Beyond!

Enroll now, and earn your first robotics certificate in the next 7 days.

👇 Click below to claim this deal.