add comments
This commit is contained in:
parent
d4b7ee18c5
commit
e8baea6f4b
@ -1,92 +1,91 @@
|
|||||||
// Flavien HAAS, 2018
|
// Flavien HAAS, 2018
|
||||||
// to compile as on the diagrams, check that you have changed the SS port as indicated on the README
|
// 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
|
||||||
|
|
||||||
#include <SPI.h> // to communicate using spi (required for our shields)
|
#include <SPI.h> // to communicate using spi (required for our shields)
|
||||||
#include <LoRa.h> // to use the LoRa shield
|
#include <LoRa.h> // to use the LoRa shield
|
||||||
#include "Ethernet.h" // to use the ethernet shield
|
#include "Ethernet.h" // to use the ethernet shield
|
||||||
#include "util.h" // to have the display of the elapsed time
|
|
||||||
|
|
||||||
#define LENMAX 80 // maximum size for the LoRa frame
|
#define LENMAX 80 // maximum size for the LoRa frame
|
||||||
#define Serial SerialUSB // serial out on the M0 use a different function
|
#define Serial SerialUSB // serial out on the M0 use a different function
|
||||||
|
|
||||||
// void setSPIFrequency(uint32_t frequency); // set the SPI at 8MHz to use logic analyser
|
// void setSPIFrequency(uint32_t frequency); // set the SPI at 8MHz to use logic analyser
|
||||||
|
|
||||||
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // set the mac address
|
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // set the mac address
|
||||||
|
|
||||||
//IPAddress ip(10, 0, 0, 49); //set the IP address for the ethernet shield, overwise the librairy use DHCP
|
//IPAddress ip(10, 0, 0, 49); // 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)
|
EthernetServer server(80); // initialize the EthernetServer library, using port 80 (default fot HTTP)
|
||||||
|
|
||||||
void setup(){
|
void setup(){
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
while (!Serial); // wait for serial to initialize
|
while (!Serial); // wait for serial to initialize
|
||||||
Serial.print("Passerelle LoRa\n"); // display on serial the name of the device
|
Serial.print("Passerelle LoRa\n"); // display on serial the name of the device
|
||||||
|
|
||||||
if( !LoRa.begin(868E6) ){
|
if( !LoRa.begin(868E6) ){
|
||||||
Serial.print("Echec de l'initialisation LoRa !\n");
|
Serial.print("Echec de l'initialisation LoRa !\n");
|
||||||
while(true);} // initialize LoRa shield LoRa at 868 MHz
|
while(true);} // initialize LoRa shield LoRa at 868 MHz
|
||||||
|
|
||||||
//Ethernet.begin(mac, ip); // initialize Ethernet shield using the set mac adress and set IP
|
//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
|
Ethernet.begin(mac); // initialize Ethernet shield uding the set mac and DHCP for the IP
|
||||||
server.begin();
|
server.begin(); // initialize WebServer part of the librairy
|
||||||
Serial.print("server is at ");
|
Serial.print("server is at "); // display on serial the IP you can find the webpage
|
||||||
Serial.println(Ethernet.localIP());
|
Serial.println(Ethernet.localIP());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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?" ":"");
|
||||||
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
static byte tampon[LENMAX]={0};
|
// LoRa receiver
|
||||||
|
static byte tampon[LENMAX]={0}; // if the module receive a frame, it willnot be null
|
||||||
int longueurTrame;
|
int longueurTrame;
|
||||||
// si le module a reçu une trame alors sa longueur sera non nulle
|
|
||||||
longueurTrame=LoRa.parsePacket();
|
longueurTrame=LoRa.parsePacket();
|
||||||
if( longueurTrame > 0 ){
|
if( longueurTrame > 0 ){
|
||||||
//---- copie de la trame depuis le modem vers le tampon ----
|
if( longueurTrame>LENMAX ){ // copy of the frame to cache (LENMAX) and verify if the frame is to big
|
||||||
if( longueurTrame>LENMAX ){
|
Serial.print("Trame reçue trop grande : ");
|
||||||
Serial.print("Trame reçue est trop grande pour le tampon : ");
|
|
||||||
Serial.println(longueurTrame);
|
Serial.println(longueurTrame);
|
||||||
longueurTrame=LENMAX; // troncature
|
longueurTrame=LENMAX; // troncature
|
||||||
}
|
}
|
||||||
for( int i=0; i<longueurTrame; i++ ){
|
for( int i=0; i<longueurTrame; i++ ){
|
||||||
tampon[i]=(byte)LoRa.read();
|
tampon[i]=(byte)LoRa.read();
|
||||||
}
|
}
|
||||||
//---- affichage de l'heure d'arrivée ----
|
SerialPrintElapsedTime(); // diplay the time the frame arrived
|
||||||
SerialPrintElapsedTime();
|
|
||||||
//---- affichage en hexadécimal ----
|
|
||||||
Serial.print("0x");
|
Serial.print("0x");
|
||||||
for( int i=0; i<longueurTrame; i++ ){
|
for( int i=0; i<longueurTrame; i++ ){ // display the frame in hexadecimal
|
||||||
if( tampon[i] < 0x0F ) Serial.print("0");
|
if( tampon[i] < 0x0F ) Serial.print("0");
|
||||||
Serial.print( tampon[i], HEX );
|
Serial.print( tampon[i], HEX );
|
||||||
}
|
}
|
||||||
//---- affichage en ASCII ----
|
|
||||||
Serial.print( " " );
|
Serial.print( " " );
|
||||||
for( int i=0; i<longueurTrame; i++ ){
|
for( int i=0; i<longueurTrame; i++ ){
|
||||||
if( (tampon[i] < 0x20)||(tampon[i] > 0x7E) ){
|
if( (tampon[i] < 0x20)||(tampon[i] > 0x7E) ){
|
||||||
Serial.print( "."); // ce caractère est non imprimable
|
Serial.print( "."); // this character isn't printable (displayable)
|
||||||
}else{
|
}else{
|
||||||
Serial.print( (char)tampon[i] );
|
Serial.print( (char)tampon[i] ); // display the frame in ASCII
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Serial.print( "\n" );
|
Serial.print( "\n" );
|
||||||
}//if LoRa.parsePacket
|
} // end of if LoRa.parsePacket
|
||||||
delay(10);
|
delay(10);
|
||||||
// listen for incoming clients
|
// WebServer
|
||||||
EthernetClient client = server.available();
|
EthernetClient client = server.available(); // WebServer :listen for incoming clients
|
||||||
if (client) {
|
if (client) {
|
||||||
Serial.println("new client");
|
Serial.println("new client");
|
||||||
// an http request ends with a blank line
|
boolean currentLineIsBlank = true; // an http request ends with a blank line
|
||||||
boolean currentLineIsBlank = true;
|
|
||||||
while (client.connected()) {
|
while (client.connected()) {
|
||||||
if (client.available()) {
|
if (client.available()) {
|
||||||
char c = client.read();
|
char c = client.read();
|
||||||
Serial.write(c);
|
Serial.write(c);
|
||||||
// if you've gotten to the end of the line (received a newline
|
if (c == '\n' && currentLineIsBlank) { // send the beginning of a standard http response header
|
||||||
// character) and the line is blank, the http request has ended,
|
|
||||||
// so you can send a reply
|
|
||||||
if (c == '\n' && currentLineIsBlank) {
|
|
||||||
// send a standard http response header
|
|
||||||
client.println("HTTP/1.1 200 OK");
|
client.println("HTTP/1.1 200 OK");
|
||||||
client.println("Content-Type: text/html");
|
client.println("Content-Type: text/html");
|
||||||
client.println("Connection: close"); // the connection will be closed after completion of the response
|
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
|
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
|
||||||
client.println();
|
client.println();
|
||||||
client.println("<!DOCTYPE HTML>");
|
client.println("<!DOCTYPE HTML>");
|
||||||
client.println("<html>");
|
client.println("<html>");
|
||||||
@ -102,19 +101,14 @@ void loop() {
|
|||||||
client.println("</html>");
|
client.println("</html>");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (c == '\n') {
|
if (c == '\n') { // send a new blank line to indicate the end of the connection
|
||||||
// you're starting a new line
|
|
||||||
currentLineIsBlank = true;
|
currentLineIsBlank = true;
|
||||||
} else if (c != '\r') {
|
} else if (c != '\r') {
|
||||||
// you've gotten a character on the current line
|
|
||||||
currentLineIsBlank = false;
|
currentLineIsBlank = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// give the web browser time to receive the data
|
delay(1); // give the web browser time to receive the data
|
||||||
delay(1);
|
client.stop(); // close the connection of the webserver
|
||||||
// close the connection:
|
|
||||||
client.stop();
|
|
||||||
Serial.println("client disconnected");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,56 +0,0 @@
|
|||||||
|
|
||||||
#ifndef DaKTASK
|
|
||||||
#define DaKTASK
|
|
||||||
|
|
||||||
// la tàche est effectuee avec un delai de PeriodeMilliSecondes entre deux executions.
|
|
||||||
// consomme 4 octets de RAM
|
|
||||||
// exemple : cron(1000){ // code <20> r<>p<EFBFBD>ter toutes les secondes }
|
|
||||||
#define cron(PeriodeMilliSecondes) for( \
|
|
||||||
static unsigned long __nextmillis = 0; \
|
|
||||||
millis() - __nextmillis >= (unsigned long)(PeriodeMilliSecondes); \
|
|
||||||
__nextmillis = millis() \
|
|
||||||
)
|
|
||||||
|
|
||||||
// la tàche est effectuee avec un delai de PeriodeMilliSecondes entre deux executions.
|
|
||||||
// consomme 4 octets de RAM
|
|
||||||
#define task(NomTache,PeriodeMilliSecondes) for( \
|
|
||||||
static unsigned long DaKTASK_ ## NomTache = 0; \
|
|
||||||
millis() - DaKTASK_ ## NomTache >= (unsigned long)(PeriodeMilliSecondes); \
|
|
||||||
DaKTASK_ ## NomTache = millis() \
|
|
||||||
)
|
|
||||||
|
|
||||||
// la tàche est effectuee pr<70>cis<69>ment toutes les PeriodeMilliSecondes.
|
|
||||||
// consomme 8 octets de RAM
|
|
||||||
#define ftask(NomTache,PeriodeMilliSecondes) for( \
|
|
||||||
static unsigned long DaKTASK_ ## NomTache = 0, millis_ ## NomTache = 0; \
|
|
||||||
(millis_ ## NomTache = millis()) - DaKTASK_ ## NomTache >= (unsigned long)(PeriodeMilliSecondes); \
|
|
||||||
DaKTASK_ ## NomTache = millis_ ## NomTache \
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(9600);
|
|
||||||
pinMode(13, OUTPUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
Task( Toto, 1500 ){
|
|
||||||
Serial.print("Toto=");
|
|
||||||
Serial.println( millis() );
|
|
||||||
}
|
|
||||||
|
|
||||||
Task( line, 3000) Serial.println(F("-----------------"));
|
|
||||||
|
|
||||||
FTask( LED, 100 ){ PORTB ^= 0x20; } // clignoter la LED
|
|
||||||
|
|
||||||
cron(500){ PORTB ^= 0x20; } // clignoter la LED toutes les secondes
|
|
||||||
|
|
||||||
delay(11);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
@ -1,40 +0,0 @@
|
|||||||
/*-----------------------------------
|
|
||||||
Petite collection deviendra grande
|
|
||||||
|
|
||||||
Astuces glanées ça et là
|
|
||||||
|
|
||||||
-----------------------------------*/
|
|
||||||
|
|
||||||
|
|
||||||
/* Permet de compacter des suites de Serial.Print
|
|
||||||
Exemple :
|
|
||||||
Serial.print("J'ai ");
|
|
||||||
Serial.print(i);
|
|
||||||
Serial.print(" bronzes de ");
|
|
||||||
Serial.print(j);
|
|
||||||
Serial.println(" kilo chacun.");
|
|
||||||
par
|
|
||||||
Serial << "J'ai" << i << " bronzes de " << j << " kilo chacun.\n"
|
|
||||||
Source : https://playground.arduino.cc/Main/StreamingOutput
|
|
||||||
|
|
||||||
Fonctionne avec toutes les librairies d<EFBFBD>riv<EFBFBD>es comme LCD, SerialUSB, ..."
|
|
||||||
*/
|
|
||||||
template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
|
|
||||||
|
|
||||||
|
|
||||||
/*-----------------------------------------
|
|
||||||
* Affichage du temps écoulé depuis le lancement
|
|
||||||
* du programme,
|
|
||||||
* sous le forme hh:mm:ss
|
|
||||||
* le paramètre optionnel place un espace à la fin
|
|
||||||
*/
|
|
||||||
void SerialPrintElapsedTime( boolean espaceFinal=true ){
|
|
||||||
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?" ":"");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user