Example node for Yodiwo's Plegma API

Dependencies:   EthernetInterface FXOS8700Q HTTPClient HTTPD MQTTS SDFileSystem YodiwoPlegma mbed-rpc mbed-rtos mbed wolfSSL

main.cpp

Committer:
mitsarionas
Date:
2015-09-01
Revision:
0:00797f1ebe04
Child:
2:b7489c070d1f

File content as of revision 0:00797f1ebe04:

#include "mbed.h"
#include "rtos.h"
#include "EthernetInterface.h"
#include "MQTTEthernet.h"
#include "MQTTClient.h"
#include "HTTPD.h"
//#include "HTTPServer.h"
//#include "LocalFileSystem.h"
#include "SDFileSystem.h"                  // SD File System functions
//#include "HTTPRequestHandler.h"
#include "FsHandler.h"
#include "RpcHandler.h"
#include "pairing_backend.h"

#define DAT0 PTE3                          // MOSI
#define CMD  PTE1                          // MISO
#define CLK  PTE2                          // SCLK
#define CD   PTE4                          // CS


EthernetInterface eth;

DigitalOut led1(LED1);
InterruptIn sw2(SW2);
uint32_t button_pressed;
Thread *thread2;
//HTTPServer server;
HTTPD *httpd;
//  Instantiate a local file system handler named 'local' which will be used later to access files on the mbed.
//LocalFileSystem local("local");
 
 
SDFileSystem sd(DAT0, CMD, CLK, CD, "sd"); // MOSI, MISO, SCLK, CS
//Serial pc(USBTX, USBRX);                   // Virtual COM Port


pairing_context pairing_state;

void pairing_done(char *nodeKey, char *secretKey);

void sw2_press(void)
{
    thread2->signal_set(0x1);
}

void led_thread(void const *argument)
{
    while (true) {
        led1 = !led1;
        Thread::wait(1000);
    }
}

void button_thread(void const *argument)
{
    while (true) {
        Thread::signal_wait(0x1);
        button_pressed++;
    }
}

void httpd_thread(void const *arg)
{
//    HTTPD *server;
//    server = HTTPD::getInstance();
//    
//    server->start(8080);
}

void callback_pairing (int id);

int main()
{
    Thread thread(led_thread);
//    thread2 = new Thread(button_thread);
    
//    FILE *File = fopen("/sd/sdfile.txt", "w");   // open file
//    if(File == NULL) {                           // check if present
//        printf("No SD Card or bad format\n"); // print message
//    } else {                                     // otherwise
//        printf("Ready to write\n");           // message preparing to write
//    }
//    fprintf(File, "FRDM-K64F");                  // write data
//    fclose(File);                                // close file on SD

    
    eth.init();
    
    eth.connect();
    printf("IP Address is %s\n", eth.getIPAddress());
    
//    MQTTEthernet ipstack = MQTTEthernet();             
//    MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
//    

//    TCPSocketConnection sock;
//    sock.connect("mbed.org", 80);
//    
//    char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n";
//    sock.send_all(http_cmd, sizeof(http_cmd)-1);
//    
//    char buffer[300];
//    int ret;
//    while (true) {
//        ret = sock.receive(buffer, sizeof(buffer)-1);
//        if (ret <= 0)
//            break;
//        buffer[ret] = '\0';
//        printf("Received %d chars from server:\n%s\n", ret, buffer);
//    }
//      
//    sock.close();
    
//    HTTPFsRequestHandler::mount("/sd/", "/");
//    server.addHandler<HTTPFsRequestHandler>("/");

//    HTTPFsRequestHandler::mount("/sd/", "/");
//    server.addHandler<HTTPRpcRequestHandler>("/");
//
//    if (!server.start(80, &eth)) {
//        error("Server not starting !");
//        exit(0);
//    }
//    
    httpd = new HTTPD;
//    httpd->attach("/cgi-bin/", &callback_cgi);
//    httpd->attach("/ws/", &callback_ws);
    httpd->attach("/pairing_functions/", &callback_pairing);
    httpd->attach("/", "/sd/www/");
    httpd->start(80);

//    Thread thread3(httpd_thread);
//    eth.disconnect();
//    

    button_pressed = 0;
    sw2.fall(&sw2_press);
    while (true) {
        Thread::wait(5000);
//        printf("SW2 was pressed (last 5 seconds): %d \n", button_pressed);
        fflush(stdout);
//        button_pressed = 0;
    }
}

void callback_pairing (int id) {
    int i, n;
    char buf[256];
    
    if (!strcmp(httpd->getFilename(id), "start_pairing")) {
        printf("starting pairing...\n");
        pairing_context_init_with_defaults(&pairing_state, pairing_done);
        start_pairing(&pairing_state);
    }
    strcpy(buf, httpd->getFilename(id));
    strcat(buf, "\r\n");
    strcat(buf, httpd->getQueryString(id));
    strcat(buf, "\r\n");
    n = strlen(buf);
 
    i = httpd->receive(id, &buf[n], sizeof(buf) - n);
    if (i < 0) return;
    i += n;
    buf[i] = 0;
 
    printf("CGI %d %s\r\n", id, buf);
    httpd->send(id, buf, i, "Content-Type: text/plain\r\n");
}

void pairing_done(char *nodeKey, char *secretKey)
{
    printf("pairing done!\n");
    printf("NokeKey: %s\n", nodeKey);
    printf("SecretKey: %s\n", secretKey);
}