![](/media/cache/profiles/logo_uOArsqL.png.50x50_q85.png)
This is a working HTTP server with I2C, GPIO and PWM commands accessible through URLs. It also includes the ability to serve files from an SD card.
Dependencies: EthernetInterfaceMuri SDFileSystem mbed-rtos mbed
muri.cpp
- Committer:
- gsteiert
- Date:
- 2013-09-07
- Revision:
- 0:28bc7ce77e20
- Child:
- 1:64f023138627
File content as of revision 0:28bc7ce77e20:
#include "mbed.h" #include "muri.h" I2C i2c(p28, p27); void cmd_i2c_write(char* qry, char* data, char* resp) { int addr = 0; int dlen = 0; int dtmp = 0; if (sscanf(qry, "?%2x", &addr) == 1) { qry += 3; while (sscanf(qry, "&%2x", &dtmp) == 1) { data[dlen] = dtmp; qry +=3; dlen +=1; } } else { dlen = -1; } if (dlen > 0 ) { i2c.write(addr, data, dlen); sprintf(resp,"200 %d\r\n", dlen); } else { if (dlen == 0) { sprintf(resp,"204 No data to write: %s\r\n", qry); } else { sprintf(resp,"400 Invalid parameters: %s\r\n", qry); } } } void cmd_i2c_read(char* qry, char* data, char* resp) { int addr = 0; int dlen = 0; int dcnt = 0; if (sscanf(qry, "?%2x", &addr) == 1) { qry += 3; if (sscanf(qry, "&%2x", &dlen) == 1) { if (i2c.read(addr, data, dlen)!=0) { dlen = -1; sprintf(resp, "500 I2C read failed\r\n"); } } else { sprintf(resp, "400 Invalid data length: %s\r\n", qry); } } else { dlen = -1; sprintf(resp, "400 Invalid address: %s\r\n", qry); } if (dlen > 0) { sprintf(resp,"200"); resp += 3; for (dcnt = 0; dcnt < dlen; dcnt++) { sprintf(resp," %02x", data[dcnt]); resp +=3; } } } void cmd_i2c_addr_read(char* qry, char* data, char* resp) { int addr = 0; int radd = 0; int dlen = 0; int dcnt = 0; if (sscanf(qry, "?%2x", &addr) == 1) { qry += 3; if (sscanf(qry, "&%2x", &radd) == 1) { qry += 3; if (sscanf(qry, "&%2x", &dlen) == 1) { data[0] = radd; i2c.write(addr, data, 1, false); if (i2c.read(addr, data, dlen)!=0) { dlen = -1; sprintf(resp, "500 I2C read failed\r\n"); } } else { sprintf(resp, "400 Invalid data length: %s\r\n", qry); } } else { dlen = -1; sprintf(resp, "400 Invalid register address: %s\r\n", qry); } } else { dlen = -1; sprintf(resp, "400 Invalid address: %s\r\n", qry); } if (dlen > 0) { sprintf(resp,"200"); resp += 3; for (dcnt = 0; dcnt < dlen; dcnt++) { sprintf(resp," %02x", data[dcnt]); resp +=3; } } } void muri(char* uri, char* data, char* resp) { // sprintf(resp, "501 %s", uri); //} //void get_cgi(char* uri, char* qry) //{ char *qry; printf("muri uri: %s\n", uri); qry = strstr(uri, "?"); if (!strncmp(uri, "i2cw?", 5)) { // i2cW [addr] [data] [data] [data] ... cmd_i2c_write(qry, data, resp); } else if (!strncmp(uri, "i2cr?", 5)) { // i2cR [addr] [count] cmd_i2c_read(qry, data, resp); } else if (!strncmp(uri, "i2car?", 6)) { // i2cAR [addr] [radd] [count] cmd_i2c_addr_read(qry, data, resp); } else { sprintf(resp, "501 %s", uri); } printf("muri resp: %s\n", resp); }