My program works well when using esp-now in the Arduino IDE, but when I transferred this program to B4R it does not work properly back to the specified CALLBack function. What are the drawbacks of my program?
C:
#include <ESP8266WiFi.h>
#include <espnow.h>
// REPLACE WITH THE MAC Address of your receiver
uint8_t broadcastAddress[] = {0x5C, 0xCF, 0x7F, 0x1A, 0x53, 0x22}; //5C CF 7F 1A 53 22
// Updates DHT readings every 10 seconds
const long interval = 10000;
unsigned long previousMillis = 0; // will store last time DHT was updated
// Define variables to store incoming readings
float incomingTemp;
float incomingHum;
// Variable to store if sending data was successful
String success;
//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
float temp;
float hum;
} struct_message;
// Create a struct_message called DHTReadings to hold sensor readings
struct_message DHTReadings;
// Create a struct_message to hold incoming sensor readings
struct_message incomingReadings;
// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
Serial.print("Last Packet Send Status: ");
if (sendStatus == 0){
Serial.println("Delivery success");
}
else{
Serial.println("Delivery fail");
}
}
// Callback when data is received
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
Serial.print("Bytes received: ");
Serial.println(len);
incomingTemp = incomingReadings.temp;
incomingHum = incomingReadings.hum;
}
void printIncomingReadings(){
// Display Readings in Serial Monitor
Serial.println("INCOMING READINGS");
Serial.print("Temperature: ");
Serial.print(incomingTemp);
Serial.println(" ºC");
Serial.print("Humidity: ");
Serial.print(incomingHum);
Serial.println(" %");
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
Serial.println(WiFi.macAddress());
WiFi.disconnect();
// Init ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Set ESP-NOW Role
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you updated the DHT values
previousMillis = currentMillis;
//Set values to send
DHTReadings.temp = random(1,20);
DHTReadings.hum = random(80,90);
// Send message via ESP-NOW
esp_now_send(broadcastAddress, (uint8_t *) &DHTReadings, sizeof(DHTReadings));
// Print incoming readings
printIncomingReadings();
}
}
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
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("")
Log("AppStart")
RunNative("init", Null)
Delay(500)
AddLooper("Looper1")
End Sub
Sub Looper1
RunNative("refresh", Null)
End Sub
#if C
#include <ESP8266WiFi.h>
#include <espnow.h>
uint8_t broadcastAddress[] = {0x5C, 0xCF, 0x7F, 0x1A, 0x53, 0x22}; //5C CF 7F 1A 53 22
// Updates DHT readings every 10 seconds
const long interval = 10000;
unsigned long previousMillis = 0; // will store last time DHT was updated
// Define variables to store incoming readings
float incomingTemp;
float incomingHum;
// Variable to store if sending data was successful
String success;
typedef struct struct_message {
float temp;
float hum;
} struct_message;
// Create a struct_message called DHTReadings to hold sensor readings
struct_message DHTReadings;
// Create a struct_message to hold incoming sensor readings
struct_message incomingReadings;
// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
Serial.print("Last Packet Send Status: ");
if (sendStatus == 0){
Serial.println("Delivery success");
}
else{
Serial.println("Delivery fail");
}
}
// Callback when data is received
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
Serial.print("Bytes received: ");
Serial.println(len);
incomingTemp = incomingReadings.temp;
incomingHum = incomingReadings.hum;
}
void printIncomingReadings(){
// Display Readings in Serial Monitor
Serial.println("INCOMING READINGS");
Serial.print("Temperature: ");
Serial.print(incomingTemp);
Serial.println(" ºC");
Serial.print("Humidity: ");
Serial.print(incomingHum);
Serial.println(" %");
}
//------------------------------------------------------setup--------------------------------
void init(B4R::Object* o) {
// Init Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
Serial.println(WiFi.macAddress());
WiFi.disconnect();
// Init ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Set ESP-NOW Role
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
}
//-------------------------------------------------- Refresh-----------------------------------
void refresh(B4R::Object* unused){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you updated the DHT values
previousMillis = currentMillis;
//Set values to send
DHTReadings.temp = random(1,20);
DHTReadings.hum = random(80,90);
// Send message via ESP-NOW
esp_now_send(broadcastAddress, (uint8_t *) &DHTReadings, sizeof(DHTReadings));
// Print incoming readings
printIncomingReadings();
}
}
#end if