Sub Process_Globals
Private VERSION As String = "rOpen62541 EnvSim v20261012"
' Communication
Public Serial1 As Serial
Private WiFi As ESP8266WiFi ' Lib rESP8266WiFi
Private SSID As String = "***"
Private PW As String = "***"
' Open62541
Private OPCUAServer As Open62541 ' Lib rOpen62541
Private PORT As Int = 4840
' Nodes
Private NODEID_TEMPERATURE As String = "ns=1;s=Temperature"
Private NODEID_HUMIDITY As String = "ns=1;s=Humidity"
' Timer simulating sensor data
Private AppTimer As Timer
Private APPTIMER_INTERVAL As ULong = 5000
'Helper
Private bc As ByteConverter 'ignore
End Sub
Sub AppStart
Serial1.Initialize(115200)
Log(CRLF, "[AppStart] ", VERSION)
' Init app timer to generate env data
AppTimer.Initialize("AppTimer_Tick", APPTIMER_INTERVAL)
' Start after opc server has been initialized and nodes created
AppTimer.Enabled = False
' Connect to the network first
If WiFi.Connect2(SSID, PW) Then
Log("[AppStart] WiFi connected > local ip=", WiFi.LocalIP)
' [AppStart] WiFi connected. IP=192.168.1.68
RunNative("DisableWiFiSleep", Null)
RunNative("SetSystemClock", Null)
If InitOPCUAServer Then
AppTimer.Enabled = True
Log("[AppStart] OPC UA server & simulation started every ", APPTIMER_INTERVAL, "ms")
End If
Else
Log("[AppStart][E] WiFi Connection Failed")
End If
End Sub
Private Sub InitOPCUAServer As Boolean
Dim TimeoutCounter As Int = 0
Dim ServerBootFailed As Boolean = False
' Init the OPC UA server no callbacks (set to null)
OPCUAServer.Initialize(WiFi.LocalIp, PORT, _ ' See log after boot which IP:Port
"", "", _ ' No auth
Null, _ ' No Trigger callback
Null) ' No data write callback
' Wait for the background thread layout initialization to complete!
Do While OPCUAServer.IsReady = False
Delay(100) ' 100ms yield ticks for the cooperative scheduler
TimeoutCounter = TimeoutCounter + 1
If TimeoutCounter >= 50 Then ' 50 ticks * 100ms = 5000ms (5 Seconds Timeout)
ServerBootFailed = True
Exit ' Break out of the endless loop safely!
End If
Loop
If ServerBootFailed Then
Log("[InitOPCUAServer][E] Failed with TIMEOUT! Core 0 failed.")
' Optional: Run local emergency fallback routine or let local sensors run offline
Else
OPCUAServer.AddFloatNode(NODEID_TEMPERATURE, "Room Temperature", 20.0)
OPCUAServer.AddFloatNode(NODEID_HUMIDITY, "Room Humidity", 68.0)
End If
Return Not(ServerBootFailed)
End Sub
Sub AppTimer_Tick
If OPCUAServer.IsReady Then
Dim CurrentTemp As Float = 24.5 + Rnd(-2.0, 3.0)
Dim CurrentHum As Float = 68 + Rnd(-10.0, 11.0)
OPCUAServer.WriteFloat(NODEID_TEMPERATURE, CurrentTemp)
OPCUAServer.WriteFloat(NODEID_HUMIDITY, CurrentHum)
Log("[AppTimer] Sensor read complete > t=", CurrentTemp, " h=", CurrentHum)
End If
End Sub
#if C
#include "esp_wifi.h"
#include "time.h"
// RunNative("DisableWiFiSleep", Null)
void DisableWiFiSleep(B4R::Object* o) {
// Force Espressif lwIP stack to set Power Save to NONE
esp_wifi_set_ps(WIFI_PS_NONE);
::Serial.println("[Hardware Engine] Wi-Fi Modem-Sleep forcefully disabled! Radio set to high-performance mode.");
}
// RunNative("SetSystemClock", Null)
void SetSystemClock(B4R::Object* o) {
// Configures the system to poll pool.ntp.org with a standard global UTC offset
// Adjust the 3600 (seconds) based on your target local timezone offset (e.g. 3600 = UTC+1)
configTime(3600, 0, "pool.ntp.org", "time.nist.gov");
::Serial.println("[SetSystemClock] SNTP Client started. Synchronizing baseline...");
}
#End If