Difference between revisions of "Smart electronics"

From Interaction Station Wiki
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 4: Line 4:
  
 
<code>https://espressif.github.io/arduino-esp32/package_esp32_index.json</code>
 
<code>https://espressif.github.io/arduino-esp32/package_esp32_index.json</code>
 +
 +
== Tester ==
 +
https://editor.p5js.org/bsmeenk/full/UuEWPceBe
  
 
== Helper functions ==  
 
== Helper functions ==  
  
Connect to WiFI
+
=== Connecting to WiFI ===
<code lang="cpp">
+
 
 +
<syntaxhighlight lang="cpp">
 +
void connectWifi() {
 +
  Serial.print("Connecting to wifi...");
 +
  while (WiFi.status() != WL_CONNECTED) {
 +
    Serial.print(".");
 +
    delay(1000);
 +
  }
 +
  Serial.println("\nWifi connected!");
 +
  delay(1000);
 +
}
 +
</syntaxhighlight>
 +
 
 +
=== Connecting to MQQT broker ===
 +
 
 +
<syntaxhighlight lang="cpp">
 +
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);
 +
}
 +
</syntaxhighlight>
 +
 
 +
== Sketches ==
 +
 
 +
=== LED ===
 +
<syntaxhighlight lang="cpp">
 +
int ledPin = 26;
 +
 
 +
void setup() {
 +
  pinMode(ledPin, OUTPUT);
 +
  digitalWrite(ACTION_PIN, HIGH);
 +
}
 +
 
 +
void loop() {
 +
}
 +
</syntaxhighlight>
 +
 
 +
=== LED + WiFi ===
 +
 
 +
<syntaxhighlight lang="cpp">
 +
#include <WiFi.h>
 +
 
 +
int ledPin = 26;
 +
 
 +
char ssid[] = "bridgenet";
 +
char pass[] = "interaction";
 +
 
 +
WiFiClient wifiClient;
 +
MQTTClient client;
 +
 
 +
void setup() {
 +
  pinMode(ledPin, OUTPUT);
 +
  digitalWrite(ledPin, HIGH);
 +
 
 +
  WiFi.begin(ssid, pass);
 +
  connectWifi();
 +
}
 +
 
 +
void loop() {
 +
}
 +
 
 
void connectWifi() {
 
void connectWifi() {
 
   Serial.print("Connecting to wifi...");
 
   Serial.print("Connecting to wifi...");
Line 18: Line 87:
 
   delay(1000);
 
   delay(1000);
 
}
 
}
</code>
+
</syntaxhighlight>
 +
 
 +
=== LED + WiFi + MQTT ===
 +
 
 +
<syntaxhighlight lang="cpp">
 +
#include <WiFi.h>
 +
#include <MQTT.h>
 +
 
 +
int ledPin = 26;
 +
 
 +
char ssid[] = "bridgenet";
 +
char pass[] = "interaction";
 +
 
 +
char host[] = "test.mosquitto.org";
 +
char topic[] = "wdka"; // change this
 +
String client_id = "boris-esp"; // change this
 +
 
 +
WiFiClient wifiClient;
 +
MQTTClient client;
 +
 
 +
void setup() {
 +
  Serial.begin(115200);
 +
 
 +
  pinMode(ledPin, OUTPUT);
 +
  digitalWrite(ledPin, HIGH);
 +
 
 +
  WiFi.begin(ssid, pass);
 +
  connectWifi();
 +
 
 +
  client.begin(host, wifiClient);
 +
  client.onMessage(messageReceived);
 +
  connectMqtt();
 +
}
 +
 
 +
void loop() {
 +
  client.loop();
 +
 
 +
  if (!client.connected()) {
 +
    Serial.println("lost connection");
 +
    connectMqtt();
 +
  }
 +
}
 +
 
 +
void messageReceived(String &topic, String &payload) {
 +
  Serial.println(payload);
 +
 
 +
  if (payload == "off") {
 +
    digitalWrite(ledPin, LOW);
 +
  } else if (payload == "on") {
 +
    digitalWrite(ledPin, HIGH);
 +
  }
 +
}
 +
 
 +
void connectWifi() {
 +
  Serial.print("Connecting to wifi...");
 +
  while (WiFi.status() != WL_CONNECTED) {
 +
    Serial.print(".");
 +
    delay(1000);
 +
  }
 +
  Serial.println("\nWifi connected!");
 +
  delay(1000);
 +
}
 +
 
 +
void connectMqtt() {
 +
  Serial.print("\nConnecting to MQTT server...");
 +
  while (!client.connect(client_id.c_str())) {
 +
    Serial.print(".");
 +
    delay(1000);
 +
  }
 +
  Serial.println("\nMQTT connected!");
 +
  delay(1000);
 +
  client.subscribe(topic);
 +
}
 +
</syntaxhighlight>

Latest revision as of 10:31, 4 November 2024


ESP Board manager

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

Tester

https://editor.p5js.org/bsmeenk/full/UuEWPceBe

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);
}

Sketches

LED

int ledPin = 26;

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ACTION_PIN, HIGH);
}

void loop() {
}

LED + WiFi

#include <WiFi.h>

int ledPin = 26;

char ssid[] = "bridgenet";
char pass[] = "interaction"; 

WiFiClient wifiClient;
MQTTClient client;

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);

  WiFi.begin(ssid, pass);
  connectWifi(); 
}

void loop() {
}

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

LED + WiFi + MQTT

#include <WiFi.h>
#include <MQTT.h>

int ledPin = 26;

char ssid[] = "bridgenet";
char pass[] = "interaction"; 

char host[] = "test.mosquitto.org";
char topic[] = "wdka"; // change this
String client_id = "boris-esp"; // change this

WiFiClient wifiClient;
MQTTClient client;

void setup() {
  Serial.begin(115200);

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);

  WiFi.begin(ssid, pass);
  connectWifi(); 

  client.begin(host, wifiClient);
  client.onMessage(messageReceived);
  connectMqtt();
}

void loop() {
  client.loop();

  if (!client.connected()) {
    Serial.println("lost connection");
    connectMqtt();
  }
}

void messageReceived(String &topic, String &payload) {
  Serial.println(payload);

  if (payload == "off") {
    digitalWrite(ledPin, LOW);
  } else if (payload == "on") {
    digitalWrite(ledPin, HIGH);
  }
}

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

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