|
| 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