// Libraries
#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <time.h>
// WiFi - MQTT variables
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "broker.hivemq.com";
const char* stopic = "project3/10660359_10656537"; // Client subscribes to this topic
const char* ptopic = "project3/10660359_10656537"; // Client publishes to this topic
WiFiClient espClient;
PubSubClient client(espClient);
// Sensor variables
#define LED 4
// Structure of the superframe
int Frame_duration = 0;
int Beacon_interval = 0;
int CAP = 0;
int CFP = 0;
int CFP_slots_clients[4]; // Array to collect the clients registered to the network
int CFP_slots_starttime[4]; // Array to collect the start times of the CFP part for the clients
int CFP_slots_endtime[4]; // Array to collect the end times of the CFP part for the clients
int Start_time; // Start time for client 4
int End_time; // End time for client 4
int Sleep = 0;
// Messages variables
const int client_id = 4;
String message_received; // String in which we collect the message received
int counter_registration = 0; // Counter to register to the network only in the first frame
int type = 0;
int sender = 0;
int receiver = 0;
long humidity = 0;
// Setup. of the WiFi connection of the board
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
// Setup. of the MQTT connection
void setup_MQTT(){
client.setSocketTimeout(60);
client.setKeepAlive(5000); // Time in which the MQTT connection to the server is alive
client.setServer(mqtt_server, 1883);
client.setCallback(callback); // Set callback function to receive messages published on the topic
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP8266_Receive", "", "" )) { //Sign in to the broker
Serial.println("Connected to broker");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
client.subscribe(stopic); // MQTT subscription to the topic
}
// Callback function
void callback(char* topic, byte* payload, unsigned int length) {
message_received = ""; // Cancellation of the content of the message_received string to process only the new messages
for (int i = 0; i < length; i++) {
message_received = message_received + (char)payload[i];
}
Serial.print(message_received); // Print the messsage on the terminal
}
// Function to publish the registration message in the CAP part
void publish_registration()
{
int type = 1; // Registration message
DynamicJsonDocument json(100);
json["Type"] = type;
json["Client"] = client_id;
json["Message"] = "I want to register";
String jsonString;
serializeJson(json, jsonString);
const char* payload = jsonString.c_str();
Serial.print("Registration: ");
Serial.println(payload);
client.publish(ptopic, payload, false); // false = not retained message
}
// Deserialization of the beacon json message
void deserializationBeacon()
{
//Deserialize BEACON
DynamicJsonDocument inf_superframe(600);
// Deserialize the JSON document
DeserializationError error1 = deserializeJson(inf_superframe, message_received);
// Test if parsing succeeds
if (error1) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error1.f_str());
return;
}
Beacon_interval = inf_superframe["Beacon_interval"];
Frame_duration = inf_superframe["Frame_duration"];
CAP = inf_superframe["CAP"];
CFP = inf_superframe["CFP"];
String CFP_slots = inf_superframe["CFP_slots"];
Sleep = inf_superframe["Sleep"];
//Deserialize CFP slot assignment
DynamicJsonDocument CFP_slotassignment(400);
// Deserialize the JSON document
DeserializationError error2 = deserializeJson(CFP_slotassignment, CFP_slots);
// Test if parsing succeeds.
if (error2) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error2.f_str());
return;
}
// Slot assignment arrays
copyArray(CFP_slotassignment["Client"], CFP_slots_clients);
copyArray(CFP_slotassignment["Start_time"], CFP_slots_starttime);
copyArray(CFP_slotassignment["End_time"], CFP_slots_endtime);
}
// Deserialization of the json message received, containing the humidity value
void deserializationPublish()
{
Serial.println(message_received);
//Deserialize publish message
DynamicJsonDocument publish(600);
// Deserialize the JSON document
DeserializationError error1 = deserializeJson(publish, message_received);
// Test if parsing succeeds
if (error1) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error1.f_str());
return;
}
// Json fields
type = publish["Type"];
sender = publish["Sender"];
receiver = publish["Receiver"];
const char* hum = publish["Humidity"];
// Extraction of the value of the humidity from the string received
humidity = atoi(hum);
}
// Extraction of the arrays from the CFP_slots beacon field
void array_extraction()
{
for (int t = 0; t<4; t++){
if(CFP_slots_clients[t]==client_id){
Start_time = CFP_slots_starttime[t];
End_time = CFP_slots_endtime[t];
}
}
}
void setup() {
// Starts the terminal
Serial.begin(115200);
// Pin
pinMode(LED, OUTPUT);
// Starts WiFi and MQTT connection
setup_wifi();
setup_MQTT();
}
void loop() {
// Verification of the MQTT connection
if (!client.connected()) {
Serial.print("!");
delay(250);
}
else if (message_received.length() > 1) {
// BEACON INTERVAL
Serial.println();
// Deserialization of Json strings
while(Beacon_interval == 0){
deserializationBeacon();
array_extraction();
}
// Timer to synchronize with Node-Red and for the duration of the beacon interval
Serial.println("Start BI");
delay(Beacon_interval);
Serial.println("End beacon interval");
// CAP
Serial.println("Start CAP");
// Publication of the registration message
if(counter_registration == 0){
publish_registration();
counter_registration = 1;
}
delay(CAP);
Serial.println("End CAP");
// CFP
Serial.println("Start CFP");
// Timer with deserialization publish message
if (counter_registration == 1){
counter_registration = 2;
delay(CFP);
}
else if (counter_registration == 2){
long difference;
long start = millis();
do{
difference = millis() - start; // Timer
deserializationPublish(); // Deserialization of the publish message received
if (type == 0){
if (receiver == client_id){
analogWrite(LED, humidity); // Turns on the led with level of luminosity equal to the humidity value received
}
}
} while (difference < CFP);
}
Serial.println("End CFP");
//Sleep
Serial.println("Start sleep");
delay(Sleep);
Serial.println("End sleep");
}
client.loop(); // To mantein the MQTT connection alive
}