Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ESP8266 MMA8451Q mbed
main.cpp
00001 #include "mbed.h" 00002 #include "MMA8451Q.h" 00003 #include "ESP8266.h" 00004 00005 Serial pc(USBTX,USBRX); 00006 00007 #if defined (TARGET_KL25Z) 00008 PinName const SDA = PTE25; 00009 PinName const SCL = PTE24; 00010 #else 00011 #error TARGET NOT DEFINED 00012 #endif 00013 00014 #define MMA8451_I2C_ADDRESS (0x1d<<1) 00015 00016 00017 //wifi UART port and baud rate 00018 ESP8266 wifi(PTE0, PTE1, 115200); 00019 00020 //buffers for wifi library 00021 char resp[1000]; 00022 char http_cmd[300], comm[300]; 00023 00024 int timeout = 3000; //timeout for wifi commands 00025 00026 //SSID and password for connection 00027 #define SSID "Karkhana" 00028 #define PASS "Karkhana2018" 00029 00030 //Remote IP 00031 #define IP "184.106.153.149" // 52.54.134.167 00032 00033 00034 char* Update_Key = "VQG4N1PCODU1C4IU"; 00035 00036 float x, y, z; 00037 00038 //Wifi init function 00039 void wifi_initialize(void) 00040 { 00041 00042 pc.printf("******** Resetting wifi module ********\r\n"); 00043 wifi.Reset(); 00044 00045 //wait for 5 seconds for response, else display no response receiveed 00046 if (wifi.RcvReply(resp, 5000)) 00047 pc.printf("%s",resp); 00048 else 00049 pc.printf("No response"); 00050 00051 00052 pc.printf("******** Setting Station mode of wifi with AP ********\r\n"); 00053 wifi.SetMode(1); // set transparent mode 00054 if (wifi.RcvReply(resp, timeout)) //receive a response from ESP 00055 pc.printf("%s",resp); //Print the response onscreen 00056 else 00057 pc.printf("No response while setting mode. \r\n"); 00058 00059 00060 pc.printf("******** Joining network with SSID and PASS ********\r\n"); 00061 wifi.Join(SSID, PASS); 00062 if (wifi.RcvReply(resp, timeout)) 00063 pc.printf("%s",resp); 00064 else 00065 pc.printf("No response while connecting to network \r\n"); 00066 00067 00068 pc.printf("******** Getting IP and MAC of module ********\r\n"); 00069 wifi.GetIP(resp); 00070 if (wifi.RcvReply(resp, timeout)) 00071 pc.printf("%s",resp); 00072 else 00073 pc.printf("No response while getting IP \r\n"); 00074 00075 00076 pc.printf("******** Setting WIFI UART passthrough ********\r\n"); 00077 wifi.setTransparent(); 00078 if (wifi.RcvReply(resp, timeout)) 00079 pc.printf("%s",resp); 00080 else 00081 pc.printf("No response while setting wifi passthrough. \r\n"); 00082 wait(1); 00083 00084 00085 pc.printf("******** Setting single connection mode ********\r\n"); 00086 wifi.SetSingle(); 00087 wifi.RcvReply(resp, timeout); 00088 if (wifi.RcvReply(resp, timeout)) 00089 pc.printf("%s",resp); 00090 else 00091 pc.printf("No response while setting single connection \r\n"); 00092 wait(1); 00093 } 00094 00095 00096 void wifi_send(void) 00097 { 00098 pc.printf("******** Starting TCP connection on IP and port ********\r\n"); 00099 wifi.startTCPConn(IP,80); //cipstart 00100 wifi.RcvReply(resp, timeout); 00101 if (wifi.RcvReply(resp, timeout)) 00102 pc.printf("%s",resp); 00103 else 00104 pc.printf("No response while starting TCP connection \r\n"); 00105 wait(1); 00106 00107 //create link 00108 sprintf(http_cmd,"/update?api_key=%s&field1=%f&field2=%f&field3=%f",Update_Key,x,y,z); 00109 00110 pc.printf(http_cmd); 00111 00112 pc.printf("******** Sending URL to wifi ********\r\n"); 00113 wifi.sendURL(http_cmd, comm); //cipsend and get command 00114 if (wifi.RcvReply(resp, timeout)) 00115 pc.printf("%s",resp); 00116 else 00117 pc.printf("No response while sending URL \r\n"); 00118 } 00119 00120 00121 int main(void) 00122 { 00123 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); 00124 00125 wifi_initialize(); 00126 00127 printf("MMA8451 ID: %d\n", acc.getWhoAmI()); 00128 00129 while (true) { 00130 00131 x = abs(acc.getAccX()) * 255; 00132 y = abs(acc.getAccY()) * 255; 00133 z = abs(acc.getAccZ()) * 255; 00134 wifi_send(); 00135 wait(3); 00136 printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z); 00137 } 00138 }
Generated on Sun Jul 17 2022 09:39:37 by
1.7.2