Project

General

Profile

Mesh network problems » esp32_mesh_ROBOR2.c

martin van beek, 18 Jul 2021 18:16

 
1
/*
2
 * File: esp32_mesh_ROBOR2.c
3
 *
4
 * Code generated with Waijung 2 ESP32 Target Blockset,
5
 * for Simulink model 'esp32_mesh_ROBOR2'.
6
 *
7
 * Model version                  : 1.67
8
 * Simulink Coder version         : 9.3 (R2020a) 18-Nov-2019
9
 * C/C++ source code generated on : Sun Jul 18 12:49:51 2021
10
 *
11
 * Target selection: esp32.tlc
12
 * Embedded hardware selection: Cadence Design Systems (Tensilica)->Xtensa? single-/dual-core 32-bit LX6 microprocessor
13
 * Code generation objectives: Unspecified
14
 * Validation result: Not run
15
 */
16

    
17
#include "esp32_mesh_ROBOR2.h"
18
#include "esp32_mesh_ROBOR2_private.h"
19

    
20
/* Block signals (default storage) */
21
B_esp32_mesh_ROBOR2_T esp32_mesh_ROBOR2_B;
22

    
23
/* Block states (default storage) */
24
DW_esp32_mesh_ROBOR2_T esp32_mesh_ROBOR2_DW;
25

    
26
/* Real-time model */
27
RT_MODEL_esp32_mesh_ROBOR2_T esp32_mesh_ROBOR2_M_;
28
RT_MODEL_esp32_mesh_ROBOR2_T *const esp32_mesh_ROBOR2_M = &esp32_mesh_ROBOR2_M_;
29

    
30
/* Model step function */
31
void esp32_mesh_ROBOR2_step(void)
32
{
33
  /* S-Function (esp32_wifi_mesh): '<Root>/MESH Receive' */
34

    
35
  /* MESHReceive: '<Root>/MESH Receive'
36
   */
37
  {
38
    if (is_mesh_connected) {
39
      //memcpy(sql_buffer,
40

    
41
      //jsonbuffer[0]='\0';
42

    
43
      //db_start
44
      //jsonbuffer[strlen(jsonbuffer)-1]= '\0';
45
      //memcpy(
46
      int flag = 0;
47
      recv_data.data = rx_buf;
48
      recv_data.size = RX_SIZE;
49
      err = esp_mesh_recv(&from, &recv_data, 1000, &flag , NULL, 0);
50
      if (err != ESP_OK || !recv_data.size) {
51
        // ESP_LOGE(MESH_TAG, "err:0x%x, size:%d", err, recv_data.size);
52
        // rx_buf[0]='\0';
53
        sprintf(&mesh_recv_from[0],""MACSTR"",MAC2STR(from.addr));
54
        received= false;
55
      }
56

    
57
      if (err == ESP_OK) {
58
        received= true;
59
        printf("In ESP_OK\n");
60
        sprintf(&mesh_recv_from[0],""MACSTR"",MAC2STR(from.addr));
61

    
62
        //sprintf(&mesh_from[0],""MACSTR"",MAC2STR(from.addr));
63
      }
64

    
65
      mesh_recv_init= true;
66
      if (received) {
67
        memcpy(&esp32_mesh_ROBOR2_B.MESHReceive_o1[0], rx_buf, 32);
68
        memcpy(&esp32_mesh_ROBOR2_B.MESHReceive_o2[0], (uint8_t *)mesh_recv_from,
69
               strlen(mesh_recv_from));
70
      } else {
71
        memset(&taskTest_B.MESH2_o1[0], 0, 32);
72
        memset(&taskTest_B.MESH2_o2[0], 0, strlen(mesh_recv_from));
73
      }
74
    }
75
  }
76

    
77
  /* S-Function (waijung2_string_processing): '<Root>/String Processing' */
78

    
79
  /* StringProcessing: '<Root>/String Processing' */
80
  waijung2_sprintf_s((char *)&esp32_mesh_ROBOR2_B.StringProcessing[0], 32,
81
                     "\r\nData= %s" , (char *)
82
                     &esp32_mesh_ROBOR2_B.MESHReceive_o1[0] );
83

    
84
  /* S-Function (waijung2_string_processing): '<Root>/String Processing1' */
85

    
86
  /* StringProcessing1: '<Root>/String Processing1' */
87
  waijung2_sprintf_s((char *)&esp32_mesh_ROBOR2_B.StringProcessing1[0], 32,
88
                     "From= %s" , (char *)&esp32_mesh_ROBOR2_B.MESHReceive_o2[0]
89
                     );
90

    
91
  /* S-Function (waijung2_string_processing): '<Root>/String Processing3' incorporates:
92
   *  Constant: '<Root>/Constant'
93
   *  Constant: '<Root>/Constant1'
94
   */
95

    
96
  /* StringProcessing3: '<Root>/String Processing3' */
97
  waijung2_sprintf_s((char *)&esp32_mesh_ROBOR2_B.StringProcessing3[0], 64,
98
                     "%c%c%s, %s" , (int8_t)((uint8_T)13U) , (int8_t)((uint8_T)
99
    10U) , (char *)&esp32_mesh_ROBOR2_B.StringProcessing[0] , (char *)
100
                     &esp32_mesh_ROBOR2_B.StringProcessing1[0] );
101

    
102
  /* S-Function (waijung2_string_processing): '<Root>/String Processing2' */
103

    
104
  /* StringProcessing2: '<Root>/String Processing2' */
105
  {
106
    uint32_T len = 0;
107
    const uint8_T *data = &esp32_mesh_ROBOR2_B.StringProcessing3[0];
108
    while ((data[len] != '\0') && (len<64))
109
      len++;
110
    esp32_mesh_ROBOR2_B.StringProcessing2 = len;
111
  }
112

    
113
  /* DataTypeConversion: '<Root>/Data Type Conversion' */
114
  esp32_mesh_ROBOR2_B.DataTypeConversion = (uint16_T)
115
    esp32_mesh_ROBOR2_B.StringProcessing2;
116

    
117
  /* S-Function (esp32_uart): '<Root>/UART Tx1' */
118

    
119
  /* UARTTx1: '<Root>/UART Tx1'
120
   * Packet: Raw Buffer
121
   */
122
  {
123
    uint16_t len = esp32_mesh_ROBOR2_B.DataTypeConversion;
124
    if (len > 64)
125
      len = 64;
126
    UART0_Write (UART_NUM_0,(uint8_t *)&esp32_mesh_ROBOR2_B.StringProcessing3[0],
127
                 len);
128
  }
129

    
130
  /* DiscretePulseGenerator: '<Root>/Pulse Generator' */
131
  esp32_mesh_ROBOR2_B.PulseGenerator = ((esp32_mesh_ROBOR2_DW.clockTickCounter <
132
    250) && (esp32_mesh_ROBOR2_DW.clockTickCounter >= 0));
133
  if (esp32_mesh_ROBOR2_DW.clockTickCounter >= 499) {
134
    esp32_mesh_ROBOR2_DW.clockTickCounter = 0;
135
  } else {
136
    esp32_mesh_ROBOR2_DW.clockTickCounter++;
137
  }
138

    
139
  /* End of DiscretePulseGenerator: '<Root>/Pulse Generator' */
140

    
141
  /* S-Function (esp32_digital_output3): '<Root>/Digital Out' */
142

    
143
  /* Updating the pins of DigitalOut */
144
  gpio_set_level(33,(uint32_t) esp32_mesh_ROBOR2_B.PulseGenerator);/* GPIO33 */
145

    
146
  /* S-Function (esp32_wifi_mesh): '<Root>/MESH Setup' */
147

    
148
  /* MESHSetup: '<Root>/MESH Setup'
149
   */
150
  {
151
    esp32_mesh_ROBOR2_B.MESHSetup = (int)is_mesh_connected;
152
    if (is_mesh_connected) {
153
      //memcpy(sql_buffer,
154

    
155
      //jsonbuffer[0]='\0';
156

    
157
      //db_start
158
      //jsonbuffer[strlen(jsonbuffer)-1]= '\0';
159
      //memcpy(
160
    }
161
  }
162
}
163

    
164
/* Model initialize function */
165
void esp32_mesh_ROBOR2_initialize(void)
166
{
167
  /* Registration code */
168

    
169
  /* initialize error status */
170
  rtmSetErrorStatus(esp32_mesh_ROBOR2_M, (NULL));
171

    
172
  /* block I/O */
173
  (void) memset(((void *) &esp32_mesh_ROBOR2_B), 0,
174
                sizeof(B_esp32_mesh_ROBOR2_T));
175

    
176
  /* states (dwork) */
177
  (void) memset((void *)&esp32_mesh_ROBOR2_DW, 0,
178
                sizeof(DW_esp32_mesh_ROBOR2_T));
179

    
180
  /* Start for DiscretePulseGenerator: '<Root>/Pulse Generator' */
181
  esp32_mesh_ROBOR2_DW.clockTickCounter = 0;
182

    
183
  /* Start for S-Function (esp32_digital_output3): '<Root>/Digital Out' */
184

    
185
  //================================================================= GPIO Out pins: 'DigitalOut' Initialization
186
  //GPIO_initialize_DigitalOut();
187

    
188
  /* Start for S-Function (esp32_wifi_mesh): '<Root>/MESH Setup' */
189
  get_board_uuid();
190
  ESP_ERROR_CHECK(nvs_flash_init());
191

    
192
  /*  tcpip initialization */
193
  tcpip_adapter_init();
194

    
195
  /* for mesh
196
   * stop DHCP server on softAP interface by default
197
   * stop DHCP client on station interface by default
198
   * */
199
  ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
200
  ESP_ERROR_CHECK(tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA));
201

    
202
  /*  event initialization */
203
  ESP_ERROR_CHECK(esp_event_loop_create_default());
204

    
205
  /*  wifi initialization */
206
  wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
207
  ESP_ERROR_CHECK(esp_wifi_init(&config));
208
  ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP,
209
    &ip_event_handler, NULL));
210
  ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
211
  ESP_ERROR_CHECK(esp_wifi_start());
212

    
213
  /*  mesh initialization */
214
  ESP_ERROR_CHECK(esp_mesh_init());
215
  ESP_ERROR_CHECK(esp_event_handler_register(MESH_EVENT, ESP_EVENT_ANY_ID,
216
    &mesh_event_handler, NULL));
217
  ESP_ERROR_CHECK(esp_mesh_set_max_layer(6));
218
  ESP_ERROR_CHECK(esp_mesh_set_vote_percentage(1));
219
  ESP_ERROR_CHECK(esp_mesh_set_ap_assoc_expire(10));
220
  mesh_cfg_t cfg = MESH_INIT_CONFIG_DEFAULT();
221

    
222
  /* mesh ID */
223
  memcpy((uint8_t *) &cfg.mesh_id, MESH_ID, 6);
224

    
225
  /* router */
226
  char routerssid[20]= "BeMyGuest";
227
  char passwrd[20]= "";
228
  cfg.channel = 13;
229
  cfg.router.ssid_len = strlen(routerssid);
230
  memcpy((uint8_t *) &cfg.router.ssid, routerssid, cfg.router.ssid_len);
231
  memcpy((uint8_t *) &cfg.router.password, passwrd,
232
         strlen(passwrd));
233

    
234
  /* mesh softAP */
235
  ESP_ERROR_CHECK(esp_mesh_set_ap_authmode(WIFI_AUTH_WPA2_PSK));
236
  cfg.mesh_ap.max_connection = 6;
237
  memcpy((uint8_t *) &cfg.mesh_ap.password, "MESH1234",
238
         strlen("MESH1234"));
239
  ESP_ERROR_CHECK(esp_mesh_set_config(&cfg));
240

    
241
  /* mesh start */
242
  ESP_ERROR_CHECK(esp_mesh_start());
243
  ESP_LOGI(MESH_TAG, "mesh starts successfully, heap:%d, %s\n",
244
           esp_get_free_heap_size(),
245
           esp_mesh_is_root_fixed() ? "root fixed" : "root not fixed");
246

    
247
  // xTaskCreate(esp_mesh_p2p_rx_main, "MPRX", 5120, NULL, 5, NULL);
248

    
249
  /* Enable for S-Function (esp32_uart): '<Root>/UART Tx1' */
250
  /* Level2 S-Function Block: '<Root>/UART Tx1' (esp32_uart) */
251
  enable_UARTTx1();
252

    
253
  /* Enable for S-Function (esp32_digital_output3): '<Root>/Digital Out' */
254
  /* Level2 S-Function Block: '<Root>/Digital Out' (esp32_digital_output3) */
255
  GPIO_initialize_DigitalOut();
256

    
257
  /* Enable for S-Function (esp32_uart): '<Root>/UART Setup' */
258
  /* Level2 S-Function Block: '<Root>/UART Setup' (esp32_uart) */
259
  enable_UARTSetup();
260

    
261
  /* ConstCode for S-Function (esp32_wifi_mesh): '<Root>/MESH Send' */
262

    
263
  /* MESHSend: '<Root>/MESH Send'
264
   */
265
  {
266
    if (is_mesh_connected) {
267
      memcpy(mesh_from,&esp32_mesh_ROBOR2_ConstB.StringtoASCII[0],31);
268
      mesh_from[31]= 0;
269
      if (strcmp(mesh_from,esp_board_uuid)==0) {
270
        data.data = tx_buf;
271
        data.size = sizeof(tx_buf);
272
        data.proto = MESH_PROTO_BIN;
273
        is_running = true;
274
        mesh_initialized= 1;
275
        memcpy(send_data,&esp32_mesh_ROBOR2_ConstB.StringtoASCII3[0],31);
276
        send_data[31]= 0;
277
        memcpy(tx_buf, (uint8_t *)send_data, strlen(send_data));
278
        tx_buf[strlen(send_data)]= 0;
279
        mesh_to[0]= 0;
280
        memcpy(mesh_to,&esp32_mesh_ROBOR2_ConstB.StringtoASCII1[0],31);
281
        mesh_to[31]= 0;
282
        if (strcmp(mesh_to,root_address)==0) {
283
          err = esp_mesh_send(NULL, &data, MESH_DATA_P2P, NULL, 0);
284
          ESP_LOGI(MESH_TAG, "Sent data to root");
285
        } else {
286
          set_MAC(&mac_add, mesh_to);
287
          err = esp_mesh_send(&mac_add, &data, MESH_DATA_P2P, NULL, 0);
288
          ESP_LOGI(MESH_TAG, "Sent data to "MACSTR" ", MAC2STR(mac_add.addr));
289
        }
290

    
291
        vTaskDelay(100 / portTICK_RATE_MS);
292
        mesh_to[0]= 0;
293
        memcpy(mesh_to,&esp32_mesh_ROBOR2_ConstB.StringtoASCII2[0],31);
294
        mesh_to[31]= 0;
295
        if (strcmp(mesh_to,root_address)==0) {
296
          err = esp_mesh_send(NULL, &data, MESH_DATA_P2P, NULL, 0);
297
          ESP_LOGI(MESH_TAG, "Sent data to root");
298
        } else {
299
          set_MAC(&mac_add, mesh_to);
300
          err = esp_mesh_send(&mac_add, &data, MESH_DATA_P2P, NULL, 0);
301
          ESP_LOGI(MESH_TAG, "Sent data to "MACSTR" ", MAC2STR(mac_add.addr));
302
        }
303

    
304
        vTaskDelay(100 / portTICK_RATE_MS);
305
      }
306

    
307
      //memcpy(sql_buffer,
308

    
309
      //jsonbuffer[0]='\0';
310

    
311
      //db_start
312
      //jsonbuffer[strlen(jsonbuffer)-1]= '\0';
313
      //memcpy(
314
    }
315
  }
316
}
317

    
318
/* Model terminate function */
319
void esp32_mesh_ROBOR2_terminate(void)
320
{
321
  /* (no terminate code required) */
322
}
323

    
324
/* [EOF] */
(5-5/5)