linux용 esp8266 드라이버 c source code - smartconfig
임베디드 리눅스 환경에서 esp8266 드라이버를 만드는 중이다.
ESP8266 모듈은 UART interface를 통해 연결하였으며, AT 커맨드를 사용하여 동작 명령 및 데이터를 주고받는다.
드라이버는 Linux user mode에서 동작하며, 현재는 smartconfig까지 구현되어 있다.
Smartconfig
Smartconfig는 esp8266이 station 모드에서 동작하며, wifi AP의 정보를 esp8266에 전달하여 ESP8266이 자동으로 wifi AP에 연결하도록 하는 기능이다.
wifi AP의 정보 전달은 wifi AP에 연결된 스마트 폰이나, 다른 디바이스로 전달할 수 있다. wifi AP의 정보 전달 app중 하나인 ESP-TOUCH는 google play에서 다운받을 수 있으며, 코드는 github( https://github.com/EspressifApp/EsptouchForAndroid )에서 다운받을 수 있다.
esp8266 모듈과는 다음과 같은 커맨드와 메세지를 주고받으며 smartconfig를 수행한다.
- AT+CWMODE=1을 사용하여 Station모드로 변경하고, AT+CWSTARTSMART 커맨드를 사용하여 Smartconfig를 시작한다.
- 스마트폰의 app을 사용하여 wifi ap의 정보를 esp8266에 전달하면, esp8266d은 이 정보를 사용하여 wifi ap에 접속을 시도한다.
- wifi ap에 접속하게 되면, WIFI CONNECTED, WIFI GOT IP등의 메시지를 리눅스 디바이스 쪽으로 보내온다.
- Smartconfig를 성공하면, esp8266은 'smartconfig connected wifi'메세지를 보내오고, 성공하지 못하면, 'smartconfig connect fail'메시지를 보내온다.
esp8266 드라이버소스
eps8266.h
esp8266.c
현재는 smartconfig 기능만 구현되어 있으며, 추후 다른 기능이 구현되면 블로그에서 공개할 예정이다.
esp8266 드라이버 사용 예제 - smartconfig
드라이버 소스를 사용하여 smartconfig를 수행하는 예는 아래와 같다.
int main(int argc, char *argv[])
{
unsigned long tick = 0;
int esp8266d = 0;
esp8266_param param = { "/dev/ttyS3", 115200, 192, 235 };
smartconfig_ctx_t smartconfig_ctx = { 0, };
esp8266d = esp8266_open(¶m);
if (esp8266d == 0) return 0;
//smart config
if (esp8266_start_smartconfig(esp8266d) < 0)
{
printf("esp8266_start_smartconfig
fail\n");
esp8266_close(esp8266d);
return 0;
}
tick = get_tick_ms();
do
{
esp8266_get_smartconfig(esp8266d,
&smartconfig_ctx);
if (smartconfig_ctx.connected == 1)
{
printf("smartconfig
success\nssid:%s\n", smartconfig_ctx.ssid);
break;
}
else if (smartconfig_ctx.connected ==
-1)
{
printf("smartconfig
fail\n");
break;
}
sleep_ms(100);
} while (get_tick_ms() - tick <= 60 * 1000);
if (esp8266_stop_smartconfig(esp8266d) < 0)
{
printf("esp8266_stop_smartconfig
fail\n");
esp8266_close(esp8266d);
return 0;
}
esp8266_close(esp8266d);
return 0;
}
실행 결과
linux 환경에서 테스트 결과는 아래와 같다.
스마트폰에서 esp8266 smartconfig app 실행 화면은 아래와 같다.
[관련 글]
linux uart 커널 설정 및 non-blocking 모드 시리얼 통신 example c code
sysfs interface를 사용한 gpio 컨트롤 및 sample c code
Linux esp8266 driver c source code - client TCP, UDP
Linux esp8266 driver c source code - TCP Server
Linux esp8266 driver c source code - AP 모드로 softAP
댓글
댓글 쓰기