Importing without issues

Dependencies:   AES CRC DHT JSON MQTT UDAES WIZnetInterface mbed-src

Fork of Mbed_Prototype_copy_4_INNO_day_15_6_2017 by Irayya Mathad

main.cpp

Committer:
irayya
Date:
2017-05-29
Revision:
0:c2a1e0a5d567
Child:
1:903414009683

File content as of revision 0:c2a1e0a5d567:

#include "mbed.h"
#include "DHT.h"
#include "AES.h"
#include "MQTTEthernet.h"
#include "MQTTClient.h"
#include "string.h"
#include "udaes.h"
#include "crc.h"
#define ECHO_SERVER_PORT   7
char msg[16];
//char message1[16];
//char message2[16];
//char sensormsg[10];
//char ackmsg[17];
//char newmsg[10];
char o_message[16];
char send_message[18];

char dataReceive[19];
char encData[16];
char decData[16];
char message[16];
int h;

char crc_check[3];

int arrivedcount = 0;

void crcCheck(int []);
Serial out(USBTX,USBRX);
Serial device(PA_13,PA_14);
void bluetooth();




void messageArrived(MQTT::MessageData& md)
{

    MQTT::Message &message = md.message;
    printf("\nMessage arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);            //printing a message on console  including qos,dup,retained,packetid
    printf("%s\n", message.payloadlen, (char*)message.payload);
    printf(" message arrived is : %.*s\n", message.payloadlen, message.payload);
    printf("%d\n", message.payloadlen);

    memcpy(msg,message.payload, message.payloadlen);                            // copy the data from message payload to msg
    printf("Original message: %s", msg);

    printData(msg, 16);

    printf("the msg is  %s\n:", msg);

    char* encrypt = encryptData(msg);                                           //encryptData() function for  data encryption
    int x3,x4;
    //Print the encrypted message
    printf("Encrypted message: \"%s\"", encrypt);
    printData(encrypt, sizeof(encrypt));                                        // printData() function for converting encrypted message to hex format

    char* encrypt2 = encrypt;                                                   // crcCheck_transmit() for crc calculation
    char* crc_check2=crcCheck_transmit(encrypt2);
    for(x3=0; x3<16; x3++) {                                                    // copy the encrypted data to another char array
        send_message[x3]=encrypt[x3];
    }
    for(x3=16,x4=0; x3<18; x3++,x4++) {                                                                 //
        send_message[x3]=crc_check2[x4];
    }

    out.printf("\nThe final data to be sent is : %s",send_message);
    device.printf(send_message);


    ++arrivedcount;
}
void baud(int baudrate)
{
    Serial s(USBTX, USBRX);
    s.baud(baudrate);
}
int main (void)
{
    //DHT sensor(D4, DHT11);

    char* topic = "light1";                                                                             //topic name for MQTT
    MQTTEthernet ipstack = MQTTEthernet();
    MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
    char* hostname = "172.16.73.22";                                                                    // MQTT broker address
    int port = 1883;                                                                                    //port no for broker
    int rc = ipstack.connect(hostname, port);
    if (rc == 0)                                                                 // 0 means it ois connected (from TCP connect)
        printf("rc from TCP connect is %d\n", rc);


    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    data.MQTTVersion = 3;
    //data.clientID.cstring = "parents";

    if ((rc = client.connect(data)) == 0)                                       // 0 means it ois connected (from MQTT connect)
        out.printf("rc from MQTT connect is %d\n", rc);

    if ((rc = client.subscribe(topic, MQTT::QOS1, messageArrived)) == 0)        // subscribe to the topic by topic name

        out.printf("rc from MQTT subscribe is %d\n", rc);


    MQTT::Message message;
    char buf[100];
    int error = 0;

    h=0;
    int p=0;
    while (true) {

        if(device.readable()) {
            p=0;
            bluetooth();                                                        // colling bluetooth() function for data reading from slave device
            out.printf("\nThe received message is : %s",dataReceive);           // printing the message after data reading from slave device




            int flg=crcCheck_receive(dataReceive);                              //calling  crcCheck_receive() function for checking crc is ok or not
            if(flg==0) {                                                        // checking for correct crc
                out.printf("\n CRC ok");
            } else
                out.printf("\n CRC failed");
            // else for crc failed
            for(int q=2; q<=17; q++) {                                          // for data storing in another char array by removing dummi start bits
                encData[p]=dataReceive[q];
                // out.printf("%c\n",dataReceive[q]);
                p++;
            }



            char* decryptedData = decryptData(encData);                         // calling decryptData() function for decrypt the data

            out.printf("\nThe final decrypted Data is: %s",decryptedData);      // printing the decrypted data


            // publishing

            message.qos = MQTT::QOS0;
            message.retained = false;                                           //A retained message is a normal MQTT message with the retained flag set to true. The broker will store the last retained message and the corresponding QoS for that topic.
                                                                                // if it is false The broker will store the last retained message.
            message.dup = false;                                                // the broker will not create duplicate message 
            message.payload = (void*)decryptedData;                             // decrypting the message

            message.payloadlen = strlen(decryptedData);                         // finding the length of the message

            rc = client.publish("bulb", message);                               // publishing the message using topic name and message
            memset(dataReceive,'\0',sizeof(dataReceive));                       // clearing the dataReceive buffer
            memset(msg,'\0',sizeof(msg));                                       // clearing the msg buffer
        }
        client.yield(20);
    }
}
void bluetooth()                                                                //bluetooth function for reading data from slave device
{
    int z=0;
    while(1) {
        if(device.readable()) {
            char c=device.getc();
            if(c=='\0') {                                                       // if char is null then return to main function where it was called
                z=0;
                return;
            } else {
                dataReceive[z]=c;                                  //
                printf("%c",c);
                z++;
            }
        }
    }
}