// 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 // to communicate using spi (required for our shields) #include // to use the LoRa shield #include // to use the ethernet shield //#include // to use a SD card #include // to use personalised LoRa class #include // to use our protocol CModemLoRa thisLoRa; // create object for personnalizeed LoRa class CProtocol12Bytes protocol; // create object to store data using our protocol //File webFile; // variable for the file containing the webpage String postData; String row; 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 EthernetServer server(80); // initialize the EthernetServer library, using port 80 (default fot HTTP) 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(){ SerialUSB.begin(9600); delay(500); SerialUSB.println("LoRa Gateway"); // display on serial the name of the device delay(100); thisLoRa.begin(); // initialise LoRa delay(500); //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 delay(500); server.begin(); // initialize WebServer part of the librairy delay(500); SerialUSB.print("server is at "); SerialUSB.println(Ethernet.localIP()); // display on serial the IP you can find the webpage //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."); }// end of setup void loop() { //SerialPrintElapsedTime(); // diplay the time the frame arrived // frame treatment int packetSize = thisLoRa.parsePacket(); if (packetSize > 0) { thisLoRa.read(&protocol); numCase = protocol.getStationId(); if(protocol.getTimestampMessage() == saveIDandTS[numCase]) {} else{ saveIDandTS[numCase] = protocol.getTimestampMessage(); row += "" + String(protocol.getStationId())+""+String(protocol.getGatewayId())+""+String(protocol.getTimestampMessage())+"" + String(protocol.getDataType())+"" + String(protocol.getDataOne())+"" + String(protocol.getDataTwo())+"" + String(protocol.getDataThree())+""; //post to server EthernetClient postClient; 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: 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); // to send the concatenated frame SerialUSB.println("Post to server sent"); // to display the sent frame SerialUSB.println(postData); delay(10); } else{ SerialUSB.println("Post failed"); SerialUSB.println(" New attempt to POST"); if (postClient.connect("btslimayrac.ovh", 80)){ postClient.print("POST /weather/formulaire/formulaireCollecteLORA.php HTTP/1.1\n"); 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); // to send the concatenated frame SerialUSB.println("Post to server sent"); // to display the sent frame SerialUSB.println(postData); delay(10); } else{ SerialUSB.println("Post miserably failed, can't connect to btslimayrac.ovh, check your network's settings"); }//end of secnd else }//end of first else 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); SerialUSB.print("You can see the web interface at :"); SerialUSB.println(Ethernet.localIP()); } }//if (packetSize > 0) // WebServer EthernetClient serverGateway = server.available(); // try to get client while (serverGateway) { // got client? boolean currentLineIsBlank = true; 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(); serverGateway.println(""); serverGateway.println(""); serverGateway.print(""); serverGateway.print(""); serverGateway.print(""); serverGateway.print(""); serverGateway.print(""); serverGateway.println(""); serverGateway.println("

IHM Web Passerelle

"); serverGateway.println("

Dernière trame envoyée

"); serverGateway.println("

ID station : "); serverGateway.print(protocol.getStationId()); serverGateway.print("
"); serverGateway.println("ID passerelle : "); serverGateway.print(protocol.getGatewayId()); serverGateway.print("
"); serverGateway.println("Timestamp : "); serverGateway.print(protocol.getTimestampMessage()); serverGateway.print("
"); serverGateway.println("Types de données : "); serverGateway.print(protocol.getDataType()); serverGateway.print("
"); serverGateway.println("Données 1 :"); serverGateway.print(protocol.getDataOne()); serverGateway.print("
"); serverGateway.println("Données 2 :"); serverGateway.print(protocol.getDataTwo()); serverGateway.print("
"); serverGateway.println("Données 3 :"); serverGateway.print(protocol.getDataThree()); serverGateway.print("

"); serverGateway.print("

"); serverGateway.print("

Historique des dernières trames envoyées :

"); serverGateway.print(""); serverGateway.print(""); serverGateway.print(""); serverGateway.print(""); serverGateway.print(""); serverGateway.print(""); serverGateway.print(""); serverGateway.print(""); serverGateway.print(""); serverGateway.print(""); serverGateway.print(row); serverGateway.print("
Identifiant stationIdentifiant passerelleTimeStampType de donnéesDonnée 1Donnée 2Donnée 3
"); serverGateway.println(""); 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 serverGateway.stop(); // close the connection break; }//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?" ":""); //}