GDP group 24 node core

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue

main.cpp

Committer:
Trumple
Date:
2014-11-18
Revision:
1:27b35752c5d0
Parent:
0:fcab3b154e49
Child:
2:1cbb20dd1733

File content as of revision 1:27b35752c5d0:

#include "mbed.h"
#include "snail.h"
#include "sensorinterface.h"
#include "sdcard.h"
#include "http.h"

#define DEBUG

time_t lastPollTime = 0;
time_t pollInterval = 30;
time_t timestamp = 0;

bool isBasenode = false;

char localAddress[8];
char baseAddress[8];
bool networkParametersUpdated = false;
bool networkParametersTimedOut = false;
 
Serial pc(USBTX, USBRX);
snail xbee = snail();
Ticker networkParametersTimeout;

void handleBaseAddressReply(snail::message& message)
{
    snail::baseaddressreply& reply = static_cast<snail::baseaddressreply&>(message);
    memcpy(baseAddress, reply.baseAddress, sizeof(baseAddress));
    //TODO: Get timestamp
    networkParametersUpdated = true;
}

void handleBaseAddressRequest(snail::message& message)
{
    if (isBasenode)
    {
        snail::baseaddressreply reply(snail::broadcastAddress, localAddress);
        xbee.send(reply, sizeof(reply));
    }
}

void handleNetworkParametersTimeout()
{
    networkParametersTimedOut = true;
}

void getLocalAddress()
{
    //TODO: get local address from xbee
}

void getNetworkParameters()
{
    #ifdef DEBUG
        pc.printf("[MAIN] Requesting time and base address...\r\n");
    #endif
    
    xbee.registerMessageCallback(MESSAGE_BASE_ADDRESS_REPLY, handleBaseAddressReply);
    
    networkParametersTimeout.attach(&handleNetworkParametersTimeout, 10.0);
    
    snail::baseaddressrequest request;
    xbee.send(request, sizeof(request));
    
    while(!networkParametersUpdated)
    {
        if (networkParametersTimedOut)
        {
            #ifdef DEBUG
                pc.printf("[MAIN] Timed out, retrying...\r\n");
            #endif
            xbee.send(request, sizeof(request));
            networkParametersTimedOut = false;
        }
        xbee.readMessage();
    }
    
    #ifdef DEBUG
        pc.printf("[MAIN] Got network parameters\r\n");
    #endif
    networkParametersTimeout.detach();
}

int main()
{
    #ifdef DEBUG
        pc.printf("[MAIN] Starting up...\r\n");
        printf("                         .       .\r\n");
        printf("                        / `.   .' \\\r\n");
        printf("                .---.  <    > <    >  .---.\r\n");
        printf("                |    \\  \\ - ~ ~ - /  /    |\r\n");
        printf("                 ~-..-~             ~-..-~\r\n");
        printf("             \\~~~\\.'                    `./~~~/\r\n");
        printf("   .-~~^-.    \\__/                        \\__/\r\n");
        printf(" .'  O    \\     /               /       \\  \\\r\n");
        printf("(_____,    `._.'               |         }  \\/~~~/\r\n");
        printf(" `----.          /       }     |        /    \\__/\r\n");
        printf("       `-.      |       /      |       /      `. ,~~|\r\n");
        printf("           ~-.__|      /_ - ~ ^|      /- _      `..-'   f: f:\r\n");
        printf("                |     /        |     /     ~-.     `-. _||_||_\r\n");
        printf("                |_____|        |_____|         ~ - . _ _ _ _ _>\r\n");
        printf("\r\n");
        printf("\r\n");
        printf("         Sensorsaurus | Team 24 GDP | Southampton | 2014\r\n");
        printf("\r\n");
        printf("\r\n");
    #endif
    
    //sdcard sd = sdcard();
    
    //TODO: read basenode pin
    #ifdef DEBUG
        pc.printf("[MAIN] Basenode switch: %i\r\n", isBasenode);
    #endif
    
    //TODO: load configuration from SD
    
    if (isBasenode)
    {
        getLocalAddress();
        http h = http();
        //TODO: get timestamp from API
        set_time(timestamp);
        
        xbee.registerMessageCallback(MESSAGE_BASE_ADDRESS_REQUEST, handleBaseAddressRequest);
        
        while(1)
        {
            #ifdef DEBUG
                pc.printf("[MAIN] Basenode is idle\r\n");
            #endif
            
            wait(5);
        }
    }
    else
    {   
        sensorinterface sensors = sensorinterface();
        getNetworkParameters();
        set_time(timestamp);
        
        while(1)
        {
            xbee.readMessage();
            //TODO: if xbee interrupt has woken us up
                //transmit 10 latest readings
            
            //check if it's time to poll TODO: add check to see if sensorinterface is ready
            if (time(NULL) - lastPollTime > 10)
            {
                #ifdef DEBUG
                    pc.printf("[MAIN] Requesting data...\r\n");
                #endif
                sensors.requestData();
                lastPollTime = time(NULL);
            }
            
            //if there is data waiting for us...
            if (sensors.isDataWaiting())
            {
                #ifdef DEBUG
                    pc.printf("[MAIN] Data waiting, reading data...\r\n");
                #endif
                
                d_reply data = sensors.readData();
                
                #ifdef DEBUG
                for (int i = 0; i < data.readings.size(); i++)
                    pc.printf("0x%.4X|", data.readings[i]);
                #endif
                
                //log
                //sd.write(static_cast<long int>(time(NULL)), data);
            }
        }
    }
}