B4R Question Someone explain this to me please?

Cesar_Morisco

Active Member
Licensed User
Longtime User
Hi, Someone explain this to me
In Arduino, Alexia finds the devices
Em B4R Com o "c" embutido, alexia não consegue encontrar os dispositivos
And that's wrong
and what am I doing wrong

B4R:
Sub Process_Globals
    Public Serial1 As Serial
    Private timer3 As Timer
    Private wifi As ESP8266WiFi
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    If wifi.Connect2("Help-Info Central Tec 2.4G", "Pp1c2s3m4") Then
        Log("Connected to network ", wifi.LocalIp)
        ' Inicializa o FauxmoESP
        RunNative("initialize", Null)
        ' Inicializa o Timer para processar o FauxmoESP
        timer3.Initialize("fauxmoHandler", 100)
        timer3.Enabled = True
    Else
        Log("Failed to connect to network")
    End If

End Sub

Sub fauxmoHandler
    ' Chama o código nativo para processar o FauxmoESP
    RunNative("loopFauxmo", Null)
End Sub


#If C
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"
fauxmoESP fauxmo;

void initialize(B4R::Object* o) {
    Serial.begin(115200);
    // WiFi já está conectado no código B4R, não precisamos conectar novamente
    Serial.println("FauxmoESP inicializado");
    fauxmo.createServer(true); // not needed, this is the default value
    fauxmo.setPort(80);  // Porta necessária para dispositivos gen3
    fauxmo.enable(true);
    // Inicializa os dispositivos no FauxmoESP
    fauxmo.addDevice("Sala");
    fauxmo.addDevice("Quarto");
    fauxmo.addDevice("Mesa");
    fauxmo.addDevice("Banheiro");
    fauxmo.addDevice("Cozinha");
    fauxmo.addDevice("Varanda");
    // Define o callback para o FauxmoESP
    fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
    Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
    });
    }
void handleFauxmo() {
// Processa os pacotes do FauxmoESP
fauxmo.handle();
}
void loopFauxmo(B4R::Object* o) {
handleFauxmo();  // Chama o método que processa os pacotes do FauxmoESP
Serial.println("fauxmo.handle");
}
#End If

Arduino:
#include <Arduino.h>
//#include <WiFi.h>
//#include <ESP8266WiFi.h>

#ifdef ESP32
  #include <WiFi.h>
#else
  #include <ESP8266WiFi.h>
#endif

#include "fauxmoESP.h"
#define SERIAL_BAUDRATE 115200
#define WIFI_SSID "Help-Info Central Tec 2.4G"
#define WIFI_PASS "Pp1c2s3m4"

fauxmoESP fauxmo;
  void wifiSetup() {
  WiFi.mode(WIFI_STA);
  Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) {
  Serial.print(".");
  delay(100);
  }
  Serial.println();
  Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}

void setup() {
  // Init serial port and clean garbage
  Serial.begin(SERIAL_BAUDRATE);
  Serial.println();

  // Wi-Fi connection
  wifiSetup();
  fauxmo.createServer(true); // not needed, this is the default value
  fauxmo.setPort(80); // This is required for gen3 devices
  fauxmo.enable(true);
 // Add virtual devices
  fauxmo.addDevice("Sala");
  fauxmo.addDevice("Quarto");
  fauxmo.addDevice("Mesa");
  fauxmo.addDevice("Banheiro");
  fauxmo.addDevice("Cozinha");
  fauxmo.addDevice("Varanda");

  fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
  Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
  });

}
void loop() {
fauxmo.handle();
}
 
Last edited:
Top