tx sensor data using esp8266
Dependencies: ESP8266 MPU6050 ledControl2 mbed
Fork of Nucleo_dht11 by
Revision 1:194423bc188a, committed 2017-01-24
- Comitter:
- shreyashpalande
- Date:
- Tue Jan 24 04:40:39 2017 +0000
- Parent:
- 0:8fe7d36af056
- Commit message:
- wifi tx for sensors
Changed in this revision
diff -r 8fe7d36af056 -r 194423bc188a ESP8266.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ESP8266.lib Tue Jan 24 04:40:39 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/quevedo/code/ESP8266/#77388e8f0697
diff -r 8fe7d36af056 -r 194423bc188a MPU6050.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MPU6050.lib Tue Jan 24 04:40:39 2017 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/BaserK/code/MPU6050/#5b90f2b5e6d9
diff -r 8fe7d36af056 -r 194423bc188a ledControl.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ledControl.lib Tue Jan 24 04:40:39 2017 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/BaserK/code/ledControl2/#1655ee0ec2ca
diff -r 8fe7d36af056 -r 194423bc188a main.cpp --- a/main.cpp Mon Mar 17 12:35:48 2014 +0000 +++ b/main.cpp Tue Jan 24 04:40:39 2017 +0000 @@ -1,102 +1,10 @@ - #include "mbed.h" - -DigitalOut LCD_RS(D8); -DigitalOut LCD_EN(D9); -DigitalOut LCD_D4(D4); -DigitalOut LCD_D5(D5); -DigitalOut LCD_D6(D6); -DigitalOut LCD_D7(D7); - - -//######################################## -// LCD Library -//######################################## - -//send data to lcd -void write_lcd(uint8_t d){ - - LCD_D4 = (d & 0x10)?1:0; - LCD_D5 = (d & 0x20)?1:0; - LCD_D6 = (d & 0x40)?1:0; - LCD_D7 = (d & 0x80)?1:0; - - wait_us(2); - LCD_EN = 1; - wait_us(2); - LCD_EN = 0; - -} - -//lcd write byte -void lcd_write_byte(uint8_t byte){ - - write_lcd(byte); - write_lcd(byte << 4); - -} - -//write command -void lcd_putcmd(uint8_t c){ - - wait_ms(5); - lcd_write_byte(c); - -} - -//write one char -void lcd_putc(uint8_t d){ - - wait_ms(1); - LCD_RS = 1; - lcd_write_byte(d); - LCD_RS = 0; - -} - - - -//print string -void lcd_puts(char * p){ - - while(* p)lcd_putc(* p++); - -} - -//lcd initial interface -void lcd_initial(void){ - - uint8_t x = 3; - - LCD_RS = 0; - LCD_EN = 0; - - LCD_D4 = 0; - LCD_D5 = 0; - LCD_D6 = 0; - LCD_D7 = 0; - - wait_ms(50); - - while(x--){ - - write_lcd(0x30); - wait_ms(5); - - } - - write_lcd(0x20); //4bit interface - - lcd_putcmd(0x28); //function set - lcd_putcmd(0x08); //display off - lcd_putcmd(0x01); //display clear - lcd_putcmd(0x06); //entry mode set - lcd_putcmd(0x0c); //display on - -} -//######################################## -// End of LCD Library -//######################################## +#include "ESP8266.h" +#include "string.h" +#include "MPU6050.h" +#include "ledControl.h" +Serial pc(USBTX, USBRX); +DigitalOut myled(LED1); #define DHTLIB_OK 0 #define DHTLIB_ERROR_CHECKSUM -1 @@ -104,10 +12,25 @@ Timer tmr; -DigitalInOut data_pin(A1); +DigitalInOut data_pin(A0); int humidity; int temperature; +ESP8266 esp(PTC4,PTC3,115200); +int x,y,z; +char rcv[1000],acc[1000],temp[100],hum[100]; +char rs[10000]; +int i; +MPU6050 mpu6050; // class: MPU6050, object: mpu6050 +Ticker toggler1; +Ticker filter; + +void toggle_led1(); +void toggle_led2(); +void compFilter(); + +float pitchAngle = 0; +float rollAngle = 0; //######################################## // DHT11 Library @@ -190,32 +113,146 @@ //######################################## int main(void){ + + pc.baud(9600); // baud rate: 9600 + mpu6050.whoAmI(); // Communication test: WHO_AM_I register reading + wait(1); + mpu6050.calibrate(accelBias,gyroBias); // Calibrate MPU6050 and load biases into bias registers + pc.printf("Calibration is completed. \r\n"); + wait(0.5); + mpu6050.init(); // Initialize the sensor + wait(1); + pc.printf("MPU6050 is initialized for operation.. \r\n\r\n"); + wait_ms(500); - lcd_initial(); + + pc.printf("Receiving Wifi List\r\n"); + esp.GetList(rcv); + pc.printf("%s", rcv); + wait(7); + + pc.printf("Connecting to AP\r\n"); + esp.Join("esp_123", "1234test"); // Replace MyAP and MyPasswd with your SSID and password + esp.RcvReply(rcv, 1000); + pc.printf("%s", rcv); + wait(10); + + pc.printf("Getting IP\r\n"); + esp.GetIP(rcv); + pc.printf("%s", rcv); + wait(10); + + pc.printf("Setting Mode=1\r\n"); + esp.SetMode(1); + esp.RcvReply(rcv, 1000); + pc.printf("%s", rcv); + wait(5); + + pc.printf("Setting AT+CIPMUX=1\r\n"); + esp.SetMultiple(); + esp.RcvReply(rcv, 1000); + pc.printf("%s", rcv); + wait(5); - lcd_puts("=Nucleo - DHT11="); + pc.printf("Starting client mode\r\n"); + esp.CloseServerMode(); + esp.RcvReply(rcv, 1000); + pc.printf("%s", rcv); + wait(5); + + pc.printf("Getting Connection Status\r\n"); + esp.GetConnStatus(rcv); + pc.printf("%s", rcv); + + + + + //lcd_initial(); + + // lcd_puts("=Nucleo - DHT11="); - for(;;){ + //for(;;){ + while(1){ + strcpy(rs, "AT+CIPSTART=1,\"TCP\",\"192.168.4.1\",333\r\n"); + esp.SendCMD(rs); + esp.RcvReply(rcv, 2000); + pc.printf("%s", rcv); + wait(0.1); + myled = !myled; + strcpy(rs, "AT+CIPSEND=1,57\r\n"); + esp.SendCMD(rs); + esp.RcvReply(rcv, 1000); + pc.printf("%s", rcv); + wait(0.1); + + + + //pc.printf(" _______________\r\n"); + //pc.printf("| Pitch: %.3f degree \r\n",pitchAngle); + //pc.printf("| Roll: %.3f degree \r\n",rollAngle); + //pc.printf("|_______________\r\n\r\n"); + //pc.printf("| Accelerometer(g) | ax=%.3f | ay=%.3f | az=%.3f \r\n",ax,ay,az); + //pc.printf("| Gyroscope(deg/s) | gx=%.3f | gy=%.3f | gz=%.3f \r\n",gx,gy,gz); + //wait(1); + + + if(!dht_read()){ - //sprintf(buffer, "Temp %2d C ", temperature); + sprintf(buffer, "T %2d H %2d%", temperature,humidity); + //pc.printf("%s \n\r", buffer); + // pc.printf(" \n\r "); //lcd_putcmd(0x80); //lcd_puts(buffer); - sprintf(buffer, "Hum %2d%% Tmp %2dc", humidity, temperature); - lcd_putcmd(0xc0); - lcd_puts(buffer); - wait(0.5); + //sprintf(buffer, "H %2d%", humidity); + //lcd_putcmd(0xc0); + //pc.printf("%s%% \n\r", buffer); + //pc.printf(" \n\r "); + //lcd_puts(buffer); + //wait(0.5); - }else{ - - lcd_putcmd(0x80); - lcd_puts("Sensor Error !!!"); - lcd_putcmd(0xc0); - lcd_puts(" "); + } + + else + { + //lcd_putcmd(0x80); + // lcd_puts("Sensor Error !!!"); + //pc.printf("!!!!Sensor Error!!! \n\r"); + //lcd_putcmd(0xc0); + //lcd_puts(" "); } + filter.attach(&compFilter, 0.005); // Call the complementaryFilter func. every 5 ms (200 Hz sampling period) + //sprintf(acc,"a x= %f y= %f z=%f ", ax,ay,az); + //sprintf(prs,"s w = %d",w); + //strcat(acc,buffer); + sprintf(acc,"a ax= %.3f ay= %.3f az=%.3f P %.3f R %.3f", ax,ay,az,pitchAngle,rollAngle); + strcat(acc,buffer); + strcpy(rs,acc); + esp.SendCMD(rs); + pc.printf("%s", rs); + esp.RcvReply(rcv, 1000); + pc.printf("%s", rcv); + wait(0.1); + + + strcpy(rs, "AT+CIPCLOSE=1"); + esp.SendCMD(rs); + esp.RcvReply(rcv, 2000); + pc.printf("%s", rcv); + + wait(0.1); + + // wait(1.5); } + } + +void toggle_led1() {ledToggle(1);} +void toggle_led2() {ledToggle(2);} + +/* This function is created to avoid address error that caused from Ticker.attach func */ +void compFilter() {mpu6050.complementaryFilter(&pitchAngle, &rollAngle);}