-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
74 lines (60 loc) · 1.99 KB
/
main.c
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
/*
* Auth: Yoann Mailland
* Date: 2016 04 18
* Copyright (c) 2016, Yoann Mailland
* Info: Main file for Smart Plug App
* on ESP8266
*/
#include <user_config.h>
#include <utils.h>
#include <driver/uart.h>
#include <http.h>
#include <espconn.h>
#define WEB_PORT 80
// Wifi Info
const char wifi_ssid[32] = "my_ap_ssid";
const char wifi_pwd[32] = "my_ap_password";
void user_init()
{
// Init UART
{
// Try BIT_RATE_460800 and BIT_RATE_921600
uart_init(BIT_RATE_115200, BIT_RATE_115200);
printf("Boot...\n");
}
// Init OI Pins
{
printf("Init IO\n");
// Set GPIO 14 and 16 to OUTPUT
PIN_FUNC_SELECT(GPIO_12_PIN, FUNC_GPIO12);
PIN_FUNC_SELECT(GPIO_14_PIN, FUNC_GPIO14);
GPIO_SET_MODE_OUTPUT(GPIO_14);
GPIO_SET_MODE_OUTPUT(GPIO_12);
}
// Connect to Wifi Network
{
printf("Connect WIFI\n");
struct station_config sc;
memcpy(&sc.ssid, wifi_ssid, 32);
memcpy(&sc.password, wifi_pwd, 32);
wifi_set_opmode( STATION_MODE );
wifi_station_set_config(&sc);
wifi_station_connect();
}
// Create an TCP listner for port 80
{
printf("Create TCP Listner on port %d\n", WEB_PORT);
struct espconn* c =
(struct espconn *)zalloc(sizeof(struct espconn));
espconn_create(c);
c->type = ESPCONN_TCP;
c->state = ESPCONN_NONE;
c->proto.tcp = (esp_tcp *)malloc(sizeof(esp_tcp));
c->proto.tcp->local_port = WEB_PORT;
// Set New Connection Callback
espconn_regist_connectcb(c, http_server);
// Enable TCP listner
espconn_accept(c);
espconn_regist_time(c, 15, 0);
}
}