B4R Question Porting Arduino Code to B4R

Mark Read

Well-Known Member
Licensed User
Longtime 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):

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

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

extern "C" {
  #include<user_interface.h>
}

/* 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(topic_str);
  Serial.print("' with data '");
  Serial.print(data_str);
  Serial.println("'");

}

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 = ");
  Serial.println(number_client);
  Serial.println("");
 
 
    while (stat_info != NULL) {
   
      IPaddress = &stat_info->ip;
      address = IPaddress->addr;
     
      Serial.print("client ");
      Serial.print(i);
      Serial.print(": IP =  ");
      Serial.print((address));
           
      stat_info = STAILQ_NEXT(stat_info, next);
      i++;
      Serial.println();
    }
   
  delay(500);
}


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);


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

  /* Register the callback */
  MQTT_server_onData(data_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:
  delay(2000);

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

    client_status();
  }
 

}

This is my B4R code to date:

B4X:
#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) {
   WiFi.mode(WIFI_AP);
}
#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
    Serial1.Initialize(115200)
    Log("AppStart")
   
    Log(wifi.StartAccessPoint(ssid))
    RunNative("SetAP", Null)
    Log(wifi.AccessPointIp)
   
    RunNative("StartBroker", Null)
   
End Sub

My problem is this line from the arduino code:

B4X:
/* Register the callback */
  MQTT_server_onData(data_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.
 

Mark Read

Well-Known Member
Licensed User
Longtime 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.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime 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.
 
Upvote 0
Top