diff --git a/LoRaReceiver-struct/LoRaReceiver-struct.ino b/LoRaReceiver-struct/LoRaReceiver-struct.ino index 1b2b312..cb61c27 100644 --- a/LoRaReceiver-struct/LoRaReceiver-struct.ino +++ b/LoRaReceiver-struct/LoRaReceiver-struct.ino @@ -1,51 +1,38 @@ #include #include +#include +#include -typedef struct{ // frame structure - uint8_t IDs = 0; // station's ID - uint8_t IDp = 0; // gateway's ID - uint16_t TS = 0; // TimeStamp - uint16_t DT = 0; // Data Type - uint16_t D1 = 0; // DATA 1 - uint16_t D2 = 0; // DATA 2 - uint16_t D3 = 0; // DATA 3 -} trame; - -trame message; - -uint16_t ID; +CModemLoRa thisLoRa; +CProtocol12Bytes protocol; void setup() { SerialUSB.begin(9600); while (!SerialUSB){}; SerialUSB.println("LoRa Receiver"); - - if (!LoRa.begin(868E6)) { - Serial.println("Starting LoRa failed!"); - while (1); - } + thisLoRa.begin(); } void loop() { - int packetSize = LoRa.parsePacket(); + int packetSize = thisLoRa.parsePacket(); if (packetSize > 0) { - SerialUSB.println("Nouveau paquet"); - message.IDs = LoRa.read(); - message.IDp = LoRa.read(); - message.IDp = 0x07; - message.TS = ((uint16_t)LoRa.read() | (LoRa.read() << 8)); - message.DT = ((uint16_t)LoRa.read() | LoRa.read() << 8); - message.D1 = ((uint16_t)LoRa.read() | LoRa.read() << 8); - message.D2 = ((uint16_t)LoRa.read() | LoRa.read() << 8); - message.D3 = ((uint16_t)LoRa.read() | LoRa.read() << 8); - ID = (uint16_t)(message.IDs | message.IDp), HEX; - SerialUSB.println(ID, HEX); - SerialUSB.println(message.TS, HEX); - SerialUSB.println(message.DT, HEX); - SerialUSB.println(message.D1, HEX); - SerialUSB.println(message.D2, HEX); - SerialUSB.println(message.D3, HEX); + thisLoRa.read(&protocol); // objet thislora qui appele classe Lora.h et rempli la stucture de l'objet protocol, ser a allèger -5lignes + SerialUSB.println("Frame received"); delay(100); + SerialUSB.print("ID = "); + SerialUSB.print(protocol.getStationId(),HEX); + SerialUSB.println(protocol.getGatewayId(),HEX); + SerialUSB.print("TS = "); + SerialUSB.println(protocol.getTimestampMessage(),HEX); + SerialUSB.print("DT = "); + SerialUSB.println(protocol.getDataType(),HEX); + SerialUSB.print("D1 = "); + SerialUSB.println(protocol.getDataOne(),HEX); + SerialUSB.print("D2 = "); + SerialUSB.println(protocol.getDataTwo(),HEX); + SerialUSB.print("D3 = "); + SerialUSB.println(protocol.getDataThree(),HEX); } + } diff --git a/fakeStation/fakeStation.ino b/fakeStation/fakeStation.ino index e0678d4..1e1fdc5 100644 --- a/fakeStation/fakeStation.ino +++ b/fakeStation/fakeStation.ino @@ -1,79 +1,46 @@ #include #include +#include +#include -typedef struct -{ - uint8_t ids = 0x45; - uint8_t idp = 0x00; - uint16_t ts = 0x0000; - uint16_t dt = 0x0001; - uint16_t d1 = 0x0000; - uint16_t d2 = 0x0000; - uint16_t d3 = 0x0000; -}message; +CProtocol12Bytes protocol; +CModemLoRa thisLoRa; -message msg; +uint16_t incrTS=0x0000; void setup() { SerialUSB.begin(9600); - while (!Serial); - SerialUSB.println("LoRa Sender"); - - if (!LoRa.begin(868E6)) - { - SerialUSB.println("Starting LoRa failed!"); - while(1); - } + thisLoRa.begin(); + protocol.codeFrame(0x05,0x07,0x0000,0x0001,0x0300,0x0005,0x0000); } void loop() { - msg.ts = msg.ts + 1; - msg.d1 = random(20, 40); - msg.d2 = random(40, 60); - msg.d3 = random(0, 100); - - SerialUSB.print("ID station : "); - SerialUSB.print(msg.ids, DEC); - SerialUSB.print(", "); - SerialUSB.println(msg.ids, HEX); - - SerialUSB.print("ID passerelle : "); - SerialUSB.print(msg.idp, DEC); - SerialUSB.print(", "); - SerialUSB.println(msg.idp, HEX); - - SerialUSB.print("Numero de message : "); - SerialUSB.print(msg.ts, DEC); - SerialUSB.print(", "); - SerialUSB.println(msg.ts, HEX); - - SerialUSB.print("Type de donne : "); - SerialUSB.print(msg.dt, DEC); - SerialUSB.print(", "); - SerialUSB.println(msg.dt, HEX); - - SerialUSB.print("Donnee 1 : "); - SerialUSB.print(msg.d1, DEC); - SerialUSB.print(", "); - SerialUSB.println(msg.d1, HEX); - - SerialUSB.print("Donnee 2 : "); - SerialUSB.print(msg.d2, DEC); - SerialUSB.print(", "); - SerialUSB.println(msg.d2, HEX); + protocol.setDataOne((uint16_t)(random(500, 750))); + protocol.setDataTwo((uint16_t)(random(40, 60))); + protocol.setDataThree((uint16_t)(random(0, 1))); + incrTS = protocol.getTimestampMessage()+1; + protocol.setTimestampMessage(incrTS); + + SerialUSB.print("ID = "); + SerialUSB.print(protocol.getStationId(),HEX); + SerialUSB.println(protocol.getGatewayId(),HEX); + SerialUSB.print("TS = "); + SerialUSB.println(protocol.getTimestampMessage(),HEX); + SerialUSB.print("DT = "); + SerialUSB.println(protocol.getDataType(),HEX); + SerialUSB.print("D1 = "); + SerialUSB.println(protocol.getDataOne(),HEX); + SerialUSB.print("D2 = "); + SerialUSB.println(protocol.getDataTwo(),HEX); + SerialUSB.print("D3 = "); + SerialUSB.println(protocol.getDataThree(),HEX); - SerialUSB.print("Donnee 3 : "); - SerialUSB.print(msg.d3, DEC); - SerialUSB.print(", "); - SerialUSB.println(msg.d3, HEX); - SerialUSB.println(" "); - for(int i=0; i<3; i++){ - SerialUSB.println("Sending packet !"); + SerialUSB.println("Frame sent !"); LoRa.beginPacket(); - LoRa.write((uint8_t*)&msg, 12); + LoRa.write((uint8_t*)&protocol, 12); LoRa.endPacket(); SerialUSB.println("-----------------------------"); diff --git a/gateway/gateway.ino b/gateway/gateway.ino index 2006e12..d4ffeb8 100644 --- a/gateway/gateway.ino +++ b/gateway/gateway.ino @@ -56,13 +56,13 @@ void loop() { // SerialPrintElapsedTime(); // diplay the time the frame arrived // frame treatment -while(readFrameAndCheckTS() == true){ +while(readFrameAndCheckID() == true){ // post to server EthernetClient postClient; String postData = "ID="+String(protocol.getStationId())+"&IDp="+String(protocol.getGatewayId())+"&TS="+String(protocol.getTimestampMessage())+"&DT="+String(protocol.getDataType())+"&D1="+String(protocol.getDataOne())+"&D2="+String(protocol.getDataTwo())+"&D3="+String(protocol.getDataThree()); if (postClient.connect("btslimayrac.ovh", 80)){ postClient.print("POST /weather/formulaire/formulaireCollecteLORA.php HTTP/1.1\n"); - postClient.print("Host: weather.btslimayrac.ovh\n"); + postClient.print("Host: btslimayrac.ovh\n"); postClient.print("Connection: close\n"); postClient.print("Content-Type: application/x-www-form-urlencoded\n"); postClient.print("Content-Length: "); @@ -119,6 +119,36 @@ while(readFrameAndCheckTS() == true){ } // end if (serverGateway) } //end void loop +bool readFrameAndCheckID(){ + int id1=0; // for comparing later the LoRa frames using ID data + int id2=0; // for comparing later the LoRa frames using ID data + int packetSize = thisLoRa.parsePacket(); + if (packetSize > 0) + { + thisLoRa.read(&protocol); // objet thislora qui appele classe Lora.h et rempli la stucture de l'objet protocol, ser a allèger -5lignes + SerialUSB.println("Frame received"); + delay(100); + } + id1 = protocol.getStationId(); + // big delay + packetSize = thisLoRa.parsePacket(); + if (packetSize > 0) + { + thisLoRa.read(&protocol); // objet thislora qui appele classe Lora.h et rempli la stucture de l'objet protocol, ser a allèger -5lignes + SerialUSB.println("Frame received"); + delay(100); + } + id2 = protocol.getStationId(); + if(id1==id2){ + readFrameAndCheckID(); + } + else{ + readFrameAndCheckTS(); + return true; + } +} //end readframeandcheckid + + bool readFrameAndCheckTS(){ int ts1=0; // for comparing later the LoRa frames using timestamp data int ts2=0; // for comparing later the LoRa frames using timestamp data @@ -157,8 +187,24 @@ bool readFrameAndCheckTS(){ SerialUSB.print("D3 = "); SerialUSB.println(protocol.getDataThree(),HEX); return true; - } -} + } +} //end readframeandcheckts + + + + + + + + + + + + + + + + //void PrintElapsedTime( boolean espaceFinal=true ){ // to display the elapsed time // unsigned long h,m,s = millis()/1000; diff --git a/postToServer/postToServer.ino b/postToServer/postToServer.ino index 67c2479..40e0a44 100644 --- a/postToServer/postToServer.ino +++ b/postToServer/postToServer.ino @@ -26,16 +26,16 @@ void loop() String postData = "ID="+String(trameToSend.getStationId())+"&IDp="+String(trameToSend.getGatewayId())+"&TS="+String(trameToSend.getTimestampMessage())+"&DT="+String(trameToSend.getDataType())+"&D1="+String(trameToSend.getDataOne())+"&D2="+String(trameToSend.getDataTwo())+"&D3="+String(trameToSend.getDataThree()); if (postClient.connect("btslimayrac.ovh", 80)){ postClient.print("POST /weather/formulaire/formulaireCollecteLORA.php HTTP/1.1\n"); - postClient.print("Host: btslimayrac.ovh\n"); - postClient.print("Connection: close\n"); - postClient.print("Content-Type: application/x-www-form-urlencoded\n"); - postClient.print("Content-Length: "); - postClient.print(postData.length()); + postClient.print("Host: btslimayrac.ovh\n"); // specifies the Internet host and port number of the resource being requested + postClient.print("Connection: close\n"); // header option to signal that the connection will be closed after completion of the response + postClient.print("Content-Type: application/x-www-form-urlencoded\n"); // values are encoded in key-value separated by '&', with a '=' between the key and the value + postClient.print("Content-Length: "); // indicates the size of the entity-body, in decimal number of bytes + postClient.print(postData.length()); // to retrieve the size and send it postClient.print("\n\n"); - postClient.print(postData); - SerialUSB.println("Post to server sent, frame :"); + postClient.print(postData); // to send the concatenated frame + SerialUSB.println("Post to server sent, frame :"); // to display the sent frame SerialUSB.println(postData); - incrTS = trameToSend.getTimestampMessage()+1, + incrTS = trameToSend.getTimestampMessage()+1; // to increment TS part of the frame trameToSend.setTimestampMessage(incrTS); delay(4000); }