Table of contents:

The 2Smart Cloud platform enables not only the connection of physical devices based on ESP32 and other microcontrollers with internet access but also virtual devices or digital twins of real ones. In this article, we explain how to create a digital twin on the 2Smart Cloud platform using the low-code development tool Node-RED. We use a Xiaomi gateway as an example, but you can apply this tutorial to any Node-RED project.

Creating a Digital Twin in 2Smart Cloud: A Xiaomi Smart Home Example

Test Device Kit

For this experiment, we used the Xiaomi Mi Smart Home gateway with connected sensors operating via the ZigBee protocol: two temperature and humidity sensors, and two motion sensors.

Test Device Kit

To integrate this device set into 2Smart Cloud using Node-RED, we used the node-red-contrib-xiaomi-smart-devices module. We previously used this module when writing the “Xiaomi Gateway” bridge to integrate the same gateway into the open-source monitoring and automation platform 2Smart Standalone.

Let’s collaborate

We’re empower your business with our technology expertise

Let’s discuss your case

Preparation

  1. Install Node-RED locally using the instructions on the project’s website.
  2. Download the project from 2Smart’s GitHub: https://github.com/2SmartCloud/2smart-cloud-node-red-xiaomi-example

    $ git clone https://github.com/2SmartCloud/2smart-cloud-node-red-xiaomi-example.git

  3. Launch Node-RED, specifying the working folder:

    $ node-red -u ./2smart-cloud-node-red-xiaomi-example
  4. Open http://localhost:1880, where you should see the following interface:
    Preparation
  5. Connect the gateway to your Xiaomi Home account if you haven’t already. This is necessary to obtain the token and connect sensors to the gateway.

Configuration

  1. Obtain the token from Xiaomi for communication with their gateway. You can use a script by PiotrMachowski or another method.
    Configuration
    Note: Using the script requires you to provide your Xiaomi account login and password, so for security, change your password immediately after obtaining the token.
  2. Find the Xiaomi Gateway block in Node-RED and double-click it. Go to the Gateway configuration and update the Key and Address fields with your details from the TOKEN and IP fields in the previous step. Save the changes.
    Configuration Configuration Configuration
  3. Log into your account at https://cloud.2smart.com (or register if you don’t have an account), go to the “Products” section, and click “Add Product”. Choose Virtual MCU as the microcontroller for your new product.
    Configuration
  4. Click the “Connection info” link on your new product’s page and copy the credentials from the modal window into Node-RED.
    Configuration Configuration
    Insert the credentials into the appropriate fields from line 50 to line 54.
    Configuration Configuration
  5. Save the changes and click “Deploy”.
    Configuration
  6. The device status on the platform should become Ready. At this point, the device will still be empty as we haven’t connected the ZigBee sensors yet.
    Configuration
    If the device status is not Ready, check the gateway token and 2Smart Cloud credentials. Alternatively, stop Node-RED, delete the context folder, and restart Node-RED.
  7. Once the device status is Ready, proceed to connect the temperature and motion sensors via the Xiaomi Home app.
    Configuration Configuration
  8. After connecting the sensors, the product page’s “State preview” modal should show two nodes with the current readings from these sensors.
    Configuration Configuration
  9. Customize the layout of the 2Smart Cloud mobile app for the Xiaomi device’s digital twin and test sensor value control from your smartphone. Publish the product on the platform, allowing end users to manage devices by pairing them with the 2Smart Cloud mobile app.
    Configuration Configuration Configuration Configuration

Using Node-RED to Integrate Various Devices into 2Smart Cloud

We acknowledge that the above example is not meant to be production-ready or even an MVP for your needs. The Xiaomi gateway still connects to the manufacturer’s server, and the native mobile app provides more control features than the 2Smart Cloud app.

However, this integration example shows that you can easily connect almost anything to the cloud without writing code. Node-RED can be a handy tool for building an MVP of your product and requires minimal effort to start creating your virtual IoT devices.

We use this approach in our projects when appropriate. For instance, in the MyBox electric vehicle charging management platform, we implemented Dynamic Load Management for numerous charging stations. We integrated Unipi’s existing equipment into the MyBox platform using Node-RED, resulting in a seamless integration of powerful functionality with third-party hardware.

The Internet of Things isn’t limited to physical devices and their firmware; it also allows for creating virtual devices that operate on other services/APIs and can act as digital twins.

Beyond Node-RED, you can also create your virtual devices. An example is the 2Smart virtual weather station. This virtual device, written in NodeJS, communicates with the OpenWeather API to display current weather metrics for a selected city.

Regarding the structure of topics and the communication format for real or virtual devices with the 2Smart Cloud platform, these are described on the 2Smart GitHub. If you have any questions after reviewing the documentation, feel free to reach out to us in the 2Smart Cloud Discussion Telegram chat.

Conclusion

Creating digital twins on the 2Smart Cloud platform using Node-RED provides a flexible and convenient way to integrate IoT solutions. With minimal programming skills, you can easily connect and manage both physical and virtual devices. This opens up opportunities for rapid prototyping and concept testing in various fields, from smart homes to industrial applications. The capabilities of Node-RED and 2Smart Cloud enable the creation of scalable and functional systems while minimizing development time and resources.

Let’s collaborate

We’re empower your business with our technology expertise

Let’s discuss your case

FAQ

What is a digital twin?

A digital twin is a virtual representation of a physical device that allows you to monitor, control, and analyze its performance in real time over the internet.

Why use Node-RED to create digital twins?

Node-RED is a low-entry barrier tool that allows for quick and easy integration and automation without requiring deep programming knowledge.

What devices can be integrated into 2Smart Cloud?

Almost any device that can connect to the internet, including physical devices based on microcontrollers (e.g., ESP32) and virtual devices created using APIs or other services.

Is it safe to use tokens and credentials from third-party services?

Using tokens and credentials requires careful attention to security. We recommend changing passwords after obtaining tokens and using secure methods for storing and transmitting data.

What features does 2Smart Cloud offer for end users?

End users can manage devices through the 2Smart Cloud mobile app, receive notifications, and monitor device status in real-time. They can share device control access with others, including those not using the mobile app.

End users of 2Smart Cloud can also control their devices via Telegram bot and phone calls, easily add devices to ecosystems like Siri and Google Home, and receive notifications from their devices not only in the mobile app but also in messengers like Telegram, Slack, and WhatsApp.

Can this approach be used to create commercial products?*

Yes, the approach using Node-RED and 2Smart Cloud is suitable for both prototyping and creating commercial products, allowing you to bring new solutions to market quickly.

What other examples of virtual devices can be created?

You can create virtual weather stations, energy monitoring systems, smart lighting, and many other devices that interact with various APIs and services, providing real-time information and control capabilities.

Rate this article:

5 / 5. 2

( 2 votes )

Don't forget to share this post!

Let’s dive into your case

Share with us your business idea and expectations about the software or additional services.

    Thanks for contacting us!

    We will get in touch soon.

    This site is protected by Google reCAPTCHA and the following Privacy policy and Terms of use apply

    Send us a message

    This site is protected by Google reCAPTCHA and the following Privacy policy and Terms of use apply