Need Help With Arduino Yun

I need help with reading temperature from my DHT11 sensor on my html page,on arduino uno this code works:

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
#include "DHT.h"
#define DHTPIN 2     // what digital pin we're connected to
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
 Serial.begin(9600);
 Serial.println("DHTxx test!");
 dht.begin();
}
void loop() {
 // Wait a few seconds between measurements.
 delay(2000);
 // Reading temperature or humidity takes about 250 milliseconds!
 // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
 float h = dht.readHumidity();
 // Read temperature as Celsius (the default)
 float t = dht.readTemperature();
 // Read temperature as Fahrenheit (isFahrenheit = true)
 float f = dht.readTemperature(true);

 // Check if any reads failed and exit early (to try again).
 if (isnan(h) || isnan(t) || isnan(f)) {
   Serial.println("Failed to read from DHT sensor!");
   return;
 }

 // 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);

 Serial.print("Humidity: ");
 Serial.print(h);
 Serial.print(" %\t");
 Serial.print("Temperature: ");
 Serial.print(t);
 Serial.print(" *C ");
 Serial.print(f);
 Serial.print(" *F\t");
 Serial.print("Heat index: ");
 Serial.print(hic);
 Serial.print(" *C ");
 Serial.print(hif);
 Serial.println(" *F");
}

on yun this doesn’t:

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
#include "DHT.h"
#define DHTPIN 2     // what digital pin we're connected to
#define DHTTYPE DHT11   
DHT dht(DHTPIN, DHTTYPE);
int dac = 0;
int DigitalPin[] = {2, 12, 13};
int DacPin = 3;
YunServer server;
void setup() {
dht.begin();
pinMode(2,INPUT);
pinMode(4,INPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
digitalWrite(13, LOW);
Bridge.begin();
digitalWrite(13, HIGH);
server.listenOnLocalhost();
server.begin();
}
void loop() {
YunClient client = server.accept();
if (client) {
process(client);
client.stop();
}
delay(50);
}
void process(YunClient client) {
String command = client.readStringUntil('/');
if (command == "digital") {
digitalCommand(client);
}
if (command == "dac") {
dacCommand(client);
}
if (command == "status") {
statusCommand(client);
}
}
void digitalCommand(YunClient client) {
int pin, value;
pin = client.parseInt();
if (client.read() == '/') {
value = client.parseInt();
digitalWrite(pin, value);
}
else {
value = digitalRead(pin);
}
client.print(F("digital,"));
client.print(pin);
client.print(F(","));
client.println(value);
}
void dacCommand(YunClient client) {
int pin, value;
pin = client.parseInt();
if (client.read() == '/') {
value = client.parseInt();
dac = value;
analogWrite(pin, value);
}
else {
value = dac;
}
client.print(F("dac,"));
client.print(pin);
client.print(F(","));
client.println(value);
}
void statusCommand(YunClient client) {
int pin, value;
client.print(F("status"));
for (int thisPin = 0; thisPin < 3; thisPin++) {
pin = DigitalPin[thisPin];
value = digitalRead(pin);
client.print(F("#"));
client.print(pin);
client.print(F("="));
client.print(value);
}
{
pin = DacPin;
value = dac;
client.print(F("#"));
client.print(pin);
client.print(F("="));
client.print(value);
}
}
void temp(YunClient client) {
 delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
client.print(F("#A0"));
client.print(F("="));
client.print(t);
client.println("");
}

index.txt (5.16 KB)

Kristijan1392:
on yun this doesn’t:

This doesn’t… what? Please provide more detail as to what is the problem you are seeing. What are you expecting it it do, and what is it actually doing? We would like to help, but there are so many changes between the two pieces of code that in not about to start looking for issues until I have some idea of what I’m looking for. I’m sure many others will feel the same way.

Also, please use code tags when posting code: click the </> button to insert the tags, then paste the code between the tags. It greatly improves readability. You can go back and edit your post to add the tags.

Thank you for the additional information, and for editing your original post. One of the issues with not using the code tags is that it can cause the loss of indenting information, and that appears to have happened here. Hopefully you have used proper indenting in your original code, if so it would be helpful if you pasted the indented code back in the original post to improve readability.

I've never used the DHT11 sensor, but the first thing I notice in your Yun code is that you are initializing the sensor on pin 2 with the dht.begin() call, and then you are setting that pin to be an input on the next line. Then, in statusCommand(), you are reading the value of pin 2. I don't know what effect setting the pin to an input, and then reading it will have on the DHT library, but it is certainly possible that it would have an effect.

How are you trying to communicate with the client object? The way you have it coded, it is not being used as a replacement for serial I/O. It appears you have based your code on the Bridge Example, in which case you should be accessing it using a web browser. Is this what you are doing? Please be specific, mentioning what commands you have issued, and exactly how you have issued them, and what responses you have received, and what response you expected to receive.

You say with the Uno it shows you everything in the Serial Monitor, but the Yun code shows you only 0. Where does it shows this? I'm sure not in the Serial Monitor, as there is nothing in your Yun code that would communicate with the Serial Monitor.

Your code as posted has an issue, it appears the trailing '}' is missing from statusCommand().

You have no viable code that actually reads from the DHT11 sensor. You have a function temp() at the end, which reads values from the sensor, does some calculations, and writes output to the client object, but that function is never called from anywhere in your code.

The more detail you can provide, the better. What you appear to be trying to do with the Yun is significantly different than what you were doing with the Uno, and it requires different methods to communicate with the device. Without more details of exactly what you are doing, all we can do is assume, and we are likely to assume that you are doing something in a certain way, when in fact you may be doing it differently, and that could be the root of the issue.

Kristijan1392:
Still no solution :confused:

I'm sorry to hear that. Please post the current version of the code.

Also:

ShapeShifter:
It appears you have based your code on the Bridge Example, in which case you should be accessing it using a web browser. Is this what you are doing? Please be specific, mentioning what commands you have issued, and exactly how you have issued them, and what responses you have received, and what response you expected to receive.

The more details you provide, the more likely you will get a meaningful answer, and the faster you will get this solved.

Kristijan1392:
Should i delete statusCommand() if it could be the problem

Too early for that, we don’t know if it causes any problems accessing the DHT11, since you haven’t actually ever called the DHT11 library code.

i have no idea how to call the function in index.html file

You can’t call it directly from the HTML file, you need to add another IF statement to the process() function that recognizes a command string and then calls the function. Then you have to add code to the HTML page that issues a URL request that causes that function to be called.

I suggest you put your sketch aside for the moment, and run the unmodified Bridge Example sketch. Make sure you understand how to access it remotely. Use a web browser to access the individual commands by manually typing them into the address bar, and make sure they work properly and you understand what is going on. Then, you can try writing a JavaScript function on an html file to issue the same URL command and set some page content with the response: you can use the TemperatureWebPanel Example to help you with those concepts.

Then, once you understand the complete flow from the HTML code through the JavaScript, through the sketch’s loop() and process() functions and into your final function, then you can start adding additional functionality like the DHT11 sensor. Don’t try to get it all working at once: start simple, and add one step at a time, making sure that step works before moving on. That way, you know the problem is with with last thing you changed or tried to do. If you try to do it all at once, then when it doesn’t work, you have no idea which of the many changes is causing the problem.

how to configure it to show me data from digital input 2 from arduino yun

What data are you trying to show from that pin? The DHT11 library is managing that pin, and you should let it do so. The sensor uses a complex bi-directional protocol over that pin to transfer data. Simply reading the value of that pin between DHT11 library calls will at best just tell you the current state of the pin while the line is idle (should always be high at that point) and at worst could interfere with the DHT11 library code. Either way, it’s not going to give you useful data, and it certainly won’t tell you any temperature or humidity data.