tinycodinglm / sys /sys-0020.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 19)
034ba67 verified
|
Raw History Blame
1.8 kB
deepseek-harness trace
System You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing c...

You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies.

User

How do I connect ESP32 to WiFi and MQTT?

Assistant
cpp
#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid = "MyWiFi";
const char* password = "password";
const char* mqtt_server = "broker.example.com";

WiFiClient espClient;
PubSubClient client(espClient);

void setup_wifi() {
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
    }
}

void callback(char* topic, byte* payload, unsigned int length) {
    String message;
    for (int i = 0; i < length; i++) {
        message += (char)payload[i];
    }
    Serial.println("Message: " + message);
}

void reconnect() {
    while (!client.connected()) {
        if (client.connect("ESP32Client")) {
            client.subscribe("sensors/#");
            client.publish("sensors/status", "online");
        }
    }
}

void loop() {
    if (!client.connected()) reconnect();
    client.loop();

    // Publish sensor data
    float temp = readTemperature();
    String payload = String(temp);
    client.publish("sensors/temperature", payload.c_str());
    delay(5000);
}