arduino_restful_server.ino 4.26 KB
Newer Older
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
/*
 * Copyright (c) 2015 Cesanta Software Limited
 * All rights reserved
 * 
 * Build and run instructions:
 * To run with Arduino Ethernet (W5100) shield:
 * -----------------------------------------------------------
 *  1. Add (Sketch->Add file...) the following files to sketch:
 *     - /mongoose/mongoose.h
 *     - /mongoose/mongoose.c
 *     - /mongoose/platforms/arduino_ethernet_W5100/avrsupport.h
 *     - /mongoose/platforms/arduino_ethernet_W5100/avrsupport.cpp
 *  2. Make board_ip and board_mac variables suitable for your network and board
 *  3. Uncomment line #include <Ethernet.h>
 *  4. Compile & flash sketch
 *  5. Run curl http://<board_ip/blink
 *     LED attached to PIN 13 will blink and board free memory size and uptime will responsed
 *
 * To run with Adafruit WiFi (CC3000) shield:
 * -----------------------------------------------------------
 *  1. Add (Sketch->Add files...) the following files to sketch:
 *     - /mongoose/mongoose.h
 *     - /mongoose/mongoose.c
 *     - /mongoose/platforms/arduino_ethernet_W5100/avrsupport.h
 *     - /mongoose/platforms/arduino_ethernet_W5100/avrsupport.cpp
 *  2. Import Adafruit CC3000 library for mongoose (select Sketch->Import Library...->Add library... and point 
 *     /mongoose/platforms/arduino_wifi_CC3000/adafruit_CC3000_lib_mongoose folder
 *  3. Make the following variables suitable for your network
 *     - board_ip
 *     - subnet_mask
 *     - gateway
 *     - dns 
 *     - wlan_ssid
 *     - wlan_pwd
 *     - wlan_security
 *  5. Uncomment line #include <Adafruit_CC3000.h>
 *  4. Compile & flash sketch
 *  5. Run curl http://<board_ip/blink
 *     LED attached to PIN 13 will blink and board free memory size and uptime will responsed
 *
 */

//#include <Ethernet.h>
//#include <Adafruit_CC3000.h>
#include <SPI.h>
#include "mongoose.h"

// CHANGE THESE VARIABLES
// NB: Devices with the same address must not end up on the same network.
// Use MAC address provided by device manufacturer (e.g. on a sticker).
// If there isn't one, use a random one from the locally administered range.
// See http://en.wikipedia.org/wiki/MAC_address for details.
static uint8_t board_mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

static uint8_t board_ip[] = {192, 168, 10, 8};

#ifdef WIFI_CC3000
static uint8_t subnet_mask[] = {255, 255, 255, 0};
static uint8_t gateway[] = {192, 168, 10, 254};
Deomid Ryabkov's avatar
Deomid Ryabkov committed
62
static uint8_t dns_ip[] = {192, 168, 10, 254};
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

static const char *wlan_ssid = "mynetwork";     
static const char *wlan_pwd = "mypassword";
static int wlan_security = WLAN_SEC_WPA2;
#endif

///////////////////////////////////////////////

static const char *s_http_port = "60000";

static uint32_t IP2U32(uint8_t* iparr) {
  return ((uint32_t)iparr[0] << 24) | ((uint32_t)iparr[1] << 16) | (iparr[2] << 8) | (iparr[3]);
}

static void rfs_ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
  struct http_message *hm = (struct http_message *) ev_data;
  char buf[100];
  int clen;

  switch (ev) {
    case NS_HTTP_REQUEST:      
      if (mg_vcmp(&hm->uri, "/blink") == 0) {
        blink(1, 500);
      }

      clen = snprintf(buf, sizeof(buf),
                      "Free memory size: %d Uptime: %d",
                      (int)get_freememsize(), (int)time(NULL));

      mg_printf_http_chunk(nc, "HTTP/1.1 200 OK\r\n"
                               "Content-Length: %d\r\n"
                               "Transfer-Encoding: chunked\r\n\r\n"
                               "%s",
                               clen, buf);

      mg_send_http_chunk(nc, "", 0);
      break;
    case NS_SEND:
101
      nc->flags |= MG_F_CLOSE_IMMEDIATELY;
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
      break;
      
    default:
      break;
  }
}

static struct mg_connection *nc;
static struct mg_mgr mgr;

void setup() {
  Serial.begin(9600);
  Serial.println("Initialization...");
#if defined(ETHERNET_W5100)
  avr_netinit(board_mac, board_ip);
#elif defined(WIFI_CC3000)
  if (avr_netinit(wlan_ssid, wlan_pwd, wlan_security, IP2U32(board_ip), 
Deomid Ryabkov's avatar
Deomid Ryabkov committed
119
              IP2U32(subnet_mask), IP2U32(gateway), IP2U32(dns_ip)) != 0) {
120 121 122 123 124 125 126 127 128 129 130 131 132 133
    Serial.println("Initialization error, check network settings");
    return;
  };
#endif

  mg_mgr_init(&mgr, NULL);
  nc = mg_bind(&mgr, s_http_port, rfs_ev_handler);
  mg_set_protocol_http_websocket(nc);
  Serial.println("Initialization done");
}

void loop() {
  mg_mgr_poll(&mgr, 1000);
}