Wireless interface using LoRa technology

Dependencies:   AlohaTransceiver RingBuffer SX1276Lib_inAir SerialInterfaceProtocol mbed L3PDU

Revision:
14:80cee3991860
Parent:
11:c7c0036efdbd
Child:
15:cfc7d817e444
--- a/main.cpp	Wed Aug 10 01:22:45 2016 +0000
+++ b/main.cpp	Thu Aug 11 03:44:11 2016 +0000
@@ -3,6 +3,8 @@
 #include "buffer.h"
 #include "SerialInterfaceProtocol.h"
 #include "AlohaFrame.h"
+#include "ControlPacket.h"
+#include "DataBlockPacket.h"
 
 Serial pc(USBTX, USBRX);
 
@@ -17,6 +19,17 @@
 Timer timer;
 InterruptIn button(USER_BUTTON);
 
+// sensors
+#define SUPPLY_VOLTAGE 3.3;
+AnalogIn TempSensor(PA_0);
+
+float getTemperature() 
+{
+    float reading = TempSensor.read();
+    float output_voltage = reading * SUPPLY_VOLTAGE;
+    return (output_voltage - 0.25) / 0.028;
+}
+
 void serialInterruptHandler() {
     // Note: you need to actually read from the serial to clear the RX interrupt
     int c = pc.getc();
@@ -54,35 +67,6 @@
     return 0;
 }
 
-int sendMessage(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
-{
-    static uint8_t seqid = 0;
-    
-    // prepare for the frame
-    txFrame.setType(AlohaFrame::Aloha_Data);
-    txFrame.setPayloadLength(0x0);
-    txFrame.setSourceAddress(aloha.getDeviceId());
-    txFrame.setDestinationAddress(0x0);
-    txFrame.setFullMessageFlag(0x1);
-    txFrame.setSequenceID(seqid);
-    txFrame.generateCrc();
-    
-    
-    uint8_t buffer[20];
-    memset(buffer, 0x0, sizeof(buffer));
-    txFrame.serialize(buffer);
-    
-    aloha.send(buffer, 20);
-    
-    seqid += 1;
-    
-    // start the timer to measure round trip time
-    timer.reset();
-    timer.start();
-    
-    return 0;   
-}
-
 /*
  * Format: 
  * <        :start flag
@@ -388,35 +372,102 @@
     return 0;
 }
 
-void AlohaDataHandler(AlohaFrame *frame)
+
+void AlohaDataPacketHandler(uint8_t *payload, uint8_t payload_length, uint8_t src_addr)
 {
-    // stop the timer to measure round trip time
-    timer.stop();
+    // try to decode packet
+    BasicPacket packet(payload);
+    
+    // verify crc
+    // skip for current development
+    
+    // process the packet based on different feature id
+    BasicPacket::L3Fid_t fid = (BasicPacket::L3Fid_t) packet.getFid();
     
-    printf("-----------------------------------------\r\n");
-    printf(">Received Frame\r\n");
-    printf(">    Type: 0x%x, PayloadLength: 0x%x\r\n", frame->getType(), frame->getPayloadLength());
-    printf(">    SrcAddr: 0x%x, DestAddr: 0x%x\r\n", frame->getSourceAddress(), frame->getDestinationAddress());
-    printf(">    FMF: 0x%x, SequenceID: 0x%x\r\n", frame->getFullMessageFlag(), frame->getSequenceID());
-    for (uint8_t i = 0; i < frame->getPayloadLength(); i++)
+    // we don't care about the type conversion. just create a new one.
+    switch (fid)
     {
-        printf(">    Payload[%d]: 0x%x\r\n", i, frame->getPayload(i));
+        case BasicPacket::L3ControlPacket:
+        {
+            printf("Control Packet\r\n");
+            ControlPacket controlPacket(payload);
+            
+            // execute command
+            uint8_t command = controlPacket.getCommand();
+            switch(command)
+            {
+                case 0x0:   // query
+                {
+                    uint8_t sensor_type = controlPacket.getData(0);
+                    switch (sensor_type)
+                    {
+                        case 0x0:   // temperature sensor
+                        {
+                            uint8_t temp = getTemperature();
+                            
+                            // create a response
+                            DataBlockPacket response;
+                            
+                            // set sequence id
+                            response.setSequenceID(0x0);
+                            
+                            // set source id
+                            response.setSourceID(aloha.getDeviceID());
+                            
+                            // set destination id
+                            response.setDestinationID(src_addr);
+                            
+                            // set source type (temperature)
+                            response.setSourceType(0x0);
+                            
+                            // set payload type
+                            response.setPayloadType(0x0);
+                            
+                            // copy temperature
+                            response.setData(0, temp);
+                            
+                            // calculate crc
+                            response.generateCrc();
+                            
+                            // serialize and send it
+                            uint8_t buffer[8];
+                            memset(buffer, 0x0, sizeof(buffer));
+                            
+                            // copy bytes into buffer
+                            response.serialize(buffer);
+                            
+                            // send to aloha transceiver
+                            aloha.send(buffer, 8, src_addr);
+                            
+                            break;
+                        }
+                        case 0x1:   // door sensor
+                        {
+                            break;
+                        }
+                        default:
+                            break;
+                    }
+                    
+                    break;
+                }
+                default:
+                    break;
+            }
+                
+            break;
+        }
+        case BasicPacket::L3DataBlockPacket:
+        {
+            printf("Data Packet\r\n");
+            DataBlockPacket dataBlockPacket(payload);
+            
+            // do something
+            break;
+        }
+        default:
+            break;
     }
-    printf(">    CRC: 0x%x\r\n", frame->getCrc());
-    
-    // decode payload (range test only)
-    int8_t snr = (int8_t) frame->getPayload(0);
-    
-    uint16_t rssi_h = frame->getPayload(1);
-    uint8_t rssi_l = frame->getPayload(2);
-    int16_t rssi = (int16_t) (rssi_h << 8 | rssi_l);
-    
-    
-    printf(">About this transmission:\r\n");
-    printf(">   Base Station:: Rssi: %d, Snr: %d\r\n", rssi, snr);
-    printf(">   Terminal::     Rssi: %d, Snr: %d\r\n", aloha.getRssi(), aloha.getSnr());
-    printf(">   Round trip time: %d ms\r\n", timer.read_ms());
-    printf("-----------------------------------------\r\n");
 }
 
 void enqueueString(char* s, int len) {
@@ -451,12 +502,12 @@
     
     // register callback functions for SIP
     SIP.registerCommand(0x00, toggleChecksum);
-    SIP.registerCommand(0x01, sendMessage);
+    SIP.registerCommand(0x01, NULL);                // disable sendMessage for now
     SIP.registerCommand(0x02, configureRadio);
     SIP.registerCommand(0x03, radioUpdateSettings);
     
     // register callback functions for aloha transceiver
-    aloha.registerType(AlohaFrame::Aloha_Data, AlohaDataHandler);
+    aloha.registerType(AlohaFrame::Aloha_Data, AlohaDataPacketHandler);
 
     // configure button interrupt
     button.fall(automaticPacketTransmit);