Skip to content

Commit 6edbe99

Browse files
committed
Merge pull request #10 from AndiceLabs/master
An example for connecting to dweet.io and sending some "thing" data
2 parents 2ed9b49 + 6cc718d commit 6edbe99

File tree

3 files changed

+293
-0
lines changed

3 files changed

+293
-0
lines changed

dweet/Makefile

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Makefile for ESP8266 projects
2+
#
3+
# Thanks to:
4+
# - zarya
5+
# - Jeroen Domburg (Sprite_tm)
6+
# - Christian Klippel (mamalala)
7+
# - Tommie Gannert (tommie)
8+
#
9+
# Changelog:
10+
# - 2014-10-06: Changed the variables to include the header file directory
11+
# - 2014-10-06: Added global var for the Xtensa tool root
12+
# - 2014-11-23: Updated for SDK 0.9.3
13+
# - 2014-12-25: Replaced esptool by esptool.py
14+
15+
# Output directors to store intermediate compiled files
16+
# relative to the project directory
17+
BUILD_BASE = build
18+
FW_BASE = firmware
19+
20+
# base directory for the compiler
21+
XTENSA_TOOLS_ROOT ?= /home/rbattles/nas/work/esp-open-sdk/xtensa-lx106-elf/bin
22+
23+
# base directory of the ESP8266 SDK package, absolute
24+
SDK_BASE ?= /home/rbattles/nas/work/esp-open-sdk/esp_iot_sdk_v1.2.0
25+
26+
# esptool.py path and port
27+
ESPTOOL ?= esptool.py
28+
ESPPORT ?= /dev/ttyUSB0
29+
30+
# name for the target project
31+
TARGET = dweet
32+
33+
# which modules (subdirectories) of the project to include in compiling
34+
MODULES = driver user
35+
EXTRA_INCDIR = include
36+
37+
# libraries used in this project, mainly provided by the SDK
38+
LIBS = c gcc hal pp phy net80211 lwip wpa main
39+
40+
# compiler flags using during compilation of source files
41+
CFLAGS = -Os -g -O2 -Wpointer-arith -Wundef -Werror -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals -D__ets__ -DICACHE_FLASH
42+
43+
# linker flags used to generate the main object file
44+
LDFLAGS = -nostdlib -Wl,--no-check-sections -u call_user_start -Wl,-static
45+
46+
# linker script used for the above linkier step
47+
LD_SCRIPT = eagle.app.v6.ld
48+
49+
# various paths from the SDK used in this project
50+
SDK_LIBDIR = lib
51+
SDK_LDDIR = ld
52+
SDK_INCDIR = include include/json
53+
54+
# we create two different files for uploading into the flash
55+
# these are the names and options to generate them
56+
FW_FILE_1_ADDR = 0x00000
57+
FW_FILE_2_ADDR = 0x40000
58+
59+
# select which tools to use as compiler, librarian and linker
60+
CC := $(XTENSA_TOOLS_ROOT)/xtensa-lx106-elf-gcc
61+
AR := $(XTENSA_TOOLS_ROOT)/xtensa-lx106-elf-ar
62+
LD := $(XTENSA_TOOLS_ROOT)/xtensa-lx106-elf-gcc
63+
64+
65+
66+
####
67+
#### no user configurable options below here
68+
####
69+
SRC_DIR := $(MODULES)
70+
BUILD_DIR := $(addprefix $(BUILD_BASE)/,$(MODULES))
71+
72+
SDK_LIBDIR := $(addprefix $(SDK_BASE)/,$(SDK_LIBDIR))
73+
SDK_INCDIR := $(addprefix -I$(SDK_BASE)/,$(SDK_INCDIR))
74+
75+
SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.c))
76+
OBJ := $(patsubst %.c,$(BUILD_BASE)/%.o,$(SRC))
77+
LIBS := $(addprefix -l,$(LIBS))
78+
APP_AR := $(addprefix $(BUILD_BASE)/,$(TARGET)_app.a)
79+
TARGET_OUT := $(addprefix $(BUILD_BASE)/,$(TARGET).out)
80+
81+
LD_SCRIPT := $(addprefix -T$(SDK_BASE)/$(SDK_LDDIR)/,$(LD_SCRIPT))
82+
83+
INCDIR := $(addprefix -I,$(SRC_DIR))
84+
EXTRA_INCDIR := $(addprefix -I,$(EXTRA_INCDIR))
85+
MODULE_INCDIR := $(addsuffix /include,$(INCDIR))
86+
87+
FW_FILE_1 := $(addprefix $(FW_BASE)/,$(FW_FILE_1_ADDR).bin)
88+
FW_FILE_2 := $(addprefix $(FW_BASE)/,$(FW_FILE_2_ADDR).bin)
89+
90+
V ?= $(VERBOSE)
91+
ifeq ("$(V)","1")
92+
Q :=
93+
vecho := @true
94+
else
95+
Q := @
96+
vecho := @echo
97+
endif
98+
99+
vpath %.c $(SRC_DIR)
100+
101+
define compile-objects
102+
$1/%.o: %.c
103+
$(vecho) "CC $$<"
104+
$(Q) $(CC) $(INCDIR) $(MODULE_INCDIR) $(EXTRA_INCDIR) $(SDK_INCDIR) $(CFLAGS) -c $$< -o $$@
105+
endef
106+
107+
.PHONY: all checkdirs flash clean
108+
109+
all: checkdirs $(TARGET_OUT) $(FW_FILE_1) $(FW_FILE_2)
110+
111+
$(FW_BASE)/%.bin: $(TARGET_OUT) | $(FW_BASE)
112+
$(vecho) "FW $(FW_BASE)/"
113+
$(Q) $(ESPTOOL) elf2image -o $(FW_BASE)/ $(TARGET_OUT)
114+
115+
$(TARGET_OUT): $(APP_AR)
116+
$(vecho) "LD $@"
117+
$(Q) $(LD) -L$(SDK_LIBDIR) $(LD_SCRIPT) $(LDFLAGS) -Wl,--start-group $(LIBS) $(APP_AR) -Wl,--end-group -o $@
118+
119+
$(APP_AR): $(OBJ)
120+
$(vecho) "AR $@"
121+
$(Q) $(AR) cru $@ $^
122+
123+
checkdirs: $(BUILD_DIR) $(FW_BASE)
124+
125+
$(BUILD_DIR):
126+
$(Q) mkdir -p $@
127+
128+
$(FW_BASE):
129+
$(Q) mkdir -p $@
130+
131+
flash: $(FW_FILE_1) $(FW_FILE_2)
132+
$(ESPTOOL) --port $(ESPPORT) write_flash $(FW_FILE_1_ADDR) $(FW_FILE_1) $(FW_FILE_2_ADDR) $(FW_FILE_2)
133+
134+
clean:
135+
$(Q) rm -rf $(FW_BASE) $(BUILD_BASE)
136+
137+
$(foreach bdir,$(BUILD_DIR),$(eval $(call compile-objects,$(bdir))))

dweet/user/user_config.h

Whitespace-only changes.

dweet/user/user_main.c

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#include "ets_sys.h"
2+
#include "osapi.h"
3+
#include "gpio.h"
4+
#include "os_type.h"
5+
#include "ip_addr.h"
6+
#include "espconn.h"
7+
#include "user_interface.h"
8+
#include "user_config.h"
9+
10+
11+
struct espconn dweet_conn;
12+
ip_addr_t dweet_ip;
13+
esp_tcp dweet_tcp;
14+
15+
char dweet_host[] = "dweet.io";
16+
char dweet_path[] = "/dweet/for/eccd882c-33d0-11e5-96b7-10bf4884d1f9";
17+
char json_data[ 256 ];
18+
char buffer[ 2048 ];
19+
20+
21+
void user_rf_pre_init( void )
22+
{
23+
}
24+
25+
26+
void data_received( void *arg, char *pdata, unsigned short len )
27+
{
28+
struct espconn *conn = arg;
29+
30+
os_printf( "%s: %s\n", __FUNCTION__, pdata );
31+
32+
espconn_disconnect( conn );
33+
}
34+
35+
36+
void tcp_connected( void *arg )
37+
{
38+
int temperature = 55; // test data
39+
struct espconn *conn = arg;
40+
41+
os_printf( "%s\n", __FUNCTION__ );
42+
espconn_regist_recvcb( conn, data_received );
43+
44+
os_sprintf( json_data, "{\"temperature\": \"%d\" }", temperature );
45+
os_sprintf( buffer, "POST %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n%s",
46+
dweet_path, dweet_host, os_strlen( json_data ), json_data );
47+
48+
os_printf( "Sending: %s\n", buffer );
49+
espconn_sent( conn, buffer, os_strlen( buffer ) );
50+
}
51+
52+
53+
void tcp_disconnected( void *arg )
54+
{
55+
struct espconn *conn = arg;
56+
57+
os_printf( "%s\n", __FUNCTION__ );
58+
wifi_station_disconnect();
59+
}
60+
61+
62+
void dns_done( const char *name, ip_addr_t *ipaddr, void *arg )
63+
{
64+
struct espconn *conn = arg;
65+
66+
os_printf( "%s\n", __FUNCTION__ );
67+
68+
if ( ipaddr == NULL)
69+
{
70+
os_printf("DNS lookup failed\n");
71+
wifi_station_disconnect();
72+
}
73+
else
74+
{
75+
os_printf("Connecting...\n" );
76+
77+
conn->type = ESPCONN_TCP;
78+
conn->state = ESPCONN_NONE;
79+
conn->proto.tcp=&dweet_tcp;
80+
conn->proto.tcp->local_port = espconn_port();
81+
conn->proto.tcp->remote_port = 80;
82+
os_memcpy( conn->proto.tcp->remote_ip, &ipaddr->addr, 4 );
83+
84+
espconn_regist_connectcb( conn, tcp_connected );
85+
espconn_regist_disconcb( conn, tcp_disconnected );
86+
87+
espconn_connect( conn );
88+
}
89+
}
90+
91+
92+
void wifi_callback( System_Event_t *evt )
93+
{
94+
os_printf( "%s: %d\n", __FUNCTION__, evt->event );
95+
96+
switch ( evt->event )
97+
{
98+
case EVENT_STAMODE_CONNECTED:
99+
{
100+
os_printf("connect to ssid %s, channel %d\n",
101+
evt->event_info.connected.ssid,
102+
evt->event_info.connected.channel);
103+
break;
104+
}
105+
106+
case EVENT_STAMODE_DISCONNECTED:
107+
{
108+
os_printf("disconnect from ssid %s, reason %d\n",
109+
evt->event_info.disconnected.ssid,
110+
evt->event_info.disconnected.reason);
111+
112+
deep_sleep_set_option( 0 );
113+
system_deep_sleep( 60 * 1000 * 1000 ); // 60 seconds
114+
break;
115+
}
116+
117+
case EVENT_STAMODE_GOT_IP:
118+
{
119+
os_printf("ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR,
120+
IP2STR(&evt->event_info.got_ip.ip),
121+
IP2STR(&evt->event_info.got_ip.mask),
122+
IP2STR(&evt->event_info.got_ip.gw));
123+
os_printf("\n");
124+
125+
espconn_gethostbyname( &dweet_conn, dweet_host, &dweet_ip, dns_done );
126+
break;
127+
}
128+
129+
default:
130+
{
131+
break;
132+
}
133+
}
134+
}
135+
136+
137+
void user_init( void )
138+
{
139+
static struct station_config config;
140+
141+
uart_div_modify( 0, UART_CLK_FREQ / ( 115200 ) );
142+
os_printf( "%s\n", __FUNCTION__ );
143+
144+
wifi_station_set_hostname( "dweet" );
145+
wifi_set_opmode_current( STATION_MODE );
146+
147+
gpio_init();
148+
149+
config.bssid_set = 0;
150+
os_memcpy( &config.ssid, "AndiceLabs", 32 );
151+
os_memcpy( &config.password, "password", 64 );
152+
wifi_station_set_config( &config );
153+
154+
wifi_set_event_handler_cb( wifi_callback );
155+
}
156+

0 commit comments

Comments
 (0)