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.
Dependencies: ConfigFile EthernetInterface mbed-rtos mbed
Fork of S05APP3 by
main.cpp@0:ec23a7ae804c, 2017-02-09 (annotated)
- Committer:
- marc1119
- Date:
- Thu Feb 09 04:24:55 2017 +0000
- Revision:
- 0:ec23a7ae804c
- Child:
- 1:ceb3f8ba8793
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marc1119 | 0:ec23a7ae804c | 1 | #include "EthernetInterface.h" |
marc1119 | 0:ec23a7ae804c | 2 | #include "ConfigFile.h" |
marc1119 | 0:ec23a7ae804c | 3 | #include "mbed.h" |
marc1119 | 0:ec23a7ae804c | 4 | |
marc1119 | 0:ec23a7ae804c | 5 | //To set the xbee |
marc1119 | 0:ec23a7ae804c | 6 | Serial xbee(p13, p14); |
marc1119 | 0:ec23a7ae804c | 7 | DigitalOut reset(p8); |
marc1119 | 0:ec23a7ae804c | 8 | |
marc1119 | 0:ec23a7ae804c | 9 | //Socket Buffer |
marc1119 | 0:ec23a7ae804c | 10 | char buffer[50]; |
marc1119 | 0:ec23a7ae804c | 11 | char serverAddrvalue[32]; |
marc1119 | 0:ec23a7ae804c | 12 | |
marc1119 | 0:ec23a7ae804c | 13 | //Read the config file |
marc1119 | 0:ec23a7ae804c | 14 | void readConfigFile() |
marc1119 | 0:ec23a7ae804c | 15 | { |
marc1119 | 0:ec23a7ae804c | 16 | LocalFileSystem local("local"); |
marc1119 | 0:ec23a7ae804c | 17 | ConfigFile cfg; |
marc1119 | 0:ec23a7ae804c | 18 | char *serverAddr = "serverAddr"; |
marc1119 | 0:ec23a7ae804c | 19 | if (!cfg.read("/local/input.cfg")) error("Failure to read a configuration file.\n"); |
marc1119 | 0:ec23a7ae804c | 20 | else cfg.getValue(serverAddr, &serverAddrvalue[0], sizeof(serverAddrvalue)); |
marc1119 | 0:ec23a7ae804c | 21 | } |
marc1119 | 0:ec23a7ae804c | 22 | |
marc1119 | 0:ec23a7ae804c | 23 | //Init RJ45 and use DHCP |
marc1119 | 0:ec23a7ae804c | 24 | void initRJ45() |
marc1119 | 0:ec23a7ae804c | 25 | { |
marc1119 | 0:ec23a7ae804c | 26 | EthernetInterface rj45; |
marc1119 | 0:ec23a7ae804c | 27 | rj45.init(); |
marc1119 | 0:ec23a7ae804c | 28 | rj45.connect(); |
marc1119 | 0:ec23a7ae804c | 29 | } |
marc1119 | 0:ec23a7ae804c | 30 | |
marc1119 | 0:ec23a7ae804c | 31 | void initSocket() |
marc1119 | 0:ec23a7ae804c | 32 | { |
marc1119 | 0:ec23a7ae804c | 33 | //Init Socket |
marc1119 | 0:ec23a7ae804c | 34 | TCPSocketConnection sock; |
marc1119 | 0:ec23a7ae804c | 35 | sock.connect(serverAddrvalue, 502); |
marc1119 | 0:ec23a7ae804c | 36 | |
marc1119 | 0:ec23a7ae804c | 37 | sprintf (buffer, serverAddrvalue); |
marc1119 | 0:ec23a7ae804c | 38 | sock.send_all(buffer, sizeof(buffer)-1); |
marc1119 | 0:ec23a7ae804c | 39 | |
marc1119 | 0:ec23a7ae804c | 40 | int repSize; |
marc1119 | 0:ec23a7ae804c | 41 | while (true) { |
marc1119 | 0:ec23a7ae804c | 42 | repSize = sock.receive(buffer, sizeof(buffer)-1); |
marc1119 | 0:ec23a7ae804c | 43 | if (repSize <= 0) { |
marc1119 | 0:ec23a7ae804c | 44 | printf("Error"); |
marc1119 | 0:ec23a7ae804c | 45 | sock.close(); |
marc1119 | 0:ec23a7ae804c | 46 | break; |
marc1119 | 0:ec23a7ae804c | 47 | } |
marc1119 | 0:ec23a7ae804c | 48 | buffer[repSize] = '\0'; |
marc1119 | 0:ec23a7ae804c | 49 | printf("Received %d chars from server:\n%s\n", repSize, buffer); |
marc1119 | 0:ec23a7ae804c | 50 | } |
marc1119 | 0:ec23a7ae804c | 51 | } |
marc1119 | 0:ec23a7ae804c | 52 | |
marc1119 | 0:ec23a7ae804c | 53 | int main() { |
marc1119 | 0:ec23a7ae804c | 54 | |
marc1119 | 0:ec23a7ae804c | 55 | readConfigFile(); |
marc1119 | 0:ec23a7ae804c | 56 | initRJ45(); |
marc1119 | 0:ec23a7ae804c | 57 | initSocket(); |
marc1119 | 0:ec23a7ae804c | 58 | |
marc1119 | 0:ec23a7ae804c | 59 | while(1) {} |
marc1119 | 0:ec23a7ae804c | 60 | } |
marc1119 | 0:ec23a7ae804c | 61 |