B4R Question Porting Arduino Code to B4R

Discussion in 'B4R Questions' started by mark35at, May 14, 2018.

  1. mark35at

    mark35at Well-Known Member Licensed User

    I have this code working in Arduino and am trying to port it to B4R for ease of programming. I have not yet tried my attempt as I am stuck with one line.

    Arduino Code (working):

     * Create a Wifi AP on Wemos D1 R2
     * Create MQTT Broker
     * M. Read May 
     * Status: Working

    /* Setup Wifi AP  */
    #include <ESP8266WiFi.h>
    #include <WiFiClient.h> 

    "C" {

    /* Set these to your desired credentials. */
    const char *ssid = "Rover AP";
    const char *password = "";

    unsigned char number_client;      //new number of clients connected

    /* Setup MQTT Broker */
    #include "uMQTTBroker.h"
    unsigned int mqttPort = 1883;       // the standard MQTT broker port
    unsigned int max_subscriptions = 10;
    unsigned int max_retained_topics = 10;

    /* Remember subroutines must be declared before they are used, ie. before setup */

    void data_callback(uint32_t *client /* we can ignore this */, const char* topic, uint32_t topic_len, const char *data, uint32_t lengh) {
      char topic_str[topic_len+1];
      os_memcpy(topic_str, topic, topic_len);
      topic_str[topic_len] = '\0';

      char data_str[lengh+1];
      os_memcpy(data_str, data, lengh);
      data_str[lengh] = '\0';

      Serial.print("received topic '");
      Serial.print("' with data '");


    void client_status() {
      struct station_info *stat_info;
      struct ip_addr *IPaddress;
      IPAddress address;
      int i=1;
      stat_info = wifi_softap_get_station_info();

      if (number_client==0){
    //    Serial.println("No Connected Clients");
        return;           //If there are no clients - exit
      Serial.print(" Connected Clients = ");
        while (stat_info != NULL) {
          IPaddress = &stat_info->ip;
          address = IPaddress->addr;
          Serial.print("client ");
          Serial.print(": IP =  ");
          stat_info = STAILQ_NEXT(stat_info, next);

    void setup() {
      // put your setup code here, to run once:

      Serial.print("Configuring access point...");
      /* You can remove the password parameter if you want the AP to be open. */
      WiFi.softAP(ssid, password);
      IPAddress myIP = WiFi.softAPIP();
      Serial.print("AP IP address: ");

      /* Register the callback */

    /* Start the broker */
      Serial.println("Starting MQTT broker");
      MQTT_server_start(mqttPort, max_subscriptions, max_retained_topics);

    /* Subscribe to anything */
      MQTT_local_subscribe((unsigned char *)"#", 0);

    void loop() {
      // put your main code here, to run repeatedly:

      // If the number of clients has changed, update serial
        number_client= wifi_softap_get_station_num();


    This is my B4R code to date:

    #Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
    #End Region

    Sub Process_Globals
    'These global variables will be declared once when the application starts.
        'Public variables can be accessed from all modules.
        Public Serial1 As Serial
    Public ssid As String="Rover AP"
    Public password As String=""
    Public wifi As ESP8266WiFi
    Public mqttport As Int=1883
    Public max_subscriptions As Int=10
    Public max_retained As Int=10
    End Sub

    'Setup the Wifi AP
    #if C
    void SetAP(B4R::Object* o) {
    #end if

    'Setup the MQTT Broker
    #if C
    #include "uMQTTBroker.h"
    void StartBroker (B4R::Object* o) {
      Serial.println("Starting MQTT broker");
      Serial.println(MQTT_server_start(1883, 30, 30));
      MQTT_local_subscribe((unsigned char *)"#", 0);
    #End If

    Private Sub AppStart
    End Sub
    My problem is this line from the arduino code:

    /* Register the callback */
    How to include this in B4R?

    Also I would like to pass the MQTT Broker values (port etc) as variables.

    I am referecing the "uMQTTBroker.h" file which is a broker and client.
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. mark35at

    mark35at Well-Known Member Licensed User

    Thanks Erel, I saw that post. After hours of trying, I cannot get the Wifi AP to work with B4R and the broker. One seems to kill the other. I am going to give up and use the arduino code directly on my ESP8266. It is working great and is VERY fast.

    I am able to run the AP + Broker on one ESP, a second ESP has a motor and a tablet controls the motor, all via MQTT. I will post the whole system later. It is a little rough at present.

    Many thanks.
  4. tigrot

    tigrot Well-Known Member Licensed User

    I have seen that misterious crashes are almost always related to lack of stackbuffer spaces. Did you tried to encrease?
  5. mark35at

    mark35at Well-Known Member Licensed User

    The serial log shows that the AP starts and the Broker as well but the AP is not visible and therefore a connection is not possible.