Thomas Morris / Mbed OS PROJ324_Final

Fork of ELEC351_Group_T by Plymouth ELEC351 Group T

NETWORK.cpp

Committer:
thomasmorris
Date:
2018-01-07
Revision:
35:26b0a9b55d82
Child:
36:a0098306fc58

File content as of revision 35:26b0a9b55d82:

#include "NETWORK.hpp"

AnalogIn ldr(PA_0);

//Now setup a web server
TCPServer srv;           //TCP/IP Server
TCPSocket clt_sock;      //Socket for communication
SocketAddress clt_addr;  //Address of incoming connection


void Network_Init()
{ 
    printf("Basic HTTP server example\n");
    //Configure an ethernet connection
    EthernetInterface eth;
    eth.set_network(IP, NETMASK, GATEWAY);
    eth.connect();
    printf("The target IP address is '%s'\n", eth.get_ip_address());
    //t1.start(Network_Thread);
    /* Open the server on ethernet stack */
    srv.open(&eth);
    
    /* Bind the HTTP port (TCP 80) to the server */
    srv.bind(eth.get_ip_address(), 80);
    
    /* Can handle 5 simultaneous connections */
    srv.listen(5);
}
void Networking()
{    
        //using namespace std;
        //Block and wait on an incoming connection
        srv.accept(&clt_sock, &clt_addr);
        printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
        
        //Uses a C++ string to make it easier to concatinate
        string response;
        //This is a C string
        char ldr_str[64];
        
        //Read the LDR value
        float u = ldr;
        
        //Convert to a C String
        sprintf(ldr_str, "%5.3f", u );
        printf("LDR: %5.3f\n\r", u);
        
        //Build the C++ string response
        response = HTTP_MESSAGE_BODY1;
        response += ldr_str;
        response += HTTP_MESSAGE_BODY2;
        
        //Send static HTML response (as a C string)
        clt_sock.send(response.c_str(), response.size()+6);    
}