tx sensor data using esp8266

Dependencies:   ESP8266 MPU6050 ledControl2 mbed

Fork of Nucleo_dht11 by Mangnimit MCU

Committer:
shreyashpalande
Date:
Tue Jan 24 04:40:39 2017 +0000
Revision:
1:194423bc188a
Parent:
0:8fe7d36af056
wifi tx for sensors

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JENG 0:8fe7d36af056 1 #include "mbed.h"
shreyashpalande 1:194423bc188a 2 #include "ESP8266.h"
shreyashpalande 1:194423bc188a 3 #include "string.h"
shreyashpalande 1:194423bc188a 4 #include "MPU6050.h"
shreyashpalande 1:194423bc188a 5 #include "ledControl.h"
shreyashpalande 1:194423bc188a 6 Serial pc(USBTX, USBRX);
shreyashpalande 1:194423bc188a 7 DigitalOut myled(LED1);
JENG 0:8fe7d36af056 8
JENG 0:8fe7d36af056 9 #define DHTLIB_OK 0
JENG 0:8fe7d36af056 10 #define DHTLIB_ERROR_CHECKSUM -1
JENG 0:8fe7d36af056 11 #define DHTLIB_ERROR_TIMEOUT -2
JENG 0:8fe7d36af056 12
JENG 0:8fe7d36af056 13 Timer tmr;
JENG 0:8fe7d36af056 14
shreyashpalande 1:194423bc188a 15 DigitalInOut data_pin(A0);
JENG 0:8fe7d36af056 16
JENG 0:8fe7d36af056 17 int humidity;
JENG 0:8fe7d36af056 18 int temperature;
shreyashpalande 1:194423bc188a 19 ESP8266 esp(PTC4,PTC3,115200);
shreyashpalande 1:194423bc188a 20 int x,y,z;
shreyashpalande 1:194423bc188a 21 char rcv[1000],acc[1000],temp[100],hum[100];
shreyashpalande 1:194423bc188a 22 char rs[10000];
shreyashpalande 1:194423bc188a 23 int i;
shreyashpalande 1:194423bc188a 24 MPU6050 mpu6050; // class: MPU6050, object: mpu6050
shreyashpalande 1:194423bc188a 25 Ticker toggler1;
shreyashpalande 1:194423bc188a 26 Ticker filter;
shreyashpalande 1:194423bc188a 27
shreyashpalande 1:194423bc188a 28 void toggle_led1();
shreyashpalande 1:194423bc188a 29 void toggle_led2();
shreyashpalande 1:194423bc188a 30 void compFilter();
shreyashpalande 1:194423bc188a 31
shreyashpalande 1:194423bc188a 32 float pitchAngle = 0;
shreyashpalande 1:194423bc188a 33 float rollAngle = 0;
JENG 0:8fe7d36af056 34
JENG 0:8fe7d36af056 35 //########################################
JENG 0:8fe7d36af056 36 // DHT11 Library
JENG 0:8fe7d36af056 37 //########################################
JENG 0:8fe7d36af056 38 int dht_read(void){
JENG 0:8fe7d36af056 39
JENG 0:8fe7d36af056 40 // BUFFER TO RECEIVE
JENG 0:8fe7d36af056 41 uint8_t bits[5];
JENG 0:8fe7d36af056 42 uint8_t cnt = 7;
JENG 0:8fe7d36af056 43 uint8_t idx = 0;
JENG 0:8fe7d36af056 44
JENG 0:8fe7d36af056 45 tmr.stop();
JENG 0:8fe7d36af056 46 tmr.reset();
JENG 0:8fe7d36af056 47
JENG 0:8fe7d36af056 48 // EMPTY BUFFER
JENG 0:8fe7d36af056 49 for(int i=0; i< 5; i++) bits[i] = 0;
JENG 0:8fe7d36af056 50
JENG 0:8fe7d36af056 51 // REQUEST SAMPLE
JENG 0:8fe7d36af056 52 data_pin.output();
JENG 0:8fe7d36af056 53 data_pin.write(0);
JENG 0:8fe7d36af056 54 wait_ms(18);
JENG 0:8fe7d36af056 55 data_pin.write(1);
JENG 0:8fe7d36af056 56 wait_us(40);
JENG 0:8fe7d36af056 57 data_pin.input();
JENG 0:8fe7d36af056 58
JENG 0:8fe7d36af056 59 // ACKNOWLEDGE or TIMEOUT
JENG 0:8fe7d36af056 60 unsigned int loopCnt = 10000;
JENG 0:8fe7d36af056 61
JENG 0:8fe7d36af056 62 while(!data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
JENG 0:8fe7d36af056 63
JENG 0:8fe7d36af056 64 loopCnt = 10000;
JENG 0:8fe7d36af056 65
JENG 0:8fe7d36af056 66 while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
JENG 0:8fe7d36af056 67
JENG 0:8fe7d36af056 68 // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
JENG 0:8fe7d36af056 69 for(int i=0; i<40; i++){
JENG 0:8fe7d36af056 70
JENG 0:8fe7d36af056 71 loopCnt = 10000;
JENG 0:8fe7d36af056 72
JENG 0:8fe7d36af056 73 while(!data_pin.read())if(loopCnt-- == 0)return DHTLIB_ERROR_TIMEOUT;
JENG 0:8fe7d36af056 74
JENG 0:8fe7d36af056 75 //unsigned long t = micros();
JENG 0:8fe7d36af056 76 tmr.start();
JENG 0:8fe7d36af056 77
JENG 0:8fe7d36af056 78 loopCnt = 10000;
JENG 0:8fe7d36af056 79
JENG 0:8fe7d36af056 80 while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
JENG 0:8fe7d36af056 81
JENG 0:8fe7d36af056 82 if(tmr.read_us() > 40) bits[idx] |= (1 << cnt);
JENG 0:8fe7d36af056 83
JENG 0:8fe7d36af056 84 tmr.stop();
JENG 0:8fe7d36af056 85 tmr.reset();
JENG 0:8fe7d36af056 86
JENG 0:8fe7d36af056 87 if(cnt == 0){ // next byte?
JENG 0:8fe7d36af056 88
JENG 0:8fe7d36af056 89 cnt = 7; // restart at MSB
JENG 0:8fe7d36af056 90 idx++; // next byte!
JENG 0:8fe7d36af056 91
JENG 0:8fe7d36af056 92 }else cnt--;
JENG 0:8fe7d36af056 93
JENG 0:8fe7d36af056 94 }
JENG 0:8fe7d36af056 95
JENG 0:8fe7d36af056 96 // WRITE TO RIGHT VARS
JENG 0:8fe7d36af056 97 // as bits[1] and bits[3] are allways zero they are omitted in formulas.
JENG 0:8fe7d36af056 98 humidity = bits[0];
JENG 0:8fe7d36af056 99 temperature = bits[2];
JENG 0:8fe7d36af056 100
JENG 0:8fe7d36af056 101 uint8_t sum = bits[0] + bits[2];
JENG 0:8fe7d36af056 102
JENG 0:8fe7d36af056 103 if(bits[4] != sum)return DHTLIB_ERROR_CHECKSUM;
JENG 0:8fe7d36af056 104
JENG 0:8fe7d36af056 105 return DHTLIB_OK;
JENG 0:8fe7d36af056 106
JENG 0:8fe7d36af056 107 }
JENG 0:8fe7d36af056 108
JENG 0:8fe7d36af056 109 char buffer[17];
JENG 0:8fe7d36af056 110
JENG 0:8fe7d36af056 111 //########################################
JENG 0:8fe7d36af056 112 // End of DHT11 Library
JENG 0:8fe7d36af056 113 //########################################
JENG 0:8fe7d36af056 114
JENG 0:8fe7d36af056 115 int main(void){
shreyashpalande 1:194423bc188a 116
shreyashpalande 1:194423bc188a 117 pc.baud(9600); // baud rate: 9600
shreyashpalande 1:194423bc188a 118 mpu6050.whoAmI(); // Communication test: WHO_AM_I register reading
shreyashpalande 1:194423bc188a 119 wait(1);
shreyashpalande 1:194423bc188a 120 mpu6050.calibrate(accelBias,gyroBias); // Calibrate MPU6050 and load biases into bias registers
shreyashpalande 1:194423bc188a 121 pc.printf("Calibration is completed. \r\n");
shreyashpalande 1:194423bc188a 122 wait(0.5);
shreyashpalande 1:194423bc188a 123 mpu6050.init(); // Initialize the sensor
shreyashpalande 1:194423bc188a 124 wait(1);
shreyashpalande 1:194423bc188a 125 pc.printf("MPU6050 is initialized for operation.. \r\n\r\n");
shreyashpalande 1:194423bc188a 126 wait_ms(500);
JENG 0:8fe7d36af056 127
shreyashpalande 1:194423bc188a 128
shreyashpalande 1:194423bc188a 129 pc.printf("Receiving Wifi List\r\n");
shreyashpalande 1:194423bc188a 130 esp.GetList(rcv);
shreyashpalande 1:194423bc188a 131 pc.printf("%s", rcv);
shreyashpalande 1:194423bc188a 132 wait(7);
shreyashpalande 1:194423bc188a 133
shreyashpalande 1:194423bc188a 134 pc.printf("Connecting to AP\r\n");
shreyashpalande 1:194423bc188a 135 esp.Join("esp_123", "1234test"); // Replace MyAP and MyPasswd with your SSID and password
shreyashpalande 1:194423bc188a 136 esp.RcvReply(rcv, 1000);
shreyashpalande 1:194423bc188a 137 pc.printf("%s", rcv);
shreyashpalande 1:194423bc188a 138 wait(10);
shreyashpalande 1:194423bc188a 139
shreyashpalande 1:194423bc188a 140 pc.printf("Getting IP\r\n");
shreyashpalande 1:194423bc188a 141 esp.GetIP(rcv);
shreyashpalande 1:194423bc188a 142 pc.printf("%s", rcv);
shreyashpalande 1:194423bc188a 143 wait(10);
shreyashpalande 1:194423bc188a 144
shreyashpalande 1:194423bc188a 145 pc.printf("Setting Mode=1\r\n");
shreyashpalande 1:194423bc188a 146 esp.SetMode(1);
shreyashpalande 1:194423bc188a 147 esp.RcvReply(rcv, 1000);
shreyashpalande 1:194423bc188a 148 pc.printf("%s", rcv);
shreyashpalande 1:194423bc188a 149 wait(5);
shreyashpalande 1:194423bc188a 150
shreyashpalande 1:194423bc188a 151 pc.printf("Setting AT+CIPMUX=1\r\n");
shreyashpalande 1:194423bc188a 152 esp.SetMultiple();
shreyashpalande 1:194423bc188a 153 esp.RcvReply(rcv, 1000);
shreyashpalande 1:194423bc188a 154 pc.printf("%s", rcv);
shreyashpalande 1:194423bc188a 155 wait(5);
JENG 0:8fe7d36af056 156
shreyashpalande 1:194423bc188a 157 pc.printf("Starting client mode\r\n");
shreyashpalande 1:194423bc188a 158 esp.CloseServerMode();
shreyashpalande 1:194423bc188a 159 esp.RcvReply(rcv, 1000);
shreyashpalande 1:194423bc188a 160 pc.printf("%s", rcv);
shreyashpalande 1:194423bc188a 161 wait(5);
shreyashpalande 1:194423bc188a 162
shreyashpalande 1:194423bc188a 163 pc.printf("Getting Connection Status\r\n");
shreyashpalande 1:194423bc188a 164 esp.GetConnStatus(rcv);
shreyashpalande 1:194423bc188a 165 pc.printf("%s", rcv);
shreyashpalande 1:194423bc188a 166
shreyashpalande 1:194423bc188a 167
shreyashpalande 1:194423bc188a 168
shreyashpalande 1:194423bc188a 169
shreyashpalande 1:194423bc188a 170 //lcd_initial();
shreyashpalande 1:194423bc188a 171
shreyashpalande 1:194423bc188a 172 // lcd_puts("=Nucleo - DHT11=");
JENG 0:8fe7d36af056 173
shreyashpalande 1:194423bc188a 174 //for(;;){
shreyashpalande 1:194423bc188a 175 while(1){
shreyashpalande 1:194423bc188a 176 strcpy(rs, "AT+CIPSTART=1,\"TCP\",\"192.168.4.1\",333\r\n");
shreyashpalande 1:194423bc188a 177 esp.SendCMD(rs);
shreyashpalande 1:194423bc188a 178 esp.RcvReply(rcv, 2000);
shreyashpalande 1:194423bc188a 179 pc.printf("%s", rcv);
shreyashpalande 1:194423bc188a 180 wait(0.1);
JENG 0:8fe7d36af056 181
shreyashpalande 1:194423bc188a 182 myled = !myled;
shreyashpalande 1:194423bc188a 183 strcpy(rs, "AT+CIPSEND=1,57\r\n");
shreyashpalande 1:194423bc188a 184 esp.SendCMD(rs);
shreyashpalande 1:194423bc188a 185 esp.RcvReply(rcv, 1000);
shreyashpalande 1:194423bc188a 186 pc.printf("%s", rcv);
shreyashpalande 1:194423bc188a 187 wait(0.1);
shreyashpalande 1:194423bc188a 188
shreyashpalande 1:194423bc188a 189
shreyashpalande 1:194423bc188a 190
shreyashpalande 1:194423bc188a 191 //pc.printf(" _______________\r\n");
shreyashpalande 1:194423bc188a 192 //pc.printf("| Pitch: %.3f degree \r\n",pitchAngle);
shreyashpalande 1:194423bc188a 193 //pc.printf("| Roll: %.3f degree \r\n",rollAngle);
shreyashpalande 1:194423bc188a 194 //pc.printf("|_______________\r\n\r\n");
shreyashpalande 1:194423bc188a 195 //pc.printf("| Accelerometer(g) | ax=%.3f | ay=%.3f | az=%.3f \r\n",ax,ay,az);
shreyashpalande 1:194423bc188a 196 //pc.printf("| Gyroscope(deg/s) | gx=%.3f | gy=%.3f | gz=%.3f \r\n",gx,gy,gz);
shreyashpalande 1:194423bc188a 197 //wait(1);
shreyashpalande 1:194423bc188a 198
shreyashpalande 1:194423bc188a 199
shreyashpalande 1:194423bc188a 200
JENG 0:8fe7d36af056 201 if(!dht_read()){
JENG 0:8fe7d36af056 202
shreyashpalande 1:194423bc188a 203 sprintf(buffer, "T %2d H %2d%", temperature,humidity);
shreyashpalande 1:194423bc188a 204 //pc.printf("%s \n\r", buffer);
shreyashpalande 1:194423bc188a 205 // pc.printf(" \n\r ");
JENG 0:8fe7d36af056 206 //lcd_putcmd(0x80);
JENG 0:8fe7d36af056 207 //lcd_puts(buffer);
shreyashpalande 1:194423bc188a 208 //sprintf(buffer, "H %2d%", humidity);
shreyashpalande 1:194423bc188a 209 //lcd_putcmd(0xc0);
shreyashpalande 1:194423bc188a 210 //pc.printf("%s%% \n\r", buffer);
shreyashpalande 1:194423bc188a 211 //pc.printf(" \n\r ");
shreyashpalande 1:194423bc188a 212 //lcd_puts(buffer);
shreyashpalande 1:194423bc188a 213 //wait(0.5);
JENG 0:8fe7d36af056 214
shreyashpalande 1:194423bc188a 215 }
shreyashpalande 1:194423bc188a 216
shreyashpalande 1:194423bc188a 217 else
shreyashpalande 1:194423bc188a 218 {
shreyashpalande 1:194423bc188a 219 //lcd_putcmd(0x80);
shreyashpalande 1:194423bc188a 220 // lcd_puts("Sensor Error !!!");
shreyashpalande 1:194423bc188a 221 //pc.printf("!!!!Sensor Error!!! \n\r");
shreyashpalande 1:194423bc188a 222 //lcd_putcmd(0xc0);
shreyashpalande 1:194423bc188a 223 //lcd_puts(" ");
JENG 0:8fe7d36af056 224
JENG 0:8fe7d36af056 225 }
shreyashpalande 1:194423bc188a 226 filter.attach(&compFilter, 0.005); // Call the complementaryFilter func. every 5 ms (200 Hz sampling period)
shreyashpalande 1:194423bc188a 227 //sprintf(acc,"a x= %f y= %f z=%f ", ax,ay,az);
shreyashpalande 1:194423bc188a 228 //sprintf(prs,"s w = %d",w);
shreyashpalande 1:194423bc188a 229 //strcat(acc,buffer);
shreyashpalande 1:194423bc188a 230 sprintf(acc,"a ax= %.3f ay= %.3f az=%.3f P %.3f R %.3f", ax,ay,az,pitchAngle,rollAngle);
shreyashpalande 1:194423bc188a 231 strcat(acc,buffer);
shreyashpalande 1:194423bc188a 232 strcpy(rs,acc);
shreyashpalande 1:194423bc188a 233 esp.SendCMD(rs);
shreyashpalande 1:194423bc188a 234 pc.printf("%s", rs);
shreyashpalande 1:194423bc188a 235 esp.RcvReply(rcv, 1000);
shreyashpalande 1:194423bc188a 236 pc.printf("%s", rcv);
shreyashpalande 1:194423bc188a 237 wait(0.1);
shreyashpalande 1:194423bc188a 238
shreyashpalande 1:194423bc188a 239
shreyashpalande 1:194423bc188a 240 strcpy(rs, "AT+CIPCLOSE=1");
shreyashpalande 1:194423bc188a 241 esp.SendCMD(rs);
shreyashpalande 1:194423bc188a 242 esp.RcvReply(rcv, 2000);
shreyashpalande 1:194423bc188a 243 pc.printf("%s", rcv);
shreyashpalande 1:194423bc188a 244
shreyashpalande 1:194423bc188a 245 wait(0.1);
shreyashpalande 1:194423bc188a 246
shreyashpalande 1:194423bc188a 247 // wait(1.5);
JENG 0:8fe7d36af056 248
JENG 0:8fe7d36af056 249 }
JENG 0:8fe7d36af056 250
shreyashpalande 1:194423bc188a 251
JENG 0:8fe7d36af056 252 }
shreyashpalande 1:194423bc188a 253
shreyashpalande 1:194423bc188a 254 void toggle_led1() {ledToggle(1);}
shreyashpalande 1:194423bc188a 255 void toggle_led2() {ledToggle(2);}
shreyashpalande 1:194423bc188a 256
shreyashpalande 1:194423bc188a 257 /* This function is created to avoid address error that caused from Ticker.attach func */
shreyashpalande 1:194423bc188a 258 void compFilter() {mpu6050.complementaryFilter(&pitchAngle, &rollAngle);}