lightweight Web server dust sensor
Dependencies: EthernetInterface HTTPD mbed-rpc mbed-rtos mbed
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "HTTPD.h" 00004 00005 EthernetInterface *eth; 00006 HTTPD *httpd; 00007 00008 Serial pc(USBTX, USBRX); 00009 InterruptIn probe(p8); 00010 LocalFileSystem local("local"); 00011 DigitalOut led1(LED1), led2(LED2); 00012 Timer timer; 00013 Ticker ticker; 00014 00015 uint32_t low_time = 0; 00016 00017 void down(); 00018 void up(); 00019 00020 void tick() 00021 { 00022 pc.printf("%d us, %f %%\r\n", low_time, low_time / (30.0 * 1000000)); 00023 low_time = 0; 00024 } 00025 00026 void down() 00027 { 00028 probe.rise(&up); 00029 led1 = 0; 00030 timer.start(); 00031 } 00032 00033 void up() 00034 { 00035 led1 = 1; 00036 timer.stop(); 00037 probe.fall(&down); 00038 00039 low_time += timer.read_us(); 00040 timer.reset(); 00041 } 00042 00043 void callback_api(int id) { 00044 int i, n; 00045 char low_time_buf[256]; 00046 char buf[256]; 00047 00048 sprintf( low_time_buf, "%f", low_time / (30.0 * 1000000)); 00049 strcpy(buf, "{"); 00050 strcat(buf, "success: true ,"); 00051 strcat(buf, "data: {"); 00052 strcat(buf, "lowpulseoccupancytime: "); 00053 strcat(buf, low_time_buf); 00054 strcat(buf, "}"); 00055 strcat(buf, "}"); 00056 00057 n = strlen(buf); 00058 i = httpd->receive(id, &buf[n], sizeof(buf) - n); 00059 if (i < 0) return; 00060 i += n; 00061 buf[i] = 0; 00062 printf("API %d %s\r\n", id, buf); 00063 httpd->send(id, buf, i, "Content-Type: text/plain\r\n"); 00064 } 00065 00066 int main() { 00067 pc.baud(115200); 00068 pc.printf("Dust Sensor test\r\n"); 00069 probe.fall(&down); 00070 ticker.attach(tick, 30); 00071 00072 printf("HTTP Server...\r\n"); 00073 00074 eth = new EthernetInterface; 00075 eth->init("192.168.21.81", "255.255.255.0", "192.168.21.2" ); 00076 00077 if (eth->connect()) return -1; 00078 00079 printf("IP Address is %s\r\n", eth->getIPAddress()); 00080 httpd = new HTTPD; 00081 httpd->attach("/1/mbed/lpc1768/sensor/dust/sen12291p", &callback_api); 00082 httpd->attach("/", "/local/"); 00083 httpd->start(80); 00084 printf("httpd ready\r\n"); 00085 led2 = 1; 00086 } 00087
Generated on Fri Jul 15 2022 01:54:45 by 1.7.2