LoRaGateway_BTS2A/gateway/gateway.ino

171 lines
9.0 KiB
Arduino
Raw Normal View History

2018-04-19 15:38:27 +02:00
// Flavien HAAS, 2018
// before transfert, check these things:
// have all the librairies needed installed on your machine
// changed the SS port for ethernet as indicated on the README or you will not be able to use the LoRa shield as the same time as the Ethernet shield
2018-04-18 13:36:39 +02:00
#include <SPI.h> // to communicate using spi (required for our shields)
#include <LoRa.h> // to use the LoRa shield
#include <Ethernet.h> // to use the ethernet shield
#include <SD.h> // to use a SD card
#include <CModemLoRa.h> // to use personalised LoRa class
#include <CProtocol12Bytes.h> // to use our protocol
2018-05-24 15:55:24 +02:00
#define LENMAX 80 // maximum size for the LoRa frame
#define Serial SerialUSB // serial out on the M0 use a different function
2018-04-18 13:36:39 +02:00
CModemLoRa thisLoRa; // create object for personnalizeed LoRa class
CProtocol12Bytes protocol; // create object to store data using our protocol
2018-05-24 15:55:24 +02:00
File webFile; // variable for the file containing the webpage
2018-05-25 10:40:14 +02:00
// void setSPIFrequency(uint32_t frequency); // set the SPI at 8MHz to use logic analyser
2018-04-18 13:36:39 +02:00
2018-05-30 08:41:08 +02:00
byte mac[] = {0xFA, 0xE3, 0x40, 0xEF, 0xFF, 0xFD}; // set the mac address
IPAddress ip(192, 1, 1, 150); // set the IP address for the ethernet shield, overwise the librairy use DHCP
2018-04-19 15:38:27 +02:00
EthernetServer server(80); // initialize the EthernetServer library, using port 80 (default fot HTTP)
2018-04-18 13:36:39 +02:00
void setup(){
Serial.begin(9600);
while (!Serial); // wait for serial to initialize
Serial.println("LoRa Gateway"); // display on serial the name of the device
2018-04-19 15:38:27 +02:00
thisLoRa.begin(); // initialise LoRa
2018-04-19 15:38:27 +02:00
//Ethernet.begin(mac, ip); // initialize Ethernet shield using the set mac adress and set IP and DHCP for the rest
Ethernet.begin(mac); // initialize Ethernet shield uding the set mac and DHCP for the rest
server.begin(); // initialize WebServer part of the librairy
2018-05-26 16:18:18 +02:00
Serial.print("server is at ");
Serial.println(Ethernet.localIP()); // display on serial the IP you can find the webpage
2018-05-26 16:18:18 +02:00
Serial.println("Initializing SD card..."); // initialize SD card
2018-05-26 16:18:18 +02:00
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
2018-05-26 16:18:18 +02:00
}
Serial.println("SUCCESS - SD card initialized.");
if (!SD.exists("index.htm")) { // check for index.htm file
2018-05-26 16:18:18 +02:00
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
2018-05-26 16:18:18 +02:00
}
Serial.println("SUCCESS - Found index.htm file.");
} // end of setup
2018-04-20 18:37:05 +02:00
2018-04-18 13:36:39 +02:00
void loop() {
2018-05-24 15:55:24 +02:00
// SerialPrintElapsedTime(); // diplay the time the frame arrived
2018-05-23 15:24:01 +02:00
// frame treatment
while(readFrameAndCheckTS() == true){
2018-05-27 18:30:33 +02:00
// post to server
EthernetClient postClient;
2018-05-30 08:41:08 +02:00
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("Connection: close\n");
postClient.print("Content-Type: application/x-www-form-urlencoded\n");
2018-05-27 18:30:33 +02:00
postClient.print("Content-Length: ");
2018-05-30 08:41:08 +02:00
postClient.print(postData.length());
postClient.print("\n\n");
postClient.print(postData);
Serial.println("Post to server sent");
2018-05-27 18:30:33 +02:00
}
2018-05-30 08:41:08 +02:00
else{
Serial.println("Post failed");
}
}
2018-05-23 15:24:01 +02:00
2018-04-20 18:37:05 +02:00
// WebServer
EthernetClient serverGateway = server.available(); // try to get client
2018-05-24 15:55:24 +02:00
if (serverGateway) { // got client?
2018-05-24 15:55:24 +02:00
boolean currentLineIsBlank = true;
2018-05-25 10:40:14 +02:00
while (serverGateway.connected()) {
if (serverGateway.available()) { // client data available to read
char c = serverGateway.read(); // read 1 byte (character) from client
// last line of client request is blank and ends with \n
// respond to client only after last line received
2018-05-24 15:55:24 +02:00
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
2018-05-25 10:40:14 +02:00
serverGateway.println("HTTP/1.1 200 OK");
serverGateway.println("Content-Type: text/html");
serverGateway.println("Connection: close");
serverGateway.println();
2018-05-24 15:55:24 +02:00
// send web page
2018-05-26 16:18:18 +02:00
webFile = SD.open("index.htm"); // open web page file
2018-05-24 15:55:24 +02:00
if (webFile) { // if the webfile exist
while(webFile.available()) { // the webfile is avaible
2018-05-25 10:40:14 +02:00
serverGateway.write(webFile.read()); // send webfile to client
2018-05-24 15:55:24 +02:00
}
webFile.close();
}
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
2018-05-26 16:18:18 +02:00
} // end while (client.connected())
2018-05-24 15:55:24 +02:00
delay(1); // give the web browser time to receive the data
2018-05-26 16:18:18 +02:00
serverGateway.stop(); // close the connection
2018-05-24 15:55:24 +02:00
} // end if (serverGateway)
} //end void loop
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
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
2018-05-30 08:41:08 +02:00
SerialUSB.println("Frame received");
delay(100);
}
ts1 = protocol.getTimestampMessage();
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
2018-05-30 08:41:08 +02:00
SerialUSB.println("Frame received");
delay(100);
}
ts2 = protocol.getTimestampMessage();
if(ts1==ts2){
return false;
}
else{
SerialUSB.println("New Frame :");
2018-05-27 01:37:37 +02:00
SerialUSB.print("ID = ");
SerialUSB.print(protocol.getStationId(),HEX);
SerialUSB.println(protocol.getGatewayId(),HEX);
2018-05-27 01:37:37 +02:00
SerialUSB.print("TS = ");
SerialUSB.println(protocol.getTimestampMessage(),HEX);
2018-05-27 01:37:37 +02:00
SerialUSB.print("DT = ");
SerialUSB.println(protocol.getDataType(),HEX);
2018-05-27 01:37:37 +02:00
SerialUSB.print("D1 = ");
SerialUSB.println(protocol.getDataOne(),HEX);
2018-05-27 01:37:37 +02:00
SerialUSB.print("D2 = ");
SerialUSB.println(protocol.getDataTwo(),HEX);
2018-05-27 01:37:37 +02:00
SerialUSB.print("D3 = ");
SerialUSB.println(protocol.getDataThree(),HEX);
return true;
}
}
//void PrintElapsedTime( boolean espaceFinal=true ){ // to display the elapsed time
// unsigned long h,m,s = millis()/1000;
// m=s/60;
// h=m/60;
// s=s-(m*60);
// m=m-(h*60);
// Serial << ((h<10)?"0":"") << h << ":" << ((m<10)?"0":"") << m << ":" << ((s<10)?"0":"") << s << (espaceFinal?" ":"");
//}