Skip to content

Commit 9c2c97d

Browse files
committed
feat: add greenhouse id to messages and change to send multiple messages
1 parent 30eaf1e commit 9c2c97d

7 files changed

Lines changed: 262 additions & 240 deletions

File tree

ArduinoProject/ArduinoProject.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#define PIN_TX A4
3737

3838
#define SCHEDULE_TIME 20UL
39+
#define GREENHOUSE_ID "greenhouse1"
3940

4041
Ventilation *ventilation;
4142
Brightness *photoresistor;
@@ -82,8 +83,8 @@ void setup() {
8283
msgServiceEsp->init();
8384
sender = new Sender(msgServiceEsp);
8485

85-
sensingTask = new SensingTask(photoresistor, soilMoistureSensor, temp, hum, sender);
86-
sensingTask->init(1000UL * 60UL * 15UL);
86+
sensingTask = new SensingTask(photoresistor, soilMoistureSensor, temp, hum, sender, GREENHOUSE_ID);
87+
sensingTask->init(1000UL * 10UL);//60UL * 3UL
8788
listenerTask = new ListenerTask(irrigation, ventilation, tempLamp, lamp, msgServiceEsp);
8889
listenerTask->init(1000UL * 3UL);
8990

ArduinoProject/SensingTask.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Implementation of the class SensingTask.
55
*/
6-
SensingTask::SensingTask(Brightness *photoresistor, SoilMoistureSensor *soilMoistureSensor, Temperature *temp, Humidity *hum, Sender *sender)
6+
SensingTask::SensingTask(Brightness *photoresistor, SoilMoistureSensor *soilMoistureSensor, Temperature *temp, Humidity *hum, Sender *sender, String greenhouseId)
77
{
88
this->photoresistor = photoresistor;
99
this->soilMoistureSensor = soilMoistureSensor;
@@ -17,6 +17,8 @@ SensingTask::SensingTask(Brightness *photoresistor, SoilMoistureSensor *soilMois
1717
this->soilMoistureValue = 0.0;
1818

1919
this->sender = sender;
20+
21+
this->greenhouseId = greenhouseId;
2022
}
2123

2224

@@ -37,12 +39,21 @@ bool SensingTask::isActive()
3739

3840
void SensingTask::tick()
3941
{
42+
4043
temperatureValue = this->temp->getTemperature();
4144
humidityValue = this->hum->getHumidity();
4245
brightnessValue = this->photoresistor->getBrightness();
4346
soilMoistureValue = this->soilMoistureSensor->getValue();
4447

45-
String msg ="{'Temp': " + String(temperatureValue) + ",'Hum': " + String(humidityValue) + ",'Bright': " + String(brightnessValue) + ",'Soil': " + String(soilMoistureValue)+ "}";
48+
this->sendMessage("Temp", String(temperatureValue));
49+
this->sendMessage("Hum", String(humidityValue));
50+
this->sendMessage("Bright", String(brightnessValue));
51+
this->sendMessage("Soil", String(soilMoistureValue));
52+
}
53+
54+
void SensingTask::sendMessage(String parameter, String value){
55+
String msg = "{'id': '" + this->greenhouseId + "','" + parameter + "': " + value+ "}";
4656
sender->notifyMsg(msg);
4757
msg = "";
58+
delay(1000);
4859
}

ArduinoProject/SensingTask.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ class SensingTask : public Task
2727
* @param soilMoistureSensor the sensor that registered the soil moisture.
2828
* @param temp the DHT sensor that registered the humidity and the temperature.
2929
* @param sender the sender to send the data to the ESP.
30-
*/
31-
SensingTask(Brightness *photoresistor, SoilMoistureSensor *soilMoistureSensor, Temperature *temp, Humidity *hum, Sender *sender);
30+
* @param greenhouseId greenhouse identification name.
31+
*/
32+
SensingTask(Brightness *photoresistor, SoilMoistureSensor *soilMoistureSensor, Temperature *temp, Humidity *hum, Sender *sender, String greenhouseId);
3233
/**
3334
* Initialize the task.
3435
* @param period the period of the task to be evaluated by the Scheduler.
@@ -54,6 +55,8 @@ class SensingTask : public Task
5455
float humidityValue;
5556
float brightnessValue;
5657
float soilMoistureValue;
58+
String greenhouseId;
59+
void sendMessage(String parameter, String value);
5760

5861
};
5962

EspProject/Esp8266.cpp

Lines changed: 82 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,82 @@
1-
#include "Esp8266.h"
2-
3-
/**
4-
* Implementation of the Esp8266 class.
5-
*/
6-
Esp8266::Esp8266(char *ssidName, char *pwd, char *mqttServer, MsgServiceArduino *msgARD):client(espClient)
7-
{
8-
this->ssidName = ssidName;
9-
this->pwd = pwd;
10-
this->msgARD = msgARD;
11-
12-
client.setServer(mqttServer, 1883);
13-
client.setCallback(std::bind(&Esp8266::callback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
14-
}
15-
16-
void Esp8266::callback(char* topic, byte* payload, unsigned int length){
17-
String message = String((char*)payload);
18-
this->msgARD->sendMsg(String(topic) + ":" + message.substring(0, length));
19-
message = "";
20-
}
21-
22-
void Esp8266::connecting()
23-
{
24-
WiFi.begin(ssidName, pwd);
25-
Serial.print("Connecting...");
26-
while (WiFi.status() != WL_CONNECTED)
27-
{
28-
delay(500);
29-
Serial.print(".");
30-
}
31-
WiFi.setSleepMode(WIFI_NONE_SLEEP);
32-
}
33-
34-
void Esp8266::reconnect(){
35-
while (!client.connected()) {
36-
Serial.print("Attempting MQTT connection...");
37-
String clientId = "ESP8266Client-";
38-
clientId += String(random(0xffff), HEX);
39-
if (client.connect(clientId.c_str())) {
40-
Serial.println("connected");
41-
client.subscribe(VENTILATION);
42-
client.subscribe(IRRIGATION);
43-
client.subscribe(LUMINOSITY);
44-
client.subscribe(TEMPERATURE);
45-
} else {
46-
Serial.print("failed, rc=");
47-
Serial.print(client.state());
48-
Serial.println(" try again in 5 seconds");
49-
// Wait 5 seconds before retrying
50-
delay(5000);
51-
}
52-
}
53-
}
54-
55-
void Esp8266::processIncomingMessages(){
56-
if(!client.connected()){
57-
this->reconnect();
58-
}
59-
client.loop();
60-
}
61-
62-
void Esp8266::sendData(char* topic, String msg){
63-
if (WiFi.status() != WL_CONNECTED)
64-
{
65-
Serial.println("Not connecting");
66-
connecting();
67-
}
68-
if (WiFi.status() == WL_CONNECTED)
69-
{
70-
client.publish(topic, msg.c_str());
71-
}
72-
}
73-
74-
bool Esp8266::isConnected()
75-
{
76-
return WiFi.status() == WL_CONNECTED;
77-
}
1+
#include "Esp8266.h"
2+
3+
/**
4+
* Implementation of the Esp8266 class.
5+
*/
6+
Esp8266::Esp8266(char *ssidName, char *pwd, char *mqttServer, MsgServiceArduino *msgARD, String greenhouseId):client(espClient)
7+
{
8+
this->ssidName = ssidName;
9+
this->pwd = pwd;
10+
this->msgARD = msgARD;
11+
12+
client.setServer(mqttServer, 1883);
13+
client.setCallback(std::bind(&Esp8266::callback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
14+
}
15+
16+
void Esp8266::callback(char* topic, byte* payload, unsigned int length){
17+
String message = String((char*)payload);
18+
//message = "greenhouseID,action"
19+
String id = String(strtok((char*)payload, ","));
20+
String action = String((char *)strtok(NULL, ","));
21+
if (id == this->greenhouseId)
22+
{
23+
this->msgARD->sendMsg(String(topic) + ":" + action);
24+
}
25+
}
26+
27+
void Esp8266::connecting()
28+
{
29+
WiFi.begin(ssidName, pwd);
30+
Serial.print("Connecting...");
31+
while (WiFi.status() != WL_CONNECTED)
32+
{
33+
delay(500);
34+
Serial.print(".");
35+
}
36+
WiFi.setSleepMode(WIFI_NONE_SLEEP);
37+
}
38+
39+
void Esp8266::reconnect(){
40+
while (!client.connected()) {
41+
Serial.print("Attempting MQTT connection...");
42+
String clientId = "ESP8266Client-";
43+
clientId += String(random(0xffff), HEX);
44+
if (client.connect(clientId.c_str())) {
45+
Serial.println("connected");
46+
client.subscribe(VENTILATION);
47+
client.subscribe(IRRIGATION);
48+
client.subscribe(LUMINOSITY);
49+
client.subscribe(TEMPERATURE);
50+
} else {
51+
Serial.print("failed, rc=");
52+
Serial.print(client.state());
53+
Serial.println(" try again in 5 seconds");
54+
// Wait 5 seconds before retrying
55+
delay(5000);
56+
}
57+
}
58+
}
59+
60+
void Esp8266::processIncomingMessages(){
61+
if(!client.connected()){
62+
this->reconnect();
63+
}
64+
client.loop();
65+
}
66+
67+
void Esp8266::sendData(char* topic, String msg){
68+
if (WiFi.status() != WL_CONNECTED)
69+
{
70+
Serial.println("Not connecting");
71+
connecting();
72+
}
73+
if (WiFi.status() == WL_CONNECTED)
74+
{
75+
client.publish(topic, msg.c_str());
76+
}
77+
}
78+
79+
bool Esp8266::isConnected()
80+
{
81+
return WiFi.status() == WL_CONNECTED;
82+
}

EspProject/Esp8266.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,23 @@ class Esp8266: public Connection {
2121
WiFiClient espClient;
2222
PubSubClient client;
2323
public:
24-
2524
/**
2625
* Constructor of Esp8266 object.
2726
* @param ssidName The SSID of the WiFi network.
2827
* @param pwd The password of the WiFi network.
2928
* @param mqttServer The address of the MQTT broker.
3029
* @param msgARD A pointer to a MsgServiceArduino object.
30+
* @param greenhouseId Greenhouse identification.
3131
*/
32-
Esp8266(char *ssidName, char *pwd, char *mqttServer, MsgServiceArduino *msgARD);
32+
Esp8266(char *ssidName, char *pwd, char *mqttServer, MsgServiceArduino *msgARD, String greenhouseId);
3333
void connecting();
3434
void sendData(char* topic, String msg);
3535
void processIncomingMessages();
3636
bool isConnected();
3737
MsgServiceArduino *msgARD;
3838
private:
39-
void callback(char* topic, byte* payload, unsigned int length);
39+
String greenhouseId;
40+
void callback(char *topic, byte *payload, unsigned int length);
4041
void reconnect();
4142
};
4243

0 commit comments

Comments
 (0)