Axeda AMMMP sample code for the mbed NXP LPC1768 Prototyping Board

Dependencies:   EthernetInterface mbed-rtos mbed

Dependents:   AxedaGo-mbed_WIZnetInterface

main.cpp

Committer:
AxedaCorp
Date:
2014-06-25
Revision:
5:1b8ad120cf29
Parent:
4:22d6467147a5
Child:
6:1f7647c5691a

File content as of revision 5:1b8ad120cf29:

#include "mbed.h"
#include "EthernetInterface.h"
 
EthernetInterface eth;
 
AnalogIn pot1(p19);
AnalogIn pot2(p20);
TCPSocketConnection sock;
 
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
 
int main() 
{
    
    char *MODEL = "mbed";
    char *SERIAL_NUM = "SerialNumber"; 
    float DEADBAND = 0.015;
    
    char* ip;
    
    int http_cmd_sz=800;
    char http_cmd[http_cmd_sz];
    int buffer_sz=300;  
    char buffer[buffer_sz];  
    int returnCode = 0;
    
    led1 = 1;
    led2 = 1;
    led3 = 1;
    led4 = 1;
    
    printf("initializing Ethernet\r\n");
    returnCode = eth.init(); //Use DHCP
 
    if ( returnCode == 0 )
    {
        printf(" - Ethernet ready\r\n");
        led1 = returnCode;
    }
    else
    {
        printf(" - Could not initialize Ethernet - ending\r\n");
        return 0;
    }
    
    printf("Ethernet.connecting \r\n");
    returnCode = eth.connect();
    printf(" - connecting returned %d \r\n", returnCode);
    led2 = returnCode != -1 ? 0: 1;
    printf("Trying to get IP address..\r\n");
    ip = eth.getIPAddress();
    led3 = strlen(ip)<4 ? 1: 0;
    printf("  -  IP address:%s\r\n", ip);
    
    float oil_level = 0.0;
    float oil_level2= 0.0;
    float oldPotVal = -2.0;
    float oldPotVal2 = -2.0;
    
    while(1) {
    
        oil_level = pot1.read();
        oil_level2=pot2.read();
        
        if ( abs(oil_level - oldPotVal) < DEADBAND && abs(oil_level2 - oldPotVal2) < DEADBAND)
        {
            continue;   
        }
        else
        {
            led4 = 1;
            oldPotVal = oil_level;
            oldPotVal2 = oil_level2;
            printf("Sending Value for well1 %.2f\n\r", oil_level);
            printf("Sending Value for well2 %.2f\n\r", oil_level2);
            sock.connect("toolbox-stage-connect.axeda.com", 80);
 
            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);
            sock.send_all(http_cmd, http_cmd_sz-1);
    
            while ( (returnCode = sock.receive(buffer, buffer_sz-1)) > 0)
            {
                buffer[returnCode] = '\0';
                printf("Received %d chars from server:\n\r%s\n", returnCode, buffer);
            }
            led4 = returnCode;
            sock.close();
        }
 
    }
 
}