miklik

MQTT test with Arduino

Feb 21st, 2018
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 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.  
  6. // Emulate Serial1 on pins 6/7 if not present
  7. #ifndef HAVE_HWSERIAL1
  8. #include <SoftwareSerial.h>
  9.   SoftwareSerial Serial1(4, 5); // RX, TX
  10. #endif
  11.  
  12. #define BUILTIN_LED 13
  13.  
  14. //wifi
  15. #include <WifiAuth.h>                       // define ssid and pass bellow
  16. //char ssid[] = "mm";                       // your network SSID (name)
  17. //char pass[] = "";                         // your network password
  18. int status = WL_IDLE_STATUS;                // the Wifi radio's status
  19.  
  20. const char* mqtt_server = "aaaaaa.messaging.internetofthings.ibmcloud.com";
  21. const int mqtt_port = 1883;
  22. const char* mqtt_clntid = "d:aaaaaa:MQTTclient:atest1";
  23. const char* mqtt_event = "iot-2/evt/event1/fmt/json";
  24. const char* mqtt_user = "use-token-auth";
  25. const char* mqtt_auth = "AAAAAAAAAAAAAAAAAA";
  26.  
  27. void callback(char* topic, byte* payload, unsigned int length) {
  28.   // handle message arrived
  29. }
  30.  
  31. // Initialize the Ethernet client object
  32. WiFiEspClient espClient;
  33. PubSubClient client(mqtt_server,mqtt_port,callback, espClient);
  34. long lastMsg = 0;
  35. char msg[50];
  36. long int value = 0;
  37.  
  38. void setup_wifi()
  39. {
  40.   WiFi.init(&Serial1);
  41.   // check for the presence of the shield
  42.   if (WiFi.status() == WL_NO_SHIELD) {
  43.     Serial.println("WiFi shield not present");
  44.     // don't continue
  45.     while (true);
  46.   }
  47.   // attempt to connect to WiFi network
  48.   while ( status != WL_CONNECTED) {
  49.     Serial.print("Attempting to connect to WPA SSID: ");
  50.     Serial.println(ssid);
  51.     // Connect to WPA/WPA2 network
  52.     status = WiFi.begin(ssid, pass);
  53.   }
  54.   // you're connected now, so print out the data
  55.   Serial.println("You're connected to the network");
  56.   Serial.print("IP address: ");
  57.   Serial.println(WiFi.localIP());
  58. }
  59.  
  60. void setup(){
  61.   pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  62.   Serial.begin(115200);
  63.   Serial1.begin(19203);  // ESP must be set to the same speed AT+UART_DEF=9600,8,1,0,1
  64.   // initialize ESP module
  65.   setup_wifi();
  66.   if (client.connect(mqtt_clntid, mqtt_user, mqtt_auth)) {
  67.     client.publish(mqtt_event,"{\"prop1\": \"Hello\",\"prop2\": \"world\",\"prop3\": \"ON\"}");
  68.     //client.subscribe("inTopic");
  69.   }
  70.   //client.setServer(mqtt_server, mqtt_port);
  71.   //client.setCallback(callback);
  72. }
  73.  
  74. void reconnect() {
  75.   // Loop until we're reconnected
  76.   while (!client.connected()) {
  77.     Serial.print("Attempting MQTT connection...");
  78.     // Attempt to connect
  79.     if (client.connect(mqtt_clntid, mqtt_user, mqtt_auth)) {
  80.       Serial.println("connected");
  81.       // Once connected, publish an announcement...
  82.       //client.publish("outTopic", "hello world");
  83.       // ... and resubscribe
  84.       //client.subscribe("inTopic");
  85.     } else {
  86.       Serial.print("failed, rc=");
  87.       Serial.print(client.state());
  88.       Serial.println(" try again in 5 seconds");
  89.       // Wait 5 seconds before retrying
  90.       delay(5000);
  91.     }
  92.   }
  93. }
  94.  
  95.  
  96. void loop() {
  97.   if (!client.connected()) {
  98.      Serial.print("cln connected?");
  99.      Serial.println(client.connected());
  100.      reconnect();
  101.   }
  102.   client.loop();
  103. }
Advertisement
Add Comment
Please, Sign In to add comment