Created
June 6, 2023 21:44
-
-
Save HaroldPetersInskipp/afb8179cbc2f0d8c382de31268dbec58 to your computer and use it in GitHub Desktop.
DIY Mouse Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This sketch defines the pins used for each button and sets them as inputs with internal pull-up resistors. | |
The loop function checks the state of each button and publishes a message to the specified MQTT topic if any button is pressed. | |
The message consists of a string of 0s and 1s representing the state of each button. | |
The sketch also includes functions to connect to the WiFi network and MQTT broker, as well as a function to publish the button state to the MQTT topic. | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
// WiFi credentials | |
const char* ssid = "your_SSID"; | |
const char* password = "your_WIFI_password"; | |
// MQTT broker details | |
const char* mqtt_server = "your_MQTT_broker_IP_address"; | |
const int mqtt_port = 1883; | |
const char* mqtt_username = "your_MQTT_username"; | |
const char* mqtt_password = "your_MQTT_password"; | |
const char* mqtt_topic = "game_controller/commands"; | |
// Pin definitions | |
const int button_a_pin = D5; | |
const int button_b_pin = D6; | |
const int button_select_pin = D7; | |
const int button_start_pin = D8; | |
const int button_up_pin = D1; | |
const int button_down_pin = D2; | |
const int button_left_pin = D3; | |
const int button_right_pin = D4; | |
// Button state variables | |
bool button_a_state = false; | |
bool button_b_state = false; | |
bool button_select_state = false; | |
bool button_start_state = false; | |
bool button_up_state = false; | |
bool button_down_state = false; | |
bool button_left_state = false; | |
bool button_right_state = false; | |
// WiFi client and MQTT client instances | |
WiFiClient espClient; | |
PubSubClient mqttClient(espClient); | |
// Function to connect to WiFi network | |
void connectWiFi() { | |
Serial.print("Connecting to WiFi"); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(); | |
Serial.println("WiFi connected"); | |
} | |
// Function to connect to MQTT broker | |
void connectMQTT() { | |
Serial.print("Connecting to MQTT broker"); | |
while (!mqttClient.connected()) { | |
if (mqttClient.connect("ESP8266Client", mqtt_username, mqtt_password)) { | |
Serial.println("connected"); | |
mqttClient.subscribe(mqtt_topic); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(mqttClient.state()); | |
Serial.println(" retrying in 5 seconds"); | |
delay(5000); | |
} | |
} | |
} | |
// Function to publish button state to MQTT topic | |
void publishButtonState() { | |
String message = ""; | |
message += button_a_state ? "1" : "0"; | |
message += button_b_state ? "1" : "0"; | |
message += button_select_state ? "1" : "0"; | |
message += button_start_state ? "1" : "0"; | |
message += button_up_state ? "1" : "0"; | |
message += button_down_state ? "1" : "0"; | |
message += button_left_state ? "1" : "0"; | |
message += button_right_state ? "1" : "0"; | |
mqttClient.publish(mqtt_topic, message.c_str()); | |
} | |
void setup() { | |
// Initialize serial port | |
Serial.begin(115200); | |
// Connect to WiFi network | |
connectWiFi(); | |
// Set up button pins as inputs with internal pull-up resistors | |
pinMode(button_a_pin, INPUT_PULLUP); | |
pinMode(button_b_pin, INPUT_PULLUP); | |
pinMode(button_select_pin, INPUT_PULLUP); | |
pinMode(button_start_pin, INPUT_PULLUP); | |
pinMode(button_up_pin, INPUT_PULLUP); | |
pinMode(button_down_pin, INPUT_PULLUP); | |
pinMode(button_left_pin, INPUT_PULLUP); | |
pinMode(button_right_pin, INPUT_PULLUP); | |
// Set up MQTT client | |
mqttClient.setServer(mqtt_server, mqtt_port); | |
// Connect to MQTT broker | |
connectMQTT(); | |
} | |
void loop() { | |
// Check button states and publish to MQTT topic if any button is pressed | |
if (digitalRead(button_a_pin) == LOW) { | |
button_a_state = true; | |
publishButtonState(); | |
delay(50); | |
button_a_state = false; | |
publishButtonState(); | |
delay(50); | |
} | |
if (digitalRead(button_b_pin) == LOW) { | |
button_b_state = true; | |
publishButtonState(); | |
delay(50); | |
button_b_state = false; | |
publishButtonState(); | |
delay(50); | |
} | |
if (digitalRead(button_select_pin) == LOW) { | |
button_select_state = true; | |
publishButtonState(); | |
delay(50); | |
button_select_state = false; | |
publishButtonState(); | |
delay(50); | |
} | |
if (digitalRead(button_start_pin) == LOW) { | |
button_start_state = true; | |
publishButtonState(); | |
delay(50); | |
button_start_state = false; | |
publishButtonState(); | |
delay(50); | |
} | |
if (digitalRead(button_up_pin) == LOW) { | |
button_up_state = true; | |
publishButtonState(); | |
while (digitalRead(button_up_pin) == LOW) { | |
delay(50); | |
} | |
button_up_state = false; | |
publishButtonState(); | |
delay(50); | |
} | |
if (digitalRead(button_down_pin) == LOW) { | |
button_down_state = true; | |
publishButtonState(); | |
while (digitalRead(button_down_pin) == LOW) { | |
delay(50); | |
} | |
button_down_state = false; | |
publishButtonState(); | |
delay(50); | |
} | |
if (digitalRead(button_left_pin) == LOW) { | |
button_left_state = true; | |
publishButtonState(); | |
while (digitalRead(button_left_pin) == LOW) { | |
delay(50); | |
} | |
button_left_state = false; | |
publishButtonState(); | |
delay(50); | |
} | |
if (digitalRead(button_right_pin) == LOW) { | |
button_right_state = true; | |
publishButtonState(); | |
while (digitalRead(button_right_pin) == LOW) { | |
delay(50); | |
} | |
button_right_state = false; | |
publishButtonState(); | |
delay(50); | |
} | |
// Reconnect to WiFi and MQTT if disconnected | |
if (WiFi.status() != WL_CONNECTED) { | |
connectWiFi(); | |
} | |
if (!mqttClient.connected()) { | |
connectMQTT(); | |
} | |
// Maintain MQTT connection | |
mqttClient.loop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment