Connections to Xively working; has 5 channels on Xively (axl_x, axl_y, axl_z, heater_status, temperature)

Dependencies:   C12832_lcd EthernetInterface LM75B MMA7660 NTPClient libxively mbed-rtos mbed

main.cpp

Committer:
wren301
Date:
2014-05-18
Revision:
3:3a48ed10d6fe
Parent:
2:cf992d90396e
Child:
4:05986b9ea330

File content as of revision 3:3a48ed10d6fe:

#include "mbed.h"
#include "EthernetInterface.h"
#include "NTPClient.h"
#include "C12832_lcd.h"
#include "LM75B.h"
#include "MMA7660.h"
#include "Websocket.h"

#define PST_OFFSET  7*60*60

Ticker timer;
C12832_LCD lcd; //Graphics LCD
EthernetInterface eth; //Networking functions
NTPClient ntp; //NTP client
LM75B tmp(p28,p27);
float lowTemp = 74;
float highTemp = 86;
float temp;
int heaterOn = 0;
AnalogIn pot1(p20);
AnalogIn pot2(p19);
#define LOOP_DELAY_MS 100
//update time every 10 minutes
#define UPDATE_TIME 60*1
MMA7660 acc(p28, p27);// accelerometer
int updateTimeFromServer = 1;

void connectToTheInternet()
{
    eth.init(); //Init and use DHCP
    wait(2);
    lcd.cls();
    lcd.printf("Getting IP Address\r\n");
    printf("\n\rGetting IP Address\r\n");
    if(eth.connect(60000)!=0) {
        lcd.printf("DHCP error - No IP");
        wait(10);
    } else {
        lcd.printf("IP is %s\n", eth.getIPAddress());
        printf("IP is %s\n", eth.getIPAddress());
        wait(2);
    }
    lcd.cls();
}

void updateTimeRoutine()
{
    if (ntp.setTime("0.pool.ntp.org") == 0) {
        printf("Time updated!");
    } else {
        lcd.locate(0,0);
        printf("Time update failed \n\r");
        lcd.printf("Time update failed");
    }
    updateTimeFromServer = 0;
}

void updateTime()
{
    updateTimeFromServer = 1;
}

void sendTempJSON()
{
    char json_str[100];
    // See the output on http://sockets.mbed.org/app-board/viewer
    Websocket ws("ws://sockets.mbed.org:443/ws/app-board/wo");
    ws.connect();
    // create json string with acc/tmp data
    sprintf(json_str, "{\"id\":\"app_board_eth_EW2013\",\"ax\":%d,\"ay\":%d,\"az\":%d, \"tmp\":%d}", (int)(acc.x()*360), (int)(acc.y()*360), (int)(acc.z()*360), (int)tmp.read());
    // send str
    ws.send(json_str);
    ws.close();
}

//POT values are 0:1, will allow high and low temperatures
//lets you set temp between 50 and 100 degrees
void updateTempsFromPots()
{
    float lowTempPot = pot2*50.0+50;
    float highTempPot = pot1*50.0+50;
    //round to the nearest whole number
    lowTempPot = floor(lowTempPot+.5);
    highTempPot = floor(highTempPot+.5);
    //high temp must be at least 1 degree above low temp
    if (highTempPot <= lowTempPot) {
        highTempPot = lowTempPot +1;
    }
    //Refresh the display if the temps have changed by more than a degree
    if ((lowTemp != lowTempPot) || (highTemp != highTempPot)) {
        lowTemp = lowTempPot;
        highTemp = highTempPot;
        lcd.locate(0,0);
        lcd.printf("\n\r%.1f LOW: %.0f HIGH: %.0f", temp, lowTemp, highTemp);
        printf("LOW: %.2f HIGH: %.2f\n\r", lowTemp, highTemp);
    }
}

int main()
{
    connectToTheInternet();
    //Variable to hold the current minute so we only update the display when the minute changes
    char currentMinute[2];
    currentMinute[1] = 'a';
    char minute[2];

    float currentTemp = -200;
    lcd.printf("Updating time...\r\n");
    printf("Updating time...\r\n");
    if (ntp.setTime("0.pool.ntp.org") == 0) {
        printf("Set time successfully\r\n");
        lcd.cls();
        timer.attach(&updateTime, UPDATE_TIME);
        lcd.printf("\n\r\n\rHEATER OFF");

        while(1) {
            if(updateTimeFromServer) {
                updateTimeRoutine();
            }
            //Sets temp from POTs
            updateTempsFromPots();
            //Fetch the time
            time_t ctTime;
            ctTime = time(NULL)- PST_OFFSET;
            char timeBuffer[32];

            //See if the minute has changed; set an update display flag if it has
            strftime(minute, 8, "%M", localtime(&ctTime));
            if ( (minute[1] != currentMinute[1]) ) {
                //Formats the time for display
                strftime(timeBuffer, 32, "%a %b %d %I:%M%p\n\r", localtime(&ctTime));
                printf("%s\r", timeBuffer);
                lcd.locate(0,0);
                lcd.printf("%s\r", timeBuffer);
                currentMinute[1] = minute[1];
            }

            //Update the temperature display if the temperature, set temp has changed
            temp = tmp.read()*9.0/5.0 + 32.0;

            //checks if the temperature (rounded to the nearest whole number) has changed
            if (floor(temp+.5) != floor(currentTemp+.5)) {
                printf("Temp change: %.1f F\n\r", temp);
                currentTemp = temp;
                lcd.locate(0,0);
                lcd.printf("\n\r%.1f LOW: %.0f HIGH: %.0f", temp, lowTemp, highTemp);
                sendTempJSON();
            }

            lcd.locate(0,0);
            //Heater logic: turns off if it has gone over the high temp, on if under the low temp
            if (heaterOn && (temp > highTemp) ) {
                printf("Heater turned OFF\n\r");
                heaterOn = 0;
                lcd.locate(0,0);
                lcd.printf("\n\r\n\rHEATER OFF");
            } else if (!heaterOn && (temp < lowTemp) ) {
                printf("Heater turned ON\n\r");
                heaterOn = 1;
                lcd.locate(0,0);
                lcd.printf("\n\r\n\rHEATER ON");
            }
            wait(LOOP_DELAY_MS*.001);
        }
    } else {
        lcd.printf("NTP Error\r\n");
    }
    eth.disconnect();
}