Example of using the ATT M2X back end. Connect over Ethernet with the WIZnet 5500 chip. This code sends random data to M2X.
Dependencies: M2XStreamClient WIZnet_Library jsonlite mbed
Fork of M2X_Nucleo411_ESP8266-wifi by
main.cpp@7:748a599726d8, 2015-12-26 (annotated)
- Committer:
- sjallouli
- Date:
- Sat Dec 26 21:27:15 2015 +0000
- Revision:
- 7:748a599726d8
- Parent:
- 6:694279899cf2
Example of using the ATT M2X back end. Connect over Ethernet with the WIZnet5500 chip. This code sends random data to M2X. This code can be used for Any board.
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 "M2XStreamClient.h" |
sjallouli | 7:748a599726d8 | 3 | #include "WIZnetInterface.h" |
mbedAustin | 3:694a1a67b156 | 4 | #include "TCPSocketConnection.h" |
jb8414 | 0:ece599ab76bb | 5 | |
jb8414 | 0:ece599ab76bb | 6 | |
mbedAustin | 3:694a1a67b156 | 7 | /* |
sjallouli | 7:748a599726d8 | 8 | * WIZnet 5500 Config for nucleo 411 |
mbedAustin | 3:694a1a67b156 | 9 | */ |
sjallouli | 7:748a599726d8 | 10 | Serial pc(SERIAL_TX, SERIAL_RX); |
sjallouli | 7:748a599726d8 | 11 | SPI spi(D11, D12, D13); // mosi, miso, sclk |
sjallouli | 7:748a599726d8 | 12 | |
sjallouli | 7:748a599726d8 | 13 | #define USE_DHCP 1 |
mbedAustin | 6:694279899cf2 | 14 | |
sjallouli | 7:748a599726d8 | 15 | const char * IP_Addr = "192.168.2.72"; |
sjallouli | 7:748a599726d8 | 16 | const char * IP_Subnet = "255.255.255.0"; |
sjallouli | 7:748a599726d8 | 17 | const char * IP_Gateway = "192.168.2.2"; |
sjallouli | 7:748a599726d8 | 18 | unsigned char MAC_Addr[6] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; |
mbedAustin | 6:694279899cf2 | 19 | // |
mbedAustin | 6:694279899cf2 | 20 | // Fill these field in from you ATT M2X Account |
mbedAustin | 6:694279899cf2 | 21 | // |
mbedAustin | 6:694279899cf2 | 22 | char deviceId[] = "<deviceID>"; // Device you want to push to |
mbedAustin | 6:694279899cf2 | 23 | char streamName[] = "<streamID>"; // Stream you want to push to |
mbedAustin | 6:694279899cf2 | 24 | char m2xKey[] = "<deviceAPIKey>"; // Your M2X API Key or Master API Key |
mbedAustin | 3:694a1a67b156 | 25 | |
jb8414 | 0:ece599ab76bb | 26 | int main() |
jb8414 | 0:ece599ab76bb | 27 | { |
sjallouli | 7:748a599726d8 | 28 | pc.baud(115200); // console terminal to 115200 baud |
sjallouli | 7:748a599726d8 | 29 | |
sjallouli | 7:748a599726d8 | 30 | pc.printf("STM32 WIZnet M2X Example\r\n"); |
sjallouli | 7:748a599726d8 | 31 | |
sjallouli | 7:748a599726d8 | 32 | pc.printf("Initializing SPI\r\n"); |
sjallouli | 7:748a599726d8 | 33 | spi.frequency(1000000); |
sjallouli | 7:748a599726d8 | 34 | pc.printf("Initializing Ethernet\r\n"); |
sjallouli | 7:748a599726d8 | 35 | WIZnetInterface ethernet(&spi,D10, D3); |
sjallouli | 7:748a599726d8 | 36 | |
sjallouli | 7:748a599726d8 | 37 | #if USE_DHCP |
sjallouli | 7:748a599726d8 | 38 | pc.printf("Ethernet Init\r\n"); |
sjallouli | 7:748a599726d8 | 39 | int ret = ethernet.init(MAC_Addr); |
sjallouli | 7:748a599726d8 | 40 | #else |
sjallouli | 7:748a599726d8 | 41 | int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway); |
sjallouli | 7:748a599726d8 | 42 | #endif |
mbedAustin | 3:694a1a67b156 | 43 | |
sjallouli | 7:748a599726d8 | 44 | if (!ret) |
sjallouli | 7:748a599726d8 | 45 | { |
sjallouli | 7:748a599726d8 | 46 | pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress()); |
sjallouli | 7:748a599726d8 | 47 | ret = ethernet.connect(); |
sjallouli | 7:748a599726d8 | 48 | |
sjallouli | 7:748a599726d8 | 49 | if (!ret) |
sjallouli | 7:748a599726d8 | 50 | { |
sjallouli | 7:748a599726d8 | 51 | pc.printf("IP: %s, MASK: %s, GW: %s\r\n", |
sjallouli | 7:748a599726d8 | 52 | ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway()); |
sjallouli | 7:748a599726d8 | 53 | } |
sjallouli | 7:748a599726d8 | 54 | else |
sjallouli | 7:748a599726d8 | 55 | { |
sjallouli | 7:748a599726d8 | 56 | pc.printf("Error ethernet.connect() - ret = %d\r\n", ret); |
sjallouli | 7:748a599726d8 | 57 | exit(0); |
sjallouli | 7:748a599726d8 | 58 | } |
sjallouli | 7:748a599726d8 | 59 | } |
sjallouli | 7:748a599726d8 | 60 | else |
sjallouli | 7:748a599726d8 | 61 | { |
sjallouli | 7:748a599726d8 | 62 | pc.printf("Error ethernet.init() - ret = %d\r\n", ret); |
sjallouli | 7:748a599726d8 | 63 | exit(0); |
sjallouli | 7:748a599726d8 | 64 | } |
jb8414 | 0:ece599ab76bb | 65 | |
jb8414 | 0:ece599ab76bb | 66 | // Initialize the M2X client |
jb8414 | 0:ece599ab76bb | 67 | Client client; |
mbedAustin | 5:b3bcb048a5a5 | 68 | M2XStreamClient m2xClient(&client, m2xKey,1,"52.22.150.98"); // api-m2x.att.com |
mbedAustin | 5:b3bcb048a5a5 | 69 | |
mbedAustin | 5:b3bcb048a5a5 | 70 | volatile int randomNumber = 0; |
jb8414 | 0:ece599ab76bb | 71 | |
jb8414 | 0:ece599ab76bb | 72 | while (true) { |
mbedAustin | 5:b3bcb048a5a5 | 73 | // send a random number to M2X every 5 seconds |
mbedAustin | 5:b3bcb048a5a5 | 74 | randomNumber = rand(); |
mbedAustin | 5:b3bcb048a5a5 | 75 | ret = m2xClient.updateStreamValue(deviceId, streamName, randomNumber); |
mbedAustin | 5:b3bcb048a5a5 | 76 | printf("send() returned %d\r\n", ret); |
mbedAustin | 5:b3bcb048a5a5 | 77 | wait(5.0); |
jb8414 | 0:ece599ab76bb | 78 | } |
jb8414 | 0:ece599ab76bb | 79 | } |