Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. /*
  2.  
  3. Module: Watering
  4. Hardware Version: 1.0
  5. Software Version: 1.0
  6.  
  7. */
  8.  
  9. #include <ESP8266httpUpdate.h>
  10. #include <ESP8266WiFi.h>
  11. #include <PubSubClient.h>
  12.  
  13. const char* ssid = "Slow-Fi";
  14. const char* pass = "5YWNT2!CEyQ3rv^6";
  15.  
  16. const char* mqtt_host = "10.0.0.2";
  17. const uint16_t mqtt_port = 1883;
  18. const char* mqtt_id = "WAT001";
  19. const char* mqtt_rx = "WAT001-RX";
  20. const char* mqtt_tx = "WAT001-TX";
  21. const char* mqtt_user = "";
  22. const char* mqtt_pass = "";
  23.  
  24. const char* ota_host = "updates.grobotics.io";
  25. const char* ota_port = "80";
  26. const String ota_path = "/watering-1.bin";
  27.  
  28. const char* module_name = "Module Name: Watering";
  29. const char* hardware_version = "Hardware Version: 1.0";
  30. const char* software_version = "Software Version: 1.0";
  31.  
  32. int top = 0;
  33. int bottom = 0;
  34.  
  35. WiFiClient WIFI;
  36. PubSubClient MQTT;
  37. char mqtt_buffer[100];
  38. String mqtt_command;
  39.  
  40. void mqtt(char* mqtt_topic, byte* mqtt_payload, unsigned int mqtt_length) {
  41.  
  42. for (int mqtt_counter = 0; mqtt_counter < mqtt_length; mqtt_counter++) {
  43. mqtt_buffer[mqtt_counter] = mqtt_payload[mqtt_counter];
  44. }
  45.  
  46. mqtt_command = String(mqtt_buffer);
  47.  
  48. Serial.print("MQTT Command: ");
  49. Serial.println(mqtt_command);
  50. memset(mqtt_buffer, '\0', sizeof(mqtt_buffer));
  51.  
  52. }
  53.  
  54. void setup() {
  55.  
  56. Serial.begin(9600);
  57. MQTT.setClient(WIFI);
  58. MQTT.setServer(mqtt_host,mqtt_port);
  59. MQTT.setCallback(mqtt);
  60.  
  61. pinMode(14, OUTPUT);
  62. pinMode(12, OUTPUT);
  63. pinMode(0, INPUT);
  64. pinMode(2, INPUT);
  65.  
  66. digitalWrite(14, LOW);
  67. digitalWrite(12, LOW);
  68.  
  69. }
  70.  
  71. void loop() {
  72.  
  73. if (WiFi.status() != WL_CONNECTED) {
  74.  
  75. Serial.println();
  76. Serial.println();
  77. Serial.print("Connecting to ");
  78. Serial.println(ssid);
  79.  
  80. WiFi.begin(ssid, pass);
  81.  
  82. if (WiFi.waitForConnectResult() != WL_CONNECTED) {
  83. return;
  84. }
  85.  
  86. Serial.print("Local IP ");
  87. Serial.println(WiFi.localIP());
  88. Serial.println("Connection Sucessful");
  89. Serial.println();
  90.  
  91. }
  92.  
  93. if (WiFi.status() == WL_CONNECTED) {
  94.  
  95. if (!MQTT.connected()) {
  96.  
  97. Serial.println("Connecting to MQTT Broker");
  98. Serial.print("Host: ");
  99. Serial.println(mqtt_host);
  100. Serial.print("Port: ");
  101. Serial.println(mqtt_port);
  102.  
  103. if (MQTT.connect(mqtt_rx, mqtt_user, mqtt_pass)) {
  104.  
  105. Serial.println("Connection Sucessful");
  106. Serial.println();
  107.  
  108. MQTT.publish("Connect",mqtt_id);
  109.  
  110. MQTT.subscribe(mqtt_rx);
  111.  
  112. }
  113.  
  114. }
  115.  
  116. if (MQTT.connected()) {
  117.  
  118. MQTT.loop();
  119.  
  120. if (mqtt_command == "module name") {
  121.  
  122. MQTT.publish(mqtt_tx, module_name);
  123.  
  124. } else if (mqtt_command == "hardware version") {
  125.  
  126. MQTT.publish(mqtt_tx, hardware_version);
  127.  
  128. } else if (mqtt_command == "software version") {
  129.  
  130. MQTT.publish(mqtt_tx, software_version);
  131.  
  132. } else if (mqtt_command == "update") {
  133.  
  134. MQTT.publish(mqtt_tx, "Updating");
  135. ESPhttpUpdate.update(ota_host, ota_port, ota_path);
  136.  
  137. } else if (mqtt_command == "fill off") {
  138.  
  139. digitalWrite(14, LOW);
  140. MQTT.publish(mqtt_tx, "Command Sucessful");
  141.  
  142. } else if (mqtt_command == "fill on") {
  143.  
  144. digitalWrite(14, HIGH);
  145. MQTT.publish(mqtt_tx, "Command Sucessful");
  146.  
  147. } else if (mqtt_command == "drain off") {
  148.  
  149. digitalWrite(12, LOW);
  150. MQTT.publish(mqtt_tx, "Command Sucessful");
  151.  
  152. } else if (mqtt_command == "drain on") {
  153.  
  154. digitalWrite(12, HIGH);
  155. MQTT.publish(mqtt_tx, "Command Sucessful");
  156.  
  157. } else if (mqtt_command != "") {
  158.  
  159. MQTT.publish(mqtt_tx, "Command Not Recognised");
  160. Serial.println("Command Not Recognised");
  161.  
  162. }
  163.  
  164. mqtt_command = "";
  165.  
  166. if (top == 1 & digitalRead(0) == HIGH) {
  167.  
  168. top = 0;
  169. MQTT.publish(mqtt_tx, "top low");
  170.  
  171. } else if (top == 0 & digitalRead(0) == LOW) {
  172.  
  173. top = 1;
  174. MQTT.publish(mqtt_tx, "top high");
  175.  
  176. }
  177.  
  178. if (bottom == 1 & digitalRead(2) == HIGH) {
  179.  
  180. bottom = 0;
  181. MQTT.publish(mqtt_tx, "bottom low");
  182.  
  183. } else if (bottom == 0 & digitalRead(2) == LOW) {
  184.  
  185. bottom = 1;
  186. MQTT.publish(mqtt_tx, "bottom high");
  187.  
  188. }
  189.  
  190. }
  191.  
  192. }
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement