ExplainerInternet of ThingsIoT HardwaresIoT Software&ToolsSensor & DevicesTutorials/DIY

Building a Real-World IoT Project Using CounterFit

Introduction

CounterFit allows developers to prototype IoT applications without the need for physical sensors and actuators. In this article, we will build a simple IoT project using CounterFit to simulate a temperature and humidity monitoring system. We will also discuss how this setup compares to a real hardware implementation.

Official CounterFit Repository: https://github.com/CounterFit-IoT/CounterFit

Project Overview

The project will simulate a temperature and humidity sensor and send the readings to an IoT dashboard. We will use the CounterFit simulator to generate sensor data and visualize it in real time.

Components Used

  • CounterFit for Virtual Sensors
  • Python for Data Processing
  • Flask for Web Dashboard
  • MQTT for Data Communication (Optional)

Step 1: Setting Up CounterFit

  1. Install CounterFit if not already installed:
    pip install counterfit
    
  2. Start the CounterFit server:
    counterfit
    
  3. Open the browser and go to:
    http://localhost:5000
    
  4. Add a virtual temperature sensor and a humidity sensor from the dashboard.

Step 2: Reading Sensor Data with Python

Install the CounterFit client library:

pip install counterfit-client

Create a Python script (read_sensors.py) and add the following code:

from counterfit_shims_grove import CounterFitConnection, GroveTemperatureSensor, GroveHumiditySensor

# Connect to CounterFit
CounterFitConnection.init('127.0.0.1', 5000)

# Initialize sensors
temp_sensor = GroveTemperatureSensor(0)
humidity_sensor = GroveHumiditySensor(1)

# Read and print sensor values
print("Temperature:", temp_sensor.read(), "°C")
print("Humidity:", humidity_sensor.read(), "%")

Run the script:

python read_sensors.py

This will output simulated temperature and humidity values.

Step 3: Creating a Web Dashboard with Flask

Install Flask:

pip install flask

Create a Flask application (app.py) to display sensor data:

from flask import Flask, render_template
from counterfit_shims_grove import CounterFitConnection, GroveTemperatureSensor, GroveHumiditySensor

app = Flask(__name__)
CounterFitConnection.init('127.0.0.1', 5000)
temp_sensor = GroveTemperatureSensor(0)
humidity_sensor = GroveHumiditySensor(1)

@app.route('/')
def index():
    temp = temp_sensor.read()
    humidity = humidity_sensor.read()
    return f"Temperature: {temp}°C, Humidity: {humidity}%"

if __name__ == '__main__':
    app.run(debug=True)

Run the web server:

python app.py

Visit http://localhost:5000 in a browser to see real-time sensor values.

Step 4: (Optional) Sending Data to MQTT Broker

To integrate with an IoT cloud platform, install the MQTT client:

pip install paho-mqtt

Modify read_sensors.py to send data via MQTT:

import paho.mqtt.client as mqtt
import time
from counterfit_shims_grove import CounterFitConnection, GroveTemperatureSensor, GroveHumiditySensor

CounterFitConnection.init('127.0.0.1', 5000)
temp_sensor = GroveTemperatureSensor(0)
humidity_sensor = GroveHumiditySensor(1)

mqtt_client = mqtt.Client()
mqtt_client.connect("broker.hivemq.com", 1883, 60)

while True:
    temp = temp_sensor.read()
    humidity = humidity_sensor.read()
    mqtt_client.publish("iot/sensor", f"Temperature: {temp}°C, Humidity: {humidity}%")
    time.sleep(5)

Comparing CounterFit with Real Hardware Implementation

Feature CounterFit (Simulator) Real Hardware
Sensor Data Simulated values Real-world values
Cost Free Requires sensors and microcontroller
Deployment Local testing only Can be deployed in the field
Cloud Integration Supports MQTT & APIs Full IoT cloud integration
Reliability Best for development Required for final implementation

Conclusion

Using CounterFit, we successfully simulated a temperature and humidity monitoring system, visualized data on a web dashboard, and sent sensor data to an MQTT broker. While this approach is excellent for development and testing, deploying an IoT solution in real-world scenarios requires transitioning to actual hardware.

With CounterFit, developers can prototype faster, reduce costs, and debug efficiently before committing to real-world deployments. Try implementing more sensors and cloud integrations to expand this project further!

Read This: Introduction to CounterFit: A Virtual IoT Hardware Simulator

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *