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 ===
 +
 
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
 
void connectWifi() {
 
void connectWifi() {
Line 17: Line 21:
 
   Serial.println("\nWifi connected!");
 
   Serial.println("\nWifi connected!");
 
   delay(1000);
 
   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() {
 +
  Serial.print("Connecting to wifi...");
 +
  while (WiFi.status() != WL_CONNECTED) {
 +
    Serial.print(".");
 +
    delay(1000);
 +
  }
 +
  Serial.println("\nWifi connected!");
 +
  delay(1000);
 +
}
 +
</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>
 +
 +
== P5.js ==
 +
 +
=== MQTT library ===
 +
<syntaxhighlight lang="html">
 +
<script src="https://cdnjs.cloudflare.com/ajax/libs/mqtt/4.2.6/mqtt.js"></script>
 +
</syntaxhighlight>
 +
 +
=== MQTT connect ===
 +
<syntaxhighlight lang="js">
 +
let client = mqtt.connect("wss://test.mosquitto.org:8081/mqtts")
 +
 +
function onConnect() {
 +
  client.subscribe("wdka", function(err) {
 +
    if (!err) {
 +
      client.publish("wdka", "on");
 +
    }
 +
  })
 +
}
 +
</syntaxhighlight>
 +
 +
=== MQTT send messages ===
 +
<syntaxhighlight lang="js">
 +
function ledOn() {
 +
  client.publish("wdka", "on");
 +
}
 +
 +
function ledOff() {
 +
  client.publish("wdka", "off");
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 09:02, 18 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);
}

P5.js

MQTT library

<script src="https://cdnjs.cloudflare.com/ajax/libs/mqtt/4.2.6/mqtt.js"></script>

MQTT connect

let client = mqtt.connect("wss://test.mosquitto.org:8081/mqtts")

function onConnect() {
  client.subscribe("wdka", function(err) {
    if (!err) {
      client.publish("wdka", "on");
    }
  })
}

MQTT send messages

function ledOn() {
  client.publish("wdka", "on");
}

function ledOff() {
  client.publish("wdka", "off");
}