About the project

Motivated by the current demands to promote renewable energies and user self-management of energy, this project seeks to facilitate energy grid operations. Leveraging the potential of blockchain technology and Chainlink's ability to obtain off-chain data, it aims to provide more efficient and sustainable energy management. By using blockchain, a more transparent and decentralized system is achieved, allowing people to have greater control and clarity over their energy consumption.

Inspiration

The inspiration came from identifying the centralization and inefficiencies in today's energy market. The energy sector and how we consume energy is a very important aspect of our lives, and it is not always easy to understand how it works or it can be inaccessible due to its high prices.

The power of blockchain's decentralization and real-time data management offered by Chainlink inspired me to create this project. An environment has been created where the energy produced and consumed is automatically recorded, and billing is done considering the real and updated prices of energy.

I live in Spain and all producers must sell energy at a price agreed upon by public entities. Thanks to Chainlink's external API calls, this data can be obtained at the time of recording the consumed energy and making the billing.

What it does

This project facilitates the management of an energy grid, from recording energy production and consumption to user bill payments.

The project consists of the following parts:

  1. Dapp for the energy consumer: from here, users can consult the energy they consume each month, how much the bill amounts to, they can also see total consumption values and consult previous months' consumption. Of course, from here they can pay their bills and check which months have been paid and which are pending. https://denergy-link-consumer.vercel.app/

  2. Dapp for the energy producer: it is a dashboard where the producer can observe the statistics of production, consumption, and profit generated each month, they can also see the total values of energy produced and consumed. What's special is that this panel is public, any user can connect their wallet and see the data of the energy grid.

    Another special feature is that these data are automatically updated through IoT devices that record the energy produced and consumed. These devices send the data to an API which is responsible for recording the values in the blockchain. Then, users can consult these data through the dapp. https://denergy-link-producer.vercel.app/

  3. API to register energy and keep the energy price updated:

    It is a server that has an API; this is used to register the consumed and produced energy in the blockchain.

    Through its endpoints, IoT MCU type devices can make calls to the API, sending the energy they record every hour. When the server receives the data, it saves it in the blockchain. https://api-denergy-server.vercel.app/

    In addition, this server is also responsible for making an hourly request for the current energy price in Spain using a call to a Chainlink contract that makes an external call to the API https://api.preciodelaluz.org/v1/prices/now?zone=PCB. In this way, the price of energy is always updated in the smart contract.

  4. IoT devices:

    These are MCU type devices that can connect to the Wifi network. Their function is to record the energy produced. For example, this will be installed in the power plant and can measure what the solar panels generate through sensors. This measured data will be sent to the API every hour to be registered in the blockchain.

    The same is done by the IoT device that records consumption. It will be installed at the energy entrance of homes and will measure energy consumption every hour to be subsequently registered in the blockchain.

How I Built It

The first thing I had to design was the architecture of this system, figuring out how to have everything interconnected and working automatically.

The backend has been one of the most critical points, as when working with blockchain, one must guarantee efficiency and always use only necessary data.

Chainlink Node and Client

For this project, I am running my own Chainlink node on Google Cloud, and I have created a job to manage calls to the external API https://api.preciodelaluz.org/v1/prices/now?zone=PCB.

From the "EnergyApiConsumer" client contract, a request for price energy is made to keep the updated price value stored. This Consumer contract is then included within the consumer contract, ensuring it always has the price value to update the energy record and billing.

Blockchain

For this project, I decided to use the Polygon blockchain, as it is fully compatible with Ethereum, faster, and has cheaper fees. Additionally, there's extensive documentation available, it has a well-supported test network, and data price feeds are available for the Mumbai network.

Furthermore, it's easier to obtain MATIC for the test network, which allows me to test the contracts.

Smart Contracts

I needed 3 smart contracts to manage the system:

  1. Energy Consumption Contract: This is the backend of the consumer dapp, and this is where the consumed energy is recorded. Using Chainlink's data price feeds and an external API call via a smart contract that implements a Chainlink client, it is possible to get real-time prices for both energy and the ETH/USD/EUR exchange rate. With these data, the user's billing can be recorded.

  2. Energy Production Contract: This is the backend of the producer dapp, and it is here where the energy produced is recorded through the information sent by IoT devices. However, the consumed energy is recorded here through a call made from the consumer contract, thus ensuring data integrity. For the energy produced, it is not necessary to obtain the current price of energy as, in the case of production, the cost depends on the producer, the regulated price is only the sale price.

  3. Energy Api Consumer: This contract calls the external API and keeps the energy price updated. To get the updated price, the consumer contract simply makes a call to this contract to get the stored price.

API Server

This is an API server built with Expressjs. Through its endpoints, it is possible to send the data recorded with IoT devices. The server has the address and private key of the contract owner, so it is authorized to set the values in the contract. For the simulation of the functionality as IoT devices would, I have created a user interface with forms to interact with the API. To connect with the blockchain, the web3js SDK and Infura services are used.

User Interfaces

For the creation of the frontends of the Dapps, I used React, which I found very useful for this task. These are single-page applications that allow you to connect with Metamask and get the consumption data of the connected user. With the web3js SDK and managing the state with React, it was easy to manage the data.

IoT Devices

They have been programmed with the Arduino IDE. It's a simple program where the device connects to the wifi network and sends the measured production and consumption data to the API every hour. For this project, the production and consumption data are simulated, obtained through functions that simulate sensor readings.

Random values within defined and realistic ranges were used for the data, emulating real production and consumption.

Challenges I ran into

One of the biggest challenges in this project was setting up and running my own Chainlink node and creating a Job to manage external API calls.

Chainlink offers a wide variety of services, so initially, it can be complicated to choose the best option for your application. But thanks to the documentation and examples, this task can be accomplished.

For instance, at the beginning of the project, I spent a lot of time researching the documentation to see what was my best option, and I believed that it would be an External adapter. But when I started to test and try out examples, I realized that for my solution, it was much simpler to use the external API calls service as I only needed a specific piece of data.

Accomplishments that I'm proud of

I'm proud of having been able to complete this project overall, as connecting so many parts in a project can be complicated.

The fact that I managed to get a Chainlink node up and running makes me want to continue researching and working with this fascinating technology. It's incredibly powerful to be able to obtain off-chain data and utilize it within your smart contracts.

What I learned

This project has helped me learn new technologies like Chainlink's services and consolidate my knowledge in web3 development, as well as the use of cloud platforms and Docker environment configurations.

It has also improved my programming strategies with React, always keeping the user experience in mind. Furthermore, the use of SDKs like web3js enables the rapid and robust construction of DApps.

What's next for DENERGY-LINK

For DENERGY-LINK, I have many ideas in mind for improvements and additions.

For instance, currently, every time energy is recorded, events are emitted. These logs could be used to show both the user and the producer how much energy is being recorded every hour, making the data more transparent.

Another enhancement for the producer's smart contract is for this contract to manage different energy grids, as the current architecture is one producer to many consumers.

Support could also be added for other blockchain networks, thus extending the system's reach and allowing each user to use their preferred blockchain.

Also add more control over the consumptiom for example add limitations in the consumption based on the user or in the produced energy.

Built With

Share this project:

Updates