凌云物联网实验室ISKBoard(IoT Starter Kits Board)开发板项目源码
guowenxue
2023-04-07 6ddecc5e53def103eec427a61f2de308552735f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**********************************************************************
*   Copyright: (C)2023 LingYun IoT System Studio
*      Author: GuoWenxue<guowenxue@gmail.com> QQ: 281143292
* Description: ESP32 WiFi/BLE module application API
*
*   ChangeLog:
*        Version    Date       Author            Description
*        V1.0.0  2023.04.3    GuoWenxue      Release initial version
***********************************************************************/
 
#include <string.h>
#include "esp32.h"
 
int esp32_hwreset(comport_t *comport)
{
    uint32_t        tickstart = 0;
    int             bytes;
 
    /* Clear the receive buffer */
    rb_clear(&comport->rb);
 
    /* Reset WiFi module by hardware GPIO */
    HAL_GPIO_WritePin(WiFiRst_GPIO_Port, WiFiRst_Pin, GPIO_PIN_RESET);
    HAL_Delay(500);
    HAL_GPIO_WritePin(WiFiRst_GPIO_Port, WiFiRst_Pin, GPIO_PIN_SET);
 
    /* Check module bootup information */
    tickstart = HAL_GetTick();
    while((HAL_GetTick() - tickstart) < 5000)
    {
        bytes = rb_data_size(&comport->rb);
 
        HAL_Delay( 100 );
 
        /* comport received data before but not receive in 100ms now, maybe data receive over */
        if( bytes>0 && rb_data_size(&comport->rb)==bytes )
        {
            break;
        }
    }
 
    /* WiFi module maybe not present */
    if( rb_data_size(&comport->rb) <= 0 )
    {
        printf("ERROR: WiFi module maybe not present\r\n");
        return -1;
    }
 
    /* Wait for 'ready' and cleanup */
    HAL_Delay(500);
    rb_clear(&comport->rb);
    return 0;
}
 
int esp32_init_module(comport_t *comport)
{
    int             rv;
    char            version[256] = {0};
 
 
    if( !comport )
        return -1;
 
    esp32_hwreset(comport);
 
    esp32_set_echo(comport, DISABLE);
 
    //esp32_set_sysstore(comport, ENABLE);
 
    rv = esp32_get_firmware(comport, version, sizeof(version));
    if( rv < 0)
    {
        log_error("Query ESP32 firmware version failed: %d\n", rv);
        return rv;
    }
 
    log_info("ESP32 firmware version:\n%s\n", version);
 
    return 0;
}
 
int esp32_setup_softap(comport_t *comport, char *ssid, char *pwd)
{
    esp32_set_wmode(comport, MODE_SOFTAP, ENABLE);
 
    esp32_set_ipaddr(comport, MODE_SOFTAP, DEF_SOFTAP_IPADDR, DEF_SOFTAP_IPADDR);
 
    esp32_set_dhcp(comport, MODE_SOFTAP, ENABLE);
 
    esp32_set_softap(comport, ssid, pwd, 11, MODE_WPA2PSK);
 
    return 0;
}
 
int esp32_join_network(comport_t *comport, char *ssid, char *pwd)
{
    int             rv, status = 0;
    int             times=10;
    char            buf[128] = {0};
 
    if( !comport )
        return -1;
 
    esp32_join_status(comport, &status, buf);
    if( 2==status && !strcmp(buf, ssid) )
    {
        log_info("ESP32 connected to \"%s\" already\n", ssid);
        return 0;
    }
 
    esp32_set_wmode(comport, MODE_STATION, ENABLE);
 
    esp32_set_dhcp(comport, MODE_STATION, ENABLE);
 
    rv = esp32_connect_ap(comport, ssid, pwd);
    if( rv < 0 )
    {
        log_error("connect to AP \"%s\" failed, rv=%d", ssid, rv);
        return rv;
    }
 
    while(times--)
    {
        rv = esp32_join_status(comport, &status, buf);
        if( 2 == status )
        {
            log_info("ESP32 connected to \"%s\" already\n", ssid);
            return 0;
        }
        rv = -3;
    }
 
    return rv;
}
 
 
int esp32_check_network(comport_t *comport)
{
    int             rv, times=5;
    char            ip[IP_LEN] = {0};
    char            gateway[IP_LEN] = {0};
 
    memset(ip, 0, sizeof(ip));
    memset(gateway, 0, sizeof(gateway));
    rv = esp32_get_ipaddr(comport, MODE_STATION, ip, gateway);
    if( rv<0 )
        return rv;
 
    if( !strlen(ip) || !strlen(gateway) )
        return -3;
 
    log_info("IP address: %s, netmask: %s\n", ip, gateway);
 
    while( times-- )
    {
        if( !(rv=esp32_ping(comport, gateway, 5000)) )
        {
            rv = 0;
            break;
        }
    }
 
    return 0;
}
 
int esp32_setup_tcp_server(comport_t *comport, int port)
{
    int             rv;
 
    rv = esp32_set_socket_mux(comport, ENABLE);
    if(rv<0)
        return rv;
 
    rv = esp32_set_socket_clients(comport, 2);
    if(rv<0)
        return rv;
 
    rv = esp32_set_tcp_server(comport, port);
    if(rv<0)
        return rv;
 
    rv = esp32_set_socket_timeout(comport, 600);
    if(rv<0)
        return rv;
 
    return 0;
}
 
int esp32_setup_tcp_client(comport_t *comport, char *host, int port)
{
    int             rv;
 
    rv = esp32_set_tcp_client(comport, DISABLE, host, port);
    if(rv<0)
        return rv;
 
 
    return 0;
}