Lawrence Lee / Mbed 2 deprecated WIZwiki-REST-io_v102

Dependencies:   Adafruit_GFX MbedJSONValue_v102 WIZnetInterface mbed

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