Advertisement
Onkytonk

Coffee Machine Servo over MQTT

Sep 10th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. /***********************************
  2.  
  3. Joseph Naim
  4. Coffee Machine Servo over MQTT
  5.  
  6. ************************************/
  7.  
  8. int servo_neutral = 6400;
  9. int servo_button1 = 5400;
  10. int servo_button2 = 7400;
  11. int servo_gpio = 22;
  12.  
  13. #include <esp32-hal-ledc.h>
  14. #include <WiFi.h>
  15. #include <PubSubClient.h>
  16.  
  17. // Replace the next variables with your SSID/Password combination
  18. const char* ssid = "YOUR SSID";
  19. const char* password = "YOUR PASSWORD";
  20.  
  21. // Add your MQTT Broker IP:
  22. const char* mqtt_server = "YOUR BROKER IP";
  23. const char* mqtt_username = "YOUR BROKER USERNAME";
  24. const char* mqtt_password = "YOUR BROKER PASSWORD";
  25.  
  26. WiFiClient espClient;
  27. PubSubClient client(espClient);
  28. long lastMsg = 0;
  29. char msg[50];
  30. int value = 0;
  31.  
  32.  
  33. void setup() {
  34. Serial.begin(115200);
  35. setup_wifi();
  36. client.setServer(mqtt_server, 1883);
  37. client.setCallback(callback);
  38.  
  39. ledcSetup(1, 50, 16); // channel 1, 50 Hz, 16-bit width
  40. ledcAttachPin(servo_gpio, 1); // GPIO 22 assigned to channel 1
  41. ledcWrite(1, servo_neutral); // Moved the servo to neutral
  42. }
  43.  
  44. void setup_wifi() {
  45. delay(10);
  46. // Connect to a WiFi network
  47. Serial.println();
  48. Serial.print("Connecting to ");
  49. Serial.println(ssid);
  50.  
  51. WiFi.begin(ssid, password);
  52.  
  53. while (WiFi.status() != WL_CONNECTED) {
  54. delay(500);
  55. Serial.print(".");
  56. }
  57.  
  58. Serial.println("");
  59. Serial.println("WiFi connected");
  60. Serial.println("IP address: ");
  61. Serial.println(WiFi.localIP());
  62. }
  63.  
  64. void callback(char* topic, byte* message, unsigned int length) {
  65. Serial.print("Message arrived on topic: ");
  66. Serial.print(topic);
  67. Serial.print(". Message: ");
  68. String messageCoffee;
  69.  
  70. for (int i = 0; i < length; i++) {
  71. Serial.print((char)message[i]);
  72. messageCoffee += (char)message[i];
  73. }
  74. Serial.println();
  75.  
  76. if(messageCoffee == "long")
  77. ledcWrite(1, servo_button1);
  78. delay(200);
  79. ledcWrite(1, servo_neutral);
  80. if(messageCoffee == "short")
  81. ledcWrite(1, servo_button2);
  82. delay(200);
  83. ledcWrite(1, servo_neutral);
  84.  
  85. if (String(topic) == "cmnd/coffeemachine/output") {
  86. Serial.print("Changing output to ");
  87. if(messageCoffee == "long"){
  88. Serial.println("long");
  89. }
  90. if(messageCoffee == "short"){
  91. Serial.println("short");
  92. }
  93. }
  94. }
  95.  
  96.  
  97. void loop() {
  98. while (!client.connected()) {
  99. Serial.println("Connecting to MQTT...");
  100. if (client.connect("ESP Coffee Servo", mqtt_username, mqtt_password )) {
  101. Serial.println("connected");
  102. // Subscribe
  103. client.subscribe("cmnd/coffeemachine");
  104. } else {
  105. Serial.print("failed with state ");
  106. Serial.println(" try again in 5 seconds");
  107. // Wait 5 seconds before retrying
  108. delay(5000);
  109. }
  110. }
  111. client.loop();
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement