This is WIZwiki-REST-io upper version. This version possible to handle PWM, I2C, GPIO.

Dependencies:   Adafruit_GFX MbedJSONValue_copy WIZnetInterface mbed

Fork of WIZwiki-REST-io by Lawrence Lee

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "HTTPServer.h"
00003 #include "RequestHandler.h"
00004 #include "EthernetInterface.h"
00005 #include "MbedJSONValue.h"
00006 // 20160630
00007 #include "Adafruit_SSD1306.h"
00008 
00009 #define SERVER_PORT 80
00010 
00011 //#define DHCP
00012 
00013 //#define DEBUG
00014 
00015 //-- I2C OLED --
00016 class I2CPreInit : public I2C
00017 {
00018 public:
00019     I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl)
00020     {
00021         frequency(100000);
00022         start();
00023     };
00024 };
00025 I2CPreInit gI2C(PA_10,PA_9);
00026 Adafruit_SSD1306_I2c gOled(gI2C,NC,0x78,64,128);
00027 
00028 //-- PWM DC --
00029 PwmOut DC(D6);
00030 
00031 //-- GPIO --
00032 DigitalInOut GP05(D5);
00033 
00034 EthernetInterface eth;
00035 HTTPServer WIZwikiWebSvr;
00036 MbedJSONValue WIZwikiREST;
00037 
00038 // Enter a MAC address for your controller below.
00039 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0xFE};
00040 char mac_str[20];
00041 char ip_addr[]      = "192.168.100.100";
00042 char subnet_mask[]  = "255.255.255.0";
00043 char gateway_addr[] = "192.168.100.1";
00044 
00045 GetRequestHandler myGetReq;
00046 PostRequestHandler myPostReq;
00047 
00048 //-- I2C OLED --
00049 bool oled_set(void* param)
00050 {
00051     printf("param : %c \r\n", param);
00052     char * oled;
00053     if(!param) return false;
00054     oled = (char*) param;
00055     gOled.clearDisplay();
00056     gOled.setTextCursor(0,0);
00057     gOled.printf("%s",oled);
00058     gOled.display();    
00059     return true;
00060 }
00061 //-- PWM DC --
00062 bool pwm_set(void* param)
00063 {
00064     printf("%d\r\n",(*(int*)param));
00065     if(!param) return false;
00066     DC.write((float)(*(int*)param)/100.0);
00067     return true;
00068 }
00069 //-- GPIO --
00070 bool p5_set(void* param)
00071 {
00072      if(!param) return false;
00073     GP05.write(*(int*)param);
00074     return true;
00075 }
00076 
00077 void debug_info()
00078 {
00079     printf("SP:0x%X\r\n",__current_sp());
00080     __heapstats((__heapprt)fprintf,stderr);
00081 #ifdef DEBUG
00082     __heapvalid((__heapprt)fprintf,stderr, 1);
00083 #endif
00084     printf("\r\n");
00085 }    
00086 
00087 void WIZwiki_REST_init();
00088 
00089 int main(void)
00090 {
00091 #ifdef DEBUG
00092     debug_info();
00093 #endif
00094     sprintf(mac_str, "%02X:%02X:%02X:%02X:%02X:%02X",mac_addr[0],mac_addr[1],mac_addr[2],mac_addr[3],mac_addr[4],mac_addr[5]);
00095     
00096     // OLED init
00097     gOled.begin();
00098     gOled.clearDisplay();
00099     
00100     // PWM init
00101     DC.period_ms(1);
00102     DC.write(0);
00103     
00104     //GPIO set & init
00105     GP05.output();
00106     GP05.write(1);
00107 
00108     printf("START \r\n");    
00109     printf("sizeof(MbedJSONValue)=%d\r\n",sizeof(MbedJSONValue));
00110     printf("sizeof(vector)=%d\r\n",sizeof(std::vector<string*>));
00111     printf("sizeof(string)=%d\r\n",sizeof(std::string));
00112 #ifdef DEBUG
00113     debug_info();
00114 #endif
00115 
00116     WIZwiki_REST_init();
00117 
00118 #ifdef DEBUG
00119     debug_info();
00120 #endif
00121                     
00122     // Serialize it into a JSON string
00123     printf("-------------------------WIZwikiREST--------------------------- \r\n");
00124     printf("\r\n%s\r\n", WIZwikiREST.serialize().c_str());
00125     printf("--------------------------------------------------------------- \r\n");
00126 
00127     WIZwikiWebSvr.add_request_handler("GET", &myGetReq);
00128     WIZwikiWebSvr.add_request_handler("POST", &myPostReq);
00129     //WIZwikiWebSvr.add_request_handler("DELETE", new PostRequestHandler());
00130     //WIZwikiWebSvr.add_request_handler("PUT", new PutRequestHandler());
00131     
00132     #ifdef DHCP
00133         eth.init(mac_addr); //Use DHCP
00134     #else
00135         eth.init(mac_addr, ip_addr, subnet_mask, gateway_addr); //Not Use DHCP
00136     #endif
00137     
00138     
00139     printf("Check Ethernet Link\r\n");
00140     
00141     do{
00142         printf("   Link - Wait... \r\n");
00143         wait(1);
00144     }while(!eth.ethernet_link());
00145     printf("-- Ethetnet PHY Link - Done -- \r\n");
00146         
00147     if (eth.connect() < 0 )
00148         printf("-- EThernet Connect - Fail -- \r\n");
00149     else
00150     {
00151         printf("-- Assigned Network Information -- \r\n");
00152         printf("   IP   : %s\r\n\r\n", eth.getIPAddress()); 
00153         printf("   MASK : %s\r\n\r\n", eth.getNetworkMask());
00154         printf("   GW   : %s\r\n\r\n", eth.getGateway());
00155     }
00156     
00157     printf("Link up\r\n");
00158     printf("IP Address is %s\r\n", eth.getIPAddress());
00159 #ifdef DEBUG
00160     debug_info();
00161 #endif
00162 
00163     if(!WIZwikiWebSvr.init(SERVER_PORT))
00164     {
00165         eth.disconnect();
00166         return -1;
00167     }
00168 
00169     while(1)
00170     {
00171         WIZwikiWebSvr.run();
00172     }
00173 }
00174 
00175 void WIZwiki_REST_init(void)
00176 {
00177     //Fill the object
00178     WIZwikiREST["Name"] = "WIZwiki-RESTful-01";
00179     WIZwikiREST["Name"].accessible = false;
00180 #ifdef DEBUG
00181     debug_info();
00182 #endif
00183     
00184     //Network
00185     WIZwikiREST["Network"]["MAC"] = mac_str;
00186     WIZwikiREST["Network"]["IP"] = ip_addr; 
00187     WIZwikiREST["Network"]["IP"].accessible = true; 
00188     WIZwikiREST["Network"]["SN"] = subnet_mask;  
00189     WIZwikiREST["Network"]["SN"].accessible = true;  
00190     WIZwikiREST["Network"]["GW"] = gateway_addr;
00191     WIZwikiREST["Network"]["GW"].accessible = true;
00192     
00193     // I2C OLED
00194     WIZwikiREST["I2C"]["OLED"] = "none";
00195     WIZwikiREST["I2C"]["OLED"].accessible = true;
00196     WIZwikiREST["I2C"]["OLED"].cb_action = oled_set;
00197     
00198     // PWM DC
00199     WIZwikiREST["PWM"]["DC"] = DC.read();
00200     WIZwikiREST["PWM"]["DC"].accessible = true;
00201     WIZwikiREST["PWM"]["DC"].cb_action = pwm_set;
00202     
00203     // GPIO
00204     WIZwikiREST["GPIOs"]["P05"] = GP05.read();
00205     WIZwikiREST["GPIOs"]["P05"].accessible = true;
00206     WIZwikiREST["GPIOs"]["P05"].cb_action = p5_set;
00207 
00208 #ifdef DEBUG
00209     debug_info();
00210 #endif
00211 }