Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Connected but no data
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Connected but no data

by play2liv on Wed Mar 02, 2016 5:23 am

Hi All

I have a yun running on IO.adafruit. i can send onoff data to and from the server. i am trying to send Photocell and DTH11 data to the server. i get a count of how many time data has been sent but not actual sensor data. What did i miss im just starting on this?

play2liv
 
Posts: 11
Joined: Sat Jan 24, 2015 3:23 am

Re: Connected but no data

by play2liv on Wed Mar 02, 2016 5:24 am

/***************************************************
Adafruit MQTT Library Arduino Yun Example

Make sure your Arduino Yun is connected to a WiFi access point which
has internet access. Also note this sketch uses the Console class
for debug output so make sure to connect to the Yun over WiFi and
open the serial monitor to see the console output.

Works great with the Arduino Yun:
----> https://www.adafruit.com/products/1498

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

Written by Tony DiCola for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include "DHT.h"
#include "Bridge.h"
#include "Console.h"
#include "YunClient.h"
#include "Adafruit_MQTT.h"

#include "Adafruit_MQTT_Client.h"



/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "***********"
#define AIO_KEY "***************"

#define DHTPIN 2 // what digital pin we're connected to
#define DHTPIN 3 // what digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// the photocell pin
#define PHOTOCELL A0 // analog 0
#define Humidity D2
#define Temperature D2
// variable to hold sensor value
int pCell;
// the on off button feed turns this LED on/off
#define LED 13

// Adjust as necessary, in seconds. Default to 5 minutes.
#define MQTT_CONN_KEEPALIVE 10

/************ Global State (you don't need to change this!) ******************/

// Create a YunClient instance to communicate using the Yun's brighe & Linux OS.
YunClient client;

// Store the MQTT server, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);



/****************************** Feeds ***************************************/

// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell";
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED, MQTT_QOS_1);

// Setup a feed called 'onoff' for subscribing to changes.
const char ONOFF_FEED[] PROGMEM = AIO_USERNAME "/feeds/onoff";
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);

// Setup feeds for temperature & humidity
const char TEMPERATURE_FEED[] PROGMEM = AIO_USERNAME "/feeds/temperature";
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, TEMPERATURE_FEED);

const char HUMIDITY_FEED[] PROGMEM = AIO_USERNAME "/feeds/humidity";
Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, HUMIDITY_FEED);


// Define a will
const char WILL_FEED[] PROGMEM = AIO_USERNAME "/feeds/onoff";
Adafruit_MQTT_Publish lastwill = Adafruit_MQTT_Publish(&mqtt, WILL_FEED, MQTT_QOS_1);




/*************************** Sketch Code ************************************/
DHT dht(DHTPIN, DHTTYPE, 2);
void setup() {
Bridge.begin();
Console.begin();

Console.println(F("Adafruit MQTT demo"));
Serial.begin(115200);
Serial.println("DHTxx test!");

dht.begin();

//Setup MQTT will to set on/off to "OFF" when we disconnect
mqtt.will(WILL_FEED, "OFF");

// Setup MQTT subscription for feeds.
mqtt.subscribe(&onoffbutton);
//mqtt.subscribe(&temperature);
// mqtt.subscribe(&photocell);
//mqtt.subscribe(&humidity);


// record the maximum sensor value
pCell = analogRead(A0);
}

uint32_t x=0;

void loop() {
// Wait a few seconds between measurements.
//delay(1500);
//read the input from A0 and store it in a variable
pCell = analogRead(A0);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
int h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();

// this is our 'wait for incoming subscription packets' busy subloop
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(1000))) {
if (subscription == &onoffbutton) {
Console.print(F("Got: "));
Console.println((char *)onoffbutton.lastread);
}
}



// Grab the current state of the sensor
//int humidity_data = (int)DTH.readHumidity();
//int temperature_data = (int)dht.readTemperature();



// Now we can publish stuff!


// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);

Console.print(F("\nSending Humidity val "));
Console.print(h);
Console.print("...");
if (! humidity.publish(x++)) {
Console.println(F("Failed"));
} else {
Console.println(F("OK!"));
}


Console.print(F("\nSending Temperature val "));
Console.print(f);
Console.print("...");
if (! temperature.publish(x++)) {
Console.println(F("Failed"));
} else {
Console.println(F("OK!"));
}

Console.print(F("\nSending photocell val "));
Console.print(pCell);
Console.print("...");
if (! photocell.publish(x++)) {
Console.println(F("Failed"));
} else {
Console.println(F("OK!"));
}
lastwill.publish("ON"); // make sure we publish ON first thing after connecting
//Console.print(t);
Serial.print(" %\t");
Serial.print(" *C ");
//Console.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");




// ping the server to keep the mqtt connection alive
if(! mqtt.ping()) {
Console.println(F("MQTT Ping failed."));
}

delay(1000);

}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;

// Stop if already connected.
if (mqtt.connected()) {
return;
}

Console.print("Connecting to MQTT... ");

while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Console.println(mqtt.connectErrorString(ret));
Console.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds



}
Console.println("MQTT Connected!");
}

play2liv
 
Posts: 11
Joined: Sat Jan 24, 2015 3:23 am

Re: Connected but no data

by play2liv on Wed Mar 02, 2016 5:26 am

Here is what the console is sending

sending photocell val 89...OK!

Sending Humidity val 64...OK!

Sending Temperature val 69.80...OK!

Sending photocell val 86...OK!

Sending Humidity val 64...OK!

Sending Temperature val 69.80...OK!

play2liv
 
Posts: 11
Joined: Sat Jan 24, 2015 3:23 am

Please be positive and constructive with your questions and comments.