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 Janhavi Kulkarni

Files at this revision

API Documentation at this revision

Comitter:
DavidElmoRoss
Date:
Sun Oct 30 16:00:06 2016 +0000
Parent:
0:9eca947a9efa
Commit message:
Minimal code to send data from the Ethernet Port of a FRDM-K64 as a stream to data.sparkfun.com

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu May 19 06:52:59 2016 +0000
+++ b/main.cpp	Sun Oct 30 16:00:06 2016 +0000
@@ -1,67 +1,94 @@
+/*
+Title:      Program to send DATA to  Sparkfun Stream
+Author:     David Elmo Ross, 123-456-789, AED_701AA
+Date:       Oct 5th, 2016
+Desription: This program simulates Celsius and Fahrenheit values. This can
+            be changed to get these values from a Temperature sensor.
+            This program will set up the necessary parameters to communicate
+            through the Ethernet Port on the FRDM-K64 that will then connect
+            to the SPARKFUN Server. The data provided by this program is formatted
+            to fit a STREAM on the Sparkfun Server that was set up before time with
+            Fahrenheit and Celsius.
+            When data is sent to the stream, it will include a PUBLIC and PRIVATE
+            key or it will be rejected. These keys are given to the user through 
+            the site    data.sparkfun.com  
+            The key values you receive must replace the ones in this program.
+            
+*/
+
 #include "mbed.h"
 #include "EthernetInterface.h"
 
-char* Public_Key = "ro9WnDLy63t4xp5n18vm";
-char* Private_Key = "jkKr0obAEnS0MgJRNa92";
-char* ServerIP = "data.sparkfun.com";
+char* Public_Key = "PublicKeyGoesHere";             // Public Key is used to access data that has been sent
+char* Private_Key = "PrivateKeyGoesHere";           // Private Key is sent with Public Key with every data xfer
+char* ServerIP = "data.sparkfun.com";               // Server IP is  data.sparkfun.com
 
-Serial pc(USBTX, USBRX);
-
-EthernetInterface ethernet;
+EthernetInterface ethernet;                         // define ethernet to be of type EthernetInterface
 
 int main()
 {
-    pc.baud(115200);
-    pc.printf("Start\r\n");
-    int a=100;
+    int counter;                                    // used as a counter to send a fixed number of values
+    putchar(0x1b);                                  // clears the PC screen
+    printf("[2J");
+
+    printf("Start\r\n");                            // send START message to PC screen
+    float fahrenheit;                               // set up fahrenhiet as a float
+    float celsius=5.0;                              // set celcius to 5.0
+   
     while(1)
     {          
-        int ret = ethernet.init();     
-        if (!ret) 
-        {
-            pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
-            ret = ethernet.connect();
-            if (!ret) 
-            {
-                pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
+        int ret = ethernet.init();                  // ret gets result of ethernet.init()
+                                                    // 0 means that initialization was successful
+        if (!ret)                                   // if initialization WAS successful
+        {                                           // output the MAC address
+            printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
+            ret = ethernet.connect();               // ret gets result of ethernet.connect()
+            if (!ret)                               // 0 means that connection was successufl
+            {                                       // if connection print out IP MASK and GATEWAY
+                printf("IP: %s, MASK: %s, GW: %s\r\n",
                 ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
             } 
-            else 
+            else                                    // if no connection then Print error and terminate 
             {
-                pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
+                printf("Error ethernet.connect() - ret = %d\r\n", ret);
                 exit(0);
             }
         } 
-        else 
+        else                                        // if ethernet would NOT initialize, print error and terminate
         {
-            pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
+            printf("Error ethernet.init() - ret = %d\r\n", ret);
             exit(0);
         }  
-        TCPSocketConnection sock;
-        char buffer[300];
+        TCPSocketConnection sock;                   // define sock to be of type TCPSocketConnection
+  
         int ret_t;   
-        char http_cmd[256];
-            while(1)
+        char http_cmd[256];                         // set up 256 byte buffer for http commands sent to Sparkfun
+            for(counter=1;counter<=10;++counter)
             {
-            
-            sock.connect(ServerIP, 80);
-            if(sock.is_connected())
+            sock.connect(ServerIP, 80);             // connect to SPARFUN on PORT 80 and print server IP
+            printf("Server IP: %s", ServerIP);      
+            if(sock.is_connected())                 // if connection is made say Socket Connected
                 printf("Socket Connected\n\r");
             else
-                printf("Socket NoT Connected\n\r");  
-            //sprintf(http_cmd,"GET /input/%s?private_key=%s&a=%2d HTTP/1.0\n\n",Public_Key,Private_Key,a);
-            sprintf(http_cmd,"GET /input/%s?private_key=%s&a=%2d HTTP/1.0\n\n",Public_Key,Private_Key,a);
-            pc.printf("Running - %s\r\n",http_cmd);
+            {                                       // else print Socket NOT connected
+                printf("Socket NoT Connected\n\r");
+                printf("\r\n Program has halted");
+                exit(0);
+             }     
+            // format string http_cmd   
+            fahrenheit= ((9.0/5.0)*celsius + 32); 
+            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);
+            // send formatted string
             sock.send_all(http_cmd, sizeof(http_cmd)-1);
+            printf("\tcelsius =%.1f\tfahrenheit=%.1f\r\n",celsius,fahrenheit);
         
-            ret_t = sock.receive(buffer, sizeof(buffer)-1);
-            buffer[ret_t] = '\0';
-            pc.printf("Received %d chars from server:\n%s\r\n", ret_t, buffer);
-            sock.close();
-            a=a+1;
+            sock.close();                           // close the socket
+ //           station++;
+            celsius+=1.1;
             wait(5);
             }
-        
+        printf("\r\n\r\nProgram has halted - all data sent.");
+        exit(0);
         }
     
     
--- a/mbed.bld	Thu May 19 06:52:59 2016 +0000
+++ b/mbed.bld	Sun Oct 30 16:00:06 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/7c328cabac7e
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/6c34061e7c34
\ No newline at end of file