Difference between revisions of "Smart electronics"
Jump to navigation
Jump to search
(3 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 == | ||
Line 33: | Line 36: | ||
delay(1000); | delay(1000); | ||
client.subscribe(topic); | 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");
}