Bonjour,
Code : Tout sélectionner
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install libjson0-dev
Modérateur : Francois
Bonjour,
Code : Tout sélectionner
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install libjson0-dev
Code : Tout sélectionner
/*
* Nom du projet : RF24_Gateway
* Auteur : jelopo
* Version : 1.0.2
* Date : 2020-04-21
* Description : Passerelle RF24 vers MQTT
* Fonctions : RF24 + ESP8266Wifi + MQTT
************************************************************************************
* Fonctionne avec un ESP-12
* Choisir le Board "Generic ESP8266 Module"
* Compiler avec Flash size = 4M
*
* Branchements: NRF24L01: GND:15; VCC:8, CE:19; CSN:20; SCK:5; MOSI:7; MISO:6
*
* Chargement du code: Appuyer sur RESET + GPIO0; Cliquer sur Téléverser
* Relacher RESET quand `Connecting...` apparait
* Relacher *GPIO0* quand `Writing at ..` apparait
************************************************************************************
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <SPI.h>
#include "nRF24L01.h"
#include <RF24.h>
#include "printf.h"
// Definition parametres WIFI
const char* ssid = "ssid_du_reseau_WIFI";
const char* password = "mot_de_passe_du_WIFI";
WiFiClient espClient;
String espname = "ESP_";
byte mac[6];
// Definition parametres MQTT
const char* mqtt_server = "192.168.1.10";
PubSubClient client(espClient);
long lastMsg = 0;
char msg[32];
char outTopic[22];
// Definition variable NRF24L01
RF24 radio(4, 5); // CE, CSN
const byte address[6] = "00001";
const int taille = 32;
char message[taille + 1];
void setup() {
Serial.begin(115200);
// WIFI setup
setup_wifi();
// MQTT Setup
client.setServer(mqtt_server, mqtt_port);
//// Initialize OutTopic
String outTopicString = "RF24GW/" + espname + "/out";
outTopicString.toCharArray(outTopic, 22);
// RF24 setup
setup_RF24();
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("MAC: ");
Serial.println (WiFi.macAddress());
Serial.print("espname: ");
WiFi.macAddress(mac);
espname = espname + String(mac[3],HEX) + String(mac[4],HEX) + String(mac[5],HEX);
espname.toUpperCase();
Serial.println(espname);
}
void reconnect() {
// loop until MQTT connection
while (!client.connected()) {
Serial.print("Attempting MQTT connection to ");
Serial.print(mqtt_server);
Serial.print(":");
Serial.println(mqtt_port);
// Attempt to connect
// client.connect require a char argument for the clientId.
// c_str change String to char.
if (client.connect(espname.c_str())) {
Serial.println("MQTT connected");
}
else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup_RF24 () {
delay(100);
printf_begin();
radio.begin();
radio.setDataRate(RF24_250KBPS);
//radio.setAutoAck(false);
Serial.println("Radio Print Details:");
radio.printDetails();
radio.openReadingPipe(1, address);
radio.startListening();
}
void loop() {
// Restart MQTT if needed
if (!client.connected()) {
reconnect();
}
client.loop();
// RF24
while ( radio.available() )
{
radio.read(message, taille);
Serial.print("Message recu : ");
Serial.println(message);
Serial.println("Publication MQTT.");
client.publish(outTopic, message);
}
}