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