Nikita Kakurnikov
/
UDPNode
1
main.cpp@3:24f12bc2b76d, 2018-07-02 (annotated)
- Committer:
- nikitoslav
- Date:
- Mon Jul 02 09:17:05 2018 +0000
- Revision:
- 3:24f12bc2b76d
- Parent:
- 2:f149c178dd58
- Child:
- 4:e584e034f9e1
5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nikitoslav | 0:bed9e78dd09c | 1 | #include "mbed.h" |
nikitoslav | 0:bed9e78dd09c | 2 | #include <string> |
nikitoslav | 1:60fc2072f184 | 3 | #include "ESP8266.h" |
nikitoslav | 0:bed9e78dd09c | 4 | |
nikitoslav | 3:24f12bc2b76d | 5 | int localOutPort = 49100; |
nikitoslav | 3:24f12bc2b76d | 6 | int localInPort = 49101; |
nikitoslav | 1:60fc2072f184 | 7 | |
nikitoslav | 2:f149c178dd58 | 8 | Serial console(USBTX,USBRX); |
nikitoslav | 2:f149c178dd58 | 9 | ESP8266 wifi(PF_7,PF_6,localOutPort,localInPort); |
nikitoslav | 2:f149c178dd58 | 10 | |
nikitoslav | 1:60fc2072f184 | 11 | const char* ap = "Clapeyron_Industries"; |
nikitoslav | 1:60fc2072f184 | 12 | const char* passPhrase = "06737184"; |
nikitoslav | 1:60fc2072f184 | 13 | |
nikitoslav | 1:60fc2072f184 | 14 | Thread listeningThread; |
nikitoslav | 1:60fc2072f184 | 15 | |
nikitoslav | 1:60fc2072f184 | 16 | void onReceive(void); |
nikitoslav | 3:24f12bc2b76d | 17 | void processReceivedData(string, char*, int); |
nikitoslav | 0:bed9e78dd09c | 18 | |
nikitoslav | 0:bed9e78dd09c | 19 | int main() { |
nikitoslav | 2:f149c178dd58 | 20 | console.baud(9600); |
nikitoslav | 1:60fc2072f184 | 21 | if (wifi.startup(1) && wifi.connect(ap,passPhrase)) |
nikitoslav | 1:60fc2072f184 | 22 | console.printf("Your IP is: %s\n",wifi.getIPAddress()); |
nikitoslav | 0:bed9e78dd09c | 23 | else |
nikitoslav | 0:bed9e78dd09c | 24 | console.printf("Can not connect to the Wi-Fi router\n"); |
nikitoslav | 3:24f12bc2b76d | 25 | // wifi.send("privetFromESP8266",17,"192.168.0.103",8000); |
nikitoslav | 1:60fc2072f184 | 26 | listeningThread.start(onReceive); |
nikitoslav | 1:60fc2072f184 | 27 | } |
nikitoslav | 1:60fc2072f184 | 28 | |
nikitoslav | 1:60fc2072f184 | 29 | void onReceive(void) { |
nikitoslav | 2:f149c178dd58 | 30 | const int maxSize = 100; |
nikitoslav | 2:f149c178dd58 | 31 | |
nikitoslav | 1:60fc2072f184 | 32 | char buffer[100]; |
nikitoslav | 2:f149c178dd58 | 33 | string buf = ""; |
nikitoslav | 1:60fc2072f184 | 34 | char IP[16]; |
nikitoslav | 1:60fc2072f184 | 35 | int port; |
nikitoslav | 1:60fc2072f184 | 36 | int bytes; |
nikitoslav | 1:60fc2072f184 | 37 | while(1) { |
nikitoslav | 2:f149c178dd58 | 38 | buf = ""; |
nikitoslav | 2:f149c178dd58 | 39 | bytes = wifi.recv(&buffer,maxSize,IP,&port); |
nikitoslav | 1:60fc2072f184 | 40 | if (bytes != -1) { |
nikitoslav | 1:60fc2072f184 | 41 | console.printf("Bytes received: %d; from %s:%d\n",bytes,IP,port); |
nikitoslav | 2:f149c178dd58 | 42 | for(int i = 0; i < bytes; i++) { |
nikitoslav | 2:f149c178dd58 | 43 | buf += buffer[i]; |
nikitoslav | 2:f149c178dd58 | 44 | } |
nikitoslav | 2:f149c178dd58 | 45 | console.printf("Data: %s\n",buf); |
nikitoslav | 3:24f12bc2b76d | 46 | processReceivedData(buf,IP,port); |
nikitoslav | 1:60fc2072f184 | 47 | } |
nikitoslav | 1:60fc2072f184 | 48 | } |
nikitoslav | 2:f149c178dd58 | 49 | } |
nikitoslav | 2:f149c178dd58 | 50 | |
nikitoslav | 3:24f12bc2b76d | 51 | void processReceivedData(string data, char* IP, int port) { |
nikitoslav | 3:24f12bc2b76d | 52 | if (port == localOutPort) { |
nikitoslav | 3:24f12bc2b76d | 53 | string msg = "?HiClientImARobotClapeyron|hardvers|0.1?"; |
nikitoslav | 3:24f12bc2b76d | 54 | if (data == "HiRobotClapeyronImAClient") wifi.send(msg.c_str(),msg.length(),IP,localInPort); |
nikitoslav | 3:24f12bc2b76d | 55 | } |
nikitoslav | 0:bed9e78dd09c | 56 | } |