lpc824lite 采集温湿度压强数据显示至oled12864,通w5500与用户进行TCP交互

Dependencies:   mbed WIZnet_Library AM2320 BME280

Committer:
dadangjia
Date:
Mon May 29 00:35:16 2017 +0000
Revision:
1:cc2094da451b
Parent:
0:ef802f772981
Child:
2:82785740745d
LPC824 AM2320 W5500 BEM280 OLED12864

Who changed what in which revision?

UserRevisionLine numberNew 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 1:cc2094da451b 25 DigitalOut myled4(P0_18);
dadangjia 1:cc2094da451b 26 DigitalOut myled5(P0_19);
dadangjia 0:ef802f772981 27 Ticker ticker;
dadangjia 0:ef802f772981 28 float sampleTime = 1.0;
dadangjia 0:ef802f772981 29 long times;
dadangjia 0:ef802f772981 30 char buffer1[40];
dadangjia 0:ef802f772981 31 char buffer2[20];
dadangjia 0:ef802f772981 32 char buffer3[20];
dadangjia 0:ef802f772981 33 char Buffer[100];
dadangjia 0:ef802f772981 34 #define bufferclear " "
dadangjia 0:ef802f772981 35 void am2320_dat();
dadangjia 0:ef802f772981 36 void bme280_dat();
dadangjia 0:ef802f772981 37 void Tlink_Tcp();
dadangjia 0:ef802f772981 38 void oled_ticker()
dadangjia 0:ef802f772981 39 {
dadangjia 0:ef802f772981 40 //每隔一秒显示一次
dadangjia 0:ef802f772981 41 myled1=!myled1;
dadangjia 0:ef802f772981 42 if(!button)
dadangjia 0:ef802f772981 43 myled3=!myled3;
dadangjia 0:ef802f772981 44 adc_data();
dadangjia 0:ef802f772981 45 sendStrXY(bufferclear,1,0);
dadangjia 0:ef802f772981 46 snprintf(buffer1,sizeof(buffer1), "%ds %dmV %dmV %dmV",times++,ADC_VALUE[0],ADC_VALUE[1],ADC_VALUE[2]);
dadangjia 0:ef802f772981 47 sendStrXY(buffer1,0,0);
dadangjia 0:ef802f772981 48 am2320_dat();
dadangjia 0:ef802f772981 49 bme280_dat();
dadangjia 0:ef802f772981 50 }
dadangjia 0:ef802f772981 51
dadangjia 0:ef802f772981 52 void oled_init()
dadangjia 0:ef802f772981 53 {
dadangjia 0:ef802f772981 54 i2c.frequency(400000);
dadangjia 0:ef802f772981 55 init_OLED();
dadangjia 0:ef802f772981 56 displayOn();
dadangjia 0:ef802f772981 57 reset_display();
dadangjia 0:ef802f772981 58 ticker.attach(&oled_ticker, sampleTime);
dadangjia 0:ef802f772981 59 }
dadangjia 0:ef802f772981 60
dadangjia 0:ef802f772981 61 //AM2320初始化
dadangjia 0:ef802f772981 62 #include "AM2320.h"
dadangjia 0:ef802f772981 63 AM2320 am2320(i2c);
dadangjia 0:ef802f772981 64 int h;
dadangjia 0:ef802f772981 65 int t;
dadangjia 0:ef802f772981 66 void am2320_dat()
dadangjia 0:ef802f772981 67 {
dadangjia 0:ef802f772981 68 h = am2320.humidity();
dadangjia 0:ef802f772981 69 t = am2320.temperature();
dadangjia 0:ef802f772981 70 snprintf(buffer2,sizeof(buffer2), "%2d.%dc %2d.%d%%",t/10,t%10,h/10,h%10);
dadangjia 0:ef802f772981 71 sendStrXY(buffer2,4,0);
dadangjia 0:ef802f772981 72 }
dadangjia 0:ef802f772981 73
dadangjia 0:ef802f772981 74 #include "BME280.h"
dadangjia 0:ef802f772981 75 BME280 sensor(I2C_SDA, I2C_SCL);
dadangjia 0:ef802f772981 76 void bme280_dat()
dadangjia 0:ef802f772981 77 {
dadangjia 0:ef802f772981 78 snprintf(buffer3,sizeof(buffer3), "%2.2fC %6.0fPa",sensor.getTemperature(), sensor.getPressure()*100);
dadangjia 0:ef802f772981 79 sendStrXY(buffer3,5,0);
dadangjia 0:ef802f772981 80 }
dadangjia 0:ef802f772981 81
dadangjia 0:ef802f772981 82 Serial pc(P0_4,P0_0,921600);
dadangjia 1:cc2094da451b 83 // Line buffers for sprintf and sscanf
dadangjia 0:ef802f772981 84 //w5500初始化函数
dadangjia 0:ef802f772981 85 #include "WIZnetInterface.h"
dadangjia 0:ef802f772981 86 #define USE_DHCP 1
dadangjia 0:ef802f772981 87
dadangjia 0:ef802f772981 88 #define LOOPBACKPORT 80
dadangjia 0:ef802f772981 89 char * IP_Addr = "192.168.0.194";
dadangjia 0:ef802f772981 90 char * IP_Subnet = "255.255.255.0";
dadangjia 0:ef802f772981 91 char * IP_Gateway = "192.168.0.1";
dadangjia 0:ef802f772981 92 unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x12,0x34,0x56};
dadangjia 0:ef802f772981 93
dadangjia 0:ef802f772981 94
dadangjia 0:ef802f772981 95 SPI spi(P0_26,P0_25,P0_24);
dadangjia 0:ef802f772981 96 WIZnetInterface ethernet(&spi,P0_16,P0_5);
dadangjia 0:ef802f772981 97
dadangjia 0:ef802f772981 98 void W5500_init()
dadangjia 0:ef802f772981 99 {
dadangjia 0:ef802f772981 100 #if USE_DHCP
dadangjia 0:ef802f772981 101 int ret = ethernet.init(MAC_Addr);
dadangjia 0:ef802f772981 102 #else
dadangjia 0:ef802f772981 103 int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway);
dadangjia 0:ef802f772981 104 #endif
dadangjia 0:ef802f772981 105 myled2=1;
dadangjia 1:cc2094da451b 106 myled3=1;
dadangjia 1:cc2094da451b 107 myled4=1;
dadangjia 1:cc2094da451b 108 myled5=1;
dadangjia 0:ef802f772981 109 if (!ret) {
dadangjia 0:ef802f772981 110 pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
dadangjia 0:ef802f772981 111 ret = ethernet.connect();
dadangjia 0:ef802f772981 112 if (!ret) {
dadangjia 0:ef802f772981 113 pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
dadangjia 0:ef802f772981 114 ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
dadangjia 0:ef802f772981 115 sendStrXY("IP:",2,0);
dadangjia 0:ef802f772981 116 sendStrXY(ethernet.getIPAddress(),2,3);
dadangjia 0:ef802f772981 117 } else {
dadangjia 0:ef802f772981 118 pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
dadangjia 0:ef802f772981 119 exit(0);
dadangjia 0:ef802f772981 120 }
dadangjia 0:ef802f772981 121 } else {
dadangjia 0:ef802f772981 122 pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
dadangjia 0:ef802f772981 123 exit(0);
dadangjia 0:ef802f772981 124 }
dadangjia 0:ef802f772981 125 }
dadangjia 0:ef802f772981 126
dadangjia 0:ef802f772981 127 void w5500_httpserver()
dadangjia 0:ef802f772981 128 {
dadangjia 0:ef802f772981 129 char buffer[128];
dadangjia 0:ef802f772981 130 W5500_init();
dadangjia 0:ef802f772981 131 TCPSocketServer server;
dadangjia 0:ef802f772981 132 server.bind(LOOPBACKPORT);
dadangjia 0:ef802f772981 133 server.listen();
dadangjia 0:ef802f772981 134 while (1) {
dadangjia 0:ef802f772981 135 pc.printf("\nWait for new connection...\r\n");
dadangjia 0:ef802f772981 136 TCPSocketConnection client;
dadangjia 0:ef802f772981 137 server.accept(client);
dadangjia 0:ef802f772981 138 client.set_blocking(false, 0);
dadangjia 0:ef802f772981 139 pc.printf("Connection from: %s\r\n", client.get_address());
dadangjia 0:ef802f772981 140 sendStrXY("C:",3,0);
dadangjia 0:ef802f772981 141 sendStrXY(client.get_address(),3,3);
dadangjia 0:ef802f772981 142 while (client.is_connected() == true) {
dadangjia 0:ef802f772981 143 myled2=0;
dadangjia 1:cc2094da451b 144 memset(buffer,'\0',sizeof(buffer));
dadangjia 0:ef802f772981 145 int n = client.receive(buffer, sizeof(buffer));
dadangjia 1:cc2094da451b 146
dadangjia 0:ef802f772981 147 if(n > 0)
dadangjia 0:ef802f772981 148 {
dadangjia 0:ef802f772981 149 //client.send_all(buffer, n); //将接受的数据发送回去
dadangjia 0:ef802f772981 150 if(strstr(buffer,"AD"))
dadangjia 0:ef802f772981 151 client.send(buffer1, sizeof(buffer1));
dadangjia 0:ef802f772981 152
dadangjia 0:ef802f772981 153 if(strstr(buffer,"AM"))
dadangjia 0:ef802f772981 154 client.send_all(buffer2, sizeof(buffer2));
dadangjia 0:ef802f772981 155
dadangjia 0:ef802f772981 156 if(strstr(buffer,"BE"))
dadangjia 0:ef802f772981 157 client.send(buffer3, sizeof(buffer3));
dadangjia 0:ef802f772981 158
dadangjia 0:ef802f772981 159 if(strstr(buffer,"LED"))
dadangjia 1:cc2094da451b 160 {
dadangjia 0:ef802f772981 161 myled3=!myled3;
dadangjia 1:cc2094da451b 162 }
dadangjia 1:cc2094da451b 163
dadangjia 1:cc2094da451b 164 if(strstr(buffer,"led"))
dadangjia 1:cc2094da451b 165 {
dadangjia 1:cc2094da451b 166 myled5=!myled5;
dadangjia 1:cc2094da451b 167 }
dadangjia 0:ef802f772981 168
dadangjia 0:ef802f772981 169 if(strstr(buffer,"ALL"))
dadangjia 0:ef802f772981 170 {
dadangjia 1:cc2094da451b 171 snprintf(Buffer,sizeof(Buffer), " %s\r\n %s\r\n %s\r\n led3:%d led5:%d\r\n",buffer1,buffer2,buffer3,!myled3,!myled5);
dadangjia 0:ef802f772981 172 client.send(Buffer, sizeof(Buffer));
dadangjia 0:ef802f772981 173 }
dadangjia 0:ef802f772981 174
dadangjia 0:ef802f772981 175 if(strstr(buffer,"IP"))
dadangjia 0:ef802f772981 176 {
dadangjia 0:ef802f772981 177 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 178 client.send(Buffer, sizeof(Buffer));
dadangjia 1:cc2094da451b 179 }
dadangjia 1:cc2094da451b 180 if(strstr(buffer,"NET"))
dadangjia 1:cc2094da451b 181 {
dadangjia 1:cc2094da451b 182 snprintf(Buffer,sizeof(Buffer), " Network: %s\r\n Gateway: %s\r\n",ethernet.getNetworkMask(), ethernet.getGateway());
dadangjia 1:cc2094da451b 183 client.send(Buffer, sizeof(Buffer));
dadangjia 1:cc2094da451b 184 }
dadangjia 0:ef802f772981 185 }
dadangjia 0:ef802f772981 186 if(client.is_fin_received())
dadangjia 0:ef802f772981 187 client.close();
dadangjia 0:ef802f772981 188 }
dadangjia 0:ef802f772981 189 pc.printf("Disconnected.\r\n");
dadangjia 0:ef802f772981 190 sendStrXY(bufferclear,3,0);
dadangjia 0:ef802f772981 191 //sendStrXY(bufferclear,7,0);
dadangjia 0:ef802f772981 192 myled2=1;
dadangjia 0:ef802f772981 193 }
dadangjia 0:ef802f772981 194 }
dadangjia 0:ef802f772981 195
dadangjia 1:cc2094da451b 196 void rxCallback() {
dadangjia 1:cc2094da451b 197 myled4 = 0;
dadangjia 1:cc2094da451b 198 while (pc.readable()) {
dadangjia 1:cc2094da451b 199 switch(pc.getc())
dadangjia 1:cc2094da451b 200 {
dadangjia 1:cc2094da451b 201 case '0':pc.printf("%s\r\n",buffer1);break;
dadangjia 1:cc2094da451b 202 case '1':pc.printf("%s\r\n",buffer2);break;
dadangjia 1:cc2094da451b 203 case '2':pc.printf("%s\r\n",buffer3);break;
dadangjia 1:cc2094da451b 204 case '3':pc.printf("%s\r\n",Buffer);break;
dadangjia 1:cc2094da451b 205 case 'O':myled3=0;pc.printf("LED3 ON\r\n");break;
dadangjia 1:cc2094da451b 206 case 'F':myled3=1;pc.printf("LED3 OFF\r\n");break;
dadangjia 1:cc2094da451b 207 case 'o':myled5=0;pc.printf("LED5 ON\r\n");break;
dadangjia 1:cc2094da451b 208 case 'f':myled5=1;pc.printf("LED5 OFF\r\n");break;
dadangjia 1:cc2094da451b 209 case 'P':pc.printf(" Network: %s\r\n Gateway: %s\r\n"
dadangjia 1:cc2094da451b 210 " MAC_Addr: %s\r\n LocalIp: %s\r\n",
dadangjia 1:cc2094da451b 211 ethernet.getNetworkMask(), ethernet.getGateway(),
dadangjia 1:cc2094da451b 212 ethernet.getMACAddress(),ethernet.getIPAddress());break;
dadangjia 1:cc2094da451b 213 default:break;
dadangjia 1:cc2094da451b 214 }
dadangjia 1:cc2094da451b 215 }
dadangjia 1:cc2094da451b 216 myled4 = 1;
dadangjia 1:cc2094da451b 217 }
dadangjia 0:ef802f772981 218
dadangjia 0:ef802f772981 219 int main() {
dadangjia 1:cc2094da451b 220 pc.printf("LPC824_W5500_SD1306_AM2320_BMP280 Demo\r\n");
dadangjia 0:ef802f772981 221 oled_init();
dadangjia 1:cc2094da451b 222 pc.attach(&rxCallback, Serial::RxIrq);
dadangjia 1:cc2094da451b 223 // wait(5);
dadangjia 0:ef802f772981 224 while(1)
dadangjia 0:ef802f772981 225 {
dadangjia 0:ef802f772981 226 w5500_httpserver();
dadangjia 0:ef802f772981 227 }
dadangjia 0:ef802f772981 228 }