Axeda AMMMP sample code for the mbed NXP LPC1768 Prototyping Board
Dependencies: EthernetInterface mbed-rtos mbed
Dependents: AxedaGo-mbed_WIZnetInterface
main.cpp@3:359e019da3b9, 2014-06-23 (annotated)
- Committer:
- AxedaCorp
- Date:
- Mon Jun 23 20:08:09 2014 +0000
- Revision:
- 3:359e019da3b9
- Parent:
- 2:3402e0dbab6b
- Child:
- 4:22d6467147a5
Updated strlen; Changed URL; changed to oil_level
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" |
AxedaCorp | 0:ab4e84579da0 | 3 | |
AxedaCorp | 0:ab4e84579da0 | 4 | EthernetInterface eth; |
AxedaCorp | 0:ab4e84579da0 | 5 | |
AxedaCorp | 0:ab4e84579da0 | 6 | AnalogIn pot1(p19); |
AxedaCorp | 0:ab4e84579da0 | 7 | TCPSocketConnection sock; |
AxedaCorp | 0:ab4e84579da0 | 8 | |
AxedaCorp | 0:ab4e84579da0 | 9 | DigitalOut led1(LED1); |
AxedaCorp | 0:ab4e84579da0 | 10 | DigitalOut led2(LED2); |
AxedaCorp | 0:ab4e84579da0 | 11 | DigitalOut led3(LED3); |
AxedaCorp | 0:ab4e84579da0 | 12 | DigitalOut led4(LED4); |
AxedaCorp | 0:ab4e84579da0 | 13 | |
AxedaCorp | 0:ab4e84579da0 | 14 | int main() |
AxedaCorp | 0:ab4e84579da0 | 15 | { |
AxedaCorp | 0:ab4e84579da0 | 16 | |
AxedaCorp | 0:ab4e84579da0 | 17 | char *MODEL = "mbed"; |
AxedaCorp | 3:359e019da3b9 | 18 | char *SERIAL_NUM = "nlr__jrogers_axeda_com___6239433"; |
AxedaCorp | 0:ab4e84579da0 | 19 | float DEADBAND = 0.015; |
AxedaCorp | 0:ab4e84579da0 | 20 | |
AxedaCorp | 0:ab4e84579da0 | 21 | char* ip; |
AxedaCorp | 0:ab4e84579da0 | 22 | |
AxedaCorp | 3:359e019da3b9 | 23 | int http_cmd_sz=800; |
AxedaCorp | 3:359e019da3b9 | 24 | char http_cmd[http_cmd_sz]; |
AxedaCorp | 3:359e019da3b9 | 25 | int buffer_sz=300; |
AxedaCorp | 3:359e019da3b9 | 26 | char buffer[buffer_sz]; |
AxedaCorp | 0:ab4e84579da0 | 27 | int returnCode = 0; |
AxedaCorp | 0:ab4e84579da0 | 28 | |
AxedaCorp | 0:ab4e84579da0 | 29 | led1 = 1; |
AxedaCorp | 0:ab4e84579da0 | 30 | led2 = 1; |
AxedaCorp | 0:ab4e84579da0 | 31 | led3 = 1; |
AxedaCorp | 0:ab4e84579da0 | 32 | led4 = 1; |
AxedaCorp | 0:ab4e84579da0 | 33 | |
AxedaCorp | 0:ab4e84579da0 | 34 | printf("initializing Ethernet\r\n"); |
AxedaCorp | 0:ab4e84579da0 | 35 | returnCode = eth.init(); //Use DHCP |
AxedaCorp | 0:ab4e84579da0 | 36 | |
AxedaCorp | 0:ab4e84579da0 | 37 | if ( returnCode == 0 ) |
AxedaCorp | 0:ab4e84579da0 | 38 | { |
AxedaCorp | 0:ab4e84579da0 | 39 | printf(" - Ethernet ready\r\n"); |
AxedaCorp | 0:ab4e84579da0 | 40 | led1 = returnCode; |
AxedaCorp | 0:ab4e84579da0 | 41 | } |
AxedaCorp | 0:ab4e84579da0 | 42 | else |
AxedaCorp | 0:ab4e84579da0 | 43 | { |
AxedaCorp | 0:ab4e84579da0 | 44 | printf(" - Could not initialize Ethernet - ending\r\n"); |
AxedaCorp | 0:ab4e84579da0 | 45 | return 0; |
AxedaCorp | 0:ab4e84579da0 | 46 | } |
AxedaCorp | 0:ab4e84579da0 | 47 | |
AxedaCorp | 0:ab4e84579da0 | 48 | printf("Ethernet.connecting \r\n"); |
AxedaCorp | 0:ab4e84579da0 | 49 | returnCode = eth.connect(); |
AxedaCorp | 0:ab4e84579da0 | 50 | printf(" - connecting returned %d \r\n", returnCode); |
AxedaCorp | 0:ab4e84579da0 | 51 | led2 = returnCode != -1 ? 0: 1; |
AxedaCorp | 0:ab4e84579da0 | 52 | printf("Trying to get IP address..\r\n"); |
AxedaCorp | 0:ab4e84579da0 | 53 | ip = eth.getIPAddress(); |
AxedaCorp | 0:ab4e84579da0 | 54 | led3 = strlen(ip)<4 ? 1: 0; |
AxedaCorp | 0:ab4e84579da0 | 55 | printf(" - IP address:%s\r\n", ip); |
AxedaCorp | 0:ab4e84579da0 | 56 | |
AxedaCorp | 3:359e019da3b9 | 57 | float oil_level = 0.0; |
AxedaCorp | 0:ab4e84579da0 | 58 | float oldPotVal = -2.0; |
AxedaCorp | 0:ab4e84579da0 | 59 | |
AxedaCorp | 0:ab4e84579da0 | 60 | while(1) { |
AxedaCorp | 0:ab4e84579da0 | 61 | |
AxedaCorp | 3:359e019da3b9 | 62 | oil_level = pot1.read(); |
AxedaCorp | 0:ab4e84579da0 | 63 | |
AxedaCorp | 3:359e019da3b9 | 64 | if ( abs(oil_level - oldPotVal) < DEADBAND) |
AxedaCorp | 0:ab4e84579da0 | 65 | { |
AxedaCorp | 0:ab4e84579da0 | 66 | continue; |
AxedaCorp | 0:ab4e84579da0 | 67 | } |
AxedaCorp | 0:ab4e84579da0 | 68 | else |
AxedaCorp | 0:ab4e84579da0 | 69 | { |
AxedaCorp | 0:ab4e84579da0 | 70 | led4 = 1; |
AxedaCorp | 3:359e019da3b9 | 71 | oldPotVal = oil_level; |
AxedaCorp | 3:359e019da3b9 | 72 | printf("Sending Value %.2f\n\r", oil_level); |
AxedaCorp | 3:359e019da3b9 | 73 | sock.connect("toolbox-stage-connect.axeda.com", 80); |
AxedaCorp | 0:ab4e84579da0 | 74 | |
AxedaCorp | 3:359e019da3b9 | 75 | snprintf(http_cmd, http_cmd_sz, "POST /ammp/data/1/%s!%s HTTP/1.1\r\nContent-Type: application/json\r\nContent-Length: 47\r\n\r\n{\"data\":[{\"di\":{\"oil_level\":%.2f}}]}\r\n\r\n", MODEL, SERIAL_NUM, oil_level); |
AxedaCorp | 3:359e019da3b9 | 76 | sock.send_all(http_cmd, strlen(http_cmd)-1); |
AxedaCorp | 0:ab4e84579da0 | 77 | |
AxedaCorp | 0:ab4e84579da0 | 78 | while ( (returnCode = sock.receive(buffer, sizeof(buffer)-1)) > 0) |
AxedaCorp | 0:ab4e84579da0 | 79 | { |
AxedaCorp | 0:ab4e84579da0 | 80 | buffer[returnCode] = '\0'; |
AxedaCorp | 0:ab4e84579da0 | 81 | printf("Received %d chars from server:\n\r%s\n", returnCode, buffer); |
AxedaCorp | 0:ab4e84579da0 | 82 | } |
AxedaCorp | 0:ab4e84579da0 | 83 | led4 = returnCode; |
AxedaCorp | 0:ab4e84579da0 | 84 | sock.close(); |
AxedaCorp | 0:ab4e84579da0 | 85 | } |
AxedaCorp | 0:ab4e84579da0 | 86 | |
AxedaCorp | 0:ab4e84579da0 | 87 | } |
AxedaCorp | 0:ab4e84579da0 | 88 | |
AxedaCorp | 0:ab4e84579da0 | 89 | } |