nova proba

Committer:
bosko001
Date:
Thu Feb 06 21:05:30 2020 +0000
Revision:
0:fe8cd064ea08
Child:
1:e1bde4e82763
copy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bosko001 0:fe8cd064ea08 1 #include "mbed.h"
bosko001 0:fe8cd064ea08 2 #include "EthernetInterface.h"
bosko001 0:fe8cd064ea08 3
bosko001 0:fe8cd064ea08 4 #define WADDR
bosko001 0:fe8cd064ea08 5
bosko001 0:fe8cd064ea08 6 #ifdef WADDR
bosko001 0:fe8cd064ea08 7 #define IP_ADDR "169.254.65.200"
bosko001 0:fe8cd064ea08 8 #define NET_MASK "255.255.0.0"
bosko001 0:fe8cd064ea08 9 #define GATW_ADDR "169.254.65.1"
bosko001 0:fe8cd064ea08 10 #define IP_ADDR_REMOTE "169.254.65.199"
bosko001 0:fe8cd064ea08 11 #else
bosko001 0:fe8cd064ea08 12 #define IP_ADDR "192.168.1.10"
bosko001 0:fe8cd064ea08 13 #define NET_MASK "255.255.255.0"
bosko001 0:fe8cd064ea08 14 #define GATW_ADDR "192.168.1.1"
bosko001 0:fe8cd064ea08 15 #define IP_ADDR_R "192.168.1.9"
bosko001 0:fe8cd064ea08 16 #endif
bosko001 0:fe8cd064ea08 17
bosko001 0:fe8cd064ea08 18
bosko001 0:fe8cd064ea08 19
bosko001 0:fe8cd064ea08 20
bosko001 0:fe8cd064ea08 21 EthernetInterface eth;
bosko001 0:fe8cd064ea08 22 nsapi_error_t err;
bosko001 0:fe8cd064ea08 23
bosko001 0:fe8cd064ea08 24
bosko001 0:fe8cd064ea08 25 int main(void)
bosko001 0:fe8cd064ea08 26 {
bosko001 0:fe8cd064ea08 27 // if( (err = eth.disconnect()) != NSAPI_ERROR_OK ) { printf(" greska diskonekcije %d \n\r", err); return 1; }
bosko001 0:fe8cd064ea08 28 // ((NetworkInterface *)&eth)->set_network("192.168.1.11","255.255.255.0","192.168.1.1");
bosko001 0:fe8cd064ea08 29
bosko001 0:fe8cd064ea08 30 if( (err = eth.set_network(IP_ADDR,NET_MASK,GATW_ADDR))!= NSAPI_ERROR_OK ) { printf(" greska setovanja mreze %d \n\r", err); return 1; }
bosko001 0:fe8cd064ea08 31
bosko001 0:fe8cd064ea08 32
bosko001 0:fe8cd064ea08 33 if( (err = eth.connect()) != NSAPI_ERROR_OK ) { printf(" greska konekcije %d \n\r", err); return 1; }
bosko001 0:fe8cd064ea08 34
bosko001 0:fe8cd064ea08 35 const char *ip = eth.get_ip_address() ;
bosko001 0:fe8cd064ea08 36
bosko001 0:fe8cd064ea08 37
bosko001 0:fe8cd064ea08 38 printf( "MAC adresa: %s\n\r", eth.get_mac_address() );
bosko001 0:fe8cd064ea08 39 printf( "IP adresa: %s\n\r", ip);
bosko001 0:fe8cd064ea08 40 printf( "net mask: %s\n\r", eth.get_netmask() );
bosko001 0:fe8cd064ea08 41 printf( "GW adresa: %s\n\r", eth.get_gateway() );
bosko001 0:fe8cd064ea08 42
bosko001 0:fe8cd064ea08 43
bosko001 0:fe8cd064ea08 44 /*
bosko001 0:fe8cd064ea08 45 TCPSocket socket;
bosko001 0:fe8cd064ea08 46 socket.open(&eth);
bosko001 0:fe8cd064ea08 47 socket.connect(
bosko001 0:fe8cd064ea08 48 */
bosko001 0:fe8cd064ea08 49
bosko001 0:fe8cd064ea08 50 UDPSocket udpSocket;
bosko001 0:fe8cd064ea08 51 if( (err = udpSocket.open( &eth )) != NSAPI_ERROR_OK ) { printf(" greska otvaranja soketa %d \n\r", err); return 1; }
bosko001 0:fe8cd064ea08 52 udpSocket.bind(2000);
bosko001 0:fe8cd064ea08 53
bosko001 0:fe8cd064ea08 54 char buff[]="text poruke";
bosko001 0:fe8cd064ea08 55 char inbuff[100];
bosko001 0:fe8cd064ea08 56 printf("POCINJE\r\n");
bosko001 0:fe8cd064ea08 57 int i=0;
bosko001 0:fe8cd064ea08 58 while(1)
bosko001 0:fe8cd064ea08 59 {
bosko001 0:fe8cd064ea08 60 printf("cekam prijem \n\r");
bosko001 0:fe8cd064ea08 61
bosko001 0:fe8cd064ea08 62 SocketAddress sockAddr;
bosko001 0:fe8cd064ea08 63 int n = udpSocket.recvfrom(&sockAddr, &inbuff, sizeof(inbuff));
bosko001 0:fe8cd064ea08 64 printf(" primljeno = %d bajtova ",n);
bosko001 0:fe8cd064ea08 65 for(i=0;i<n;i++) printf(" %c ", inbuff[i] );
bosko001 0:fe8cd064ea08 66
bosko001 0:fe8cd064ea08 67 if(0 > udpSocket.sendto(sockAddr.get_ip_address(), sockAddr.get_port(), buff, sizeof(buff)))
bosko001 0:fe8cd064ea08 68 {
bosko001 0:fe8cd064ea08 69 printf("\n\rError sending data\n");
bosko001 0:fe8cd064ea08 70 return -1;
bosko001 0:fe8cd064ea08 71 }
bosko001 0:fe8cd064ea08 72 else printf("\n\rPredaja %s na adresu %s , port %d\r\n",buff,sockAddr.get_ip_address(), sockAddr.get_port());
bosko001 0:fe8cd064ea08 73
bosko001 0:fe8cd064ea08 74 wait(1);
bosko001 0:fe8cd064ea08 75 }
bosko001 0:fe8cd064ea08 76 }
bosko001 0:fe8cd064ea08 77
bosko001 0:fe8cd064ea08 78 //
bosko001 0:fe8cd064ea08 79 //enum nsapi_error {
bosko001 0:fe8cd064ea08 80 // NSAPI_ERROR_OK = 0, /*!< no error */
bosko001 0:fe8cd064ea08 81 // NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */
bosko001 0:fe8cd064ea08 82 // NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported functionality */
bosko001 0:fe8cd064ea08 83 // NSAPI_ERROR_PARAMETER = -3003, /*!< invalid configuration */
bosko001 0:fe8cd064ea08 84 // NSAPI_ERROR_NO_CONNECTION = -3004, /*!< not connected to a network */
bosko001 0:fe8cd064ea08 85 // NSAPI_ERROR_NO_SOCKET = -3005, /*!< socket not available for use */
bosko001 0:fe8cd064ea08 86 // NSAPI_ERROR_NO_ADDRESS = -3006, /*!< IP address is not known */
bosko001 0:fe8cd064ea08 87 // NSAPI_ERROR_NO_MEMORY = -3007, /*!< memory resource not available */
bosko001 0:fe8cd064ea08 88 // NSAPI_ERROR_NO_SSID = -3008, /*!< ssid not found */
bosko001 0:fe8cd064ea08 89 // NSAPI_ERROR_DNS_FAILURE = -3009, /*!< DNS failed to complete successfully */
bosko001 0:fe8cd064ea08 90 // NSAPI_ERROR_DHCP_FAILURE = -3010, /*!< DHCP failed to complete successfully */
bosko001 0:fe8cd064ea08 91 // NSAPI_ERROR_AUTH_FAILURE = -3011, /*!< connection to access point failed */
bosko001 0:fe8cd064ea08 92 // NSAPI_ERROR_DEVICE_ERROR = -3012, /*!< failure interfacing with the network processor */
bosko001 0:fe8cd064ea08 93 // NSAPI_ERROR_IN_PROGRESS = -3013, /*!< operation (eg connect) in progress */
bosko001 0:fe8cd064ea08 94 // NSAPI_ERROR_ALREADY = -3014, /*!< operation (eg connect) already in progress */
bosko001 0:fe8cd064ea08 95 // NSAPI_ERROR_IS_CONNECTED = -3015, /*!< socket is already connected */
bosko001 0:fe8cd064ea08 96 // NSAPI_ERROR_CONNECTION_LOST = -3016, /*!< connection lost */
bosko001 0:fe8cd064ea08 97 // NSAPI_ERROR_CONNECTION_TIMEOUT = -3017, /*!< connection timed out */
bosko001 0:fe8cd064ea08 98 //};