This commit is contained in:
Rémi Heredero
2021-04-16 21:15:54 +02:00
commit fec6f19309
19 changed files with 1122 additions and 0 deletions

View File

@ -0,0 +1,50 @@
#include <Arduino.h>
#include <BLEDevice.h>
#include <vector>
#include <string>
#include <Bluetooth.h>
static BLEAddress *pServerAddress;
BLEScan* pBLEScan;
BLEClient* pClient;
std::vector<BeaconBLE> beacons;
unsigned long entry;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
//Called for each advertising BLE server.
void onResult(BLEAdvertisedDevice advertisedDevice) {
pServerAddress = new BLEAddress(advertisedDevice.getAddress());
BeaconBLE tag;
tag.adresse = pServerAddress->toString().c_str();
tag.qualite = advertisedDevice.getRSSI();
beacons.push_back(tag);
/*
Serial.println();
Serial.print("Device found: ");
Serial.print(tag.adresse);
Serial.print(" => ");
Serial.print(tag.qualite);
*/
}
}; // MyAdvertisedDeviceCallbacks
void setup_ble(){
Serial.println("Starting Arduino BLE Client application...");
BLEDevice::init("");
pClient = BLEDevice::createClient();
Serial.println(" - Created client");
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
}
std::vector<BeaconBLE> loop_ble(){
beacons.clear();
BLEScanResults scanResults = pBLEScan->start(1);
return beacons;
}

12
lib/Bluetooth/Bluetooth.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef BLUETOOTH_H_INCLUDED
#define BLUETOOTH_H_INCLUDED
struct BeaconBLE {
String adresse;
int qualite;
};
void setup_ble();
std::vector<BeaconBLE> loop_ble();
#endif

74
lib/DFPlayer/DFPlayer.cpp Normal file
View File

@ -0,0 +1,74 @@
#include <Arduino.h>
#include <DFPlayer.h>
#include <SoftwareSerial.h>
#include <DFMiniMp3.h>
class Mp3Notify{
public:
static void PrintlnSourceAction(DfMp3_PlaySources source, const char* action)
{
if (source & DfMp3_PlaySources_Sd){
Serial.print("SD Card, ");
}
Serial.println(action);
}
static void OnError(uint16_t errorCode)
{
// see DfMp3_Error for code meaning
Serial.println();
Serial.print("Com Error ");
Serial.println(errorCode);
}
static void OnPlayFinished(DfMp3_PlaySources source, uint16_t track)
{
Serial.print("Play finished for #");
Serial.println(track);
}
static void OnPlaySourceOnline(DfMp3_PlaySources source)
{
PrintlnSourceAction(source, "online");
}
static void OnPlaySourceInserted(DfMp3_PlaySources source)
{
PrintlnSourceAction(source, "inserted");
}
static void OnPlaySourceRemoved(DfMp3_PlaySources source)
{
PrintlnSourceAction(source, "removed");
}
};
SoftwareSerial secondarySerial(32, 33); // RX, TX
DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(secondarySerial);
void setup_dfp(){
mp3.begin();
uint16_t volume = mp3.getVolume();
Serial.print("volume ");
Serial.println(volume);
mp3.setVolume(10);
uint16_t count = mp3.getTotalTrackCount(DfMp3_PlaySource_Sd);
Serial.print("files ");
Serial.println(count);
}
void play(int fichier){
mp3.loop();
mp3.playMp3FolderTrack(fichier);
}
void stop(){
mp3.loop();
mp3.stop();
}
boolean statut(){
return (mp3.getStatus() == 513);
}
void volume(int vol){
mp3.setVolume(vol);
}

10
lib/DFPlayer/DFPlayer.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef DFPLAYER_H_INCLUDED
#define DFPLAYER_H_INCLUDED
void setup_dfp();
void play(int fichier);
void stop();
boolean statut();
void volume(int vol = 10);
#endif

72
lib/Leds/Leds.cpp Normal file
View File

@ -0,0 +1,72 @@
#include <Arduino.h>
#include <Leds.h>
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_NOMBRE, LED_PIN, NEO_GRB + NEO_KHZ800);
#ifdef __AVR__
#include <avr/power.h>
#endif
int last_puissance;
int lr;
int lg;
int lb;
boolean firstOFF(true);
boolean change(true);
/*
* IMPORTANT !!!! Pour réduire les risque sur le bandeau, ajoutez un condensateur de 1000 uF sur l'alimentation
* Mettez une résistance entre 300 et 500 Ohm sur le DataIn de la première LED
* Minimiser la distance entre l'Arduino et la première led
* Pour préserver le circuit... si vous le voulez, brancher le GND en premier
*/
void setup_led(){
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
strip.begin();
strip.show(); // Éteint toutes les LEDs
}
void LEDoff(){
if (firstOFF){
strip.clear();
strip.show();
firstOFF = false;
Serial.println("OFF");
last_puissance = 0;
lr = 0;
lg = 0;
lb = 0;
}
}
void LEDon(int puissance, int r, int g, int b){
if(r!=lr || g!=lg || b!=lb){
for (int LED=0; LED<LED_NOMBRE; LED++){
strip.setPixelColor(LED, r, g, b);
}
lr = r;
lg = g;
lb = b;
change = true;
}
if(puissance != last_puissance){
strip.setBrightness(puissance);
last_puissance = puissance;
change = true;
}
if(change) {
strip.show();
change = false;
}
firstOFF = true;
}

10
lib/Leds/Leds.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef LEDS_H_INCLUDED
#define LEDS_H_INCLUDED
#define LED_NOMBRE 47
#define LED_PIN 22
void setup_led();
void LEDoff();
void LEDon(int puissance, int r, int g, int b);
#endif

46
lib/README Normal file
View File

@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

174
lib/Reseau/Reseau.cpp Normal file
View File

@ -0,0 +1,174 @@
#include <Arduino.h>
#include <vector>
#include <SPI.h>
#include <string>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <Reseau.h>
const byte last_mac = ID;
int id = ID;
byte mac[] = { 0x02, 0x00, 0x00, 0x7A, 0x38, last_mac };
IPAddress ip (192, 168, 8, 100+id);
IPAddress server(SERVEUR_MQTT);
uint32_t lastReconnectAttempt = 0;
unsigned long check;
//Just Header
void callback(char* topic, byte* payload, unsigned int length);
boolean connection();
EthernetClient ethClient;
PubSubClient client(server, PORT, callback, ethClient);
struct TopicParametre {
String topic;
int parametre;
int defaut;
boolean subscribe;
boolean define;
};
std::vector<TopicParametre> topic_parametre;
void AddParametre (String topic, int defaut){
String sujet = String(id)+"/"+topic;
TopicParametre valeure;
valeure.topic = sujet;
valeure.subscribe = false;
valeure.define = false;
valeure.defaut = defaut;
topic_parametre.push_back(valeure);
Serial.println("add");
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
for (size_t i = 0; i < topic_parametre.size(); i++){
if(String(topic) == topic_parametre[i].topic){
String valeure;
for (size_t j=0;j<length;j++) {
valeure += ((char)payload[j]);
}
topic_parametre[i].parametre = (int)(valeure.toInt());
topic_parametre[i].define = true;
}
}
}
boolean setup_reseau (){
Ethernet.init(5); // ESP32 with Adafruit Featherwing Ethernet
// start the Ethernet connection:
Serial.println("Initialize Ethernet:");
Ethernet.begin(mac,ip);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet");
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
} else if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
return false;
}
//Ethernet.begin(mac,ip);
// print your local IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
connection();
String msg = "Démarrage de la lampe "+String(id);
client.publish("outTopic",msg.c_str());
}
boolean connection(){
Serial.println("connection");
if (client.connect((String(id)).c_str())) {
String msg = "Connexion de la lampe "+String(id);
client.publish("outTopic",msg.c_str());
client.subscribe("inTopic");
for(auto str:topic_parametre){
const char* topic = str.topic.c_str();
client.subscribe(topic);
str.subscribe = true;
Serial.print("sub: ");
Serial.println(topic);
}
Serial.println("subscribe OK");
return true;
}
}
boolean loop_reseau(){
unsigned long now = millis();
unsigned long intervalle = now - check;
client.loop();
if(client.state()==0){
check = now;
return true;
}
if (intervalle > 20000) {
ESP.restart();
}
if (!client.connected()) {
Serial.println("=== MQTT NOT CONNECTED ===");
// Reconnect every 10 seconds
uint32_t t = millis();
if (t - lastReconnectAttempt > 10000L) {
lastReconnectAttempt = t;
if (connection()) {
lastReconnectAttempt = 0;
}
}
return false;
}
//NO-USE - Pour tester la connexion avec la carte ethernet
if (Ethernet.linkStatus() == LinkOFF) {
}
else if (Ethernet.linkStatus() == LinkON) {
}
else {
}
}
boolean publish(String topic, String msg){
String sujet = String(id)+"/"+topic;
return (client.publish(sujet.c_str(),msg.c_str()) == 1);
}
int ReadParametre (String topic){
//String sujet = String(ID)+"/"+topic;
for(auto val:topic_parametre){
String sujet = String(id)+"/"+topic;
if(sujet == val.topic){
if(val.define){
return val.parametre;
} else {
return val.defaut;
}
}
}
}
void ResetValues (){
for(auto& val:topic_parametre){
val.define = false;
}
}

16
lib/Reseau/Reseau.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef RESEAU_H_INCLUDED
#define RESEAU_H_INCLUDED
#define ID 18
//de 0 à 255
#define SERVEUR_MQTT 192,168,8,100
#define PORT 1883
boolean setup_reseau ();
boolean loop_reseau();
boolean publish(String topic, String msg);
void AddParametre (String topic, int defaut = 0);
int ReadParametre (String topic);
void ResetValues ();
#endif