Seeed-Studio / CodeCraft

Codecraft is a graphical programming software which is based on Scratch 3.0
https://ide.tinkergen.com/
Apache License 2.0
18 stars 3 forks source link

Codecraft Requests - MQTT over WIFI #10

Closed vongomben closed 5 months ago

vongomben commented 6 months ago

I have this code in mind. I'd like to bring this code in CodeCraft, or something simpler

Set Wifi

Image:

setWifi

Code

const char ssid[] = SECRET_SSID;
const char pass[] = SECRET_PASS;
vongomben commented 6 months ago

Image

image

Code


#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(){

}
vongomben commented 6 months ago

This one is new, from scratch, because if you really drag the corresponding command from tinkergen you start don't understanding anything lol

Image

setMqtt

(ping me if you want the svg, I suspect you use the scratch engine to produce them though)

Code

#include <MQTT.h>

#define BROKER_IP    "broker.hivemq.com"
#define DEV_NAME     "mqttdevice"
#define MQTT_USER    "mqtt_user"
#define MQTT_PW      "mqtt_password"
vongomben commented 6 months ago

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.

mqtt-UI-4

vongomben commented 6 months ago

mqtt-pub

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); 
}
vongomben commented 6 months ago

mqtt-connected

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

}
vongomben commented 6 months ago

MESSAGE-RECEIVED

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()

chenwenhao568 commented 5 months ago

Done