You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
LoRaGateway_BTS2A/gateway/gateway.ino

206 lines
13 KiB
Arduino

7 years ago
// 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
#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
7 years ago
//#include <SD.h> // to use a SD card
#include <CModemLoRa.h> // to use personalised LoRa class
#include <CProtocol12Bytes.h> // to use our protocol
CModemLoRa thisLoRa; // create object for personnalizeed LoRa class
CProtocol12Bytes protocol; // create object to store data using our protocol
7 years ago
//File webFile; // variable for the file containing the webpage
7 years ago
String postData;
String tsarray;
String oldpostData;
//Pour comparer les timestamps
uint16_t tstamp_old;
uint16_t tstamp_now;
7 years ago
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
7 years ago
EthernetServer server(80); // initialize the EthernetServer library, using port 80 (default fot HTTP)
7 years ago
uint16_t saveIDandTS[255]; // used to send the correct frame
int numCase; // used to associates timestamp and id in the arraw
// void setSPIFrequency(uint32_t frequency); // set the SPI at 8MHz to use logic analyser
void setup(){
7 years ago
SerialUSB.begin(9600);
7 years ago
delay(500);
7 years ago
SerialUSB.println("LoRa Gateway"); // display on serial the name of the device
7 years ago
delay(100);
thisLoRa.begin(); // initialise LoRa
7 years ago
delay(500);
7 years ago
//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
7 years ago
delay(500);
server.begin(); // initialize WebServer part of the librairy
7 years ago
delay(500);
7 years ago
SerialUSB.print("server is at ");
SerialUSB.println(Ethernet.localIP()); // display on serial the IP you can find the webpage
7 years ago
//SerialUSB.println("Initializing SD card..."); // initialize SD card
//SD.begin(4);
//delay(500);
//SerialUSB.println("SUCCESS - SD card initialized.");
//SD.exists("index.htm");
//delay(500);
//SerialUSB.println("SUCCESS - Found index.htm file.");
7 years ago
}// end of setup
7 years ago
void loop() {
7 years ago
//SerialPrintElapsedTime(); // diplay the time the frame arrived
// frame treatment
7 years ago
int packetSize = thisLoRa.parsePacket();
if (packetSize > 0)
{
thisLoRa.read(&protocol);
numCase = protocol.getStationId();
if(protocol.getTimestampMessage() == saveIDandTS[numCase])
{}
else{
7 years ago
tstamp_now = protocol.getTimestampMessage();
7 years ago
saveIDandTS[numCase] = protocol.getTimestampMessage();
//post to server
EthernetClient postClient;
7 years ago
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());
7 years ago
if (postClient.connect("btslimayrac.ovh", 80)){
postClient.print("POST /weather/formulaire/formulaireCollecteLORA.php HTTP/1.1\n");
7 years ago
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");
7 years ago
postClient.print(postData); // to send the concatenated frame
SerialUSB.println("Post to server sent"); // to display the sent frame
SerialUSB.println(postData);
delay(4000);
}
7 years ago
else{
SerialUSB.println("Post failed");
}
delay(100);
SerialUSB.println("New frame recieved : ");
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);
}
}//if (packetSize > 0)
7 years ago
// WebServer
EthernetClient serverGateway = server.available(); // try to get client
7 years ago
while (serverGateway) { // got client?
boolean currentLineIsBlank = true;
7 years ago
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
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
serverGateway.println("HTTP/1.1 200 OK");
serverGateway.println("Content-Type: text/html");
serverGateway.println("Connection: close");
serverGateway.println();
7 years ago
serverGateway.println("<!DOCTYPE HTML>");
serverGateway.println("<html>");
serverGateway.print("<head>");
serverGateway.print("<meta http-equiv=\"refresh\" content=\"30\">");
serverGateway.print("<meta charset=\"utf-8\" />");
serverGateway.print("<link href=\"data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAABILAAASCwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZWZmAHV2dgKNjo4Rent7A3JzcwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADhIrAL+/wgCnqKw3zMvMvayoqD0AAAACFRMaAMwAAAAAAAAAAAAAAAAAAAAAAAAA0stPANLLTwLTzFAi08xQPNPMUD3Sy0g6ycWOiq6+tP00gorYHHV1mLO2UDfd01AL08xQAM3ITAAAAAAA0stPANLLTgLTzFBm08xQ3tPMUPTTzFD108x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AD/gwAA//8AAA==\" rel=\"icon\" type=\"image/x-icon\" />");
serverGateway.print("</head>");
serverGateway.println("<style>");
serverGateway.println("html{font-family:\"Trebuchet MS\";text-align:center;background-color:white;color:black}");
serverGateway.println("h1{text-align:center;color:red;font-size:72px}");
serverGateway.println("h2{text-align:center;color:red;font-size:36px}");
serverGateway.println("p{text-align:center;font-size:36px}");
serverGateway.println("table,th,td{border:2px outset black;font-size:56px}");
serverGateway.println("</style>");
serverGateway.println("<h1>IHM Web Passerelle</h1>");
serverGateway.println("<p>ID station : ");
serverGateway.print(protocol.getStationId());
serverGateway.print("<br />");
serverGateway.println("ID passerelle : ");
serverGateway.print(protocol.getGatewayId());
serverGateway.print("<br />");
serverGateway.println("Timestamp : ");
serverGateway.print(protocol.getTimestampMessage());
serverGateway.print("<br />");
serverGateway.println("Types de données : ");
serverGateway.print(protocol.getDataType());
serverGateway.print("<br />");
serverGateway.println("Données 1 :");
serverGateway.print(protocol.getDataOne());
serverGateway.print("<br />");
serverGateway.println("Données 2 :");
serverGateway.print(protocol.getDataTwo());
serverGateway.print("<br />");
serverGateway.println("Données 3 :");
serverGateway.print(protocol.getDataThree());
serverGateway.print("</p>");
serverGateway.print("<br /> <br /> <br /> <br />");
serverGateway.print("<h2>20 dernières trames reçues :</h2>");
serverGateway.print("<table style=\"width:100%\">");
serverGateway.print("<tr>");
serverGateway.print("<th>ID</th>");
serverGateway.print("<th>IDp</th>");
serverGateway.print("<th>TS</th>");
serverGateway.print("<th>DT</th>");
serverGateway.print("<th>D1</th>");
serverGateway.print("<th>D2</th>");
serverGateway.print("<th>D3</th>");
serverGateway.print("</tr>");
//serverGateway.print(tsarray);
serverGateway.print("</table>");
serverGateway.println("</html>");
7 years ago
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())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
7 years ago
serverGateway.stop(); // close the connection
break;
7 years ago
}//end if (serverGateway)
}//end void loop
//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?" ":"");
//}