LoRaGateway_BTS2A/gateway/gateway.ino

124 lines
6.2 KiB
Arduino
Raw Normal View History

2018-04-19 15:38:27 +02:00
// Flavien HAAS, 2018
2018-04-24 19:47:29 +02:00
// before transfert, check that you have changed the SS port 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
2018-04-20 18:37:05 +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
2018-04-18 13:36:39 +02:00
2018-04-20 18:37:05 +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
2018-04-20 18:37:05 +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-04-20 18:37:05 +02:00
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // set the mac address
2018-04-19 15:38:27 +02:00
2018-04-20 18:37:05 +02:00
//IPAddress ip(10, 0, 0, 49); // set the IP address for the ethernet shield, overwise the librairy use DHCP
2018-04-19 15:38:27 +02:00
2018-04-20 18:37:05 +02:00
EthernetServer server(80); // initialize the EthernetServer library, using port 80 (default fot HTTP)
2018-04-18 13:36:39 +02:00
2018-04-24 14:52:49 +02:00
struct message { // frame structure
uint16_t ID; // ID
uint16_t TS; // TimeStamp
uint16_t DT; // Data Type
uint16_t D1; // DATA 1
uint16_t D2; // DATA 2
uint16_t D3; // DATA 3
};
2018-04-18 13:36:39 +02:00
void setup(){
Serial.begin(9600);
2018-04-20 18:37:05 +02:00
while (!Serial); // wait for serial to initialize
Serial.print("Passerelle LoRa\n"); // display on serial the name of the device
2018-04-19 15:38:27 +02:00
2018-04-18 13:36:39 +02:00
if( !LoRa.begin(868E6) ){
Serial.print("Echec de l'initialisation LoRa !\n");
2018-04-20 18:37:05 +02:00
while(true);} // initialize LoRa shield LoRa at 868 MHz
2018-04-19 15:38:27 +02:00
2018-04-20 18:37:05 +02:00
//Ethernet.begin(mac, ip); // initialize Ethernet shield using the set mac adress and set IP
Ethernet.begin(mac); // initialize Ethernet shield uding the set mac and DHCP for the IP
server.begin(); // initialize WebServer part of the librairy
Serial.print("server is at "); // display on serial the IP you can find the webpage
2018-04-18 13:36:39 +02:00
Serial.println(Ethernet.localIP());
}
2018-04-23 21:24:05 +02:00
//void SerialPrintElapsedTime( 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?" ":"");
//}
2018-04-20 18:37:05 +02:00
2018-04-18 13:36:39 +02:00
void loop() {
2018-04-20 18:37:05 +02:00
// LoRa receiver
2018-04-20 18:42:48 +02:00
static byte tampon[LENMAX]={0}; // if the module receive a frame, it willnot be null
2018-04-18 13:36:39 +02:00
int longueurTrame;
longueurTrame=LoRa.parsePacket();
if( longueurTrame > 0 ){
2018-04-20 18:42:48 +02:00
if( longueurTrame>LENMAX ){ // copy of the frame to cache (LENMAX) and verify if the frame is to big
2018-04-20 18:37:05 +02:00
Serial.print("Trame reçue trop grande : ");
2018-04-18 13:36:39 +02:00
Serial.println(longueurTrame);
2018-04-20 18:42:48 +02:00
longueurTrame=LENMAX; // cut the frame to LENMAX size
2018-04-18 13:36:39 +02:00
}
for( int i=0; i<longueurTrame; i++ ){
tampon[i]=(byte)LoRa.read();
}
2018-04-23 21:24:05 +02:00
// SerialPrintElapsedTime(); // diplay the time the frame arrived
2018-04-18 13:36:39 +02:00
Serial.print("0x");
2018-04-20 18:42:48 +02:00
for( int i=0; i<longueurTrame; i++ ){ // display the frame in hexadecimal
2018-04-18 13:36:39 +02:00
if( tampon[i] < 0x0F ) Serial.print("0");
Serial.print( tampon[i], HEX );
}
Serial.print( " " );
for( int i=0; i<longueurTrame; i++ ){
if( (tampon[i] < 0x20)||(tampon[i] > 0x7E) ){
2018-04-20 18:42:48 +02:00
Serial.print( "."); // this character isn't printable (displayable)
2018-04-18 13:36:39 +02:00
}else{
2018-04-20 18:42:48 +02:00
Serial.print( (char)tampon[i] ); // display the frame in ASCII
2018-04-18 13:36:39 +02:00
}
}
Serial.print( "\n" );
2018-04-20 18:42:48 +02:00
} // end of if LoRa.parsePacket
2018-04-18 13:36:39 +02:00
delay(10);
2018-04-20 18:37:05 +02:00
// WebServer
2018-04-20 18:42:48 +02:00
EthernetClient client = server.available(); // WebServer :listen for incoming clients
2018-04-18 13:36:39 +02:00
if (client) {
Serial.println("new client");
2018-04-20 18:42:48 +02:00
boolean currentLineIsBlank = true; // an http request ends with a blank line
2018-04-18 13:36:39 +02:00
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
2018-04-20 18:42:48 +02:00
if (c == '\n' && currentLineIsBlank) { // send the beginning of a standard http response header
2018-04-18 13:36:39 +02:00
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
2018-04-20 18:42:48 +02:00
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
2018-04-18 13:36:39 +02:00
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("<br />");
}
client.println("</html>");
break;
}
2018-04-20 18:42:48 +02:00
if (c == '\n') { // send a new blank line to indicate the end of the connection
2018-04-18 13:36:39 +02:00
currentLineIsBlank = true;
} else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
2018-04-20 18:42:48 +02:00
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection of the webserver
2018-04-18 13:36:39 +02:00
}
}