Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How di i puplich a message to a topic from the website #19

Open
0XZEROX0 opened this issue Apr 14, 2024 · 0 comments
Open

How di i puplich a message to a topic from the website #19

0XZEROX0 opened this issue Apr 14, 2024 · 0 comments

Comments

@0XZEROX0
Copy link

Hey I was trying to make a website that can toggle the state of an LED using the liberally but I don't know how to publish a message from the website to the server here is that code if it help the last part is what should send the message to the MQTT server but It doesn't work

#include <Arduino.h>
#include <WiFi.h>
#include "EmbeddedMqttBroker.h"

using namespace mqttBrokerName;

const char *ssid = ".....";
const char *password = "....";
const char *AP_NameChar = "WebServerAndBroker";

/******************* mqtt broker ********************/
uint16_t mqttPort = 1883;
MqttBroker broker(mqttPort);

/****************** http server ********************/
uint16_t httpPort = 80;
WiFiServer server(httpPort);
WiFiClient httpClient;

void setup() {
Serial.begin(115200);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.println("http server and mqtt broker");
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.mode(WIFI_STA);
//WiFi.mode(WIFI_AP_STA);
//MDNS.begin(AP_NameChar,WiFi.localIP());

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password);
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");

// Start http server
server.begin();
Serial.println("http server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("");

// Start the mqtt broker
broker.setMaxNumClients(10); // set according to your system.
broker.startBroker();
Serial.println("broker started");

// Print the IP address
Serial.print("Use this ip: ");
Serial.println(WiFi.localIP());
Serial.print("and this port: ");
Serial.println(mqttPort);
Serial.println("To connect to mqtt broker");
}

void loop() {
// Check if a client has connected
httpClient = server.available();
if (!httpClient) {
vTaskDelay(10);
return;
}

boolean wait = true;
while (!httpClient.available() and wait) {
vTaskDelay(300);
wait = false;
}

if (!httpClient.available()) {
return;
}

String request = httpClient.readStringUntil('\r');
Serial.println(request);

httpClient.println("HTTP/1.1 200 OK");
httpClient.println("Content-type:text/html");
httpClient.println();

// Adding the HTML form with a button to toggle the ledControl topic
httpClient.println("");
httpClient.println("");
httpClient.println("<title>Toggle LED</title>");
httpClient.println("");
httpClient.println("

Toggle LED

");
httpClient.println("<form action="/toggle" method="get">");
httpClient.println("<button type="submit">Toggle LED");
httpClient.println("");
httpClient.println("");
httpClient.println("");

// Handling the toggle request
if (request.indexOf("/toggle") != -1) {
// Toggle the ledControl topic
static bool ledState = false;
ledState = !ledState;
if (ledState) {
broker.publish("ledControl", "1");
} else {
broker.publish("ledControl", "0");
}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant