<root>
<document scada='0' schema='103' license='1,FREE_VERSION,DF7JCQ' title='' description='' fcversion='655618' target='ESP.ESP32.ESP32_WROOM_32' >
	<config data='' clkspd='240000000' simspd='0' usewdt='0' constif='0' commport='7' Use3V3='0' />
	<plugins >
		<dll_models enabled='1' />
	</plugins>
	<supplement use='1' head='#include &quot;esp_now.h&quot;
#include &quot;esp_wifi.h&quot;
#include &quot;nvs_flash.h&quot;
#include &quot;esp_netif.h&quot;
#include &quot;esp_event.h&quot;
#include &lt;string.h&gt;
#include &lt;stdio.h&gt;

//**************************************************************************************//
void ESPNow_OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status);
void ESPNow_OnDataRecv(const uint8_t *mac_addr, const uint8_t *incomingData, int len);
void ESPNow_Initialize_Internal();
void ESPNow_Send_Data_Internal(const char *mac_str, const char *data_str);
void ESPNow_Get_My_MAC_Internal(char* out_str);
bool ESPNow_Check_For_Data_Internal();
char* ESPNow_Get_Received_MAC_Internal();
char* ESPNow_Get_Received_Data_Internal();
int ESPNow_Get_Received_Data_Length_Internal();

volatile bool g_espnow_send_status;
volatile bool g_espnow_new_data_available;
volatile uint8_t g_espnow_last_received_mac[6];
volatile char g_espnow_last_received_data[251];
volatile int g_espnow_last_received_data_len;
volatile char g_espnow_last_received_mac_str[18];

//**************************************************************************************//

void ESPNow_Initialize_Internal() {
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);
    ESP_ERROR_CHECK(esp_netif_init());

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&amp;cfg));
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    ESP_ERROR_CHECK(esp_wifi_start());
    ESP_ERROR_CHECK(esp_now_init());
    
    ESP_ERROR_CHECK(esp_now_register_send_cb(ESPNow_OnDataSent));
    ESP_ERROR_CHECK(esp_now_register_recv_cb(ESPNow_OnDataRecv));
}

//**************************************************************************************//
void ESPNow_Get_My_MAC_Internal(char* out_str) {
    uint8_t mac_addr[6];
    esp_wifi_get_mac(WIFI_IF_STA, mac_addr);
    sprintf(out_str, &quot;%02X:%02X:%02X:%02X:%02X:%02X&quot;,
            mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
}
//**************************************************************************************//
void ESPNow_OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
    // Dummy implementation
}
//**************************************************************************************//
void ESPNow_Send_Data_Internal(const char *mac_str, const char *data_str) {
    uint8_t peer_mac[6];
    if (sscanf(mac_str, &quot;%hhx:%hhx:%hhx:%hhx:%hhx:%hhx&quot;,
               &amp;peer_mac[0], &amp;peer_mac[1], &amp;peer_mac[2],
               &amp;peer_mac[3], &amp;peer_mac[4], &amp;peer_mac[5]) != 6) {
        g_espnow_send_status = false;
        return;
    }

    esp_now_peer_info_t peerInfo;
    memset(&amp;peerInfo, 0, sizeof(peerInfo));
    
    if (esp_now_get_peer(peer_mac, &amp;peerInfo) != ESP_OK) {
        memcpy(peerInfo.peer_addr, peer_mac, 6);
        peerInfo.channel = 0;
        peerInfo.encrypt = false;
        esp_now_add_peer(&amp;peerInfo);
    }

    size_t data_len = strlen(data_str);
    if (data_len &gt; 250) {
        data_len = 250;
    }

    esp_err_t result = esp_now_send(peer_mac, (const uint8_t *)data_str, data_len);
    g_espnow_send_status = (result == ESP_OK);
}

//**************************************************************************************//
void mac_to_string(const uint8_t *mac_addr, char *mac_str_buf) {
    sprintf(mac_str_buf, &quot;%02X:%02X:%02X:%02X:%02X:%02X&quot;,
            mac_addr[0], mac_addr[1], mac_addr[2],
            mac_addr[3], mac_addr[4], mac_addr[5]);
}

//**************************************************************************************//
void ESPNow_OnDataRecv(const uint8_t *mac_addr, const uint8_t *incomingData, int len) {
    char *separator = strchr((char *)incomingData, &apos;,&apos;);

    if (separator != NULL) {
        char mac_str_from_payload[18];
        int mac_len = separator - (char *)incomingData;
        memcpy(mac_str_from_payload, incomingData, mac_len);
        mac_str_from_payload[mac_len] = &apos;\0&apos;;
        
        uint8_t mac_bytes_from_payload[6];
        sscanf(mac_str_from_payload, &quot;%hhx:%hhx:%hhx:%hhx:%hhx:%hhx&quot;,
               &amp;mac_bytes_from_payload[0], &amp;mac_bytes_from_payload[1], &amp;mac_bytes_from_payload[2],
               &amp;mac_bytes_from_payload[3], &amp;mac_bytes_from_payload[4], &amp;mac_bytes_from_payload[5]);
        
        memcpy((void*)g_espnow_last_received_mac, mac_bytes_from_payload, 6);
        memcpy((char*)g_espnow_last_received_mac_str, mac_str_from_payload, mac_len);
        g_espnow_last_received_mac_str[mac_len] = &apos;\0&apos;;
        
        int data_len = len - mac_len - 1;
        if (data_len &gt; 250) {
            data_len = 250;
        }
        memcpy((void*)g_espnow_last_received_data, separator + 1, data_len);
        g_espnow_last_received_data[(data_len &lt; 251) ? data_len : 250] = &apos;\0&apos;;

        g_espnow_last_received_data_len = data_len;
        g_espnow_new_data_available = true;
    } else {
        memcpy((void*)g_espnow_last_received_mac, mac_addr, 6);
        mac_to_string(mac_addr, (char*)g_espnow_last_received_mac_str);
    
        if (len &gt; 250) {
            len = 250;
        }
        memcpy((void*)g_espnow_last_received_data, incomingData, len);
        g_espnow_last_received_data[(len &lt; 251) ? len : 250] = &apos;\0&apos;;
        
        g_espnow_last_received_data_len = len;
        g_espnow_new_data_available = true;
    }
}

//**************************************************************************************//
bool ESPNow_Check_For_Data_Internal() {
    if (g_espnow_new_data_available) {
        g_espnow_new_data_available = false;
        return true;
    }
    return false;
}

//**************************************************************************************//
char* ESPNow_Get_Received_MAC_Internal() {
    return (char*)g_espnow_last_received_mac_str;
}

char* ESPNow_Get_Received_Data_Internal() {
    return (char*)g_espnow_last_received_data;
}

int ESPNow_Get_Received_Data_Length_Internal() {
    return g_espnow_last_received_data_len;
}
' body='' />
	<debug />
	<traces />
	<ghost >
		<FK2 >
			<data name='FK2D0' port='4294967295' pin='4294967295' />
			<data name='FK2D1' port='4294967295' pin='4294967295' />
			<data name='FK2D2' port='4294967295' pin='4294967295' />
			<data name='FK2D3' port='4294967295' pin='4294967295' />
			<data name='FK2D4' port='4294967295' pin='4294967295' />
			<data name='FK2D5' port='4294967295' pin='4294967295' />
			<data name='FK2D6' port='4294967295' pin='4294967295' />
			<data name='FK2D7' port='4294967295' pin='4294967295' />
			<data name='FK2D8' port='4294967295' pin='4294967295' />
			<data name='FK2D9' port='4294967295' pin='4294967295' />
			<data name='FK2D10' port='4294967295' pin='4294967295' />
			<data name='FK2D11' port='4294967295' pin='4294967295' />
			<data name='FK2D12' port='4294967295' pin='4294967295' />
			<data name='FK2D13' port='4294967295' pin='4294967295' />
			<data name='FK2D14' port='4294967295' pin='4294967295' />
			<data name='FK2D15' port='4294967295' pin='4294967295' />
			<data name='FK2A0' port='4294967295' pin='4294967295' />
			<data name='FK2A1' port='4294967295' pin='4294967295' />
			<data name='FK2A2' port='4294967295' pin='4294967295' />
			<data name='FK2A3' port='4294967295' pin='4294967295' />
			<data name='FK2A4' port='4294967295' pin='4294967295' />
			<data name='FK2A5' port='4294967295' pin='4294967295' />
		</FK2>
		<ICD >
			<data name='AnalogPrescaleValue' value='19' />
			<data name='DigitalSampleRate' value='100000' />
			<data name='BreakpointCount' value='8' />
			<data name='CallStackDepthCount' value='8' />
			<data name='ClockPort' value='1' />
			<data name='ClockPin' value='6' />
			<data name='DataPort' value='1' />
			<data name='DataPin' value='7' />
			<data name='UseDefaultPins' value='1' />
			<data name='WrapEnabled' value='1' />
			<data name='CommsDelayOverridden' value='0' />
			<data name='CommsDelay' value='1' />
			<data name='CalculatedCommsDelay' value='1' />
			<data name='AnalogEB2PrescaleValue' value='2' />
			<data name='DigitalEB2SampleRate' value='100000' />
		</ICD>
		<pins >
			<digital A='0' B='0' C='0' D='0' E='0' F='0' G='0' H='0' I='0' J='0' K='0' L='0' M='0' N='0' O='0' P='0' Q='0' R='0' S='0' T='0' U='0' V='0' W='0' X='0' Y='0' Z='0' />
			<analog A='0' B='0' C='0' D='0' E='0' F='0' G='0' H='0' I='0' J='0' K='0' L='0' M='0' N='0' O='0' P='0' Q='0' R='0' S='0' T='0' U='0' V='0' W='0' X='0' Y='0' Z='0' />
		</pins>
	</ghost>
	<components >
		<settings autoimg='1' center='1' unitscale='0' fixedscale='0' fixedx='25' fixedy='25' fixedz='25' headcode='1' />
		<definition guid='b2e7b3ef-8972-48e9-a192-398127b49d12' vstate='30' vmin='0' vmaj='1' srcleaf='ESPNowV1.fcfx' visiblename='ESPNowV1' description='' category='Wireless' category2='' category3='' bIs2dOnly='0' bIs3dOnly='0' catenable='1' author='' manuname='' manucode='' sysinfo='139' keywords='' dynamic='1' scadaCompatible='0' embeddedCompatible='1' showmacros='1' iconpath='' />
		<component class_type='root' codename='ESPNowV1' panelId='-1' x='0' y='0' z='0' xsz='1' ysz='1' zsz='1' xang='0' yang='0' zang='0' xquat='0' yquat='0' zquat='0' wquat='1' visible='1' scadavisible='1' interactive='1' solid='1' layer='0' poslock='0' comp2dType='0' >
			<resources />
			<properties />
			<values />
			<events />
			<apis >
				<api name='SendString' alt='SendString' type='1' proto='1' />
				<api name='GetSenderMAC' alt='GetSenderMAC' type='1' proto='1' />
				<api name='GetReceivedData' alt='GetReceivedData' type='1' proto='1' />
				<api name='GetReceivedDataLength' alt='GetReceivedDataLength' type='1' proto='1' />
				<api name='CheckForIncoming' alt='CheckForIncoming' type='1' proto='1' />
				<api name='Initialize' alt='Initialize' type='1' proto='1' />
				<api name='GetMyMAC' alt='GetMyMAC' type='1' proto='1' />
			</apis>
			<variables >
				<variable public='0' >
					<def class_type='variable' name='full_message' type='T8' description='' isconst='0' isHidden='0' isinit='0' usrinit='&quot;&quot;' setinit='' >
						<array size='250' />
					</def>
				</variable>
				<variable public='0' >
					<def class_type='variable' name='false' type='b1' description='' isconst='1' isHidden='0' isinit='1' usrinit='0' setinit='0' />
				</variable>
				<variable public='0' >
					<def class_type='variable' name='my_mac' type='T8' description='' isconst='0' isHidden='0' isinit='0' usrinit='&quot;&quot;' setinit='' >
						<array size='20' />
					</def>
				</variable>
				<variable public='0' >
					<def class_type='variable' name='true' type='b1' description='' isconst='1' isHidden='0' isinit='1' usrinit='1' setinit='1' />
				</variable>
			</variables>
			<macros >
				<macro >
					<flowline name='SendString' description='Sends a string of data to a specific peer.' statediag='0' >
						<return name='Return' type='b1' description='' isconst='0' isHidden='0' isinit='0' usrinit='0' setinit='' />
						<param name='RECEIVERMAC' type='T8' description='' isconst='0' isHidden='0' isinit='0' usrinit='&quot;&quot;' setinit='' >
							<array size='20' />
						</param>
						<param name='DATA' type='T8' description='' isconst='0' isHidden='0' isinit='0' usrinit='&quot;&quot;' setinit='' >
							<array size='240' />
						</param>
						<local name='FULL_STRING' type='T8' description='' isconst='0' isHidden='0' isinit='0' usrinit='&quot;&quot;' setinit='' >
							<array size='250' />
						</local>
						<command class_type='call' title='User Macro' cmdcolor='4487093' cmdcolor_sec='11066367' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' macro='GetMyMAC' >
							<return exp='my_mac' />
						</command>
						<command class_type='calculation' title='Calculation' cmdcolor='4206456' cmdcolor_sec='10785756' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' >
							<exp exp='.FULL_STRING = my_mac + &quot;,&quot; + .DATA' />
						</command>
						<command class_type='native' title='Code' ccode='ESPNow_Send_Data_Internal(FCL_RECEIVERMAC,FCL_FULL_STRING);
' cmdcolor='4206456' cmdcolor_sec='10785756' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' language='C' />
						<command class_type='native' title='Code' ccode='FCR_RETVAL = g_espnow_send_status;
' cmdcolor='4206456' cmdcolor_sec='10785756' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' language='C' />
					</flowline>
				</macro>
				<macro >
					<flowline name='GetSenderMAC' description='Retrieves the MAC address of the device that sent the last message.' statediag='0' >
						<return name='Return' type='T8' description='' isconst='0' isHidden='0' isinit='0' usrinit='&quot;&quot;' setinit='' >
							<array size='2000' />
						</return>
						<command class_type='native' title='Code' ccode='strcpy(FCR_RETVAL, ESPNow_Get_Received_MAC_Internal());
' cmdcolor='4206456' cmdcolor_sec='10785756' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' language='C' />
					</flowline>
				</macro>
				<macro >
					<flowline name='GetReceivedData' description='Retrieves the data payload of the last message. Call this after a successful CheckForIncoming.' statediag='0' >
						<return name='Return' type='T8' description='' isconst='0' isHidden='0' isinit='0' usrinit='&quot;&quot;' setinit='' >
							<array size='2000' />
						</return>
						<command class_type='native' title='Code' ccode='strcpy(FCR_RETVAL, ESPNow_Get_Received_Data_Internal());
' cmdcolor='4206456' cmdcolor_sec='10785756' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' language='C' />
					</flowline>
				</macro>
				<macro >
					<flowline name='GetReceivedDataLength' description='Retrieves the byte length of the last received data.' statediag='0' >
						<return name='Return' type='s16' description='' isconst='0' isHidden='0' isinit='0' usrinit='0' setinit='' />
						<command class_type='native' title='Code' ccode='FCR_RETVAL = ESPNow_Get_Received_Data_Length_Internal();
' cmdcolor='4206456' cmdcolor_sec='10785756' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' language='C' />
					</flowline>
				</macro>
				<macro >
					<flowline name='CheckForIncoming' description='Checks if a new ESP-NOW packet has been received. Returns 1 if new data is available.' statediag='0' >
						<return name='Return' type='b1' description='' isconst='0' isHidden='0' isinit='0' usrinit='0' setinit='' />
						<command class_type='native' title='Code' ccode='FCR_RETVAL = ESPNow_Check_For_Data_Internal();' cmdcolor='4206456' cmdcolor_sec='10785756' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' language='C' />
					</flowline>
				</macro>
				<macro >
					<flowline name='Initialize' description='Sets up Wi-Fi in Station mode and the ESP-NOW driver' statediag='0' >
						<return name='Return' type='v0' description='' isconst='0' isHidden='0' isinit='0' usrinit='' setinit='' />
						<command class_type='native' title='Code' ccode='ESPNow_Initialize_Internal();
' cmdcolor='4206456' cmdcolor_sec='10785756' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' language='C' />
						<command class_type='delay' title='Delay' cmdcolor='4206456' cmdcolor_sec='10785756' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' exp='50' type='1' />
					</flowline>
				</macro>
				<macro >
					<flowline name='Main' description='' statediag='0' >
						<return name='Return' type='v0' description='' isconst='0' isHidden='0' isinit='0' usrinit='' setinit='' />
					</flowline>
				</macro>
				<macro >
					<flowline name='GetMyMAC' description='Retrieves the local device&apos;s MAC address.' statediag='0' >
						<return name='Return' type='T8' description='' isconst='0' isHidden='0' isinit='0' usrinit='&quot;&quot;' setinit='' >
							<array size='2000' />
						</return>
						<command class_type='native' title='Code' ccode='char my_mac_buffer[18];
ESPNow_Get_My_MAC_Internal(my_mac_buffer);
strcpy(FCR_RETVAL, my_mac_buffer);
' cmdcolor='4206456' cmdcolor_sec='10785756' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' language='C' />
					</flowline>
				</macro>
			</macros>
			<component class_type='ref' guid='5c934fad-aebc-4d51-bb76-fc0c4fae27f1' vmin='0' vmaj='1' codename='LibraryComponentSimple1' panelId='0' x='61' y='-60' z='0' xsz='100' ysz='100' zsz='100' xang='0' yang='0' zang='0' xquat='0' yquat='0' zquat='0' wquat='1' visible='1' scadavisible='1' interactive='1' solid='0' layer='2' poslock='1' comp2dType='0' >
				<resources />
				<properties />
				<values >
					<value target='Text::sText' data='ESPNow' />
					<value target='Text::nVerticalAlignment' data='002' />
				</values>
				<events />
				<apis />
				<variables />
				<macros />
			</component>
		</component>
	</components>
	<scadaresourcelookup />
	<keymap />
	<panel2d shadows='0' lighting='2' brightness='0' >
		<background rgb='7292207' img='' style='0' />
		<camera xe='0' ye='0' ze='2455.47' xt='0' yt='0' zt='0' xquat='0' yquat='0' zquat='0' wquat='1' />
		<viewport dx='548' dy='783' zoom='27.0042' fix_topleft='0' />
		<page x='1000' y='1000' show='0' rgb='7360576' />
		<winpos ID='7005' RectRecentFloat='0,508,650,958' RectRecentDocked='0,0,548,812' RecentFrameAlignment='16384' RecentRowIndex='0' IsFloating='0' MRUWidth='32767' PinState='0' IsMaximized='0' IsVisible='0' />
	</panel2d>
	<panel3d shadows='0' lighting='2' brightness='0' perspective='1' >
		<background rgb='8409120' img='' style='0' />
		<table rgb='7360576' img='' style='0' size='0' />
		<camera xe='0' ye='0' ze='781.66' xt='0' yt='0' zt='0' xquat='0' yquat='0' zquat='0' wquat='1' />
		<camerakey0 xe='0' ye='0' ze='300' xt='0' yt='0' zt='0' xquat='0' yquat='0' zquat='0' wquat='1' />
		<camerakey1 xe='-3.67394e-14' ye='0' ze='-300' xt='0' yt='0' zt='0' xquat='0' yquat='1' zquat='0' wquat='6.12323e-17' />
		<camerakey2 xe='0' ye='-300' ze='6.66134e-14' xt='0' yt='0' zt='0' xquat='-0.707107' yquat='0' zquat='0' wquat='0.707107' />
		<camerakey3 xe='-3.67394e-14' ye='300' ze='6.66134e-14' xt='0' yt='0' zt='0' xquat='-4.32978e-17' yquat='0.707107' zquat='0.707107' wquat='4.32978e-17' />
		<camerakey4 xe='-300' ye='-6.66134e-14' ze='6.66134e-14' xt='0' yt='0' zt='0' xquat='-0.5' yquat='0.5' zquat='0.5' wquat='0.5' />
		<camerakey5 xe='300' ye='-6.66134e-14' ze='6.66134e-14' xt='0' yt='0' zt='0' xquat='-0.5' yquat='-0.5' zquat='-0.5' wquat='0.5' />
		<camerakey6 xe='-173.205' ye='-173.205' ze='173.205' xt='0' yt='0' zt='0' xquat='-0.424708' yquat='0.17592' zquat='0.339851' wquat='0.820473' />
		<winpos ID='7004' RectRecentFloat='0,630,650,1080' RectRecentDocked='0,0,484,450' RecentFrameAlignment='16384' RecentRowIndex='0' IsFloating='0' MRUWidth='32767' PinState='0' IsMaximized='0' IsVisible='0' />
	</panel3d>
	<panels2d count='1' >
		<panel2d_0 id='0' name='2D Panel' >
			<winpos ID='1171' RectRecentFloat='828,256,1478,706' RectRecentDocked='992,183,1634,995' RecentFrameAlignment='16384' RecentRowIndex='0' IsFloating='0' MRUWidth='32767' PinState='0' IsMaximized='0' IsVisible='0' />
			<background fill='288230376146135278' showgrid='1' gridstyle='1' gridsize='10' gridbrush='71776119075691740' snaptogrid='0' />
		</panel2d_0>
	</panels2d>
	<layout >
		<view type='0' name='Main' mode='0' placement='LAAAAAAAAAABAAAA---------------------wUAAAAeAAAACAUAACcDAAA' zoom='115' scrollx='0' scrolly='0' flags='0' />
		<view type='0' name='Initialize' mode='0' placement='LAAAAAAAAAABAAAA---------------------wUAAAAeAAAACAUAACcDAAA' zoom='100' scrollx='0' scrolly='0' flags='0' />
		<view type='0' name='SendString' mode='0' placement='LAAAAAAAAAABAAAA---------------------wUAAAAeAAAACAUAACcDAAA' zoom='100' scrollx='0' scrolly='0' flags='0' />
		<view type='0' name='CheckForIncoming' mode='0' placement='LAAAAAAAAAABAAAA---------------------wUAAAAeAAAACAUAACcDAAA' zoom='100' scrollx='0' scrolly='0' flags='0' />
		<view type='0' name='GetSenderMAC' mode='0' placement='LAAAAAAAAAABAAAA---------------------wUAAAAeAAAACAUAACcDAAA' zoom='100' scrollx='0' scrolly='0' flags='0' />
		<view type='0' name='GetReceivedData' mode='0' placement='LAAAAAAAAAABAAAA---------------------wUAAAAeAAAACAUAACcDAAA' zoom='100' scrollx='0' scrolly='0' flags='0' />
		<view type='0' name='GetReceivedDataLength' mode='0' placement='LAAAAAAAAAABAAAA---------------------wUAAAAeAAAACAUAACcDAAA' zoom='100' scrollx='0' scrolly='0' flags='0' />
		<view type='0' name='GetMyMAC' mode='0' placement='LAAAAAAAAAABAAAA---------------------wUAAAAeAAAACAUAACcDAAA' zoom='100' scrollx='0' scrolly='0' flags='0' />
	</layout>
</document>
</root>
