Difference between revisions of "Smart electronics"

From Interaction Station Wiki
Jump to navigation Jump to search
Line 23: Line 23:
 
=== Connecting to MQQT broker ===
 
=== Connecting to MQQT broker ===
  
 +
<syntaxhighlight lang="cpp">
 
void connectMqtt() {
 
void connectMqtt() {
 
   Serial.print("\nConnecting to MQTT broker...");
 
   Serial.print("\nConnecting to MQTT broker...");
Line 33: Line 34:
 
   client.subscribe(topic);
 
   client.subscribe(topic);
 
}
 
}
 +
</syntaxhighlight>

Revision as of 21:37, 3 November 2024


ESP Board manager

https://espressif.github.io/arduino-esp32/package_esp32_index.json

Helper functions

Connecting to WiFI

void connectWifi() {
  Serial.print("Connecting to wifi...");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println("\nWifi connected!");
  delay(1000);
}

Connecting to MQQT broker

void connectMqtt() {
  Serial.print("\nConnecting to MQTT broker...");
  while (!client.connect(client_id.c_str())) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println("\nMQTT connected!");
  delay(1000);
  client.subscribe(topic);
}