This is minimal code that will send a stream to data.sparkfun.com using the FRDM-K64 ethernet port
Dependencies: EthernetInterface mbed-rtos mbed
Fork of Ethernet_K64F by
main.cpp@1:8c684ad7d00e, 2016-10-30 (annotated)
- Committer:
- DavidElmoRoss
- Date:
- Sun Oct 30 16:00:06 2016 +0000
- Revision:
- 1:8c684ad7d00e
- Parent:
- 0:9eca947a9efa
Minimal code to send data from the Ethernet Port of a FRDM-K64 as a stream to data.sparkfun.com
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DavidElmoRoss | 1:8c684ad7d00e | 1 | /* |
DavidElmoRoss | 1:8c684ad7d00e | 2 | Title: Program to send DATA to Sparkfun Stream |
DavidElmoRoss | 1:8c684ad7d00e | 3 | Author: David Elmo Ross, 123-456-789, AED_701AA |
DavidElmoRoss | 1:8c684ad7d00e | 4 | Date: Oct 5th, 2016 |
DavidElmoRoss | 1:8c684ad7d00e | 5 | Desription: This program simulates Celsius and Fahrenheit values. This can |
DavidElmoRoss | 1:8c684ad7d00e | 6 | be changed to get these values from a Temperature sensor. |
DavidElmoRoss | 1:8c684ad7d00e | 7 | This program will set up the necessary parameters to communicate |
DavidElmoRoss | 1:8c684ad7d00e | 8 | through the Ethernet Port on the FRDM-K64 that will then connect |
DavidElmoRoss | 1:8c684ad7d00e | 9 | to the SPARKFUN Server. The data provided by this program is formatted |
DavidElmoRoss | 1:8c684ad7d00e | 10 | to fit a STREAM on the Sparkfun Server that was set up before time with |
DavidElmoRoss | 1:8c684ad7d00e | 11 | Fahrenheit and Celsius. |
DavidElmoRoss | 1:8c684ad7d00e | 12 | When data is sent to the stream, it will include a PUBLIC and PRIVATE |
DavidElmoRoss | 1:8c684ad7d00e | 13 | key or it will be rejected. These keys are given to the user through |
DavidElmoRoss | 1:8c684ad7d00e | 14 | the site data.sparkfun.com |
DavidElmoRoss | 1:8c684ad7d00e | 15 | The key values you receive must replace the ones in this program. |
DavidElmoRoss | 1:8c684ad7d00e | 16 | |
DavidElmoRoss | 1:8c684ad7d00e | 17 | */ |
DavidElmoRoss | 1:8c684ad7d00e | 18 | |
janhavi | 0:9eca947a9efa | 19 | #include "mbed.h" |
janhavi | 0:9eca947a9efa | 20 | #include "EthernetInterface.h" |
janhavi | 0:9eca947a9efa | 21 | |
DavidElmoRoss | 1:8c684ad7d00e | 22 | char* Public_Key = "PublicKeyGoesHere"; // Public Key is used to access data that has been sent |
DavidElmoRoss | 1:8c684ad7d00e | 23 | char* Private_Key = "PrivateKeyGoesHere"; // Private Key is sent with Public Key with every data xfer |
DavidElmoRoss | 1:8c684ad7d00e | 24 | char* ServerIP = "data.sparkfun.com"; // Server IP is data.sparkfun.com |
janhavi | 0:9eca947a9efa | 25 | |
DavidElmoRoss | 1:8c684ad7d00e | 26 | EthernetInterface ethernet; // define ethernet to be of type EthernetInterface |
janhavi | 0:9eca947a9efa | 27 | |
janhavi | 0:9eca947a9efa | 28 | int main() |
janhavi | 0:9eca947a9efa | 29 | { |
DavidElmoRoss | 1:8c684ad7d00e | 30 | int counter; // used as a counter to send a fixed number of values |
DavidElmoRoss | 1:8c684ad7d00e | 31 | putchar(0x1b); // clears the PC screen |
DavidElmoRoss | 1:8c684ad7d00e | 32 | printf("[2J"); |
DavidElmoRoss | 1:8c684ad7d00e | 33 | |
DavidElmoRoss | 1:8c684ad7d00e | 34 | printf("Start\r\n"); // send START message to PC screen |
DavidElmoRoss | 1:8c684ad7d00e | 35 | float fahrenheit; // set up fahrenhiet as a float |
DavidElmoRoss | 1:8c684ad7d00e | 36 | float celsius=5.0; // set celcius to 5.0 |
DavidElmoRoss | 1:8c684ad7d00e | 37 | |
janhavi | 0:9eca947a9efa | 38 | while(1) |
janhavi | 0:9eca947a9efa | 39 | { |
DavidElmoRoss | 1:8c684ad7d00e | 40 | int ret = ethernet.init(); // ret gets result of ethernet.init() |
DavidElmoRoss | 1:8c684ad7d00e | 41 | // 0 means that initialization was successful |
DavidElmoRoss | 1:8c684ad7d00e | 42 | if (!ret) // if initialization WAS successful |
DavidElmoRoss | 1:8c684ad7d00e | 43 | { // output the MAC address |
DavidElmoRoss | 1:8c684ad7d00e | 44 | printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress()); |
DavidElmoRoss | 1:8c684ad7d00e | 45 | ret = ethernet.connect(); // ret gets result of ethernet.connect() |
DavidElmoRoss | 1:8c684ad7d00e | 46 | if (!ret) // 0 means that connection was successufl |
DavidElmoRoss | 1:8c684ad7d00e | 47 | { // if connection print out IP MASK and GATEWAY |
DavidElmoRoss | 1:8c684ad7d00e | 48 | printf("IP: %s, MASK: %s, GW: %s\r\n", |
janhavi | 0:9eca947a9efa | 49 | ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway()); |
janhavi | 0:9eca947a9efa | 50 | } |
DavidElmoRoss | 1:8c684ad7d00e | 51 | else // if no connection then Print error and terminate |
janhavi | 0:9eca947a9efa | 52 | { |
DavidElmoRoss | 1:8c684ad7d00e | 53 | printf("Error ethernet.connect() - ret = %d\r\n", ret); |
janhavi | 0:9eca947a9efa | 54 | exit(0); |
janhavi | 0:9eca947a9efa | 55 | } |
janhavi | 0:9eca947a9efa | 56 | } |
DavidElmoRoss | 1:8c684ad7d00e | 57 | else // if ethernet would NOT initialize, print error and terminate |
janhavi | 0:9eca947a9efa | 58 | { |
DavidElmoRoss | 1:8c684ad7d00e | 59 | printf("Error ethernet.init() - ret = %d\r\n", ret); |
janhavi | 0:9eca947a9efa | 60 | exit(0); |
janhavi | 0:9eca947a9efa | 61 | } |
DavidElmoRoss | 1:8c684ad7d00e | 62 | TCPSocketConnection sock; // define sock to be of type TCPSocketConnection |
DavidElmoRoss | 1:8c684ad7d00e | 63 | |
janhavi | 0:9eca947a9efa | 64 | int ret_t; |
DavidElmoRoss | 1:8c684ad7d00e | 65 | char http_cmd[256]; // set up 256 byte buffer for http commands sent to Sparkfun |
DavidElmoRoss | 1:8c684ad7d00e | 66 | for(counter=1;counter<=10;++counter) |
janhavi | 0:9eca947a9efa | 67 | { |
DavidElmoRoss | 1:8c684ad7d00e | 68 | sock.connect(ServerIP, 80); // connect to SPARFUN on PORT 80 and print server IP |
DavidElmoRoss | 1:8c684ad7d00e | 69 | printf("Server IP: %s", ServerIP); |
DavidElmoRoss | 1:8c684ad7d00e | 70 | if(sock.is_connected()) // if connection is made say Socket Connected |
janhavi | 0:9eca947a9efa | 71 | printf("Socket Connected\n\r"); |
janhavi | 0:9eca947a9efa | 72 | else |
DavidElmoRoss | 1:8c684ad7d00e | 73 | { // else print Socket NOT connected |
DavidElmoRoss | 1:8c684ad7d00e | 74 | printf("Socket NoT Connected\n\r"); |
DavidElmoRoss | 1:8c684ad7d00e | 75 | printf("\r\n Program has halted"); |
DavidElmoRoss | 1:8c684ad7d00e | 76 | exit(0); |
DavidElmoRoss | 1:8c684ad7d00e | 77 | } |
DavidElmoRoss | 1:8c684ad7d00e | 78 | // format string http_cmd |
DavidElmoRoss | 1:8c684ad7d00e | 79 | fahrenheit= ((9.0/5.0)*celsius + 32); |
DavidElmoRoss | 1:8c684ad7d00e | 80 | sprintf(http_cmd,"GET /input/%s?private_key=%s&fahrenheit=%0.1f&celsius=%.1f HTTP/1.0\n\n",Public_Key,Private_Key,fahrenheit, celsius); |
DavidElmoRoss | 1:8c684ad7d00e | 81 | // send formatted string |
janhavi | 0:9eca947a9efa | 82 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
DavidElmoRoss | 1:8c684ad7d00e | 83 | printf("\tcelsius =%.1f\tfahrenheit=%.1f\r\n",celsius,fahrenheit); |
janhavi | 0:9eca947a9efa | 84 | |
DavidElmoRoss | 1:8c684ad7d00e | 85 | sock.close(); // close the socket |
DavidElmoRoss | 1:8c684ad7d00e | 86 | // station++; |
DavidElmoRoss | 1:8c684ad7d00e | 87 | celsius+=1.1; |
janhavi | 0:9eca947a9efa | 88 | wait(5); |
janhavi | 0:9eca947a9efa | 89 | } |
DavidElmoRoss | 1:8c684ad7d00e | 90 | printf("\r\n\r\nProgram has halted - all data sent."); |
DavidElmoRoss | 1:8c684ad7d00e | 91 | exit(0); |
janhavi | 0:9eca947a9efa | 92 | } |
janhavi | 0:9eca947a9efa | 93 | |
janhavi | 0:9eca947a9efa | 94 | |
janhavi | 0:9eca947a9efa | 95 | } |