From 5dc43247ae33d9cd6dad25fd2951221231defb22 Mon Sep 17 00:00:00 2001 From: "Jephthah Onyeaghala (onyeaghalj)" Date: Mon, 1 Jul 2024 10:33:32 +0100 Subject: [PATCH] Add files via upload --- MQTT Project-Hardware.py | 41 ++++++++++++++++++++++++++++++++ MQTT Project-Software.py.py | 47 +++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 MQTT Project-Hardware.py create mode 100644 MQTT Project-Software.py.py diff --git a/MQTT Project-Hardware.py b/MQTT Project-Hardware.py new file mode 100644 index 0000000..a136e78 --- /dev/null +++ b/MQTT Project-Hardware.py @@ -0,0 +1,41 @@ +#include +#include + +const char* ssid = "YOUR_WIFI_SSID"; +const char* password = "YOUR_WIFI_PASSWORD"; +const char* mqttServer = "YOUR_MQTT_BROKER_IP"; +const int mqttPort = 1883; +const char* mqttUser = "YOUR_MQTT_USERNAME"; +const char* mqttPassword = "YOUR_MQTT_PASSWORD"; + +WiFiClient espClient; +PubSubClient client(espClient); + +void setup_wifi() { + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + } +} + +void reconnect() { + while (!client.connected()) { + if (client.connect("ESP32Client", mqttUser, mqttPassword)) { + // Subscription and publishing logic + } else { + delay(5000); + } + } +} + +void setup() { + setup_wifi(); + client.setServer(mqttServer, mqttPort); +} + +void loop() { + if (!client.connected()) { + reconnect(); + } + client.loop(); +} \ No newline at end of file diff --git a/MQTT Project-Software.py.py b/MQTT Project-Software.py.py new file mode 100644 index 0000000..ff094c5 --- /dev/null +++ b/MQTT Project-Software.py.py @@ -0,0 +1,47 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_mqtt/mqtt_client.dart'; + +void main() { + runApp(MyApp()); +} + +class MyApp extends StatelessWidget { + @override + Widget build(BuildContext context) { + return MaterialApp( + home: MQTTScreen(), + ); + } +} + +class MQTTScreen extends StatefulWidget { + @override + _MQTTScreenState createState() => _MQTTScreenState(); +} + +class _MQTTScreenState extends State { + MQTTClient mqttClient = MQTTClient(); + + @override + void initState() { + super.initState(); + mqttClient.prepareClient(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text('MQTT Mobile App'), + ), + body: Center( + child: ElevatedButton( + onPressed: () { + mqttClient.publishMessage('Topic', 'Message'); + }, + child: Text('Publish Message'), + ), + ), + ); + } +} \ No newline at end of file