Ethernet

Dependencies:   ASDL_using_Ethernet MMA8451Q mbed

main.cpp

Committer:
RITVIK_DAVE
Date:
2017-02-25
Revision:
0:13b0a84d31ec

File content as of revision 0:13b0a84d31ec:

#include "mbed.h"
#include "WIZnetInterface.h"
//MAC address of sheild
unsigned char MAC_Addr[6] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char* ServerIP = "192.168.0.2"; //server ip were server is set
//public and private key of stream
char* public_key = "YmYBg4do89c4MvQoLxXBhb6DpD9";
char* private_key = "YK6DmdejWAF0Bwdj5pzGH7vP8Pn";
int Count = 15;
int x,y;
Serial pc(USBTX, USBRX);
SPI spi(PTD2,PTD3,PTD1);
//wiznet object
WIZnetInterface ethernet(&spi,PTD0,PTA20);
//analog input
AnalogIn pot(PTB1);
AnalogIn ldr(PTB0);
int writtenLen,eth;
int main()
{
//Set serial port baudrate speed: 9600
pc.baud(9600);
pc.printf("Start\r\n");
while(1)
{
//pc.printf("%d\r\n",x);
int ret = ethernet.init(MAC_Addr);
if (!ret)
{
//print MAC address of device
pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
ret = ethernet.connect();
if (!ret)
{
//print IP alloted and details obtianed from network
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);
}
//create TCP socket
TCPSocketConnection sock;
while(1){
x=255*pot.read();
y=255*ldr.read();
//connect the server to port 8080
sock.connect(ServerIP, 8080);
if(sock.is_connected())
pc.printf("Socket Connected\n\r");
else
pc.printf("Socket NoT Connected\n\r");
//create character buffer for URL
char buffer[300];
int ret_t;
char http_cmd[100];
//make URL
sprintf(http_cmd,"GET /input/%s?private_key=%s&pot=%d&ldr=%d HTTP/1.0\n\n",public_key,private_key,x,y);
printf("sending %d characters",strlen(http_cmd));
printf("Running - %s\r\n",http_cmd);
writtenLen = sock.send_all(http_cmd, strlen(http_cmd));
//print the result of URL GET method
if(writtenLen == -1)
printf("sending failed\r\n");
else
printf("sending success\r\n");
eth=ret_t = sock.receive(buffer, strlen(buffer));
buffer[ret_t] = '\0';
printf("Received %d chars from server:\r\n%s\r\n", ret_t, buffer);
sock.close();
//ethernet.disconnect();
pc.printf("Socket Closed\r\n");
wait(3);
}
}
}