#include <WiFi.h>
#include <PubSubClient.h>
#include <WiFiClient.h>
#include <string.h>
#include <math.h>
#define I_sensor_ph1 36
#define U_sensor_ph1 39
#define I_sensor_ph2 34
#define U_sensor_ph2 35
#define I_sensor_ph3 32
#define U_sensor_ph3 33
#define I_sensor_N 25
#define PIN_V1 4
//////////////////////////////////////////////////
// MQTT ThingSpeak et les Ids des channel utiliser
#define MQTT_USERNAME "ISc4Iho4EiMMKgglKyQaEwI"
#define MQTT_CLIENT_ID "ISc4Iho4EiMMKgglKyQaEwI"
#define MQTT_PASSWORD "yzzqKgytDwOPxF/2VuLheFrY"
// Publish Settings
const char* Pub_Tension_Courant = "channels/2375024/publish";
const char* Pub_Power_Energie = "channels/2377699/publish";
// Subscribed Settings
const char* Sub_Tension_Courant = "channels/2375024/subscribe/fields/field8";
const char* Sub_Power_Energie = "channels/2377699/subscribe/fields/field6";
// MQTT broker setting
const char* mqtt_server = "mqtt3.thingspeak.com";
const int mqtt_port = 1883;
/////////////////////////////////////////////////
const float T = 1/50;
const unsigned int pi = 3.14;
float I[3] = {0, 0, 0}; // Courants des 3 phases
float V[3] = {0, 0, 0}; // Tensions des 3 phases
float Ph[3] = {0, 0, 0}; // les dephasage des 3 phases
float IN = 0; // Courant de neutre
float Pa_T = 0; // puissance Active Total
float Ea_T = 0; // puissance Active Total
unsigned long lastMessage = 0;
String myTopic;
String myMessage;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi()
{
Serial.print("\nConnecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi...");
}
Serial.println("Wifi connected !\nIP address: " + String(WiFi.localIP()));
}
void callback(char* topic, byte* message, unsigned int length)
{
myTopic = String(topic);
myMessage = "";
if( myTopic == Sub_Tension_Courant)
{
for (int i = 0; i < length; i++)
{
myMessage += (char)message[i];
}
courantNeutre();
}
else if( myTopic == Sub_Power_Energie)
{
for (int i = 0; i < length; i++)
{
myMessage += (char)message[i];
}
signalisationPuissance();
}
}
void setup() {
pinMode(PIN_V1, OUTPUT);
Serial.begin(9600);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void Aquisiation()
{
String Voltage;
String Courant;
String Pub_Data;
int X;
V[0] = rand() % 42 + 190; //analogRead(U_sensor_ph3)*(230/4096);
V[1] = rand() % 42 + 190; //analogRead(U_sensor_ph2)*(230/4096);
V[3] = rand() % 42 + 190; //analogRead(U_sensor_ph3)*(230/4096);
Voltage = String("field1=" + String(V[0])+ "&field2=" + String(V[1])+ "&field3=" + String(V[2]));
I[0] = rand() % 16 + 15; //analogRead(I_sensor_ph1)*(40/4096);
I[1] = rand() % 16 + 15; //analogRead(I_sensor_ph2)*(40/4096);
I[2] = rand() % 16 + 15; //analogRead(I_sensor_ph3)*(40/4096);
Courant = String("&field4=" + String(I[0])+ "&field5=" + String(I[1])+ "&field6=" + String(I[2]));
IN = random(3); //analogRead(I_sensor_N)*(10/4096);
Pub_Data = String(Voltage + Courant+ "&field7=" + String(IN)+"&status=MQTTPUBLISH");
X = client.publish(Pub_Tension_Courant, Pub_Data.c_str());
if (X == 1)
Serial.println("The data on voltage and current has been successfully published.");
}
void pubPuissance()
{
String Puissance;
String Pub_Data;
float puissanceL1 = 0;
float puissanceL2 = 0;
float puissanceL3 = 0;
int X;
puissanceL1 = rand() % 1001 + 500; // V[0]*I[0]*cos(Ph[0]);
puissanceL2 = rand() % 1001 + 500; // V[1]*I[1]*cos(Ph[1]);
puissanceL3 = rand() % 1001 + 500; // V[2]*I[2]*cos(Ph[2]);
Pa_T = puissanceL1 + puissanceL2 + puissanceL3;
Puissance = String("field1=" + String(puissanceL1) + "&field2=" + String(puissanceL2) + "&field3=" + String(puissanceL3));
Pub_Data = String(Puissance + "&field4=" + String(Pa_T) + "&status=MQTTPUBLISH");
X = client.publish(Pub_Power_Energie, Pub_Data.c_str());
if (X == 1)
Serial.println("The data on Power has been successfully published.");
}
void pubEnergie()
{
int X;
String Pub_Data;
Ea_T = rand() % 3001 + 1000; // (Pa_T*T/10)/3600;
Pub_Data = String("field5=" + String(Ea_T) + "&status=MQTTPUBLISH");
X = client.publish(Pub_Power_Energie, Pub_Data.c_str());
if (X == 1)
Serial.println("The data on Energy has been successfully published.");
}
void signalisationPuissance()
{
float puissanceL1;
puissanceL1 = rand() % 3001 + 1000; //V[0]*I[0]*cos(2*pi*Frequence + Ph[0]);
if( myTopic == Sub_Power_Energie)
{
if (puissanceL1 > myMessage.toFloat())
{
digitalWrite(PIN_V1, HIGH);
}
else
{
digitalWrite(PIN_V1, LOW);
}
}
}
void courantNeutre()
{
String State_Neutre;
int X;
if (IN > myMessage.toFloat())
{
State_Neutre = String("field7="+ String(1) + "&status=MQTTPUBLISH");
X = client.publish(Pub_Power_Energie, State_Neutre.c_str());
if (X == 1)
Serial.println("The State_Neutre has been successfully published.");
}
else
{
State_Neutre = String("field7=" + String(0) + "&status=MQTTPUBLISH");
X = client.publish(Pub_Power_Energie, State_Neutre.c_str());
if (X == 1)
Serial.println("The State_Neutre has been successfully published.");
}
}
void reconnect()
{
while (!client.connected())
{
if (client.connect(MQTT_CLIENT_ID, MQTT_USERNAME, MQTT_PASSWORD))
{
client.subscribe(Sub_Tension_Courant);
client.subscribe(Sub_Power_Energie);
}
else
{
Serial.print("failed,");
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void loop()
{
if (!client.connected())
{
reconnect();
}
client.loop();
unsigned long now = millis();
if(now - lastMessage > 2000)
{
Serial.println("Start");
Aquisiation();
pubPuissance();
pubEnergie();
signalisationPuissance();
courantNeutre();
lastMessage = now;
}
}