Local copy

Dependencies:   C12832_lcd ConfigFile EthernetInterface LM75B MMA7660 MQTTPacket mbed-rtos mbed

Fork of IBMIoTClientExampleForLPC1768 by Sam Danbury

src/main.cpp

Committer:
rajathishere
Date:
2014-07-01
Revision:
7:2c5d0dbd7985
Parent:
6:a022f983f94b
Child:
8:e58e10ca4352

File content as of revision 7:2c5d0dbd7985:

/*******************************************************************************
* Copyright (c) 2014 IBM Corporation and other Contributors.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Sam Danbury
* IBM - Initial Contribution
*******************************************************************************/

#include "stdio.h"
#include "mbed.h"
#include "rtos.h"
#include "EthernetInterface.h"
#include "C12832_lcd.h"
#include "LM75B.h"
#include "MMA7660.h"
#include "C12832_lcd.h"
#include "QuickstartClient.h"
#include "QuickstartMessage.h"

#include <string>

using namespace std;

//LCD
C12832_LCD lcd;

//LED
DigitalOut led1(LED1);

//Accelerometer
MMA7660 MMA(p28, p27);

//Temperature sensor
LM75B tmp(p28, p27);

//Joystick
BusIn Down(p12);
BusIn Left(p13);
BusOut Click(p14);
BusIn Up(p15);
BusIn Right(p16);
string joystickPos;
void joystickThread(void const *args);

//Potentiometer
AnalogIn ain1(p19);
AnalogIn ain2(p20);
float pot1;
float pot2;

LocalFileSystem local("local");

int main()
{
    //RAJATH's COMMIT
    //Connect to the network
    EthernetInterface eth;
    eth.init();
    eth.connect();
    
    //Obtain mac address of mbed
    string mac = eth.getMACAddress();
    
    //Remove colons from mac address
    mac.erase(remove(mac.begin(), mac.end(), ':'), mac.end());
    
    //Start thread to read data from joystick
    Thread jThd(joystickThread);
    joystickPos = "CENTRE";
    
    QuickstartClient* c = new QuickstartClient(mac);
    lcd.cls();
    lcd.locate(0,0);
        
    lcd.printf("Mac address: %s\n", mac);
    wait(3.0);
    
    //c->subscribe();
    char value[10];
    //string data_points = "";
    while(1) 
    {
        //Initialize lcd
        //Flash led to show message has been sent successfully
        led1 = 1;
        string data_points = "\"myName\": \"IoT mbed\"";
        //Construct quickstart message with desired datapoints
        //QuickstartMessage* m = new QuickstartMessage();
        
        //m->add("accelX", MMA.x());
        sprintf(value, "%0.4f", MMA.x());
        data_points+=",\"accelX\":";
        data_points+=value;
        //m->add("accelY", MMA.y());
        sprintf(value, "%0.4f", MMA.y());
        data_points+=",\"accelY\":";
        data_points+=value;
        //m->add("accelZ", MMA.z());
        sprintf(value, "%0.4f", MMA.z());
        data_points+=",\"accelZ\":";
        data_points+=value;
        //m->add("temp", tmp.read());
        sprintf(value, "%0.4f", tmp.read());
        data_points+=",\"temp\":";
        data_points+=value;
        //m->add("joystick", joystickPos);
        data_points+=",\"joystick\":" + joystickPos;
        pot1 = ain1;
        pot2 = ain2;
        //m->add("potentiometer1", pot1);
        sprintf(value, "%0.4f", pot1);
        data_points+=",\"potentiometer1\":";
        data_points+=value;
        //m->add("potentiometer2", pot2);
        sprintf(value, "%0.4f", pot1);
        data_points+=",\"potentiometer1\":";
        data_points+=value;
        
        string message = "{\"d\":{" + data_points + "}}";
        
        //Message is converted from datapoints into json format and then published
        c->publish(message);//c->publish(m->convertToJson());
        lcd.printf("After");
        wait(3.0);
        //if (m) {
          //  delete m;
        //}
        
        led1 = 0;
        
        //Message published every second
        wait(1);
    }
    
    eth.disconnect();
}

void joystickThread(void const *args) {
    while (1) {
        if (Down)
            joystickPos = "DOWN";
        else if (Left)
            joystickPos = "LEFT";
        else if (Click)
            joystickPos = "CLICK";
        else if (Up)
            joystickPos = "UP";
        else if (Right)
            joystickPos = "RIGHT";
        else
            joystickPos = "CENTRE";
    }
}