Axeda AMMMP sample code for the mbed NXP LPC1768 Prototyping Board
Dependencies: EthernetInterface mbed-rtos mbed
Dependents: AxedaGo-mbed_WIZnetInterface
main.cpp@2:3402e0dbab6b, 2013-10-29 (annotated)
- Committer:
- AxedaCorp
- Date:
- Tue Oct 29 16:51:32 2013 +0000
- Revision:
- 2:3402e0dbab6b
- Parent:
- 1:16dd56f0f6c7
- Child:
- 3:359e019da3b9
Update protocol to latest version
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 | 1:16dd56f0f6c7 | 18 | char *SERIAL_NUM = "abc-123"; |
AxedaCorp | 0:ab4e84579da0 | 19 | float DEADBAND = 0.015; |
AxedaCorp | 0:ab4e84579da0 | 20 | |
AxedaCorp | 0:ab4e84579da0 | 21 | char* ip; |
AxedaCorp | 0:ab4e84579da0 | 22 | |
AxedaCorp | 0:ab4e84579da0 | 23 | char http_cmd[300]; |
AxedaCorp | 0:ab4e84579da0 | 24 | char buffer[300]; |
AxedaCorp | 0:ab4e84579da0 | 25 | int returnCode = 0; |
AxedaCorp | 0:ab4e84579da0 | 26 | |
AxedaCorp | 0:ab4e84579da0 | 27 | led1 = 1; |
AxedaCorp | 0:ab4e84579da0 | 28 | led2 = 1; |
AxedaCorp | 0:ab4e84579da0 | 29 | led3 = 1; |
AxedaCorp | 0:ab4e84579da0 | 30 | led4 = 1; |
AxedaCorp | 0:ab4e84579da0 | 31 | |
AxedaCorp | 0:ab4e84579da0 | 32 | printf("initializing Ethernet\r\n"); |
AxedaCorp | 0:ab4e84579da0 | 33 | returnCode = eth.init(); //Use DHCP |
AxedaCorp | 0:ab4e84579da0 | 34 | |
AxedaCorp | 0:ab4e84579da0 | 35 | if ( returnCode == 0 ) |
AxedaCorp | 0:ab4e84579da0 | 36 | { |
AxedaCorp | 0:ab4e84579da0 | 37 | printf(" - Ethernet ready\r\n"); |
AxedaCorp | 0:ab4e84579da0 | 38 | led1 = returnCode; |
AxedaCorp | 0:ab4e84579da0 | 39 | } |
AxedaCorp | 0:ab4e84579da0 | 40 | else |
AxedaCorp | 0:ab4e84579da0 | 41 | { |
AxedaCorp | 0:ab4e84579da0 | 42 | printf(" - Could not initialize Ethernet - ending\r\n"); |
AxedaCorp | 0:ab4e84579da0 | 43 | return 0; |
AxedaCorp | 0:ab4e84579da0 | 44 | } |
AxedaCorp | 0:ab4e84579da0 | 45 | |
AxedaCorp | 0:ab4e84579da0 | 46 | printf("Ethernet.connecting \r\n"); |
AxedaCorp | 0:ab4e84579da0 | 47 | returnCode = eth.connect(); |
AxedaCorp | 0:ab4e84579da0 | 48 | printf(" - connecting returned %d \r\n", returnCode); |
AxedaCorp | 0:ab4e84579da0 | 49 | led2 = returnCode != -1 ? 0: 1; |
AxedaCorp | 0:ab4e84579da0 | 50 | printf("Trying to get IP address..\r\n"); |
AxedaCorp | 0:ab4e84579da0 | 51 | ip = eth.getIPAddress(); |
AxedaCorp | 0:ab4e84579da0 | 52 | led3 = strlen(ip)<4 ? 1: 0; |
AxedaCorp | 0:ab4e84579da0 | 53 | printf(" - IP address:%s\r\n", ip); |
AxedaCorp | 0:ab4e84579da0 | 54 | |
AxedaCorp | 0:ab4e84579da0 | 55 | float potVal = 0.0; |
AxedaCorp | 0:ab4e84579da0 | 56 | float oldPotVal = -2.0; |
AxedaCorp | 0:ab4e84579da0 | 57 | |
AxedaCorp | 0:ab4e84579da0 | 58 | while(1) { |
AxedaCorp | 0:ab4e84579da0 | 59 | |
AxedaCorp | 0:ab4e84579da0 | 60 | potVal = pot1.read(); |
AxedaCorp | 0:ab4e84579da0 | 61 | |
AxedaCorp | 0:ab4e84579da0 | 62 | if ( abs(potVal - oldPotVal) < DEADBAND) |
AxedaCorp | 0:ab4e84579da0 | 63 | { |
AxedaCorp | 0:ab4e84579da0 | 64 | continue; |
AxedaCorp | 0:ab4e84579da0 | 65 | } |
AxedaCorp | 0:ab4e84579da0 | 66 | else |
AxedaCorp | 0:ab4e84579da0 | 67 | { |
AxedaCorp | 0:ab4e84579da0 | 68 | led4 = 1; |
AxedaCorp | 0:ab4e84579da0 | 69 | oldPotVal = potVal; |
AxedaCorp | 0:ab4e84579da0 | 70 | printf("Sending Value %.2f\n\r", potVal); |
AxedaCorp | 0:ab4e84579da0 | 71 | sock.connect("dev6-connect.axeda.com", 52689); |
AxedaCorp | 0:ab4e84579da0 | 72 | |
AxedaCorp | 2:3402e0dbab6b | 73 | sprintf(http_cmd, "POST /ammp/data/1/%s!%s HTTP/1.1\r\nContent-Type: application/json\r\nContent-Length: 47\r\n\r\n{\"data\":[{\"di\":{\"bar\":\"camp\",\"pot1\":%.2f}}]}\r\n\r\n", MODEL, SERIAL_NUM,potVal); |
AxedaCorp | 0:ab4e84579da0 | 74 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
AxedaCorp | 0:ab4e84579da0 | 75 | |
AxedaCorp | 0:ab4e84579da0 | 76 | while ( (returnCode = sock.receive(buffer, sizeof(buffer)-1)) > 0) |
AxedaCorp | 0:ab4e84579da0 | 77 | { |
AxedaCorp | 0:ab4e84579da0 | 78 | buffer[returnCode] = '\0'; |
AxedaCorp | 0:ab4e84579da0 | 79 | printf("Received %d chars from server:\n\r%s\n", returnCode, buffer); |
AxedaCorp | 0:ab4e84579da0 | 80 | } |
AxedaCorp | 0:ab4e84579da0 | 81 | led4 = returnCode; |
AxedaCorp | 0:ab4e84579da0 | 82 | sock.close(); |
AxedaCorp | 0:ab4e84579da0 | 83 | } |
AxedaCorp | 0:ab4e84579da0 | 84 | |
AxedaCorp | 0:ab4e84579da0 | 85 | } |
AxedaCorp | 0:ab4e84579da0 | 86 | |
AxedaCorp | 0:ab4e84579da0 | 87 | } |