Statistics
| Branch: | Tag: | Revision:

mongoose / examples / mbed / main.cpp @ eaef5bd1

History | View | Annotate | Download (2.54 KB)

1
/*
2
 * Copyright (c) 2014-2016 Cesanta Software Limited
3
 * All rights reserved
4
 */
5

    
6
#include "mbed.h"
7
#include "mongoose.h"
8

    
9
#define WIFI_STA_SSID "yourssid"
10
#define WIFI_STA_PASS "yourpass"
11

    
12
#define HTTP_SERVER_PORT "80"
13

    
14
DigitalOut led_green(LED1);
15
DigitalOut led_red(LED3);
16
DigitalOut led_blue(LED4);
17

    
18
void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
19
  switch (ev) {
20
    case MG_EV_ACCEPT: {
21
      char addr[32];
22
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
23
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
24
      printf("%p: Connection from %s\r\n", nc, addr);
25
      break;
26
    }
27
    case MG_EV_HTTP_REQUEST: {
28
      struct http_message *hm = (struct http_message *) ev_data;
29
      char addr[32];
30
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
31
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
32
      printf("%p: %.*s %.*s\r\n", nc, (int) hm->method.len, hm->method.p,
33
             (int) hm->uri.len, hm->uri.p);
34
      mg_send_response_line(nc, 200,
35
                            "Content-Type: text/html\r\n"
36
                            "Connection: close");
37
      mg_printf(nc,
38
                "\r\n<h1>Hello, %s!</h1>\r\n"
39
                "You asked for %.*s\r\n",
40
                addr, (int) hm->uri.len, hm->uri.p);
41
      nc->flags |= MG_F_SEND_AND_CLOSE;
42

    
43
      led_blue = !led_blue;
44
      break;
45
    }
46
    case MG_EV_CLOSE: {
47
      printf("%p: Connection closed\r\n", nc);
48
      break;
49
    }
50
  }
51
}
52

    
53
int main() {
54
  struct mg_mgr mgr;
55

    
56
  Serial pc(SERIAL_TX, SERIAL_RX, 115200);
57
  printf("Mongoose demo\n");
58
  led_green = 1; /* off */
59
  led_blue = 1;  /* off */
60

    
61
  SimpleLinkInterface wifi(PG_10, PG_11, SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS);
62

    
63
  sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID);
64

    
65
  wifi.connect(WIFI_STA_SSID, WIFI_STA_PASS);
66

    
67
  const char *ip = wifi.get_ip_address();
68
  const char *gw = wifi.get_gateway();
69
  const char *mac = wifi.get_mac_address();
70
  printf("IP address is: %s\n", ip ? ip : "No IP");
71
  printf("GW address is: %s\n", gw ? gw : "No IP");
72
  printf("MAC address is: %s\n", mac ? mac : "No MAC");
73

    
74
  mg_mgr_init(&mgr, NULL);
75

    
76
  const char *err;
77
  struct mg_bind_opts opts = {};
78
  opts.error_string = &err;
79
  mg_connection *nc = mg_bind_opt(&mgr, HTTP_SERVER_PORT, ev_handler, opts);
80
  if (nc == NULL) {
81
    printf("Failed to create listener: %s\r\n", err);
82
    led_red = 0; /* on */
83
    return 1;
84
  }
85
  mg_set_protocol_http_websocket(nc);
86
  printf("Server address: http://%s:%s\n", ip, HTTP_SERVER_PORT);
87

    
88
  while (true) {
89
    mg_mgr_poll(&mgr, 1000);
90
    led_green = !led_green;
91
  }
92
}