Blynk Speedo Meter Code for ESP8266
Blynk Speedo Meter Code for ESP8266
The flag variable serves as a state indicator to manage when a speed alert should be logged, specifically preventing repeated alerts during sustained high RPM conditions. This state management avoids flooding the Blynk interface with redundant events. However, potential improvements include defining multiple alert levels with a flag for each, providing more granular feedback. Additionally, implementing a debounce or time-based reset on the flag might help in more controlled alert triggering, ensuring that only genuine breaches prompt the system to log alerts, thus increasing reliability and user functionality .
The LiquidCrystal_I2C library is used in the system to control a 16x2 LCD display connected via I2C, a communication protocol that allows interfacing with the LCD using fewer wires. The system uses this library to initialize the LCD with lcd.begin and control the display of text information, such as speed measured in km/h and RPM values on the screen. It provides functions to clear the display, set cursor position, and print text, facilitating user interaction by presenting real-time feedback directly on the LCD .
The motor speed control logic efficiently maps the sensor input to motor speed using the map function, allowing for a direct translation from analog input (0-1023) to a defined speed range (0-300). This enables nuanced control over the motor's output. The system updates motor speed in real time, recalculating every loop iteration and using analogWrite for PWM signal adjustments, providing rapid, continuous feedback. However, efficiency could be improved by decoupling sensor reading frequency from main loop execution to ensure consistent performance and by optimizing the map ranges to suit specific motor demands, potentially reducing unnecessary output granularity .
To improve error handling in this system, adding checks to ensure all operations, like WiFi connections and Blynk integration, are successful can prevent issues at runtime. Implementing retries and timeout mechanisms for network-dependent functions could enhance stability. Additionally, diagnostic logs with descriptive error messages for failed operations would allow easier debugging. Ensuring that interrupt service routines execute quickly to avoid missing pulses and confirming that mapped values stay within expected ranges can prevent system misbehavior .
A speed alert is triggered in the system when the RPM value exceeds 100, which is represented by the variable Map_rpm being greater than 100, and the flag variable is 0. When these conditions are met, the system prints "Gas Detected" to the serial output and logs a "Speed Alert" event through Blynk using Blynk.logEvent. The flag is then set to 1 to prevent repeated alerts immediately following, and this state is reset to 0 when Map_rpm falls below 100, allowing new alerts for future threshold breaches .
The setup function initializes the hardware and software components of the speedometer system by configuring the wire communication on D2 and D1 for the LCD, initializing the LCD with backlight, setting pin modes for output and input (motorPin for PWM output and SENSOR_PIN for interrupt input), and enabling serial communication at 115200 baud. It also establishes a connection to the Blynk server using the defined authentication token and WiFi credentials, and sets up the timer to periodically call UpdateBlynk function. The attachInterrupt function is configured to handle sensor pulse counting, completing the setup necessary for the system's operation .
The system detects changes in motor speed using an interrupt-driven mechanism on the SENSOR_PIN (D5). Each time a pulse is detected (indicating part of a revolution of the motor), the ISR countPulse is invoked, incrementing the pulseCount variable. The RPM is calculated by multiplying the pulseCount by 60, assuming one pulse per revolution. This is done every 1000 milliseconds in the main loop, by comparing the current time to the last time the RPM was calculated, ensuring the RPM reflects the motor's speed in a real-time manner .
The Blynk Timer in the speedometer system is used to call the UpdateBlynk function at regular intervals. Specifically, in the setup function, the timer is set to trigger every 2000 milliseconds (2 seconds) using timer.setInterval(2000L, UpdateBlynk). This timely invocation allows the system to regularly update the Blynk interface with the latest RPM reading by writing Map_rpm to the virtual pin V0, and it checks for speed alerts if the RPM exceeds 100, generating a log event in the Blynk app .
The speed of the motor is controlled by reading the sensor value from an analog pin (A0), which ranges from 0 to 1023. This value is then mapped to a speed range from 0 to 300 using the map function. The resulting speed is used as a scalar in the analogWrite function to set the output on motorPin. By adjusting the PWM signal proportional to the sensor value, the analogWrite function effectively controls the motor's speed, enabling speed variation from a complete stop to the maximum defined by the system .
The attachInterrupt function is critical as it configures an external interrupt on the SENSOR_PIN (D5), enabling the speedometer system to respond to changes in the signal from the sensor rapidly and efficiently. The interrupt is set to trigger on a FALLING edge, which occurs when the signal goes from high to low, indicative of a part of the motor's rotation. This interrupt-driven approach provides precise timing and real-time reaction by updating the pulseCount variable every time the sensor detects a drop in voltage level, thereby contributing accurate real-time calculations of RPM .