Advertisement
vitareinforce

Arduino MQTT Vending

Sep 10th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.49 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. // Update these with values suitable for your network.
  5.  
  6. const char* ssid = "MIC - Gigabit";
  7. const char* password = "micanjas";
  8. const char* mqtt_server = "167.205.7.226";
  9. const char* mqtt_user = "/vendingmachine:fahmi";
  10. const char* mqtt_pass = "12345678";
  11. const char* mqtt_topic = "/vending.machine";
  12.  
  13.  
  14. WiFiClient espClient;
  15. PubSubClient client(espClient);
  16. long lastMsg = 0;
  17. char msg[50];
  18. int value = 0;
  19.  
  20. void setup() {
  21.   pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  22.   Serial.begin(115200);
  23.   setup_wifi();
  24.   client.setServer(mqtt_server, 1883);
  25.   client.setCallback(callback);
  26. }
  27.  
  28. void setup_wifi() {
  29.  
  30.   delay(10);
  31.   // We start by connecting to a WiFi network
  32.   Serial.println();
  33.   Serial.print("Connecting to ");
  34.   Serial.println(ssid);
  35.  
  36.   WiFi.begin(ssid, password);
  37.  
  38.   while (WiFi.status() != WL_CONNECTED) {
  39.     delay(500);
  40.     Serial.print(".");
  41.   }
  42.  
  43.   Serial.println("");
  44.   Serial.println("WiFi connected");
  45.   Serial.println("IP address: ");
  46.   Serial.println(WiFi.localIP());
  47.  
  48.   client.setCallback(callback);
  49. }
  50.  
  51. void callback(char* topic, byte* payload, unsigned int length) {
  52.   Serial.print("Message arrived [");
  53.   Serial.print(topic);
  54.   Serial.print("] ");
  55.   for (int i = 0; i < length; i++) {
  56.     Serial.print((char)payload[i]);
  57.   }
  58.   Serial.println();
  59.  
  60.   // Switch on the LED if an 1 was received as first character
  61.   if ((char)payload[0] == '1') {
  62.     digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
  63.     // but actually the LED is on; this is because
  64.     // it is acive low on the ESP-01)
  65.   } else {
  66.     digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
  67.   }
  68.  
  69. }
  70.  
  71. void reconnect() {
  72.   // Loop until we're reconnected
  73.   while (!client.connected()) {
  74.     Serial.print("Attempting MQTT connection...");
  75.     // Attempt to connect
  76.     if (client.connect("ESP8266A992", mqtt_user, mqtt_pass)) {
  77.       Serial.println("connected");
  78.       // Once connected, publish an announcement...
  79.       // client.publish("outTopic", "hello world");
  80.       // ... and resubscribe
  81.       client.subscribe(mqtt_topic);
  82.     } else {
  83.       Serial.print("failed, rc=");
  84.       Serial.print(client.state());
  85.       Serial.println(" try again in 5 seconds");
  86.       // Wait 5 seconds before retrying
  87.       delay(5000);
  88.     }
  89.   }
  90. }
  91. void loop() {
  92.  
  93.   if (!client.connected()) {
  94.     reconnect();
  95.   }
  96.   client.loop();
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement