Contents


Overview

Skill Level: Any Skill Level

The long anticipated ESP32 is now finally starting to be available , and the Arduino extensions to make it easily programmed are becoming usable. This recipe gives a quick how-to on getting up and running connecting an ESP32 to the Watson IoT platform.

Step-by-step

  1. Get an ESP32

    The ESP32 is a souped up version of the ESP8266 microcontroller that took the MCU/IoT hobbyist world by storm in 2016. The ESP32 has a faster and dual core processor, more memory, more I/O, and supports Bluetooth as well as WiFi. However getting hold of one since its release in September has been like finding hens teeth. Thats starting to change now and a variety of boards are becoming available through the usual retail channels.

    This recipe should work with any ESP32 module. The dev style boards with built-in USB are easiest to get going with. Here I'm using an ESP32 Thing from Sparkfun.

    esp32-3

  2. Get the Arduino IDE and add the ESP32 extensions

    To create your development environment you need to (1) get the Arduino IDE, (2) install the ESP32 extensions into the IDE, and also (3) add the MQTT library.

    1) Get the Arduino IDE from here. Any not too old release should work if you already have this installed. I've been testing this recipe using 1.6.12.

    (2) Install the ESP32 extensions for the Arduino IDE. Presently the ESP32 extensions have not had an official release and are not available via the Arduino Board manager, so you need to install these manually from the Github repository. Follow the instructions here, its actually pretty straight forward and worked right away with no hitches for me.

    (3) Add the MQTT library. The recipe in this sketch uses MQTT to communicate with the Watson IoT Platform, so you need to add the MQTT library to the Arduino IDE. This is using the PubSubClient by Nick O'Leary, its the standard Arduino MQTT library and works fine on the ESP32. To install it, in the Arduino IDE goto the menu bar “Sketch” -> “Include Library” -> “Manage Libraries…” to bring up the Library Manager and then in the “Filter your search…” box enter PubSub. Select the entry and click “Install”.

    Thats you're ready to go programming the ESP32. You can select your ESP32 from the Arduino IDE menu bar “Tools” -> “Board” -> “ESP32 Dev Module” as shown here:

    Esp32Arduino-2

     

  3. Program the ESP32 with this sketch

    Copy and paste the following sketch into the Arduino IDE and update the bits inbetween the “Customise these values” lines. As a minimum all you need to change is your WiFi SSID and password and then it will use the Watson Quickstart service with a DeviceId of “Test1”, or if you have registered you own organisation and devices you can update from the quickstart defaults to use your own device:

    /**
    * Helloworld style, connect an ESP32 to IBM's Watson IoT Platform
    *
    * Author: Anthony Elder
    * License: Apache License v2
    */
    #include <SPI.h>
    #include <WiFi.h>
    #include <PubSubClient.h>

    //-------- Customise these values -----------
    const char* ssid = "<yourWifiSSID>";
    const char* password = "<yourWifiPassword>";

    #define ORG "quickstart" // your organization or "quickstart"
    #define DEVICE_TYPE "esp32" // use this default for quickstart or customize to your registered device type
    #define DEVICE_ID "test1" // use this default for quickstart or customize to your registered device id
    #define TOKEN "<yourDeviceToken>" // your device token or not used with "quickstart"
    //-------- Customise the above values --------

    char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
    char topic[] = "iot-2/evt/status/fmt/json";
    char authMethod[] = "use-token-auth";
    char token[] = TOKEN;
    char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;

    WiFiClient wifiClient;
    PubSubClient client(server, 1883, wifiClient);

    void setup() {
    Serial.begin(115200); delay(1); Serial.println();

    initWiFi();
    }

    void loop() {

    if (!!!client.connected()) {
    Serial.print("Reconnecting client to "); Serial.println(server);
    while (!!!client.connect(clientId, authMethod, token)) {
    Serial.print(".");
    delay(500);
    }
    Serial.println();
    }

    String payload = "{ \"d\" : {\"counter\":";
    payload += millis()/1000;
    payload += "}}";

    Serial.print("Sending payload: "); Serial.println(payload);

    if (client.publish(topic, (char*) payload.c_str())) {
    Serial.println("Publish ok");
    } else {
    Serial.println("Publish failed");
    }

    delay(3000);
    }

    void initWiFi() {
    Serial.print("Connecting to "); Serial.print(ssid);
    if (strcmp (WiFi.SSID().c_str(), ssid) != 0) {
    WiFi.begin(ssid, password);
    }
    while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    }
    Serial.println(""); Serial.print("WiFi connected, IP address: "); Serial.println(WiFi.localIP());
    }

    Click the Upload button in the Arduino IDE and the sketch will be compiled and uploaded to the ESP32. Depending on your ESP32 module you may need to reset and manually set the ESP32 into upload flashing mode however the ESP32 Thing board I have works seamlessly with auto reset.

    Have the Serial Monitor window open and you should see the sketch running right away:

    Running1

  4. View the published data at Watson

    To view the data being published to Watson go to this url: https://quickstart.internetofthings.ibmcloud.com/#/device/test1/sensor/

    (in that URL the “test1” portion matches the DEVICE_ID definition from the sketch, if you have modified that name or are using your own Watson organisaition and device then update the URL accordingly) 

    Running2

     

  5. Thats it!

    So thats how easy it is to get going with using the ESP32 with the Watson IoT platform.

    The ESP32 is a powerful MCU with many advanced capabilities that can be usefully combined with Watson platform – a remote sensor device with many I/O functions, but also exciting possibilities as a powerful gateway device combining WiFi and Bluetooth enabling connecting very low power BLE sensors to Watson without needing a complex gateway computer. 

    Stay tuned for more more developerWorks ESP32 recipes demonstrating these and other capabilities.

    27th Feb – check out the new recipes showing ESP32/Arduino connecting Bluetooth LE devices to the Watson IoT Platform.

     

     

3 comments on"Connect an ESP32 to the Watson IoT Platform!"

  1. Note that as identified by ccfoo242 presently in the very lastest ESP32/Arduino code in github this example now fails with errno 88, see https://github.com/espressif/arduino-esp32/issues/85.
    Simplest way to avoid that for now is to revert to an earlier revision, eg e77ec63 – https://github.com/espressif/arduino-esp32/commit/e77ec634a9d1b54f410eb6a46150874e6dc5c464

    • The “errno 88” problem has been fixed now in Github so the sketch in this recipe should be working fine again with the latest ESP32 Github code.

  2. […] Its fun to run a sample program to send sensor data to IBM Bluemix IoT platform by developer.ibm.com/recipes/tutorials/connect-an-esp32-to-t… developer.ibm.com/recipes/tutorials/experiments-with-blue… Later , uploaded data can be […]

Join The Discussion