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

main.cpp

Committer:
janhavi
Date:
2016-05-19
Revision:
0:9eca947a9efa
Child:
1:8c684ad7d00e

File content as of revision 0:9eca947a9efa:

#include "mbed.h"
#include "EthernetInterface.h"

char* Public_Key = "ro9WnDLy63t4xp5n18vm";
char* Private_Key = "jkKr0obAEnS0MgJRNa92";
char* ServerIP = "data.sparkfun.com";

Serial pc(USBTX, USBRX);

EthernetInterface ethernet;

int main()
{
    pc.baud(115200);
    pc.printf("Start\r\n");
    int a=100;
    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",
                ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
            } 
            else 
            {
                pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
                exit(0);
            }
        } 
        else 
        {
            pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
            exit(0);
        }  
        TCPSocketConnection sock;
        char buffer[300];
        int ret_t;   
        char http_cmd[256];
            while(1)
            {
            
            sock.connect(ServerIP, 80);
            if(sock.is_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);
            sock.send_all(http_cmd, sizeof(http_cmd)-1);
        
            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;
            wait(5);
            }
        
        }
    
    
    }