TA的每日心情 | 无聊 2017-4-26 18:34 |
---|
签到天数: 3 天 连续签到: 1 天 [LV.2]偶尔看看I
|
//mbed开发平台#include "mbed.h"DigitalIn button(P0_1, PullUp); //按键myled3改变状态//adc采集电压函数float meas; long ADC_VALUE[3]; AnalogIn ADC_MQ2(P0_6); //ADC1,MQ2空气质量检测AnalogIn ADC_MQ3(P0_14); //ADC2,MQ3空气质量检测AnalogIn analog_value(P0_23); // ADC3,可调电位器void adc_data(){ meas = ADC_MQ2.read(); ADC_VALUE[0] = (long)(meas * 3300); meas = ADC_MQ3.read(); ADC_VALUE[1] = (long)(meas * 3300); meas = analog_value.read(); ADC_VALUE[2] = (long)(meas * 3300); }//oled显示函数#include "CriusOLED.h"DigitalOut myled1(P0_27);DigitalOut myled2(P0_13);DigitalOut myled3(P0_17); //用户ledTicker ticker;float sampleTime = 1.0;long times;char buffer1[40];char buffer2[20];char buffer3[20];char Buffer[100];#define bufferclear " "void am2320_dat();void bme280_dat();void Tlink_Tcp();void oled_ticker() { //每隔一秒显示一次 myled1=!myled1; if(!button) myled3=!myled3; adc_data(); sendStrXY(bufferclear,1,0); snprintf(buffer1,sizeof(buffer1), "%ds %dmV %dmV %dmV",times++,ADC_VALUE[0],ADC_VALUE[1],ADC_VALUE[2]); sendStrXY(buffer1,0,0); am2320_dat(); bme280_dat();}void oled_init(){ i2c.frequency(400000); init_OLED(); displayOn(); reset_display(); ticker.attach(&oled_ticker, sampleTime); }//AM2320初始化#include "AM2320.h"AM2320 am2320(i2c);int h;int t;void am2320_dat(){ h = am2320.humidity(); t = am2320.temperature(); snprintf(buffer2,sizeof(buffer2), "%2d.%dc %2d.%d%%",t/10,t,h/10,h); sendStrXY(buffer2,4,0); }#include "BME280.h"BME280 sensor(I2C_SDA, I2C_SCL);void bme280_dat(){ snprintf(buffer3,sizeof(buffer3), "%2.2fC %6.0fPa",sensor.getTemperature(), sensor.getPressure()*100); sendStrXY(buffer3,5,0); }Serial pc(P0_4,P0_0,921600);//w5500初始化函数#include "WIZnetInterface.h"#define USE_DHCP 1#define LOOPBACKPORT 80char * IP_Addr = "192.168.0.194";char * IP_Subnet = "255.255.255.0";char * IP_Gateway = "192.168.0.1";unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x12,0x34,0x56};SPI spi(P0_26,P0_25,P0_24);WIZnetInterface ethernet(&spi,P0_16,P0_5);void W5500_init(){ #if USE_DHCP int ret = ethernet.init(MAC_Addr); #else int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway); #endif myled2=1; if (!ret) { pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress()); ret = ethernet.connect(); if (!ret) { pc.printf("IP: %s, MASK: %s, GW: %s\r\n", ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway()); sendStrXY("IP:",2,0); sendStrXY(ethernet.getIPAddress(),2,3); } else { pc.printf("Error ethernet.connect() - ret = %d\r\n", ret); exit(0); } } else { pc.printf("Error ethernet.init() - ret = %d\r\n", ret); exit(0); } }void w5500_httpserver(){ char buffer[128]; W5500_init(); TCPSocketServer server; server.bind(LOOPBACKPORT); server.listen(); while (1) { pc.printf("\nWait for new connection...\r\n"); TCPSocketConnection client; server.accept(client); client.set_blocking(false, 0); pc.printf("Connection from: %s\r\n", client.get_address()); sendStrXY("C:",3,0); sendStrXY(client.get_address(),3,3); while (client.is_connected() == true) { myled2=0; int n = client.receive(buffer, sizeof(buffer)); if(n > 0) { //client.send_all(buffer, n); //将接受的数据发送回去 if(strstr(buffer,"AD")) client.send(buffer1, sizeof(buffer1)); if(strstr(buffer,"AM")) client.send_all(buffer2, sizeof(buffer2)); if(strstr(buffer,"BE")) client.send(buffer3, sizeof(buffer3)); if(strstr(buffer,"LED")) myled3=!myled3; if(strstr(buffer,"ALL")) { snprintf(Buffer,sizeof(Buffer), " %s\r\n %s\r\n %s\r\n ledstatus:%d\r\n",buffer1,buffer2,buffer3,!myled3); client.send(Buffer, sizeof(Buffer)); } if(strstr(buffer,"IP")) { snprintf(Buffer,sizeof(Buffer), " MAC_Addr: %s\r\n LocalIp: %s\r\n ClientIp: %s\r\n",ethernet.getMACAddress(),ethernet.getIPAddress(),client.get_address()); client.send(Buffer, sizeof(Buffer)); } } if(client.is_fin_received()) client.close(); } pc.printf("Disconnected.\r\n"); sendStrXY(bufferclear,3,0); //sendStrXY(bufferclear,7,0); myled2=1; } }int main() { pc.printf("Start\r\n"); oled_init(); wait(5); while(1) { w5500_httpserver(); }}我用mbed给LPC824lite编写了一套程序。这个是代码的地址是https://developer.mbed.org/users/dadangjia/code/LP...
我的想法是利用LPC824LITE+w5500 用TCP方式进行局域网通信。
整个系统的硬件配置是
主系统:LPC824LITE+w5500
显示器:ssd1306(oled12864)
传感器:AM2320 ,BMP280,mq2,mq3
实现功能:
1 oled12864显示: AM2320的温湿度;BMP280的温度及海拔高度;MQ2,MQ3的传感器值。
2 TCP通信:oLED显示当前设备的地址,手机连接这个地址后发送特定指令就可以双向通信。因为整个程序已经编译31k,不能继续太多的操作,我是通过lpc824判断tcp接受到的特定字符串进行数据操作,当然如有更好的办法,大家可以推荐。
3 显示状态:我用lpc824lite自带的led显示网络的连接状态,程序的运行状态。
4按键功能:这个有待开发,lpc824lite上有个用户按键,按下这个按键,一个led翻转下,这个可以用来定义其他功能,暂时未知,就作为一个演示而已。
图片没有拍,代码其实很简单,大家看下就懂了。 |
|