Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
onyeaghalj committed Jul 1, 2024
1 parent cb32ca5 commit 5dc4324
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
41 changes: 41 additions & 0 deletions MQTT Project-Hardware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <WiFi.h>
#include <PubSubClient.h>

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();
}
47 changes: 47 additions & 0 deletions MQTT Project-Software.py.py
Original file line number Diff line number Diff line change
@@ -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<MQTTScreen> {
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'),
),
),
);
}
}

0 comments on commit 5dc4324

Please sign in to comment.