David Ross / Mbed 2 deprecated Ethernet_K64F_minimal

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of Ethernet_K64F by Janhavi Kulkarni

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 Title:      Program to send DATA to  Sparkfun Stream
00003 Author:     David Elmo Ross, 123-456-789, AED_701AA
00004 Date:       Oct 5th, 2016
00005 Desription: This program simulates Celsius and Fahrenheit values. This can
00006             be changed to get these values from a Temperature sensor.
00007             This program will set up the necessary parameters to communicate
00008             through the Ethernet Port on the FRDM-K64 that will then connect
00009             to the SPARKFUN Server. The data provided by this program is formatted
00010             to fit a STREAM on the Sparkfun Server that was set up before time with
00011             Fahrenheit and Celsius.
00012             When data is sent to the stream, it will include a PUBLIC and PRIVATE
00013             key or it will be rejected. These keys are given to the user through 
00014             the site    data.sparkfun.com  
00015             The key values you receive must replace the ones in this program.
00016             
00017 */
00018 
00019 #include "mbed.h"
00020 #include "EthernetInterface.h"
00021 
00022 char* Public_Key = "PublicKeyGoesHere";             // Public Key is used to access data that has been sent
00023 char* Private_Key = "PrivateKeyGoesHere";           // Private Key is sent with Public Key with every data xfer
00024 char* ServerIP = "data.sparkfun.com";               // Server IP is  data.sparkfun.com
00025 
00026 EthernetInterface ethernet;                         // define ethernet to be of type EthernetInterface
00027 
00028 int main()
00029 {
00030     int counter;                                    // used as a counter to send a fixed number of values
00031     putchar(0x1b);                                  // clears the PC screen
00032     printf("[2J");
00033 
00034     printf("Start\r\n");                            // send START message to PC screen
00035     float fahrenheit;                               // set up fahrenhiet as a float
00036     float celsius=5.0;                              // set celcius to 5.0
00037    
00038     while(1)
00039     {          
00040         int ret = ethernet.init();                  // ret gets result of ethernet.init()
00041                                                     // 0 means that initialization was successful
00042         if (!ret)                                   // if initialization WAS successful
00043         {                                           // output the MAC address
00044             printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
00045             ret = ethernet.connect();               // ret gets result of ethernet.connect()
00046             if (!ret)                               // 0 means that connection was successufl
00047             {                                       // if connection print out IP MASK and GATEWAY
00048                 printf("IP: %s, MASK: %s, GW: %s\r\n",
00049                 ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
00050             } 
00051             else                                    // if no connection then Print error and terminate 
00052             {
00053                 printf("Error ethernet.connect() - ret = %d\r\n", ret);
00054                 exit(0);
00055             }
00056         } 
00057         else                                        // if ethernet would NOT initialize, print error and terminate
00058         {
00059             printf("Error ethernet.init() - ret = %d\r\n", ret);
00060             exit(0);
00061         }  
00062         TCPSocketConnection sock;                   // define sock to be of type TCPSocketConnection
00063   
00064         int ret_t;   
00065         char http_cmd[256];                         // set up 256 byte buffer for http commands sent to Sparkfun
00066             for(counter=1;counter<=10;++counter)
00067             {
00068             sock.connect(ServerIP, 80);             // connect to SPARFUN on PORT 80 and print server IP
00069             printf("Server IP: %s", ServerIP);      
00070             if(sock.is_connected())                 // if connection is made say Socket Connected
00071                 printf("Socket Connected\n\r");
00072             else
00073             {                                       // else print Socket NOT connected
00074                 printf("Socket NoT Connected\n\r");
00075                 printf("\r\n Program has halted");
00076                 exit(0);
00077              }     
00078             // format string http_cmd   
00079             fahrenheit= ((9.0/5.0)*celsius + 32); 
00080             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);
00081             // send formatted string
00082             sock.send_all(http_cmd, sizeof(http_cmd)-1);
00083             printf("\tcelsius =%.1f\tfahrenheit=%.1f\r\n",celsius,fahrenheit);
00084         
00085             sock.close();                           // close the socket
00086  //           station++;
00087             celsius+=1.1;
00088             wait(5);
00089             }
00090         printf("\r\n\r\nProgram has halted - all data sent.");
00091         exit(0);
00092         }
00093     
00094     
00095     }