Creating an iPhone IoT Gateway

Gal Zahavi
5 min readAug 10, 2020

--

Photo by Louis Reed on Unsplash

If you’ve read the previous article in this series, you should now have the ability to successfully connect to Google Cloud IoT Core (IoT Core) and send/receive commands from your phone. The next step is to transmit and receive data from an ESP32 using an iPhone. This post will go through the steps of connecting your iPhone to the ESP32 (delegate device) using BLE and send the delegate device’s data to IoT Core.

The ESP32 is a $5 micro-controller with WiFi & Bluetooth 4.2+ BLE support, but we will only use it’s Bluetooth capabilities today. You will need a DHT22 sensor for the environmental data but you can also use the less accurate internal temperature reading of the ESP32.

Gateway Basics

You may have never heard of Gateways but they are all around us. A simple example of a gateway is the WiFi router that you have in your house which bridges the connection from your internet service provider to your local network. Gateways communicate on behalf of other devices across networks for security reasons or because some of the devices are constrained.

That’s what a gateway is, it bridges the connection between a service provider and a device. The delegate devices connect to the gateway and the gateway performs several tasks on their behalf, such as:

  • authenticating to Cloud IoT Core when the device can’t calculate its credentials
  • adding a layer of security by bridging a secure internal network hosting delegate devices to the internet
  • publishing telemetry events, getting configuration data or setting device state on behalf of delegates
  • or storing and processing data from the edge

Gateways are especially useful when you need to connect large numbers of devices. Pub/Sub limits to 10,000 connections per project, so once you start exceeding this number of simultaneously connected devices, you’ll need to use a gateway. On top of that, using a gateway reduces the load on networks because the delegate devices will only communicate with the gateway using BLE so there will only be one connection using WiFi.

The diagram below shows how the gateway interacts with IoT Core. The gateway will first connect to IoT Core, then it will subscribe to the command and configuration topic of the delegate device. After the gateway is subscribed to these topics, the gateway can publish telemetry data on behalf of the delegate device.

Setting Up ESP32

For the iPhone to connect to the ESP32, you must define the Service UUID, Read UUID, and Notify UUID. This UUID will be used to connect to the iPhone, receive commands, and send telemetry messages:

After the UUIDs are defined, you will need to create the BLE Server, set the Read UUID, set the Notify UUID and advertise the service:

Advertising the service allows for other devices to see this device with the Service UUID that was defined above.

Now that your iPhone can see and connect to the ESP32, there needs to be a callback function that would be able to handle commands sent from the other device:

The callback function will get called when the iPhone notifies the ESP32; if the command is “d” then the dataDump variable will be set to true and the sendTemp function will be called.

The sendTemp function will use the DHT22 sensor and get the temperature and humidity. After that, it will put these two measurements into JSON format and send it to the iPhone by notifying the characteristic:

Connecting an iPhone to ESP32 devices over BLE

The architecture below shows how the iPhone Gateway works. First, the gateway subscribes to the command and configuration topic of the device. Next, after successfully subscribing, the gateway publishes a state event letting IoT Core know that the device is connected. The gateway publishes telemetry data in the form of JSON to Pub/Sub. With data coming in and out of Pub/Sub, you will have access to any Google Cloud service so you can move your data to BigQuery, use cloud functions with your data, or even use AutoML to make predictions with your data.

While the ESP32 advertises its Service UUID, the iPhone will be scanning for it. When the iPhone finds a matching UUID, it will connect to it and the ESP32 will send a connected message after it’s successfully connected. With the device connected, the iPhone will request the environmental data :

Publishing Telemetry Data

When the iPhone receives data from the ESP32, the iPhone will check if the UUID matches the expected data UUID. If it does match the expected UUID, it will decode the sent data and publish the sensor data.

Running the Sample

To run the sample, all you need to do is to clone the repo from GitHub by running the following command:

git clone git@github.com:GoogleCloudPlatoform/google-cloud-iot-arduino.git

Then in your terminal go to google-cloud-iot-arduino/examples/complex/esp32/ios-gateway/ and run:

pod install

This will install your dependencies. If you’re getting an error that says “SwiftJWT or CocoaMQTT is not included” you will need to add them to the project dependencies.

You can see how to do this in the Before You Begin section.

After the cocoapods are set up, you can run the project on your laptop or build it on your device.

With your device connected for flashing open the ios-gateway.ino file and flash it onto your ESP32 with the DHT22 sensor connected. The picture below shows how to set up the DHT22 sensor. Make sure that you connect to the 3.5v, ground, and that you connect the 4 GPIO pin to the data pin on the sensor.

Note: if you chose to use the internal sensor of the board, don’t worry about this part.

If everything is set up correctly then you should start seeing temperature and humidity data on the application.

Next Steps

Now that you know how to create an iPhone gateway and send telemetry data from another board, you can add more sensors and create a dashboard or instead of sensors add motors, and have a drivable cloud car. Something interesting that comes from this MQTT Gateway application is the abstraction of networking. Usually, when you have a gateway you need to worry about its connection whether WiFi or GSM but the iPhone automatically handles it for you all you need to focus on is the BLE connection to your delegate devices

Follow my blog so you don’t miss out on more interesting articles.

--

--

Gal Zahavi

The best thing about a boolean is even if you are wrong, you are only off by a bit.