In order to demonstrate a use case for IoT and blockchain, we will be looking at applying the library from above in the supply chain workflow. Most of the countries have a legislation on how pharmaceutics are being transported from the producers to the end destination. This legislation defines, among other things, the environmental conditions (temperature, humidity, etc.) under which medicines can be transported. Let’s see what would it take conceptualy to expose temperature data to all stakeholders in this supply chain scenario in such way that parties involved do not need to trust each other.

The overall idea is that the IoT device (Particle Photon) would capture the current temperature with the dedicated sensor (DS18B20) and then somehow record it on the Ethereum Smart Contract making it in that way accessible to other parties in the supply chain. Keeping the temperature value on the Smart Contract would result in its immutability which is beneficial for auditing purposes. In real world scenario, we would avoid storing data directly on Smart Contract due to the costs but rather we would keep the hash of it and the actual content could be saved on some other distributed file storage system (IPFS for example). The whole process has quite a few moving pieces, so bare with me on this one.

Getting sensor data on blockchain

Everything starts with sensor on IoT device capturing the current temperature. Once that happens, we would create a transaction containing details about the Smart Contract we are about to interact with and the actual value from the temperature sensor.

Our Smart Contract structure is quite simple and it’s only able to store the most recent temperature coming from the sensor.

This is followed by signing the transaction with the private key and encoding it. Ethereum is using something that is called RLP encoding which results in transaction being encoded in binary format. You can find out in details on how the transaction creation, signing and encoding work again in Mastering Ethereum book. At this point, the transaction is ready to leave the IoT device and to be broadcasted to the blockchain network. In order to do that, we would need to get the signed transaction off the IoT device and send it to Ethereum “full node”.

Transaction signed and encoded. Now what?

We will use INFURA to transmit our signed transaction to Ethereum network. INFURA is exposing Ethereum network through HTTPS based RESTful service. This is great for mobile, desktop and web apps but it’s quite heavyweight for embedded devices like Particle Photon which has 1MB flash and 128kb of RAM.

In order to bridge IoT device and Ethereum node behind INFURA, we will use IoT industry standard protocol for small footprint message exchange called MQTT (there is an open source library for Particle Photon). IoT device would then publish message with the transaction payload to MQTT broker. On the other side of the broker, there will be a MQTT client which listens for those messages and passes them to INFURA. Be aware that MQTT is running on TCP/IP and that there should be additional effort done in order to make it secure. Consider using TLS enforced message exchange on both clients and broker (TLS version of MQTT library for Particle).

There are many options out there when choosing MQTT broker. I’ve settled with EMQ. It’s quite straightforward to setup in the cloud environment and it has a nice dashboard for tracking connected clients. Small heads up for the MQTT library on Particle Photon. Default message size (MQTT_MAX_PACKET_SIZE) needs to be increased in order for encoded transaction to fit into message payload. Otherwise, the transaction content would be cut off.