Created
January 26, 2016 09:08
-
-
Save shunkino/6b1734ee892fe2efbd12 to your computer and use it in GitHub Desktop.
ADXL345 and ESP8266
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "esp_common.h" | |
#include "i2c.h" | |
#include "freertos/FreeRTOS.h" | |
#include "lwip/sockets.h" | |
#include "lwip/dns.h" | |
#include "lwip/netdb.h" | |
#define SSID "" | |
#define PASS "" | |
#define TCP_PORT 3080 | |
#define DEV_ADDR 0x1D | |
#define REG_ADDR 0x32 | |
// protos | |
void read_acc_reg(int32 client_sock, uint8_t reg_addr); | |
void soft_ap_init(void *pvparameters); | |
void tcp_wait(int32 listenfd); | |
void user_tcp_init(void *pvparameters); | |
void write_to(int32 client_sock, uint8_t reg_addr, uint8_t val); | |
void read_reg(int *result, uint8_t reg_addr); | |
void adxl_init(); | |
// tcp functions | |
void soft_ap_init(void *pvparameters) { | |
wifi_set_opmode(SOFTAP_MODE); | |
//allocate memmory for soft AP config. | |
struct softap_config *config = (struct softap_config *)zalloc(sizeof(struct softap_config)); | |
wifi_softap_get_config(config); | |
sprintf(config->ssid, SSID); | |
sprintf(config->password, PASS); | |
config->authmode = AUTH_WPA_WPA2_PSK; | |
config->ssid_len = 0; | |
config->max_connection = 4; | |
wifi_softap_set_config(config); | |
free(config); | |
//接続してきているステーションの情報を表示する。 | |
struct station_info * station = wifi_softap_get_station_info(); | |
while(station) { | |
station = STAILQ_NEXT(station, next); | |
} | |
wifi_softap_free_station_info(); | |
vTaskDelete(NULL); | |
} | |
void tcp_wait(int32 listenfd) { | |
int32 client_sock; | |
int32 len = sizeof(struct sockaddr_in); | |
struct sockaddr_in remote_addr; | |
for (;;) { | |
printf("TCP server task > wait client\n"); | |
if ((client_sock = accept(listenfd, (struct sockaddr *)&remote_addr, (socklen_t *)&len)) < 0) { | |
printf("TCP server task > accept fail\n"); | |
continue; | |
} | |
printf("TCP server task > Client from %s %d\n", inet_ntoa(remote_addr.sin_addr), htons(remote_addr.sin_port)); | |
char *recv_buf = (char *)zalloc(128); | |
int recbytes, i; | |
while((recbytes = read(client_sock, recv_buf, 128)) > 0) { | |
if (recv_buf[recbytes - 2] == '\n' ) { | |
recv_buf[recbytes - 2] = '\0'; // chomp | |
} else { | |
recv_buf[recbytes - 1] = '\0'; // chomp | |
} | |
printf("TCP server task > read data success %d!\n TCP server task > %s\n", recbytes, recv_buf); | |
if (strcmp(recv_buf, "readacc") == 0) { | |
read_acc_reg(client_sock, REG_ADDR); | |
} else if (strcmp(recv_buf, "readstream") == 0) { | |
for (i = 0; i < 300; ++i) { | |
read_acc_reg(client_sock, REG_ADDR); | |
} | |
} else if (strcmp(recv_buf, "wake") == 0) { | |
// power control register | |
write_to(client_sock, 0x2d, 0x08); | |
} else if (strcmp(recv_buf, "device") == 0) { | |
read_acc_reg(client_sock, 0x00); | |
} | |
} | |
free(recv_buf); | |
if (recbytes <= 0) { | |
printf("TCP server task > read data fail! \n"); | |
close(client_sock); | |
} | |
} | |
vTaskDelete(NULL); | |
} | |
void user_tcp_init(void *pvparameters) { | |
//file discripter for listening. | |
int32 listenfd; | |
int32 ret; | |
//sockaddr_in structであるserver_addrを宣言 | |
struct sockaddr_in server_addr; | |
int stack_counter = 0; | |
//宣言してものを0で埋める | |
memset(&server_addr, 0, sizeof(server_addr)); | |
server_addr.sin_family = AF_INET; | |
server_addr.sin_addr.s_addr = INADDR_ANY; | |
server_addr.sin_len = sizeof(server_addr); | |
server_addr.sin_port = htons(TCP_PORT); | |
do { | |
//socketがopenするのを待つ | |
listenfd = socket(AF_INET, SOCK_STREAM, 0); | |
if (listenfd == -1) { | |
printf("TCP server task > socket open error \n"); | |
//無限ループ処理を遅らせる | |
vTaskDelay(1000/portTICK_RATE_MS); | |
} | |
} while(listenfd == -1); | |
printf("TCP server task > create socket: %d", stack_counter); | |
do { | |
ret = bind(listenfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); | |
if (ret != 0) { | |
printf("TCP server task > bind fail \n"); | |
vTaskDelay(1000/portTICK_RATE_MS); | |
} | |
} while (ret != 0); | |
printf("TCP server task > port:%d", ntohs(server_addr.sin_port)); | |
do { | |
ret = listen(listenfd, 4); | |
if (ret != 0) { | |
printf("TCP server task > failed to set listen queue! \n"); | |
vTaskDelay(1000/portTICK_RATE_MS); | |
} | |
} while (ret != 0); | |
tcp_wait(listenfd); | |
} | |
// i2c functions | |
void read_reg(int *result, uint8_t reg_addr) { | |
uint8_t ack; | |
i2c_start(); | |
ack = i2c_write((DEV_ADDR << 1) + 0); | |
if (!ack) { | |
printf("addr not ack when tx write command \n"); | |
i2c_stop(); | |
} | |
ack = i2c_write(reg_addr); | |
if (!ack) { | |
printf("register addr not ack \n"); | |
i2c_stop(); | |
} | |
os_delay_us(40000); | |
i2c_stop(); | |
i2c_start(); | |
ack = i2c_write((DEV_ADDR << 1) + 1); | |
if (!ack) { | |
printf("read device addr not ack when tx write command \n"); | |
i2c_stop(); | |
} | |
os_delay_us(40000); | |
int res = i2c_read(); | |
i2c_stop(); | |
*result = res; | |
printf("reading %x succeed\n", res); | |
} | |
void read_acc_reg(int32 client_sock, uint8_t reg_addr) { | |
uint8_t i, ack; | |
short x, y, z; | |
char buf[100]; | |
short temp[6]; | |
i2c_start(); | |
ack = i2c_write((DEV_ADDR << 1) + 0); | |
if (!ack) { | |
printf("addr not ack when tx write command \n"); | |
i2c_stop(); | |
} | |
ack = i2c_write(reg_addr); | |
if (!ack) { | |
printf("register addr not ack \n"); | |
i2c_stop(); | |
} | |
os_delay_us(40000); | |
i2c_stop(); | |
i2c_start(); | |
ack = i2c_write((DEV_ADDR << 1) + 1); | |
if (!ack) { | |
printf("read device add not ack\n"); | |
i2c_stop(); | |
} | |
os_delay_us(40000); | |
for (i = 0; i < 6; ++i) { | |
temp[i] = i2c_read(); | |
if (i == 5) { | |
i2c_set_ack(false); | |
} else { | |
i2c_set_ack(true); | |
} | |
} | |
i2c_stop(); | |
x = (temp[1] << 8 | temp[0]); | |
y = (temp[3] << 8 | temp[2]); | |
z = (temp[5] << 8 | temp[4]); | |
printf("x:%d y:%d z:%d \n", x, y, z); | |
sprintf(buf, "x:%d y:%d z:%d \n", x, y, z); | |
write(client_sock, buf, strlen(buf)); | |
} | |
void write_to(int32 client_sock, uint8_t reg_addr, uint8_t val) { | |
uint8_t ack; | |
i2c_start(); | |
ack = i2c_write((DEV_ADDR << 1) + 0); | |
if (!ack) { | |
printf("addr not ack when tx write command \n"); | |
i2c_stop(); | |
} | |
os_delay_us(40000); | |
ack = i2c_write(reg_addr); | |
if (!ack) { | |
printf("reg addr not ack when tx write command \n"); | |
i2c_stop(); | |
} | |
os_delay_us(40000); | |
ack = i2c_write(val); | |
if (!ack) { | |
printf("val not ack when tx write command \n"); | |
i2c_stop(); | |
} | |
i2c_stop(); | |
} | |
void adxl_init() { | |
int response; | |
i2c_init(); | |
// read device id. | |
read_reg(&response, 0x00); | |
printf("device: %x\n", response); | |
} | |
void user_init(void) { | |
printf("SDK version:%s\n", system_get_sdk_version()); | |
adxl_init(); | |
xTaskCreate(soft_ap_init, "ap", 256, NULL, 2, NULL); | |
xTaskCreate(user_tcp_init, "tcp", 256, NULL, 2, NULL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment