In this lab, you will be collecting light data with your sensor and sending it wirelessly over the lora network to the server. In this lab you will: Utilize the system that you created for lab 9 to collect light data. Collect a light sample each hour for 24 hours. Send the 24 samples 1 time per day. Run your system over a period of 2 days in Luddy 4150 to collect light data on this room.

Dependencies:   ATParser TSL2561 mbed

main.cpp

Committer:
csinders
Date:
2018-04-13
Revision:
0:8d98d7f7d672
Child:
1:fb25b13e6ab3

File content as of revision 0:8d98d7f7d672:

#include "mbed.h"
#include "string.h"
#include "TSL2561.h"
#include "ATParser.h"

BufferedSerial pc(SERIAL_TX, SERIAL_RX);
BufferedSerial device(PA_9, PA_10);  // tx, rx
DigitalOut myled(LED1);
TSL2561 lightsensor(PB_7,PB_6);

/*  Checks to see if the xDot has a buffered message OK to 
    confirm that operation has succeeded
*/
int confirmOK() { 
    // Wait until device has a response 
    while (!device.readable()) {
        wait_ms(100);
    }
    int bufferSize = 100;
    char buf[bufferSize];
    int i = 0;
    for (i = 0; i < bufferSize; i++) {
        buf[i] = 0;
    }
    i = 0;
    
    //Read message from device into buffer
    for (i = 0; device.readable(); i++) { 
        buf[i] = device.getc();
        if (buf[i] == '\n') { 
        // Read new line so check to see if we got an OK or Error
        // else keep reading til we get one
        //pc.printf("The message from the device is %s\r\n",buf);
            if (!strncmp("OK",buf,2)) {
                //pc.printf("I compared against %s and got OK\r\n",buf);
                return 1;
            } else if (!strncmp("ERROR",buf,5)) { 
                //pc.printf("I compared against %s and got ERROR\r\n",buf);
                return 0;
            }
            return confirmOK();
        }
        if (i >= bufferSize) { 
            return 0;
        }
    }
    return confirmOK();
}
/*  Sends a message to the xDot
    Messages are expected to end in \n so that an OK confirmation will be 
    recieved
*/
int sendAtMessage(char * message) {
    while (*message) { 
        device.putc(*message);
        message++;
    }
    return confirmOK();
}

/*
Send a message to the xDot without checking for acknowledge
*/
void sendAtMessageNACK(char * message, int size) {
    int i = 0;
    while (i < size) { 
        device.putc(*message);
        //wait_ms(500); // Not succesfully sending the message unless it waits.
        //pc.printf("putting %c on device\n\r", *message);
        message++;
        i++;
    }
}
int main() {
    pc.baud(115200);
    device.baud(115200);
    myled = 1;
    wait(1);
    myled = 0;
  
    pc.printf("Starting program\n\r");  
    if (!sendAtMessage("AT\n")) { 
        pc.printf("xDot not responding to message\n\r");
    }
    //Try to connect xDot to network
    // Provice network name
    char networkName[] = "AT+NI=1,MTCDT-19400691\n";
    if (!sendAtMessage(networkName)) {
        pc.printf("Unable to set network name\n\r");
    }
    //Provide network passphrase
    char networkPassphrase[] = "AT+NK=1,MTCDT-19400691\n";
    if (!sendAtMessage(networkPassphrase)) { 
        pc.printf("Network passphrase not successfully set\n\r");
    }
    //Set the frequency sub band
    char frequencySubBand[] = "AT+FSB=1\n";
    if (!sendAtMessage(frequencySubBand)) { 
        pc.printf("Network frequency SubBand not successfully set\n\r");
    }
    if (!sendAtMessage("AT+TXDR3")) {
        pc.printf("Unable to set dataRate\n\r");
    }
    //pc.printf("I got here\r\n");
    
    //Join the network
    char joinNetwork[] = "AT+JOIN\n"; // Unable to join network atm.
    if (!sendAtMessage(joinNetwork)) { 
        pc.printf("Failed to joined the network\n\r");
    }
    
    int i = 0;
    while(1) {
        for (i = 0; i < 24; i++) {
            sendAtMessageNACK("AT+SEND=",8);
            int lightBufSize = 10;
            double light = lightsensor.lux();
            char lightBuf[lightBufSize];
            //pc.printf("Current light is %f\n\r",light);
            sprintf(lightBuf, "%6.2f",light);
            //pc.printf("After convert with sprintf light = %s\n\r",lightBuf);
            sendAtMessageNACK(lightBuf,lightBufSize);
            sendAtMessageNACK(",",1);
            wait(3600);
        }
        if (sendAtMessage("\n")) {
            pc.printf("message succesfully sent\n\r");
            myled = 1;
        }  else {
            pc.printf("Failed to send the message for some reason\r\n");   
        }
    }
    
    
    //When an s is read from the serial pc,
    // read the current pressure/temperature from the I2C pressure sensor
    // and send it to the MQTT server pivot.iuiot
    //Unsure of what size to set Buffer so that it doesn't go over
    //when trying to send a message
    
    /*
    while(1) {
        if (pc.readable()) {
            //pc.printf("pc.readable\r\n");
            if (pc.getc() == 's') { 
                pc.printf("I recieved an s\r\n");
                int tempBufSize = 4;
                int presBufSize = 6;
                double temp = pressure_sensor.getTemperature();
                char tempBuf[tempBufSize];
                double pres = pressure_sensor.getPressure();
                char presBuf[presBufSize];
                pc.printf("Current temp is %f\n\r",temp);
                pc.printf("Current pres is %f\n\r",pres);
                sprintf(tempBuf, "%2.1f",temp);
                sprintf(presBuf, "%4.1f",pres);
                pc.printf("After convert with sprintf temp = %s\n\r",tempBuf);
                pc.printf("After convert with sprintf pres = %s\n\r",presBuf);
                sendAtMessageNACK("AT+SEND=",8);
                sendAtMessageNACK(tempBuf,tempBufSize);
                sendAtMessageNACK(",",1);
                sendAtMessageNACK(presBuf,presBufSize);
                wait_ms(500);
                if (sendAtMessage("\n")) { 
                    pc.printf("message succesfully sent\n\r");
                }  else {
                    pc.printf("Failed to send the message for some reason\r\n");   
                }
            }
        }
    }
    */
    
    /*
    while(1) {
        if(pc.readable()) {
            device.putc(pc.getc());
            myled = !myled;
        }
        if(device.readable()) {
            pc.putc(device.getc());
            myled = !myled;
        }
    }
    */
}