Example of using the ATT M2X back end. Connect over Wifi with the ESP8266 chip. This code sends random data to M2X. This code can be used for Any board, just change the RX/TX pins the wifi adapter is connected to.
Dependencies: ESP8266Interface M2XStreamClient jsonlite mbed
Fork of M2X_K64F_Accel_ESP8266-wifi by
main.cpp@4:3c1d712dcc48, 2015-12-11 (annotated)
- Committer:
- mbedAustin
- Date:
- Fri Dec 11 00:25:01 2015 +0000
- Revision:
- 4:3c1d712dcc48
- Parent:
- 3:694a1a67b156
- Child:
- 5:b3bcb048a5a5
made default wifi pins match K64F
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jb8414 | 0:ece599ab76bb | 1 | #include "mbed.h" |
jb8414 | 0:ece599ab76bb | 2 | #include "FXOS8700Q.h" |
jb8414 | 0:ece599ab76bb | 3 | #include "M2XStreamClient.h" |
mbedAustin | 3:694a1a67b156 | 4 | #include "ESP8266Interface.h" |
mbedAustin | 3:694a1a67b156 | 5 | #include "TCPSocketConnection.h" |
jb8414 | 0:ece599ab76bb | 6 | |
jb8414 | 0:ece599ab76bb | 7 | |
jb8414 | 0:ece599ab76bb | 8 | #ifndef MAX |
jb8414 | 0:ece599ab76bb | 9 | #define max(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a > _b ? _a : _b; }) |
jb8414 | 0:ece599ab76bb | 10 | #endif |
jb8414 | 0:ece599ab76bb | 11 | |
jb8414 | 0:ece599ab76bb | 12 | #ifndef MIN |
jb8414 | 0:ece599ab76bb | 13 | #define min(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a < _b ? _a : _b; }) |
jb8414 | 0:ece599ab76bb | 14 | #endif |
jb8414 | 0:ece599ab76bb | 15 | |
jb8414 | 1:c7b9e522cc8e | 16 | char deviceId[] = "<device id>"; // Device you want to push to |
jb8414 | 1:c7b9e522cc8e | 17 | char streamName[] = "<stream name>"; // Stream you want to push to |
jb8414 | 1:c7b9e522cc8e | 18 | char m2xKey[] = "<m2x api key>"; // Your M2X API Key or Master API Key |
jb8414 | 0:ece599ab76bb | 19 | |
mbedAustin | 3:694a1a67b156 | 20 | /* |
mbedAustin | 4:3c1d712dcc48 | 21 | * ESP8266 Wifi Config, connect it to D0 on Seeed Grove shield |
mbedAustin | 3:694a1a67b156 | 22 | */ |
mbedAustin | 4:3c1d712dcc48 | 23 | ESP8266Interface wifi(D1,D0,D3,"wifi-name","wifi-password",115200); // TX,RX,Reset,SSID,Password,Baud |
mbedAustin | 3:694a1a67b156 | 24 | |
jb8414 | 0:ece599ab76bb | 25 | int main() |
jb8414 | 0:ece599ab76bb | 26 | { |
mbedAustin | 3:694a1a67b156 | 27 | printf("Starting...\r\n"); |
mbedAustin | 3:694a1a67b156 | 28 | |
mbedAustin | 3:694a1a67b156 | 29 | // connect to wifi |
mbedAustin | 3:694a1a67b156 | 30 | wifi.init(); //Reset |
mbedAustin | 3:694a1a67b156 | 31 | wifi.connect(); //Use DHCP |
mbedAustin | 3:694a1a67b156 | 32 | printf("IP Address is %s \n\r", wifi.getIPAddress()); |
jb8414 | 0:ece599ab76bb | 33 | |
jb8414 | 0:ece599ab76bb | 34 | // Initialize the M2X client |
jb8414 | 0:ece599ab76bb | 35 | Client client; |
mbedAustin | 3:694a1a67b156 | 36 | M2XStreamClient m2xClient(&client, m2xKey,1,"52.22.150.98"); |
jb8414 | 0:ece599ab76bb | 37 | int ret; |
jb8414 | 0:ece599ab76bb | 38 | |
jb8414 | 0:ece599ab76bb | 39 | // Create an accelerometer instance |
jb8414 | 0:ece599ab76bb | 40 | FXOS8700Q_acc acc( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board |
jb8414 | 0:ece599ab76bb | 41 | acc.enable(); |
jb8414 | 0:ece599ab76bb | 42 | printf("\r\n\nFXOS8700Q Who Am I= %X\r\n", acc.whoAmI()); |
jb8414 | 0:ece599ab76bb | 43 | |
jb8414 | 0:ece599ab76bb | 44 | while (true) { |
jb8414 | 0:ece599ab76bb | 45 | |
jb8414 | 0:ece599ab76bb | 46 | float x, y, z; |
jb8414 | 0:ece599ab76bb | 47 | acc.getX(&x); |
jb8414 | 0:ece599ab76bb | 48 | acc.getY(&y); |
jb8414 | 0:ece599ab76bb | 49 | acc.getZ(&z); |
jb8414 | 0:ece599ab76bb | 50 | |
jb8414 | 0:ece599ab76bb | 51 | printf("Accel X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); |
jb8414 | 0:ece599ab76bb | 52 | |
jb8414 | 0:ece599ab76bb | 53 | // Calculate pitch and roll. Find the maximum tilt angle. |
jb8414 | 0:ece599ab76bb | 54 | float pitch = atan(x / sqrt(y * y + z * z)); |
jb8414 | 0:ece599ab76bb | 55 | float roll = atan(y / sqrt(x * x + z * z)); |
jb8414 | 0:ece599ab76bb | 56 | float maxTilt = |
jb8414 | 0:ece599ab76bb | 57 | max(abs(roll), abs(pitch)) * 180.0 / 3.14159; |
jb8414 | 0:ece599ab76bb | 58 | printf("pitch: %5.1f roll: %5.1f maxTilt: %5.1f\n\r", |
jb8414 | 0:ece599ab76bb | 59 | pitch, roll, maxTilt); |
jb8414 | 0:ece599ab76bb | 60 | |
jb8414 | 0:ece599ab76bb | 61 | // If the maximum title is over 20 degrees, then send |
jb8414 | 0:ece599ab76bb | 62 | // data to stream |
mbedAustin | 3:694a1a67b156 | 63 | |
jb8414 | 1:c7b9e522cc8e | 64 | ret = m2xClient.updateStreamValue(deviceId, streamName, maxTilt); |
jb8414 | 0:ece599ab76bb | 65 | printf("send() returned %d\r\n", ret); |
jb8414 | 2:68b759c89fd9 | 66 | wait(1.0); |
mbedAustin | 3:694a1a67b156 | 67 | |
jb8414 | 0:ece599ab76bb | 68 | } |
jb8414 | 0:ece599ab76bb | 69 | } |