miklik

MQTT test

Apr 10th, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.08 KB | None | 0 0
  1. /* Test comunication from Arduino via ESP-01 */
  2. #include <Arduino.h>
  3. #include <WiFiEsp.h>
  4. #include <PubSubClient.h>
  5. #include <DHT.h>
  6.  
  7. // Emulate Serial1 on pins 6/7 if not present
  8. #ifndef HAVE_HWSERIAL1
  9. #include <SoftwareSerial.h>
  10.   SoftwareSerial Serial1(4, 5); // RX, TX
  11. #endif
  12.  
  13. #define BUILTIN_LED 13
  14. #define BUTTON_PIN 6
  15. #define DHTTYPE DHT11   // DHT 11
  16. #define DHTPIN 7
  17.  
  18. DHT dht;
  19.  
  20. //wifi
  21. #include <WifiAuth.h>                       // define ssid and pass bellow
  22. //char ssid[] = "mm";                       // your network SSID (name)
  23. //char pass[] = "";                         // your network password
  24. int status = WL_IDLE_STATUS;                // the Wifi radio's status
  25.  
  26. const char* mqtt_server = "aaaaa.messaging.internetofthings.ibmcloud.com";
  27. const int mqtt_port = 1883;
  28. const char* mqtt_clntid = "d:aaaa:MQTTtest:Arduino001";
  29. const char* mqtt_event = "iot-2/evt/event1/fmt/json";
  30. const char* mqtt_user = "use-token-auth";
  31. const char* mqtt_auth = "aaaaaaaaaaaaaaaaaaaaa";
  32.  
  33.  
  34. void callback(char* topic, byte* payload, unsigned int length) {
  35.   // handle message arrived
  36. }
  37.  
  38. // Initialize the Ethernet client object
  39. WiFiEspClient espClient;
  40. PubSubClient client(mqtt_server,mqtt_port,callback, espClient);
  41. long lastMsg = 0;
  42. char msg[50];
  43. long int value = 0;
  44. unsigned long last_time = millis();
  45. unsigned long repeat_time = 300000;
  46. bool buton_state = false;
  47.  
  48. void setup_wifi()
  49. {
  50.   WiFi.init(&Serial1);
  51.   // check for the presence of the shield
  52.   if (WiFi.status() == WL_NO_SHIELD) {
  53.     Serial.println("WiFi shield not present");
  54.     // don't continue
  55.     while (true);
  56.   }
  57.   // attempt to connect to WiFi network
  58.   while ( status != WL_CONNECTED) {
  59.     Serial.print("Attempting to connect to WPA SSID: ");
  60.     Serial.println(ssid);
  61.     // Connect to WPA/WPA2 network
  62.     status = WiFi.begin(ssid, pass);
  63.   }
  64.   // you're connected now, so print out the data
  65.   Serial.println("You're connected to the network");
  66.   Serial.print("IP address: ");
  67.   Serial.println(WiFi.localIP());
  68. }
  69.  
  70. // void callback(char* topic, byte* payload, unsigned int length) {
  71. //   Serial.print("Message arrived [");
  72. //   Serial.print(topic);
  73. //   Serial.print("] ");
  74. //   for (unsigned int i = 0; i < length; i++) {
  75. //     Serial.print((char)payload[i]);
  76. //   }
  77. //   Serial.println();
  78. //
  79. //   // Switch on the LED if an 1 was received as first character
  80. //   if ((char)payload[0] == '1') {
  81. //     digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
  82. //     // but actually the LED is on; this is because
  83. //     // it is acive low on the ESP-01)
  84. //   } else {
  85. //     digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
  86. //   }
  87. //
  88. // }
  89.  
  90. void setup(){
  91.   Serial.println("Start");
  92.   pinMode(BUTTON_PIN, INPUT_PULLUP);
  93.   pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  94.   dht.setup(DHTPIN);
  95.   Serial.begin(115200);
  96.   Serial1.begin(19203);  // ESP must be set to the same speed AT+UART_DEF=19203,8,1,0,1
  97.   // initialize ESP module
  98.   setup_wifi();
  99.   if (client.connect(mqtt_clntid, mqtt_user, mqtt_auth)) {
  100.     Serial.print("cln connected?");
  101.     Serial.println(client.connected());
  102.  
  103.   }
  104. }
  105.  
  106. void reconnect() {
  107.   // Loop until we're reconnected
  108.   while (!client.connected()) {
  109.     Serial.print("Attempting MQTT connection...");
  110.     // Attempt to connect
  111.     if (client.connect(mqtt_clntid, mqtt_user, mqtt_auth)) {
  112.       Serial.println("connected");
  113.     } else {
  114.       Serial.print("failed, rc=");
  115.       Serial.print(client.state());
  116.       Serial.println(" try again in 5 seconds");
  117.       // Wait 5 seconds before retrying
  118.       delay(5000);
  119.     }
  120.   }
  121. }
  122.  
  123.  
  124. void loop() {
  125.   if (!client.connected()) {
  126.      Serial.print("cln connected?");
  127.      Serial.println(client.connected());
  128.      reconnect();
  129.   } else
  130.   {
  131.     if (last_time + repeat_time < millis())
  132.     {
  133.       Serial.println(last_time);
  134.       Serial.println(last_time + repeat_time);
  135.       last_time = millis();
  136.       float h = dht.getHumidity();
  137.       float t = dht.getTemperature();
  138.       String str_temp1 = "{\"temp1\":" + String(t) + "}";
  139.       String str_humi1 = "{\"humi1\":" + String(h) + "}";
  140.       int len_temp1 = str_temp1.length()+1;
  141.       int len_humi1 = str_humi1.length()+1;
  142.       char mqtt_temp1[len_temp1];
  143.       str_temp1.toCharArray(mqtt_temp1,len_temp1);
  144.       char mqtt_humi1[len_humi1];
  145.       str_humi1.toCharArray(mqtt_humi1,len_humi1);
  146.       Serial.println("temp");
  147.       Serial.println(t);
  148.       Serial.println(len_temp1);
  149.       Serial.println(str_temp1);
  150.       Serial.println("humi");
  151.       Serial.println(len_humi1);
  152.       Serial.println(h);
  153.       Serial.println(str_humi1);
  154.       client.publish(mqtt_event,mqtt_temp1);
  155.       client.publish(mqtt_event,mqtt_humi1);
  156.       Serial.println(last_time);
  157.     }
  158.     if(! digitalRead(BUTTON_PIN) && ! buton_state)
  159.     {
  160.       buton_state = true;
  161.       Serial.print("button1: ");
  162.       Serial.println(buton_state);
  163.       client.publish(mqtt_event,"{\"button1\": 1}");
  164.     }
  165.     if(digitalRead(BUTTON_PIN)) buton_state = false;
  166.   }
  167.   client.loop();
  168. }
Advertisement
Add Comment
Please, Sign In to add comment