TCP client mbed RTOS
Dependencies: mbed LM75B mbed-rtos EthernetInterface
main.cpp@16:3d431098b650, 2020-07-16 (annotated)
- Committer:
- metroboominwantsoen
- Date:
- Thu Jul 16 12:45:14 2020 +0000
- Revision:
- 16:3d431098b650
- Parent:
- 15:954b6cd3d853
Incorporating the temperature
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:bb128f0e952f | 1 | #include "mbed.h" |
donatien | 0:bb128f0e952f | 2 | #include "EthernetInterface.h" |
metroboominwantsoen | 16:3d431098b650 | 3 | #include "LM75B.h" |
donatien | 0:bb128f0e952f | 4 | |
cathaldigi | 15:954b6cd3d853 | 5 | static const char* mbedIP = "192.168.5.10"; //IP |
cathaldigi | 15:954b6cd3d853 | 6 | static const char* mbedMask = "255.255.255.0"; // Mask |
cathaldigi | 15:954b6cd3d853 | 7 | static const char* mbedGateway = "192.168.5.1"; //Gateway |
cathaldigi | 15:954b6cd3d853 | 8 | |
cathaldigi | 15:954b6cd3d853 | 9 | const int ECHO_SERVER_PORT = 2224; |
cathaldigi | 15:954b6cd3d853 | 10 | const char* ECHO_SERVER_ADDRESS = "192.168.5.15"; // laptop address |
cathaldigi | 15:954b6cd3d853 | 11 | |
metroboominwantsoen | 16:3d431098b650 | 12 | LM75B sensor(p28,p27); |
metroboominwantsoen | 16:3d431098b650 | 13 | |
emilmont | 7:65188f4a8c25 | 14 | int main() { |
donatien | 0:bb128f0e952f | 15 | EthernetInterface eth; |
cathaldigi | 15:954b6cd3d853 | 16 | eth.init(mbedIP,mbedMask,mbedGateway); //Use DHCP |
donatien | 0:bb128f0e952f | 17 | eth.connect(); |
emilmont | 2:e087e9b789e9 | 18 | printf("IP Address is %s\n", eth.getIPAddress()); |
donatien | 0:bb128f0e952f | 19 | |
cathaldigi | 15:954b6cd3d853 | 20 | while(1) { |
metroboominwantsoen | 16:3d431098b650 | 21 | if (sensor.open()) { |
metroboominwantsoen | 16:3d431098b650 | 22 | printf("Device detected!\n"); |
metroboominwantsoen | 16:3d431098b650 | 23 | |
metroboominwantsoen | 16:3d431098b650 | 24 | while (1) { |
metroboominwantsoen | 16:3d431098b650 | 25 | cls(); |
metroboominwantsoen | 16:3d431098b650 | 26 | locate(0,3); |
metroboominwantsoen | 16:3d431098b650 | 27 | printf("Temp = %.3f\n", (float)sensor); |
metroboominwantsoen | 16:3d431098b650 | 28 | wait(1.0); |
metroboominwantsoen | 16:3d431098b650 | 29 | TCPSocketConnection sock; |
metroboominwantsoen | 16:3d431098b650 | 30 | sock.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); |
metroboominwantsoen | 16:3d431098b650 | 31 | char http_cmd[] = "Testing"; |
metroboominwantsoen | 16:3d431098b650 | 32 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
cathaldigi | 15:954b6cd3d853 | 33 | |
metroboominwantsoen | 16:3d431098b650 | 34 | char buffer[300]; |
metroboominwantsoen | 16:3d431098b650 | 35 | int ret; |
metroboominwantsoen | 16:3d431098b650 | 36 | while (false) { |
metroboominwantsoen | 16:3d431098b650 | 37 | ret = sock.receive(buffer, sizeof(buffer)-1); |
metroboominwantsoen | 16:3d431098b650 | 38 | if (ret <= 0) |
metroboominwantsoen | 16:3d431098b650 | 39 | break; |
metroboominwantsoen | 16:3d431098b650 | 40 | buffer[ret] = '\0'; |
metroboominwantsoen | 16:3d431098b650 | 41 | printf("Temp = %.3f\n", (float)sensor); |
metroboominwantsoen | 16:3d431098b650 | 42 | printf("Received %d chars from server:\n%s\n", ret, buffer); |
metroboominwantsoen | 16:3d431098b650 | 43 | } |
metroboominwantsoen | 16:3d431098b650 | 44 | |
metroboominwantsoen | 16:3d431098b650 | 45 | sock.close(); |
metroboominwantsoen | 16:3d431098b650 | 46 | Thread::wait(1000); |
metroboominwantsoen | 16:3d431098b650 | 47 | |
cathaldigi | 15:954b6cd3d853 | 48 | } |
metroboominwantsoen | 16:3d431098b650 | 49 | |
metroboominwantsoen | 16:3d431098b650 | 50 | } else { |
metroboominwantsoen | 16:3d431098b650 | 51 | error("Device not detected!\n"); |
metroboominwantsoen | 16:3d431098b650 | 52 | } |
emilmont | 7:65188f4a8c25 | 53 | } |
donatien | 0:bb128f0e952f | 54 | |
emilmont | 7:65188f4a8c25 | 55 | eth.disconnect(); |
donatien | 5:01f6c3e112af | 56 | |
cathaldigi | 15:954b6cd3d853 | 57 | |
donatien | 0:bb128f0e952f | 58 | } |