Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- MQTT IOT Example
- - continuously obtains values from the Virtuabotix DHT11 temperature/humidity sensor
- - formats the results as a JSON string for the IBM IOT example
- - connects to an MQTT server (either local or at the IBM IOT Cloud)
- - and publishes the JSON String to the topic named quickstart:MY_MAC_ADDRESS
- */
- #include <SPI.h>
- #include <Ethernet.h>
- #include <PubSubClient.h>
- // Update this to either the MAC address found on the sticker on your ethernet shield (newer shields)
- // or a different random hexadecimal value (change at least the last four bytes)
- byte mac[] = {0xDE, 0xED, 0xBA, 0xFE, 0x1F, 0xDF};
- char macstr[] = "deedbafe1fdf";
- // Note this next value is only used if you intend to test against a local MQTT server
- byte localserver[] = {192, 168, 1, 14};
- // Update this value to an appropriate open IP on your local network
- byte ip[] = {192, 168, 1, 20};
- //char servername[]="quickstart.messaging.internetofthings.ibmcloud.com";
- char servername[]="mqtt://test.mosquitto.org";
- String clientName = String("d:quickstart:arduino:") + macstr;
- String topicName = String("/sentinel");
- //String topicName = String("iot-2/evt/status/fmt/json");
- float umidade = 0.0;
- float umidadePercentual = 0.0;
- EthernetClient ethClient;
- // Uncomment this next line and comment out the line after it to test against a local MQTT server
- PubSubClient client(localserver, 1883, 0, ethClient);
- //PubSubClient client(servername, 1883, 0, ethClient);
- void setup()
- {
- // Start the ethernet client, open up serial port for debugging, and attach the DHT11 sensor
- Ethernet.begin(mac, ip);
- Serial.begin(9600);
- }
- void loop()
- {
- char clientStr[34];
- clientName.toCharArray(clientStr,34);
- char topicStr[26];
- topicName.toCharArray(topicStr,26);
- getData();
- if (!client.connected()) {
- Serial.print("Trying to connect to: ");
- Serial.println(clientStr);
- client.connect(clientStr);
- }
- if (client.connected() ) {
- String json = buildJson();
- char jsonStr[200];
- json.toCharArray(jsonStr,200);
- boolean pubresult = client.publish(topicStr,jsonStr);
- Serial.print("attempt to send ");
- Serial.println(jsonStr);
- Serial.print("to ");
- Serial.println(topicStr);
- if (pubresult)
- Serial.println("successfully sent");
- else
- Serial.println("unsuccessfully sent");
- }
- delay(5000);
- }
- String buildJson() {
- String data = "{";
- data+="\n";
- data+= "\"d\": {";
- data+="\n";
- data+="\"myName\": \"Estacao Arduino\",";
- data+="\n";
- data+="\"umidade percentual\": ";
- data+=(int)umidadePercentual;
- data+="\n";
- data+="}";
- data+="\n";
- data+="}";
- return data;
- }
- void getData() {
- umidade = analogRead(A0);
- umidadePercentual = map(umidade, 1023, 0, 0, 100);
- Serial.println("Read OK");
- Serial.println(umidadePercentual);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement