lpc824lite 采集温湿度压强数据显示至oled12864,通w5500与用户进行TCP交互
Dependencies: mbed WIZnet_Library AM2320 BME280
main.cpp@0:ef802f772981, 2017-05-11 (annotated)
- Committer:
- dadangjia
- Date:
- Thu May 11 16:51:19 2017 +0000
- Revision:
- 0:ef802f772981
- Child:
- 1:cc2094da451b
lpc824,w5500,am2320,bep280,ssd1306
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dadangjia | 0:ef802f772981 | 1 | #include "mbed.h" |
dadangjia | 0:ef802f772981 | 2 | DigitalIn button(P0_1, PullUp); //按键myled3改变状态 |
dadangjia | 0:ef802f772981 | 3 | //adc采集电压函数 |
dadangjia | 0:ef802f772981 | 4 | float meas; |
dadangjia | 0:ef802f772981 | 5 | long ADC_VALUE[3]; |
dadangjia | 0:ef802f772981 | 6 | AnalogIn ADC_MQ2(P0_6); //ADC1,MQ2空气质量检测 |
dadangjia | 0:ef802f772981 | 7 | AnalogIn ADC_MQ3(P0_14); //ADC2,MQ2空气质量检测 |
dadangjia | 0:ef802f772981 | 8 | AnalogIn analog_value(P0_23); // ADC3,可调电位器 |
dadangjia | 0:ef802f772981 | 9 | |
dadangjia | 0:ef802f772981 | 10 | void adc_data() |
dadangjia | 0:ef802f772981 | 11 | { |
dadangjia | 0:ef802f772981 | 12 | meas = ADC_MQ2.read(); |
dadangjia | 0:ef802f772981 | 13 | ADC_VALUE[0] = (long)(meas * 3300); |
dadangjia | 0:ef802f772981 | 14 | meas = ADC_MQ3.read(); |
dadangjia | 0:ef802f772981 | 15 | ADC_VALUE[1] = (long)(meas * 3300); |
dadangjia | 0:ef802f772981 | 16 | meas = analog_value.read(); |
dadangjia | 0:ef802f772981 | 17 | ADC_VALUE[2] = (long)(meas * 3300); |
dadangjia | 0:ef802f772981 | 18 | } |
dadangjia | 0:ef802f772981 | 19 | |
dadangjia | 0:ef802f772981 | 20 | //oled显示函数 |
dadangjia | 0:ef802f772981 | 21 | #include "CriusOLED.h" |
dadangjia | 0:ef802f772981 | 22 | DigitalOut myled1(P0_27); |
dadangjia | 0:ef802f772981 | 23 | DigitalOut myled2(P0_13); |
dadangjia | 0:ef802f772981 | 24 | DigitalOut myled3(P0_17); //用户led |
dadangjia | 0:ef802f772981 | 25 | Ticker ticker; |
dadangjia | 0:ef802f772981 | 26 | float sampleTime = 1.0; |
dadangjia | 0:ef802f772981 | 27 | long times; |
dadangjia | 0:ef802f772981 | 28 | char buffer1[40]; |
dadangjia | 0:ef802f772981 | 29 | char buffer2[20]; |
dadangjia | 0:ef802f772981 | 30 | char buffer3[20]; |
dadangjia | 0:ef802f772981 | 31 | char Buffer[100]; |
dadangjia | 0:ef802f772981 | 32 | #define bufferclear " " |
dadangjia | 0:ef802f772981 | 33 | void am2320_dat(); |
dadangjia | 0:ef802f772981 | 34 | void bme280_dat(); |
dadangjia | 0:ef802f772981 | 35 | void Tlink_Tcp(); |
dadangjia | 0:ef802f772981 | 36 | void oled_ticker() |
dadangjia | 0:ef802f772981 | 37 | { |
dadangjia | 0:ef802f772981 | 38 | //每隔一秒显示一次 |
dadangjia | 0:ef802f772981 | 39 | myled1=!myled1; |
dadangjia | 0:ef802f772981 | 40 | if(!button) |
dadangjia | 0:ef802f772981 | 41 | myled3=!myled3; |
dadangjia | 0:ef802f772981 | 42 | adc_data(); |
dadangjia | 0:ef802f772981 | 43 | sendStrXY(bufferclear,1,0); |
dadangjia | 0:ef802f772981 | 44 | snprintf(buffer1,sizeof(buffer1), "%ds %dmV %dmV %dmV",times++,ADC_VALUE[0],ADC_VALUE[1],ADC_VALUE[2]); |
dadangjia | 0:ef802f772981 | 45 | sendStrXY(buffer1,0,0); |
dadangjia | 0:ef802f772981 | 46 | am2320_dat(); |
dadangjia | 0:ef802f772981 | 47 | bme280_dat(); |
dadangjia | 0:ef802f772981 | 48 | } |
dadangjia | 0:ef802f772981 | 49 | |
dadangjia | 0:ef802f772981 | 50 | void oled_init() |
dadangjia | 0:ef802f772981 | 51 | { |
dadangjia | 0:ef802f772981 | 52 | i2c.frequency(400000); |
dadangjia | 0:ef802f772981 | 53 | init_OLED(); |
dadangjia | 0:ef802f772981 | 54 | displayOn(); |
dadangjia | 0:ef802f772981 | 55 | reset_display(); |
dadangjia | 0:ef802f772981 | 56 | ticker.attach(&oled_ticker, sampleTime); |
dadangjia | 0:ef802f772981 | 57 | } |
dadangjia | 0:ef802f772981 | 58 | |
dadangjia | 0:ef802f772981 | 59 | //AM2320初始化 |
dadangjia | 0:ef802f772981 | 60 | #include "AM2320.h" |
dadangjia | 0:ef802f772981 | 61 | AM2320 am2320(i2c); |
dadangjia | 0:ef802f772981 | 62 | int h; |
dadangjia | 0:ef802f772981 | 63 | int t; |
dadangjia | 0:ef802f772981 | 64 | void am2320_dat() |
dadangjia | 0:ef802f772981 | 65 | { |
dadangjia | 0:ef802f772981 | 66 | h = am2320.humidity(); |
dadangjia | 0:ef802f772981 | 67 | t = am2320.temperature(); |
dadangjia | 0:ef802f772981 | 68 | snprintf(buffer2,sizeof(buffer2), "%2d.%dc %2d.%d%%",t/10,t%10,h/10,h%10); |
dadangjia | 0:ef802f772981 | 69 | sendStrXY(buffer2,4,0); |
dadangjia | 0:ef802f772981 | 70 | } |
dadangjia | 0:ef802f772981 | 71 | |
dadangjia | 0:ef802f772981 | 72 | #include "BME280.h" |
dadangjia | 0:ef802f772981 | 73 | BME280 sensor(I2C_SDA, I2C_SCL); |
dadangjia | 0:ef802f772981 | 74 | void bme280_dat() |
dadangjia | 0:ef802f772981 | 75 | { |
dadangjia | 0:ef802f772981 | 76 | snprintf(buffer3,sizeof(buffer3), "%2.2fC %6.0fPa",sensor.getTemperature(), sensor.getPressure()*100); |
dadangjia | 0:ef802f772981 | 77 | sendStrXY(buffer3,5,0); |
dadangjia | 0:ef802f772981 | 78 | } |
dadangjia | 0:ef802f772981 | 79 | |
dadangjia | 0:ef802f772981 | 80 | Serial pc(P0_4,P0_0,921600); |
dadangjia | 0:ef802f772981 | 81 | //w5500初始化函数 |
dadangjia | 0:ef802f772981 | 82 | #include "WIZnetInterface.h" |
dadangjia | 0:ef802f772981 | 83 | #define USE_DHCP 1 |
dadangjia | 0:ef802f772981 | 84 | |
dadangjia | 0:ef802f772981 | 85 | #define LOOPBACKPORT 80 |
dadangjia | 0:ef802f772981 | 86 | char * IP_Addr = "192.168.0.194"; |
dadangjia | 0:ef802f772981 | 87 | char * IP_Subnet = "255.255.255.0"; |
dadangjia | 0:ef802f772981 | 88 | char * IP_Gateway = "192.168.0.1"; |
dadangjia | 0:ef802f772981 | 89 | unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x12,0x34,0x56}; |
dadangjia | 0:ef802f772981 | 90 | |
dadangjia | 0:ef802f772981 | 91 | |
dadangjia | 0:ef802f772981 | 92 | SPI spi(P0_26,P0_25,P0_24); |
dadangjia | 0:ef802f772981 | 93 | WIZnetInterface ethernet(&spi,P0_16,P0_5); |
dadangjia | 0:ef802f772981 | 94 | |
dadangjia | 0:ef802f772981 | 95 | void W5500_init() |
dadangjia | 0:ef802f772981 | 96 | { |
dadangjia | 0:ef802f772981 | 97 | #if USE_DHCP |
dadangjia | 0:ef802f772981 | 98 | int ret = ethernet.init(MAC_Addr); |
dadangjia | 0:ef802f772981 | 99 | #else |
dadangjia | 0:ef802f772981 | 100 | int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway); |
dadangjia | 0:ef802f772981 | 101 | #endif |
dadangjia | 0:ef802f772981 | 102 | myled2=1; |
dadangjia | 0:ef802f772981 | 103 | if (!ret) { |
dadangjia | 0:ef802f772981 | 104 | pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress()); |
dadangjia | 0:ef802f772981 | 105 | ret = ethernet.connect(); |
dadangjia | 0:ef802f772981 | 106 | if (!ret) { |
dadangjia | 0:ef802f772981 | 107 | pc.printf("IP: %s, MASK: %s, GW: %s\r\n", |
dadangjia | 0:ef802f772981 | 108 | ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway()); |
dadangjia | 0:ef802f772981 | 109 | sendStrXY("IP:",2,0); |
dadangjia | 0:ef802f772981 | 110 | sendStrXY(ethernet.getIPAddress(),2,3); |
dadangjia | 0:ef802f772981 | 111 | } else { |
dadangjia | 0:ef802f772981 | 112 | pc.printf("Error ethernet.connect() - ret = %d\r\n", ret); |
dadangjia | 0:ef802f772981 | 113 | exit(0); |
dadangjia | 0:ef802f772981 | 114 | } |
dadangjia | 0:ef802f772981 | 115 | } else { |
dadangjia | 0:ef802f772981 | 116 | pc.printf("Error ethernet.init() - ret = %d\r\n", ret); |
dadangjia | 0:ef802f772981 | 117 | exit(0); |
dadangjia | 0:ef802f772981 | 118 | } |
dadangjia | 0:ef802f772981 | 119 | } |
dadangjia | 0:ef802f772981 | 120 | |
dadangjia | 0:ef802f772981 | 121 | void w5500_httpserver() |
dadangjia | 0:ef802f772981 | 122 | { |
dadangjia | 0:ef802f772981 | 123 | char buffer[128]; |
dadangjia | 0:ef802f772981 | 124 | W5500_init(); |
dadangjia | 0:ef802f772981 | 125 | TCPSocketServer server; |
dadangjia | 0:ef802f772981 | 126 | server.bind(LOOPBACKPORT); |
dadangjia | 0:ef802f772981 | 127 | server.listen(); |
dadangjia | 0:ef802f772981 | 128 | while (1) { |
dadangjia | 0:ef802f772981 | 129 | pc.printf("\nWait for new connection...\r\n"); |
dadangjia | 0:ef802f772981 | 130 | TCPSocketConnection client; |
dadangjia | 0:ef802f772981 | 131 | server.accept(client); |
dadangjia | 0:ef802f772981 | 132 | client.set_blocking(false, 0); |
dadangjia | 0:ef802f772981 | 133 | pc.printf("Connection from: %s\r\n", client.get_address()); |
dadangjia | 0:ef802f772981 | 134 | sendStrXY("C:",3,0); |
dadangjia | 0:ef802f772981 | 135 | sendStrXY(client.get_address(),3,3); |
dadangjia | 0:ef802f772981 | 136 | while (client.is_connected() == true) { |
dadangjia | 0:ef802f772981 | 137 | myled2=0; |
dadangjia | 0:ef802f772981 | 138 | int n = client.receive(buffer, sizeof(buffer)); |
dadangjia | 0:ef802f772981 | 139 | if(n > 0) |
dadangjia | 0:ef802f772981 | 140 | { |
dadangjia | 0:ef802f772981 | 141 | //client.send_all(buffer, n); //将接受的数据发送回去 |
dadangjia | 0:ef802f772981 | 142 | if(strstr(buffer,"AD")) |
dadangjia | 0:ef802f772981 | 143 | client.send(buffer1, sizeof(buffer1)); |
dadangjia | 0:ef802f772981 | 144 | |
dadangjia | 0:ef802f772981 | 145 | if(strstr(buffer,"AM")) |
dadangjia | 0:ef802f772981 | 146 | client.send_all(buffer2, sizeof(buffer2)); |
dadangjia | 0:ef802f772981 | 147 | |
dadangjia | 0:ef802f772981 | 148 | if(strstr(buffer,"BE")) |
dadangjia | 0:ef802f772981 | 149 | client.send(buffer3, sizeof(buffer3)); |
dadangjia | 0:ef802f772981 | 150 | |
dadangjia | 0:ef802f772981 | 151 | if(strstr(buffer,"LED")) |
dadangjia | 0:ef802f772981 | 152 | myled3=!myled3; |
dadangjia | 0:ef802f772981 | 153 | |
dadangjia | 0:ef802f772981 | 154 | if(strstr(buffer,"ALL")) |
dadangjia | 0:ef802f772981 | 155 | { |
dadangjia | 0:ef802f772981 | 156 | snprintf(Buffer,sizeof(Buffer), " %s\r\n %s\r\n %s\r\n ledstatus:%d\r\n",buffer1,buffer2,buffer3,!myled3); |
dadangjia | 0:ef802f772981 | 157 | client.send(Buffer, sizeof(Buffer)); |
dadangjia | 0:ef802f772981 | 158 | } |
dadangjia | 0:ef802f772981 | 159 | |
dadangjia | 0:ef802f772981 | 160 | if(strstr(buffer,"IP")) |
dadangjia | 0:ef802f772981 | 161 | { |
dadangjia | 0:ef802f772981 | 162 | snprintf(Buffer,sizeof(Buffer), " MAC_Addr: %s\r\n LocalIp: %s\r\n ClientIp: %s\r\n",ethernet.getMACAddress(),ethernet.getIPAddress(),client.get_address()); |
dadangjia | 0:ef802f772981 | 163 | client.send(Buffer, sizeof(Buffer)); |
dadangjia | 0:ef802f772981 | 164 | } |
dadangjia | 0:ef802f772981 | 165 | } |
dadangjia | 0:ef802f772981 | 166 | if(client.is_fin_received()) |
dadangjia | 0:ef802f772981 | 167 | client.close(); |
dadangjia | 0:ef802f772981 | 168 | } |
dadangjia | 0:ef802f772981 | 169 | pc.printf("Disconnected.\r\n"); |
dadangjia | 0:ef802f772981 | 170 | sendStrXY(bufferclear,3,0); |
dadangjia | 0:ef802f772981 | 171 | //sendStrXY(bufferclear,7,0); |
dadangjia | 0:ef802f772981 | 172 | myled2=1; |
dadangjia | 0:ef802f772981 | 173 | } |
dadangjia | 0:ef802f772981 | 174 | } |
dadangjia | 0:ef802f772981 | 175 | |
dadangjia | 0:ef802f772981 | 176 | |
dadangjia | 0:ef802f772981 | 177 | |
dadangjia | 0:ef802f772981 | 178 | int main() { |
dadangjia | 0:ef802f772981 | 179 | pc.printf("Start\r\n"); |
dadangjia | 0:ef802f772981 | 180 | oled_init(); |
dadangjia | 0:ef802f772981 | 181 | wait(5); |
dadangjia | 0:ef802f772981 | 182 | while(1) |
dadangjia | 0:ef802f772981 | 183 | { |
dadangjia | 0:ef802f772981 | 184 | w5500_httpserver(); |
dadangjia | 0:ef802f772981 | 185 | } |
dadangjia | 0:ef802f772981 | 186 | } |