Afficheurs
This commit is contained in:
320
autom.cpp
320
autom.cpp
@@ -1,6 +1,6 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#undef timeout
|
||||
#include "mqtt/async_client.h"
|
||||
@@ -8,8 +8,6 @@
|
||||
#include <nlohmann/json.hpp>
|
||||
using json = nlohmann::json;
|
||||
|
||||
#include <csignal>
|
||||
|
||||
/* From Arduino.h */
|
||||
|
||||
#define HIGH 0x1
|
||||
@@ -27,14 +25,6 @@ using json = nlohmann::json;
|
||||
#define true 1
|
||||
#define false 0
|
||||
|
||||
/* Configuration MQTT */
|
||||
const std::string ADDRESS = "tcp://rabbitmq:1883";
|
||||
const std::string CLIENTID = "CppClientTP";
|
||||
const std::string TOPIC = "geii/in/#";
|
||||
const int QOS = 1;
|
||||
|
||||
int s0, s1;
|
||||
|
||||
// Temps
|
||||
|
||||
// Timestamp unix en millisecondes
|
||||
@@ -58,132 +48,124 @@ unsigned long millis()
|
||||
#define OP_ANALOG_WRITE 3
|
||||
#define OP_PIN_MODE 4
|
||||
|
||||
typedef struct PinIO
|
||||
{
|
||||
unsigned char mode;
|
||||
int ivalue;
|
||||
double dvalue;
|
||||
int error; // % = 1 /error ex 1 / 20 = 5 %
|
||||
double efficacite; // 0 - 100%
|
||||
unsigned long start;
|
||||
unsigned long time;
|
||||
double duration;
|
||||
unsigned int nb; // compteur d'activation
|
||||
int memory;
|
||||
unsigned char raising;
|
||||
unsigned char falling;
|
||||
} PinIO;
|
||||
int marche = 0;
|
||||
int arret = 1;
|
||||
int b0, b1, b2, b3, b4, b5, b6, b7;
|
||||
int i0, i1, i2, i3, i4, i5, i6, i7;
|
||||
int s0, s1, s2, s3, s4, s5, s6, s7;
|
||||
|
||||
PinIO _digital[256];
|
||||
/* ********************************************************
|
||||
* MQTT *
|
||||
* *
|
||||
******************************************************** */
|
||||
|
||||
void pinMode(unsigned char p, unsigned char mode)
|
||||
{
|
||||
_digital[p].mode = 0x01 | mode;
|
||||
|
||||
_digital[p].ivalue = 0;
|
||||
_digital[p].dvalue = 0.0;
|
||||
|
||||
_digital[p].nb = 0;
|
||||
_digital[p].time = 0.0;
|
||||
_digital[p].duration = 0.0;
|
||||
_digital[p].start = 0;
|
||||
_digital[p].raising = 0;
|
||||
_digital[p].memory = 0;
|
||||
}
|
||||
|
||||
// Réception des messages MQTT
|
||||
// ************************************************************
|
||||
class callback : public virtual mqtt::callback
|
||||
{
|
||||
public:
|
||||
void message_arrived(mqtt::const_message_ptr msg) override
|
||||
{
|
||||
std::string payload = msg->to_string();
|
||||
|
||||
std::cout << "MQTT IN : ";
|
||||
|
||||
try
|
||||
{
|
||||
json j = json::parse(payload);
|
||||
|
||||
// Ne rien faire si l'objet JSON est vide
|
||||
if (j.empty())
|
||||
return;
|
||||
|
||||
if (j.contains("b0"))
|
||||
s0 = j["b0"].get<int>() != 0;
|
||||
|
||||
std::cout << "boutons : " << s0 << std::endl;
|
||||
}
|
||||
catch (const json::parse_error &e)
|
||||
{
|
||||
std::cerr << "Erreur JSON : " << e.what() << "\n";
|
||||
}
|
||||
}
|
||||
};
|
||||
// ************************************************************
|
||||
/* Configuration MQTT */
|
||||
const std::string ADDRESS = "tcp://rabbitmq:1883";
|
||||
const std::string CLIENTID = "CppClientTP";
|
||||
const std::string TOPIC = "geii/in/#";
|
||||
const int QOS = 1;
|
||||
const int CYCLE_MS = 100;
|
||||
|
||||
mqtt::async_client client(ADDRESS, CLIENTID);
|
||||
|
||||
void open() {
|
||||
|
||||
callback cb;
|
||||
client.set_callback(cb);
|
||||
|
||||
mqtt::connect_options connOpts;
|
||||
connOpts.set_clean_session(true);
|
||||
connOpts.set_user_name("admin");
|
||||
connOpts.set_password("geii2025");
|
||||
|
||||
try
|
||||
{
|
||||
client.connect(connOpts)->wait();
|
||||
client.start_consuming();
|
||||
client.subscribe(TOPIC, QOS)->wait();
|
||||
}
|
||||
catch (const mqtt::exception &exc)
|
||||
{
|
||||
std::cerr << "Erreur MQTT: " << exc.what() << "\n";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void close() {
|
||||
try
|
||||
{
|
||||
client.unsubscribe(TOPIC)->wait();
|
||||
client.stop_consuming();
|
||||
client.disconnect()->wait();
|
||||
}
|
||||
catch (const mqtt::exception &exc)
|
||||
{
|
||||
std::cerr << "Erreur déconnexion MQTT: " << exc.what() << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Fin du programme" << std::endl;
|
||||
}
|
||||
|
||||
void send()
|
||||
void mqtt_process(mqtt::async_client* client)
|
||||
{
|
||||
json obj = {
|
||||
{"s0", s0},
|
||||
{"s1", s1},
|
||||
{"s2", 0},
|
||||
{"s3", 1},
|
||||
{"s4", 1},
|
||||
};
|
||||
json obj = {
|
||||
{"s0", s0 },
|
||||
{"s1", s1 },
|
||||
{"s2", s2 },
|
||||
{"s3", s3 },
|
||||
{"s4", s4 },
|
||||
{"s5", s5 },
|
||||
{"s6", s6 },
|
||||
{"s7", s7 },
|
||||
};
|
||||
|
||||
std::string payload = obj.dump();
|
||||
auto msg = mqtt::make_message("geii/out", payload);
|
||||
msg->set_qos(0);
|
||||
client.publish(msg);
|
||||
std::string payload = obj.dump();
|
||||
auto msg = mqtt::make_message("geii/out", payload);
|
||||
msg->set_qos(1);
|
||||
client->publish(msg);
|
||||
|
||||
std::cout << s0 << std::endl;
|
||||
|
||||
usleep(100000);
|
||||
usleep(100000);
|
||||
}
|
||||
|
||||
|
||||
// Réception des messages MQTT
|
||||
// ************************************************************
|
||||
class callback : public virtual mqtt::callback {
|
||||
public:
|
||||
void message_arrived(mqtt::const_message_ptr msg) override {
|
||||
std::string payload = msg->to_string();
|
||||
|
||||
try {
|
||||
json j = json::parse(payload);
|
||||
|
||||
marche = 0; arret = 1;
|
||||
b0 = b1 = b2 = b3 = b4 = b5 = b6 = b7 = 0;
|
||||
|
||||
// Ne rien faire si l'objet JSON est vide
|
||||
if (j.empty()) return;
|
||||
|
||||
if (j.contains("marche")) marche = j["marche"].get<int>() != 0;
|
||||
if (j.contains("arret")) arret = j["arret"].get<int>() != 0;
|
||||
|
||||
if (j.contains("b0")) b0 = j["b0"].get<int>() != 0;
|
||||
if (j.contains("b1")) b1 = j["b1"].get<int>() != 0;
|
||||
if (j.contains("b2")) b2 = j["b2"].get<int>() != 0;
|
||||
if (j.contains("b3")) b3 = j["b3"].get<int>() != 0;
|
||||
if (j.contains("b4")) b4 = j["b4"].get<int>() != 0;
|
||||
if (j.contains("b5")) b5 = j["b5"].get<int>() != 0;
|
||||
if (j.contains("b6")) b6 = j["b6"].get<int>() != 0;
|
||||
if (j.contains("b7")) b7 = j["b7"].get<int>() != 0;
|
||||
|
||||
if (j.contains("i0")) i0 = j["i0"].get<int>() != 0;
|
||||
if (j.contains("i1")) i1 = j["i1"].get<int>() != 0;
|
||||
if (j.contains("i2")) i2 = j["i2"].get<int>() != 0;
|
||||
if (j.contains("i3")) i3 = j["i3"].get<int>() != 0;
|
||||
if (j.contains("i4")) i4 = j["i4"].get<int>() != 0;
|
||||
if (j.contains("i5")) i5 = j["i5"].get<int>() != 0;
|
||||
if (j.contains("i6")) i6 = j["i6"].get<int>() != 0;
|
||||
if (j.contains("i7")) i7 = j["i7"].get<int>() != 0;
|
||||
|
||||
std::cout << "Pompes : " << marche << " " << arret << " B " << b0 << " " << b1 << " " << b2 << " " << b3 << " " << b4 << " " << b5 << " " << b6 << " " << b7 << " I " << i0 << " " << i1 << " " << i2 << " " << i3 << " " << i4 << " " << i5 << " " << i6 << " " << i7 << std::endl;
|
||||
}
|
||||
catch (const json::parse_error& e) {
|
||||
std::cerr << "Erreur JSON : " << e.what() << "\n";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
callback cb;
|
||||
|
||||
void mqtt_open(mqtt::async_client* client) {
|
||||
|
||||
client->set_callback(cb);
|
||||
mqtt::connect_options connOpts;
|
||||
connOpts.set_clean_session(true);
|
||||
connOpts.set_user_name("admin");
|
||||
connOpts.set_password("geii2025");
|
||||
try {
|
||||
client->connect(connOpts)->wait();
|
||||
client->start_consuming();
|
||||
client->subscribe(TOPIC, QOS)->wait();
|
||||
} catch (const mqtt::exception &exc) {
|
||||
std::cerr << "Erreur MQTT: " << exc.what() << "\n";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void mqtt_close() {
|
||||
try {
|
||||
client.unsubscribe(TOPIC)->wait();
|
||||
client.stop_consuming();
|
||||
client.disconnect()->wait();
|
||||
} catch(const mqtt::exception &exc){
|
||||
std::cerr << "Erreur déconnexion MQTT: " << exc.what() << std::endl;
|
||||
}
|
||||
}
|
||||
// ************************************************************
|
||||
|
||||
|
||||
/* ********************************************************
|
||||
* TEMPORISATION RETARD A LA MONTEE *
|
||||
* La sortie passe à 1 au bout de la tempo *
|
||||
@@ -250,7 +232,7 @@ class TemporisationRetardMontee
|
||||
};
|
||||
|
||||
/********************************************************
|
||||
* TEMPORISATION RETARD A LA DESCENTE *
|
||||
* TEMPORISATION RETARD A LA DESCENTE *
|
||||
* La sortie passe à 0 au bout de la tempo *
|
||||
*********************************************************/
|
||||
class TemporisationRetardDescente
|
||||
@@ -410,87 +392,3 @@ class MiseAEchelle
|
||||
float maxEntree;
|
||||
float maxSortie;
|
||||
};
|
||||
|
||||
/* READ */
|
||||
|
||||
int digitalRead(int p)
|
||||
{
|
||||
return ((_digital[p].mode & IO_INPUT) && (_digital[p].mode & DIGITAL) && (_digital[p].mode & 0x01)) ? _digital[p].ivalue : 0;
|
||||
}
|
||||
|
||||
double analogRead(int p)
|
||||
{
|
||||
return ((_digital[p].mode & IO_INPUT) && (_digital[p].mode & ANALOG) && (_digital[p].mode & 0x01)) ? _digital[p].dvalue : 0.0;
|
||||
}
|
||||
|
||||
/* WRITE */
|
||||
|
||||
void digitalWrite(unsigned int p, int value)
|
||||
{
|
||||
if (p > 255)
|
||||
{
|
||||
printf("Pin %d is out of Range.", p);
|
||||
return;
|
||||
}
|
||||
|
||||
// En panne !
|
||||
if (!(_digital[p].mode & 0x01))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(_digital[p].mode & IO_OUTPUT && _digital[p].mode & DIGITAL))
|
||||
{
|
||||
printf("Pin %d is not a digital input.", p);
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned long m = millis();
|
||||
|
||||
if (value != _digital[p].ivalue)
|
||||
{
|
||||
_digital[p].time = _digital[p].time > 5000 ? 0 : 5000 - _digital[p].time;
|
||||
}
|
||||
else
|
||||
{
|
||||
_digital[p].time += m - _digital[p].start;
|
||||
}
|
||||
|
||||
if (value && !_digital[p].ivalue)
|
||||
{
|
||||
_digital[p].start = m;
|
||||
_digital[p].nb += 1;
|
||||
}
|
||||
else if (value)
|
||||
{
|
||||
_digital[p].duration += dt;
|
||||
}
|
||||
|
||||
_digital[p].raising = (_digital[p].memory < _digital[p].ivalue);
|
||||
_digital[p].falling = (_digital[p].memory > _digital[p].ivalue);
|
||||
_digital[p].memory = _digital[p].ivalue;
|
||||
_digital[p].ivalue = value;
|
||||
}
|
||||
|
||||
void analogWrite(unsigned int p, double value)
|
||||
{
|
||||
if (p > 31)
|
||||
{
|
||||
printf("Pin %d is out of Range.", p);
|
||||
return;
|
||||
}
|
||||
|
||||
// En panne
|
||||
if (!(_digital[p].mode & 0x01))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(_digital[p].mode & IO_OUTPUT) || !(_digital[p].mode & ANALOG))
|
||||
{
|
||||
printf("Pin %d is not a analog input.", p);
|
||||
return;
|
||||
}
|
||||
|
||||
_digital[p].dvalue = value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user