Closed vongomben closed 5 months ago
#include "rpcWiFi.h"
void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
return 1;
}
void setup(){
WiFi.begin(ssid, pass);
}
void loop(){
}
This one is new, from scratch, because if you really drag the corresponding command from tinkergen you start don't understanding anything lol
(ping me if you want the svg, I suspect you use the scratch engine to produce them though)
#include <MQTT.h>
#define BROKER_IP "broker.hivemq.com"
#define DEV_NAME "mqttdevice"
#define MQTT_USER "mqtt_user"
#define MQTT_PW "mqtt_password"
We can subscribe to a new channel by creating it, a "payload" appears in the corresponding palette. The payload will later allow us to read and analyse the value arriving.
Publishing on a channel affects the declaration of the publishing data
#include <MQTT.h>
#define MQTT_PUB "/pubChannel"
voide setup(){
}
void loop(){
client.publish(MQTT_PUB, data);
}
We add to the connect() function this mqtt part to make things simpler. Another way could be using two different functions. What do you think?
void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.print("\nconnecting...");
while (!client.connect(DEV_NAME, MQTT_USER, MQTT_PW)) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, pass);
client.begin(BROKER_IP, 1883, net);
client.onMessage(messageReceived);
connect();
}
The messageReceived function is called whenever a message is received. We can use the payload to compare with other elements of the code.
void messageReceived(String &topic, String &payload) {
}
client.subscribe(MQTT_SUB);
has to be addedd to the connect()
function, the #define MQTT_SUB
as an absolute declaration.
void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.print("\nconnecting...");
while (!client.connect(DEV_NAME, MQTT_USER, MQTT_PW)) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
client.subscribe(MQTT_SUB); //SUBSCRIBE TO TOPIC /hello
}
I would need a prototype to make a better example of messageReveived()
Done
I have this code in mind. I'd like to bring this code in CodeCraft, or something simpler
Set Wifi
Image:
Code