Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Module: Watering
- Hardware Version: 1.0
- Software Version: 1.0
- */
- #include <ESP8266httpUpdate.h>
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- const char* ssid = "Slow-Fi";
- const char* pass = "5YWNT2!CEyQ3rv^6";
- const char* mqtt_host = "10.0.0.2";
- const uint16_t mqtt_port = 1883;
- const char* mqtt_id = "WAT001";
- const char* mqtt_rx = "WAT001-RX";
- const char* mqtt_tx = "WAT001-TX";
- const char* mqtt_user = "";
- const char* mqtt_pass = "";
- const char* ota_host = "updates.grobotics.io";
- const char* ota_port = "80";
- const String ota_path = "/watering-1.bin";
- const char* module_name = "Module Name: Watering";
- const char* hardware_version = "Hardware Version: 1.0";
- const char* software_version = "Software Version: 1.0";
- int top = 0;
- int bottom = 0;
- WiFiClient WIFI;
- PubSubClient MQTT;
- char mqtt_buffer[100];
- String mqtt_command;
- void mqtt(char* mqtt_topic, byte* mqtt_payload, unsigned int mqtt_length) {
- for (int mqtt_counter = 0; mqtt_counter < mqtt_length; mqtt_counter++) {
- mqtt_buffer[mqtt_counter] = mqtt_payload[mqtt_counter];
- }
- mqtt_command = String(mqtt_buffer);
- Serial.print("MQTT Command: ");
- Serial.println(mqtt_command);
- memset(mqtt_buffer, '\0', sizeof(mqtt_buffer));
- }
- void setup() {
- Serial.begin(9600);
- MQTT.setClient(WIFI);
- MQTT.setServer(mqtt_host,mqtt_port);
- MQTT.setCallback(mqtt);
- pinMode(14, OUTPUT);
- pinMode(12, OUTPUT);
- pinMode(0, INPUT);
- pinMode(2, INPUT);
- digitalWrite(14, LOW);
- digitalWrite(12, LOW);
- }
- void loop() {
- if (WiFi.status() != WL_CONNECTED) {
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, pass);
- if (WiFi.waitForConnectResult() != WL_CONNECTED) {
- return;
- }
- Serial.print("Local IP ");
- Serial.println(WiFi.localIP());
- Serial.println("Connection Sucessful");
- Serial.println();
- }
- if (WiFi.status() == WL_CONNECTED) {
- if (!MQTT.connected()) {
- Serial.println("Connecting to MQTT Broker");
- Serial.print("Host: ");
- Serial.println(mqtt_host);
- Serial.print("Port: ");
- Serial.println(mqtt_port);
- if (MQTT.connect(mqtt_rx, mqtt_user, mqtt_pass)) {
- Serial.println("Connection Sucessful");
- Serial.println();
- MQTT.publish("Connect",mqtt_id);
- MQTT.subscribe(mqtt_rx);
- }
- }
- if (MQTT.connected()) {
- MQTT.loop();
- if (mqtt_command == "module name") {
- MQTT.publish(mqtt_tx, module_name);
- } else if (mqtt_command == "hardware version") {
- MQTT.publish(mqtt_tx, hardware_version);
- } else if (mqtt_command == "software version") {
- MQTT.publish(mqtt_tx, software_version);
- } else if (mqtt_command == "update") {
- MQTT.publish(mqtt_tx, "Updating");
- ESPhttpUpdate.update(ota_host, ota_port, ota_path);
- } else if (mqtt_command == "fill off") {
- digitalWrite(14, LOW);
- MQTT.publish(mqtt_tx, "Command Sucessful");
- } else if (mqtt_command == "fill on") {
- digitalWrite(14, HIGH);
- MQTT.publish(mqtt_tx, "Command Sucessful");
- } else if (mqtt_command == "drain off") {
- digitalWrite(12, LOW);
- MQTT.publish(mqtt_tx, "Command Sucessful");
- } else if (mqtt_command == "drain on") {
- digitalWrite(12, HIGH);
- MQTT.publish(mqtt_tx, "Command Sucessful");
- } else if (mqtt_command != "") {
- MQTT.publish(mqtt_tx, "Command Not Recognised");
- Serial.println("Command Not Recognised");
- }
- mqtt_command = "";
- if (top == 1 & digitalRead(0) == HIGH) {
- top = 0;
- MQTT.publish(mqtt_tx, "top low");
- } else if (top == 0 & digitalRead(0) == LOW) {
- top = 1;
- MQTT.publish(mqtt_tx, "top high");
- }
- if (bottom == 1 & digitalRead(2) == HIGH) {
- bottom = 0;
- MQTT.publish(mqtt_tx, "bottom low");
- } else if (bottom == 0 & digitalRead(2) == LOW) {
- bottom = 1;
- MQTT.publish(mqtt_tx, "bottom high");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement