The code you provided is a good starting point for implementing the functionality you described.
However, it seems that you want to replace the user input through the Serial Monitor with input from
push buttons. To achieve that, you need to make some modifications to the code. Here's an updated
version that uses push buttons for input:
```cpp
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// WiFi credentials
const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";
// MQTT Broker
const char* mqtt_server = "mqtt.server.com";
const int mqtt_port = 1883;
const char* mqtt_username = "mqtt_username";
const char* mqtt_password = "mqtt_password";
const char* mqtt_topic = "tire_pressure";
// I2C LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Pressure sensor
const int pressurePin = A0;
const float pressureOffset = 0.0;
const float pressureScale = 1.0;
// Relay module
const int relayPin = D1;
const int compressorPin = D2;
// Global variables
float tirePressure = 0.0;
float pressureThreshold = 0.0; // User-defined pressure threshold
WiFiClient espClient;
PubSubClient client(espClient);
// Button pins
const int incrementButtonPin = D3;
const int decrementButtonPin = D4;
// Button state variables
int incrementButtonState = HIGH;
int decrementButtonState = HIGH;
int lastIncrementButtonState = HIGH;
int lastDecrementButtonState = HIGH;
void setup() {
// Initialize LCD
lcd.begin(16, 2);
lcd.backlight();
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
Serial.println("");
Serial.println("WiFi connected");
// Connect to MQTT Broker
client.setServer(mqtt_server, mqtt_port);
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP8266Client", mqtt_username, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" retrying in 5 seconds");
delay(5000);
}
// Set MQTT callback function
client.setCallback(callback);
// Initialize LCD display
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Tire Pressure:");
// Prompt user to enter the pressure threshold
lcd.setCursor(0, 1);
lcd.print("Enter threshold:");
// Wait for increment button press
while (digitalRead(incrementButtonPin) == HIGH) {
delay(100);
// Increment the pressure threshold
pressureThreshold++;
updateThresholdDisplay();
// Wait for decrement button press
while (digitalRead(decrementButtonPin) == HIGH) {
delay(100);
// Decrement the pressure threshold
pressureThreshold--;
updateThresholdDisplay();
// Wait for button release
while (digitalRead(incrementButtonPin) == LOW || digitalRead(decrementButtonPin) == LOW) {
delay(100);
// Clear the screen
lcd.clear();
void loop() {
if (!client.connected()) {
reconnect();
client.loop();
// Read tire pressure
float voltage = analogRead(pressurePin) * (5.0 / 1023.0);
tirePressure = (voltage - pressureOffset) * pressureScale;
// Publish tire pressure to MQTT broker
char pressureStr[8];
dtostrf(tirePressure, 2, 2, pressureStr);
client.publish(mqtt_topic, pressureStr);
// Update LCD
lcd.setCursor(0, 1);
lcd.print(pressureStr);
lcd.print(" psi");
// Check if tire pressure is below threshold
if (tirePressure < pressureThreshold) {
// Activate relay to turn on compressor
digitalWrite(relayPin, HIGH);
digitalWrite(compressorPin, HIGH);
delay(1000); // Adjust the delay for compressor activation
// Monitor tire pressure until it reaches the desired level
while (tirePressure < pressureThreshold) {
// Read tire pressure
voltage = analogRead(pressurePin) * (5.0 / 1023.0);
tirePressure = (voltage - pressureOffset) * pressureScale;
// Update LCD
lcd.setCursor(9, 1);
lcd.print("Inflating...");
//I apologize for the incomplete response. Here's the continuation of the code:
```cpp
// Publish updated tire pressure to MQTT broker
dtostrf(tirePressure, 2, 2, pressureStr);
client.publish(mqtt_topic, pressureStr);
// Turn off compressor and relay
digitalWrite(compressorPin, LOW);
digitalWrite(relayPin, LOW);
lcd.setCursor(9, 1);
lcd.print("Done ");
delay(1000);
void callback(char* topic, byte* payload, unsigned int length) {
// Handle MQTT messages if needed
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP8266Client", mqtt_username, mqtt_password)) {
Serial.println("connected");
client.subscribe(mqtt_topic);
} else {
Serial.print("failed, rc=");
Serial.print(clientstate());
Serial.println(" retrying in 5 seconds");
delay(5000);
void updateThresholdDisplay() {
lcd.setCursor(0, 1);
lcd.print("Threshold: ");
lcd.print(pressureThreshold);
lcd.print(" psi");
```
In this updated code, we have added two button pins: `incrementButtonPin` and `decrementButtonPin`.
These pins are used to increment and decrement the pressure threshold value. We also added button
state variables to keep track of the button states.
During the setup phase, the code waits for the increment button to be pressed, increments the pressure
threshold, and updates the LCD display. Then it waits for the decrement button to be pressed,
decrements the pressure threshold, and updates the LCD display. Finally, it waits for both buttons to be
released before clearing the LCD display.
The `updateThresholdDisplay()` function is introduced to update the LCD display with the current
pressure threshold value.
Please make sure you connect the push buttons to the appropriate pins on your ESP8266 board and
adjust the pin assignments in the code accordingly.