Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Controle_ventilo_ethernet_v1_2_4 by
main.cpp@4:fb63da7601aa, 2017-04-21 (annotated)
- Committer:
- YuK41
- Date:
- Fri Apr 21 17:30:06 2017 +0000
- Revision:
- 4:fb63da7601aa
- Parent:
- 3:6c54aa6db861
- Child:
- 5:cee119979b55
why you no work ?
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Brutus | 0:5ca563ae05db | 1 | #include "mbed.h" |
Brutus | 0:5ca563ae05db | 2 | #include "EthernetInterface.h" |
Brutus | 0:5ca563ae05db | 3 | #include "Ethernet.h" |
Brutus | 0:5ca563ae05db | 4 | |
Brutus | 3:6c54aa6db861 | 5 | #include "TCPSocket.h" |
Brutus | 0:5ca563ae05db | 6 | |
Brutus | 0:5ca563ae05db | 7 | #include "SocketAddress.h" |
Brutus | 0:5ca563ae05db | 8 | #include "Socket.h" |
Brutus | 0:5ca563ae05db | 9 | #include "UDPSocket.h" |
Brutus | 0:5ca563ae05db | 10 | |
Brutus | 3:6c54aa6db861 | 11 | #include <string.h> |
Brutus | 3:6c54aa6db861 | 12 | //using namespace std; |
Brutus | 3:6c54aa6db861 | 13 | |
YuK41 | 4:fb63da7601aa | 14 | const char* ECHO_SERVER_ADDRESS = "192.168.137.1"; // addresse du PC home "192.168.1.2" sergio "169.254.61.104" or "129.194.185.33" or "192.168.137.1" liotard "10.136.134.73" |
Brutus | 3:6c54aa6db861 | 15 | //#define IP "10.136.134.100" |
Brutus | 3:6c54aa6db861 | 16 | //#define GATEWAY "10.136.132.1" //"129.194.184.1" |
Brutus | 3:6c54aa6db861 | 17 | //#define MASK "255.255.252.0" |
YuK41 | 4:fb63da7601aa | 18 | const int ECHO_SERVER_PORT = 1036; |
Brutus | 3:6c54aa6db861 | 19 | |
Brutus | 3:6c54aa6db861 | 20 | // Front fans |
Brutus | 3:6c54aa6db861 | 21 | //PwmOut frontFans[] = {(PB_11), (PE_14), (PE_9), (PB_3), (PB_15), (PB_6), (PB_13), (PE_5), (PC_8)}; // PE_11 also possible for front fan n°5 |
Brutus | 3:6c54aa6db861 | 22 | |
Brutus | 3:6c54aa6db861 | 23 | // Rear fans |
Brutus | 3:6c54aa6db861 | 24 | //PwmOut rearFans[] = {(PB_10), (PE_12), (PE_10), (PE_11), (PC_6), (PD_13), (PC_7), (PE_6), (PC_9)}; // PE_10 also possible for rear fans n°5 |
Brutus | 3:6c54aa6db861 | 25 | |
Brutus | 3:6c54aa6db861 | 26 | PwmOut frontFans[9]={PB_11,PE_14,PE_9,PB_3,PB_15,PB_6,PB_13,PE_5,PC_8}; |
Brutus | 3:6c54aa6db861 | 27 | PwmOut rearFans[9]={PB_10,PE_12,PE_10,PE_11,PC_6,PD_13,PC_7,PE_6,PC_9}; |
Brutus | 3:6c54aa6db861 | 28 | |
Brutus | 3:6c54aa6db861 | 29 | DigitalOut fantest(LED1); |
Brutus | 3:6c54aa6db861 | 30 | |
Brutus | 3:6c54aa6db861 | 31 | // Receive string |
Brutus | 3:6c54aa6db861 | 32 | char FR_str[2] = {0}; |
Brutus | 3:6c54aa6db861 | 33 | char fan_str[10] = {0}; |
Brutus | 3:6c54aa6db861 | 34 | char pwm_str[4] = {0}; |
Brutus | 3:6c54aa6db861 | 35 | int FR = 0; |
Brutus | 3:6c54aa6db861 | 36 | int fan[10] = {0}; |
Brutus | 3:6c54aa6db861 | 37 | float pwm_f = 0.0; |
Brutus | 3:6c54aa6db861 | 38 | int FR_len = 0; |
Brutus | 3:6c54aa6db861 | 39 | int fan_len = 0; |
Brutus | 3:6c54aa6db861 | 40 | int pwm_len = 0; |
Brutus | 3:6c54aa6db861 | 41 | |
Brutus | 3:6c54aa6db861 | 42 | void splitString(char stringToSplit[]); |
Brutus | 3:6c54aa6db861 | 43 | |
Brutus | 3:6c54aa6db861 | 44 | int main(int argc, char *argv[]) { |
Brutus | 3:6c54aa6db861 | 45 | |
Brutus | 3:6c54aa6db861 | 46 | for(int j = 0; j < 9; j++){ |
Brutus | 3:6c54aa6db861 | 47 | frontFans[j].period_ms(10); |
Brutus | 3:6c54aa6db861 | 48 | rearFans[j].period_ms(10); |
Brutus | 3:6c54aa6db861 | 49 | frontFans[j].write(0.0); |
Brutus | 3:6c54aa6db861 | 50 | rearFans[j].write(0.0); |
Brutus | 3:6c54aa6db861 | 51 | } |
Brutus | 3:6c54aa6db861 | 52 | |
Brutus | 3:6c54aa6db861 | 53 | /*fantest.period_ms(10); |
Brutus | 3:6c54aa6db861 | 54 | fantest.write(0.1);*/ |
Brutus | 3:6c54aa6db861 | 55 | |
Brutus | 3:6c54aa6db861 | 56 | EthernetInterface eth; // init interface ethernet de la carte |
Brutus | 3:6c54aa6db861 | 57 | UDPSocket sock; // init le socket en UDP de la carte |
Brutus | 3:6c54aa6db861 | 58 | SocketAddress echo_server; |
Brutus | 3:6c54aa6db861 | 59 | |
Brutus | 0:5ca563ae05db | 60 | eth.connect(); |
Brutus | 3:6c54aa6db861 | 61 | sock.open(ð); |
Brutus | 3:6c54aa6db861 | 62 | |
Brutus | 3:6c54aa6db861 | 63 | //eth.set_network(IP,MASK,GATEWAY); |
Brutus | 3:6c54aa6db861 | 64 | |
Brutus | 1:179393386b47 | 65 | printf("\nClient IP Address is %s \n", eth.get_ip_address()); |
Brutus | 1:179393386b47 | 66 | |
Brutus | 1:179393386b47 | 67 | echo_server.set_ip_address(ECHO_SERVER_ADDRESS); |
Brutus | 1:179393386b47 | 68 | echo_server.set_port(ECHO_SERVER_PORT); |
Brutus | 0:5ca563ae05db | 69 | |
Brutus | 3:6c54aa6db861 | 70 | fantest = 1; |
Brutus | 3:6c54aa6db861 | 71 | wait(1); |
Brutus | 3:6c54aa6db861 | 72 | fantest = 0; |
Brutus | 3:6c54aa6db861 | 73 | wait(1); |
Brutus | 1:179393386b47 | 74 | |
YuK41 | 4:fb63da7601aa | 75 | bool ack = 0; |
Brutus | 3:6c54aa6db861 | 76 | //wait(10); |
Brutus | 1:179393386b47 | 77 | |
YuK41 | 4:fb63da7601aa | 78 | while(ack == 0){ |
YuK41 | 4:fb63da7601aa | 79 | char out_buffer[256]; |
YuK41 | 4:fb63da7601aa | 80 | snprintf(out_buffer, sizeof(out_buffer), "%s", eth.get_mac_address()); |
YuK41 | 4:fb63da7601aa | 81 | sock.sendto(echo_server, out_buffer, sizeof(out_buffer)); |
YuK41 | 4:fb63da7601aa | 82 | |
YuK41 | 4:fb63da7601aa | 83 | //printf("%s\n", out_buffer); |
YuK41 | 4:fb63da7601aa | 84 | |
YuK41 | 4:fb63da7601aa | 85 | sock.set_timeout(1000); |
YuK41 | 4:fb63da7601aa | 86 | |
YuK41 | 4:fb63da7601aa | 87 | char in_buffer[256]; |
YuK41 | 4:fb63da7601aa | 88 | int n = sock.recvfrom(&echo_server, in_buffer, sizeof(in_buffer)); |
YuK41 | 4:fb63da7601aa | 89 | in_buffer[n] = '\0'; |
YuK41 | 4:fb63da7601aa | 90 | |
YuK41 | 4:fb63da7601aa | 91 | printf("%s\n", in_buffer); |
YuK41 | 4:fb63da7601aa | 92 | |
YuK41 | 4:fb63da7601aa | 93 | if(in_buffer == "got you"){ |
YuK41 | 4:fb63da7601aa | 94 | ack = 1; |
YuK41 | 4:fb63da7601aa | 95 | sock.set_timeout(-1); |
YuK41 | 4:fb63da7601aa | 96 | } |
YuK41 | 4:fb63da7601aa | 97 | } |
YuK41 | 4:fb63da7601aa | 98 | |
YuK41 | 4:fb63da7601aa | 99 | //printf(out_buffer); |
YuK41 | 4:fb63da7601aa | 100 | |
Brutus | 3:6c54aa6db861 | 101 | while(1){ |
Brutus | 3:6c54aa6db861 | 102 | char out_buffer[] = "Envoyez moi une vitesse plz !"; |
Brutus | 3:6c54aa6db861 | 103 | printf("Sending message '%s' to server (%s)\n",out_buffer,ECHO_SERVER_ADDRESS); |
Brutus | 3:6c54aa6db861 | 104 | sock.sendto(echo_server, out_buffer, sizeof(out_buffer)); |
Brutus | 3:6c54aa6db861 | 105 | |
Brutus | 3:6c54aa6db861 | 106 | char in_buffer[256]; |
Brutus | 3:6c54aa6db861 | 107 | int n = sock.recvfrom(&echo_server, in_buffer, sizeof(in_buffer)); |
Brutus | 3:6c54aa6db861 | 108 | |
Brutus | 3:6c54aa6db861 | 109 | in_buffer[n] = '\0'; |
Brutus | 3:6c54aa6db861 | 110 | |
Brutus | 3:6c54aa6db861 | 111 | printf("%s\n", in_buffer); |
Brutus | 3:6c54aa6db861 | 112 | |
Brutus | 3:6c54aa6db861 | 113 | //int buff_in = atoi(in_buffer); |
Brutus | 3:6c54aa6db861 | 114 | splitString(in_buffer); |
Brutus | 3:6c54aa6db861 | 115 | |
Brutus | 3:6c54aa6db861 | 116 | printf("%d\n%d\n%lf\n", FR, fan[2], pwm_f); |
Brutus | 3:6c54aa6db861 | 117 | |
Brutus | 3:6c54aa6db861 | 118 | //wait(10); |
Brutus | 3:6c54aa6db861 | 119 | |
Brutus | 3:6c54aa6db861 | 120 | switch(FR){ |
Brutus | 3:6c54aa6db861 | 121 | case 0: // front fans |
Brutus | 3:6c54aa6db861 | 122 | for(int i = 0; i < fan_len; i++){ |
Brutus | 3:6c54aa6db861 | 123 | frontFans[fan[i] - 1].write(pwm_f); |
Brutus | 3:6c54aa6db861 | 124 | } |
Brutus | 3:6c54aa6db861 | 125 | break; |
Brutus | 3:6c54aa6db861 | 126 | |
Brutus | 3:6c54aa6db861 | 127 | case 1: // rear fans |
Brutus | 3:6c54aa6db861 | 128 | for(int i = 0; i < fan_len; i++){ |
Brutus | 3:6c54aa6db861 | 129 | rearFans[fan[i] - 1].write(pwm_f); |
Brutus | 3:6c54aa6db861 | 130 | } |
Brutus | 3:6c54aa6db861 | 131 | break; |
Brutus | 3:6c54aa6db861 | 132 | |
Brutus | 3:6c54aa6db861 | 133 | case 2: // both fans |
Brutus | 3:6c54aa6db861 | 134 | for(int i = 0; i < fan_len; i++){ |
Brutus | 3:6c54aa6db861 | 135 | frontFans[fan[i] - 1].write(pwm_f); |
Brutus | 3:6c54aa6db861 | 136 | rearFans[fan[i] - 1].write(pwm_f); |
Brutus | 3:6c54aa6db861 | 137 | } |
Brutus | 3:6c54aa6db861 | 138 | break; |
Brutus | 3:6c54aa6db861 | 139 | |
Brutus | 3:6c54aa6db861 | 140 | default: |
Brutus | 3:6c54aa6db861 | 141 | for(int j = 0; j < 9; j++){ |
Brutus | 3:6c54aa6db861 | 142 | frontFans[j].write(0.0); |
Brutus | 3:6c54aa6db861 | 143 | rearFans[j].write(0.0); |
Brutus | 3:6c54aa6db861 | 144 | } |
Brutus | 3:6c54aa6db861 | 145 | } |
Brutus | 3:6c54aa6db861 | 146 | |
Brutus | 3:6c54aa6db861 | 147 | //wait(5); |
Brutus | 3:6c54aa6db861 | 148 | } |
Brutus | 1:179393386b47 | 149 | } |
Brutus | 1:179393386b47 | 150 | |
Brutus | 3:6c54aa6db861 | 151 | void splitString(char stringToSplit[]){ |
Brutus | 3:6c54aa6db861 | 152 | char *comma_1 = strchr(stringToSplit, ',') + 1; |
Brutus | 3:6c54aa6db861 | 153 | char *comma_2 = strchr(comma_1, ',') + 1; |
Brutus | 3:6c54aa6db861 | 154 | char *comma_3 = strchr(comma_3, '\0'); |
Brutus | 3:6c54aa6db861 | 155 | |
Brutus | 3:6c54aa6db861 | 156 | FR_len = comma_1 - stringToSplit - 1; |
Brutus | 3:6c54aa6db861 | 157 | fan_len = comma_2 - comma_1 - 1; |
Brutus | 3:6c54aa6db861 | 158 | pwm_len = comma_3 - comma_2; |
Brutus | 3:6c54aa6db861 | 159 | |
Brutus | 3:6c54aa6db861 | 160 | strncpy(FR_str, stringToSplit, 1); |
Brutus | 3:6c54aa6db861 | 161 | FR = atoi(FR_str); |
Brutus | 3:6c54aa6db861 | 162 | |
Brutus | 3:6c54aa6db861 | 163 | strncpy(fan_str, comma_1, fan_len); |
Brutus | 3:6c54aa6db861 | 164 | for(int i = 0; i < fan_len; i++){ |
Brutus | 3:6c54aa6db861 | 165 | fan[i] = fan_str[i] - '0'; |
Brutus | 3:6c54aa6db861 | 166 | } |
Brutus | 3:6c54aa6db861 | 167 | |
Brutus | 3:6c54aa6db861 | 168 | strncpy(pwm_str, comma_2, pwm_len); |
Brutus | 3:6c54aa6db861 | 169 | int pwm = atoi(pwm_str); |
Brutus | 3:6c54aa6db861 | 170 | pwm_f = (float)pwm/100.0; |
Brutus | 3:6c54aa6db861 | 171 | } |