/****************************************
Include Libraries
****************************************/
#include <PubSubClient.h>
#include <TFT_eSPI.h>
#include <rpcWiFi.h>
TFT_eSPI tft;
#include <multi_channel_relay.h> //
Multi_Channel_Relay relay;
int sensorPin_0 = A0;
int sensorPin_1 = A1;
int sensorPin_2 = A2;
float sensorValue_0 = 0;
float sensorValue_1 = 0;
float sensorValue_2 = 0;
int Relay = 400;
/****************************************
Define Constants
****************************************/
#define WIFISSID "x.factory" // Put your WifiSSID here
#define PASSWORD "make0314" // Put your wifi password here
#define TOKEN "BBFF-kVnBwT4h3leAslyRqe6LX1HzfGbrbT" // Put your Ubidots' TOKEN
#define VARIABLE_LABEL "light" // Assign the variable label
#define VARIABLE_LABEL1 "soil1" // Assign the variable label
#define VARIABLE_LABEL2 "soil2" // Assign the variable label
#define VARIABLE_LABEL3 "soil3" // Assign the variable label
#define DEVICE_LABEL "wio-terminal" // Assign the device label
#define MQTT_CLIENT_NAME "7711538145" // MQTT client Name
//#define LCD_BACKLIGHT (72Ul)
const long interval = 100;
unsigned long previousMillis = 0;
char mqttBroker[] = "industrial.api.ubidots.com";
// Space to store values to send
/****************************************
Initialize constructors for objects
****************************************/
WiFiClient wifiClient;
PubSubClient client(wifiClient);
static int light = 0;
static int soil_1 = 0;
static int soil_2 = 0;
static int soil_3 = 0;
static int conf = 0; //
static int cont = 0;
static int cons = 0;
char str_light[6];
char str_Soil_1[6];
char str_Soil_2[6];
char str_Soil_3[6];
char payload[700];
char topic[150];
/****************************************
Auxiliar functions
****************************************/
void read_soil() //
{
soil_1 = analogRead(A0);
soil_2 = analogRead(A1);
soil_3 = analogRead(A2);
light = analogRead(WIO_LIGHT);
Serial.print("soil_1 = ");
Serial.println(soil_1);
Serial.print("soil_2 = ");
Serial.println(soil_2);
Serial.print("soil_3 = ");
Serial.println(soil_3);
if (soil_1 < 500) {
relay.turn_on_channel(4);
}else{
relay.turn_off_channel(4);
}
if (soil_2 < 500) {
relay.turn_on_channel(3);
}else{
relay.turn_off_channel(3);
}
if (soil_3 < 500) {
relay.turn_on_channel(2);
}else{
relay.turn_off_channel(2);
}
if (light < 80) {
relay.turn_on_channel(1);
}else{
relay.turn_off_channel(1);
}
}
void jidianqi(int zhaungtai)
{
if (zhaungtai == 1) relay.turn_on_channel(4);Serial.println("jiaoshui1");
if (zhaungtai == 2) relay.turn_on_channel(3);Serial.println("jiaoshui2");
if (zhaungtai == 3) relay.turn_on_channel(2);Serial.println("jiaoshui3");
if (zhaungtai == 4) relay.turn_on_channel(1);Serial.println("kaideng");
if (zhaungtai == 5) {
relay.turn_off_channel(1);
relay.turn_off_channel(2);
relay.turn_off_channel(3);
relay.turn_off_channel(4);
}
}
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]);
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) {
Serial.println("connected");
}
else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
}
/****************************************
Main Functions
****************************************/
void setup() {
Serial.begin(115200);
relay.begin(0x11); //
tft.begin();
tft.setRotation(3);
tft.setTextSize(2);
tft.fillScreen(TFT_BLACK);
digitalWrite(LCD_BACKLIGHT, HIGH);
pinMode(WIO_LIGHT, INPUT);
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
tft.drawString("Connecting to WiFi...", 20, 120);
WiFi.begin(WIFISSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
tft.drawString("Connecting to WiFi...", 20, 0);
WiFi.begin(WIFISSID, PASSWORD);
}
tft.fillScreen(TFT_BLACK);
tft.drawString("Connected to the WiFi network", 20, 120);
tft.fillScreen(TFT_BLACK);
delay(1000);
client.setServer(mqttBroker, 1883);
client.setCallback(callback);
}
void loop() {
send_date();
read_soil();
//jidianqi(cont);
Serial.println(cont);
}
void send_date() {
float light = analogRead(WIO_LIGHT);
dtostrf(soil_1, 4, 0, str_Soil_1);
dtostrf(soil_2, 4, 0, str_Soil_2);
dtostrf(soil_3, 4, 0, str_Soil_3);
dtostrf(light, 4, 0, str_light);
tft.fillRect(140, 110, 100, 15, TFT_BLACK);
tft.drawString(str_light, 140, 110);
if (!client.connected()) {
reconnect();
}
//***Publish***
// Builds the topic
sprintf(topic, "%s", ""); // Cleans the topic content
sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
//Builds the payload
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL); // Adds the variable label
sprintf(payload, "%s%s", payload, str_light); // Adds the value
sprintf(payload, "%s}", payload); // Closes the dictionary brackets
client.publish(topic, payload);
delay(500);
tft.drawString("Topic", 0, 140);
tft.drawString(topic, 0, 160);
tft.drawString("Payload", 0, 190);
tft.fillRect(100, 210, 100, 15, TFT_BLACK);
tft.drawString(payload, 0, 210);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL1); // Adds the variable label
sprintf(payload, "%s%s", payload, str_Soil_1); // Adds the value
sprintf(payload, "%s}", payload); // Closes
client.publish(topic, payload);
delay(500);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL2); // Adds the variable label
sprintf(payload, "%s%s", payload, str_Soil_2); // Adds the value
sprintf(payload, "%s}", payload); // Closes
client.publish(topic, payload);
delay(500);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL3); // Adds the variable label
sprintf(payload, "%s%s", payload, str_Soil_3); // Adds the value
sprintf(payload, "%s}", payload); // Closes
client.publish(topic, payload);
delay(500);
client.loop();
}
Comments
Please log in or sign up to comment.