Axeda AMMP sample code for the STMicro Nucleo-F103RB and W5500 Ethernet Shield
Dependencies: W5500Interface mbed
main.cpp@7:4ca0e960882a, 2015-05-17 (annotated)
- Committer:
- kzl108
- Date:
- Sun May 17 13:45:00 2015 +0000
- Revision:
- 7:4ca0e960882a
- Parent:
- 6:1f7647c5691a
Axeda AMMP sample code for the mbed STMicro Nucleo-F103RB and W5500 Ethernet Shield
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AxedaCorp | 0:ab4e84579da0 | 1 | #include "mbed.h" |
AxedaCorp | 0:ab4e84579da0 | 2 | #include "EthernetInterface.h" |
kzl108 | 7:4ca0e960882a | 3 | |
kzl108 | 7:4ca0e960882a | 4 | #define USE_DHCP 1 |
kzl108 | 7:4ca0e960882a | 5 | |
kzl108 | 7:4ca0e960882a | 6 | const char * IP_Addr = "192.168.0.194"; |
kzl108 | 7:4ca0e960882a | 7 | const char * IP_Subnet = "255.255.255.0"; |
kzl108 | 7:4ca0e960882a | 8 | const char * IP_Gateway = "192.168.0.1"; |
kzl108 | 7:4ca0e960882a | 9 | unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x12,0x34,0x56}; |
kzl108 | 7:4ca0e960882a | 10 | |
kzl108 | 7:4ca0e960882a | 11 | #if defined(TARGET_NUCLEO_F103RB) |
kzl108 | 7:4ca0e960882a | 12 | SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk |
kzl108 | 7:4ca0e960882a | 13 | EthernetInterface eth(&spi, PB_6, PA_9); // spi, cs, reset |
kzl108 | 7:4ca0e960882a | 14 | |
kzl108 | 7:4ca0e960882a | 15 | AnalogIn pot1(PA_0); // A0, Analog Input 0, F103RB |
kzl108 | 7:4ca0e960882a | 16 | Serial pc(USBTX, USBRX); |
kzl108 | 7:4ca0e960882a | 17 | #else |
kzl108 | 7:4ca0e960882a | 18 | EthernetInterface eth; |
AxedaCorp | 0:ab4e84579da0 | 19 | |
kzl108 | 7:4ca0e960882a | 20 | AnalogIn pot1(p19); |
kzl108 | 7:4ca0e960882a | 21 | AnalogIn pot2(p20); |
kzl108 | 7:4ca0e960882a | 22 | #endif |
kzl108 | 7:4ca0e960882a | 23 | |
AxedaCorp | 0:ab4e84579da0 | 24 | TCPSocketConnection sock; |
kzl108 | 7:4ca0e960882a | 25 | |
kzl108 | 7:4ca0e960882a | 26 | #if defined(TARGET_LPC1768) |
AxedaCorp | 0:ab4e84579da0 | 27 | DigitalOut led1(LED1); |
AxedaCorp | 0:ab4e84579da0 | 28 | DigitalOut led2(LED2); |
AxedaCorp | 0:ab4e84579da0 | 29 | DigitalOut led3(LED3); |
AxedaCorp | 0:ab4e84579da0 | 30 | DigitalOut led4(LED4); |
kzl108 | 7:4ca0e960882a | 31 | #endif |
AxedaCorp | 0:ab4e84579da0 | 32 | |
AxedaCorp | 0:ab4e84579da0 | 33 | int main() |
AxedaCorp | 0:ab4e84579da0 | 34 | { |
AxedaCorp | 0:ab4e84579da0 | 35 | |
AxedaCorp | 0:ab4e84579da0 | 36 | char *MODEL = "mbed"; |
kzl108 | 7:4ca0e960882a | 37 | //char *SERIAL_NUM = "SerialNumber"; |
kzl108 | 7:4ca0e960882a | 38 | char *SERIAL_NUM = "nlr__kevinlee_wiznet_co_kr___5138257"; |
AxedaCorp | 0:ab4e84579da0 | 39 | float DEADBAND = 0.015; |
AxedaCorp | 0:ab4e84579da0 | 40 | |
AxedaCorp | 0:ab4e84579da0 | 41 | char* ip; |
AxedaCorp | 0:ab4e84579da0 | 42 | |
AxedaCorp | 3:359e019da3b9 | 43 | int http_cmd_sz=800; |
AxedaCorp | 3:359e019da3b9 | 44 | char http_cmd[http_cmd_sz]; |
AxedaCorp | 3:359e019da3b9 | 45 | int buffer_sz=300; |
AxedaCorp | 3:359e019da3b9 | 46 | char buffer[buffer_sz]; |
AxedaCorp | 0:ab4e84579da0 | 47 | int returnCode = 0; |
AxedaCorp | 0:ab4e84579da0 | 48 | |
kzl108 | 7:4ca0e960882a | 49 | #if defined(TARGET_LPC1768) |
AxedaCorp | 0:ab4e84579da0 | 50 | led1 = 1; |
AxedaCorp | 0:ab4e84579da0 | 51 | led2 = 1; |
AxedaCorp | 0:ab4e84579da0 | 52 | led3 = 1; |
AxedaCorp | 0:ab4e84579da0 | 53 | led4 = 1; |
kzl108 | 7:4ca0e960882a | 54 | #endif |
AxedaCorp | 0:ab4e84579da0 | 55 | |
AxedaCorp | 0:ab4e84579da0 | 56 | printf("initializing Ethernet\r\n"); |
kzl108 | 7:4ca0e960882a | 57 | //returnCode = eth.init(); //Use DHCP |
kzl108 | 7:4ca0e960882a | 58 | #if USE_DHCP |
kzl108 | 7:4ca0e960882a | 59 | returnCode = eth.init(MAC_Addr); //Use DHCP |
kzl108 | 7:4ca0e960882a | 60 | #else |
kzl108 | 7:4ca0e960882a | 61 | returnCode = eth.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway); |
kzl108 | 7:4ca0e960882a | 62 | #endif |
AxedaCorp | 0:ab4e84579da0 | 63 | |
AxedaCorp | 0:ab4e84579da0 | 64 | if ( returnCode == 0 ) |
AxedaCorp | 0:ab4e84579da0 | 65 | { |
AxedaCorp | 0:ab4e84579da0 | 66 | printf(" - Ethernet ready\r\n"); |
kzl108 | 7:4ca0e960882a | 67 | #if defined(TARGET_LPC1768) |
AxedaCorp | 0:ab4e84579da0 | 68 | led1 = returnCode; |
kzl108 | 7:4ca0e960882a | 69 | #endif |
AxedaCorp | 0:ab4e84579da0 | 70 | } |
AxedaCorp | 0:ab4e84579da0 | 71 | else |
AxedaCorp | 0:ab4e84579da0 | 72 | { |
AxedaCorp | 0:ab4e84579da0 | 73 | printf(" - Could not initialize Ethernet - ending\r\n"); |
AxedaCorp | 0:ab4e84579da0 | 74 | return 0; |
AxedaCorp | 0:ab4e84579da0 | 75 | } |
AxedaCorp | 0:ab4e84579da0 | 76 | |
AxedaCorp | 0:ab4e84579da0 | 77 | printf("Ethernet.connecting \r\n"); |
AxedaCorp | 0:ab4e84579da0 | 78 | returnCode = eth.connect(); |
AxedaCorp | 0:ab4e84579da0 | 79 | printf(" - connecting returned %d \r\n", returnCode); |
kzl108 | 7:4ca0e960882a | 80 | #if defined(TARGET_LPC1768) |
AxedaCorp | 0:ab4e84579da0 | 81 | led2 = returnCode != -1 ? 0: 1; |
kzl108 | 7:4ca0e960882a | 82 | #endif |
AxedaCorp | 0:ab4e84579da0 | 83 | printf("Trying to get IP address..\r\n"); |
AxedaCorp | 0:ab4e84579da0 | 84 | ip = eth.getIPAddress(); |
kzl108 | 7:4ca0e960882a | 85 | #if defined(TARGET_LPC1768) |
AxedaCorp | 0:ab4e84579da0 | 86 | led3 = strlen(ip)<4 ? 1: 0; |
kzl108 | 7:4ca0e960882a | 87 | #endif |
AxedaCorp | 0:ab4e84579da0 | 88 | printf(" - IP address:%s\r\n", ip); |
AxedaCorp | 0:ab4e84579da0 | 89 | |
AxedaCorp | 3:359e019da3b9 | 90 | float oil_level = 0.0; |
AxedaCorp | 4:22d6467147a5 | 91 | float oil_level2= 0.0; |
AxedaCorp | 0:ab4e84579da0 | 92 | float oldPotVal = -2.0; |
AxedaCorp | 4:22d6467147a5 | 93 | float oldPotVal2 = -2.0; |
AxedaCorp | 0:ab4e84579da0 | 94 | |
AxedaCorp | 0:ab4e84579da0 | 95 | while(1) { |
AxedaCorp | 0:ab4e84579da0 | 96 | |
AxedaCorp | 3:359e019da3b9 | 97 | oil_level = pot1.read(); |
kzl108 | 7:4ca0e960882a | 98 | #if defined(TARGET_LPC1768) |
AxedaCorp | 4:22d6467147a5 | 99 | oil_level2=pot2.read(); |
kzl108 | 7:4ca0e960882a | 100 | #endif |
AxedaCorp | 0:ab4e84579da0 | 101 | |
AxedaCorp | 4:22d6467147a5 | 102 | if ( abs(oil_level - oldPotVal) < DEADBAND && abs(oil_level2 - oldPotVal2) < DEADBAND) |
AxedaCorp | 0:ab4e84579da0 | 103 | { |
AxedaCorp | 0:ab4e84579da0 | 104 | continue; |
AxedaCorp | 0:ab4e84579da0 | 105 | } |
AxedaCorp | 0:ab4e84579da0 | 106 | else |
AxedaCorp | 0:ab4e84579da0 | 107 | { |
kzl108 | 7:4ca0e960882a | 108 | //led4 = 1; |
AxedaCorp | 3:359e019da3b9 | 109 | oldPotVal = oil_level; |
kzl108 | 7:4ca0e960882a | 110 | #if defined(TARGET_LPC1768) |
AxedaCorp | 4:22d6467147a5 | 111 | oldPotVal2 = oil_level2; |
kzl108 | 7:4ca0e960882a | 112 | #endif |
AxedaCorp | 4:22d6467147a5 | 113 | printf("Sending Value for well1 %.2f\n\r", oil_level); |
kzl108 | 7:4ca0e960882a | 114 | #if defined(TARGET_LPC1768) |
AxedaCorp | 4:22d6467147a5 | 115 | printf("Sending Value for well2 %.2f\n\r", oil_level2); |
kzl108 | 7:4ca0e960882a | 116 | #endif |
AxedaCorp | 6:1f7647c5691a | 117 | sock.connect("toolbox-connect.axeda.com", 80); |
AxedaCorp | 0:ab4e84579da0 | 118 | |
AxedaCorp | 4:22d6467147a5 | 119 | snprintf(http_cmd, http_cmd_sz, "POST /ammp/data/1/%s!%s HTTP/1.1\r\nContent-Type: application/json\r\nContent-Length: 65\r\n\r\n{\"data\":[{\"di\":{\"oil_level\":%.2f, \"oil_level2\":%.2f}}]}\r\n\r\n", MODEL, SERIAL_NUM, oil_level, oil_level2); |
AxedaCorp | 5:1b8ad120cf29 | 120 | sock.send_all(http_cmd, http_cmd_sz-1); |
AxedaCorp | 0:ab4e84579da0 | 121 | |
AxedaCorp | 5:1b8ad120cf29 | 122 | while ( (returnCode = sock.receive(buffer, buffer_sz-1)) > 0) |
AxedaCorp | 0:ab4e84579da0 | 123 | { |
AxedaCorp | 0:ab4e84579da0 | 124 | buffer[returnCode] = '\0'; |
AxedaCorp | 0:ab4e84579da0 | 125 | printf("Received %d chars from server:\n\r%s\n", returnCode, buffer); |
AxedaCorp | 0:ab4e84579da0 | 126 | } |
kzl108 | 7:4ca0e960882a | 127 | //led4 = returnCode; |
AxedaCorp | 0:ab4e84579da0 | 128 | sock.close(); |
AxedaCorp | 0:ab4e84579da0 | 129 | } |
AxedaCorp | 0:ab4e84579da0 | 130 | |
AxedaCorp | 0:ab4e84579da0 | 131 | } |
AxedaCorp | 0:ab4e84579da0 | 132 | |
AxedaCorp | 0:ab4e84579da0 | 133 | } |