Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  MQTT IOT Example
  3.  - continuously obtains values from the Virtuabotix DHT11 temperature/humidity sensor
  4.  - formats the results as a JSON string for the IBM IOT example
  5.  - connects to an MQTT server (either local or at the IBM IOT Cloud)
  6.  - and publishes the JSON String to the topic named quickstart:MY_MAC_ADDRESS
  7.  */
  8.  
  9. #include <SPI.h>
  10. #include <Ethernet.h>
  11. #include <PubSubClient.h>
  12.  
  13. // Update this to either the MAC address found on the sticker on your ethernet shield (newer shields)
  14. // or a different random hexadecimal value (change at least the last four bytes)
  15. byte mac[]    = {0xDE, 0xED, 0xBA, 0xFE, 0x1F, 0xDF};
  16. char macstr[] = "deedbafe1fdf";
  17. // Note this next value is only used if you intend to test against a local MQTT server
  18. byte localserver[] = {192, 168, 1, 14};
  19. // Update this value to an appropriate open IP on your local network
  20. byte ip[]     = {192, 168, 1, 20};
  21.  
  22. //char servername[]="quickstart.messaging.internetofthings.ibmcloud.com";
  23. char servername[]="mqtt://test.mosquitto.org";
  24. String clientName = String("d:quickstart:arduino:") + macstr;
  25. String topicName = String("/sentinel");
  26. //String topicName = String("iot-2/evt/status/fmt/json");
  27.  
  28. float umidade = 0.0;
  29. float umidadePercentual = 0.0;
  30. EthernetClient ethClient;
  31.  
  32. // Uncomment this next line and comment out the line after it to test against a local MQTT server
  33. PubSubClient client(localserver, 1883, 0, ethClient);
  34. //PubSubClient client(servername, 1883, 0, ethClient);
  35.  
  36. void setup()
  37. {
  38.  
  39.   // Start the ethernet client, open up serial port for debugging, and attach the DHT11 sensor
  40.   Ethernet.begin(mac, ip);
  41.   Serial.begin(9600);
  42. }
  43.  
  44. void loop()
  45. {
  46.   char clientStr[34];
  47.   clientName.toCharArray(clientStr,34);
  48.   char topicStr[26];
  49.   topicName.toCharArray(topicStr,26);
  50.   getData();
  51.   if (!client.connected()) {
  52.     Serial.print("Trying to connect to: ");
  53.     Serial.println(clientStr);
  54.     client.connect(clientStr);
  55.   }
  56.   if (client.connected() ) {
  57.     String json = buildJson();
  58.     char jsonStr[200];
  59.     json.toCharArray(jsonStr,200);
  60.     boolean pubresult = client.publish(topicStr,jsonStr);
  61.     Serial.print("attempt to send ");
  62.     Serial.println(jsonStr);
  63.     Serial.print("to ");
  64.     Serial.println(topicStr);
  65.     if (pubresult)
  66.       Serial.println("successfully sent");
  67.     else
  68.       Serial.println("unsuccessfully sent");
  69.   }
  70.   delay(5000);
  71. }
  72.  
  73. String buildJson() {
  74.   String data = "{";
  75.   data+="\n";
  76.   data+= "\"d\": {";
  77.   data+="\n";
  78.   data+="\"myName\": \"Estacao Arduino\",";
  79.   data+="\n";
  80.   data+="\"umidade percentual\": ";
  81.   data+=(int)umidadePercentual;
  82.   data+="\n";
  83.   data+="}";
  84.   data+="\n";
  85.   data+="}";
  86.   return data;
  87. }
  88.  
  89. void getData() {
  90.   umidade = analogRead(A0);
  91.   umidadePercentual = map(umidade, 1023, 0, 0, 100);
  92.   Serial.println("Read OK");
  93.   Serial.println(umidadePercentual);
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement