0

When I load this code into my Arduino, he connects with the WiFi and shows up at my IP-Scanner. But when I open the IP of the device in FireFox nothing loads. (http://10.0.0.40/env) Is there an issue with my code, or doesnt it work like this. I have a sensor which reads the temperature and humidity. These two value should be available in my network to fetch(). With Javascript I want to display these on my local html-page. The network credentials are just censored.

#include <ArduinoJson.h>
#include <WiFi.h>
#include <WebServer.h>
#include <DHT.h>

#define DHTPIN 13
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

const char *ssid = "XXX";
const char *pwd = "XXX";

StaticJsonDocument<250> jsonDocument;
char buffer[250];

float temperature;
float humidity;

WebServer server(80);

void connectToWiFi() {
  Serial.print("Connect to: ");
  Serial.println(ssid);

  WiFi.begin(ssid, pwd);

  while (WiFi.status() !=WL_CONNECTED){
    Serial.print(".");
    delay(1000);
  }

  Serial.print("Connected. IP: ");
  Serial.println(WiFi.localIP());
}



void setup_routing(){
  server.on("/temperature", getTemperature);
  server.on("/humidity", getHumidity);
  server.on("/env", getEnv);
  
  server.begin();
}

void create_json(char *tag, float value, char *unit){
  jsonDocument.clear();
  jsonDocument["type"] = tag;
  jsonDocument["value"] = value;
  jsonDocument["unit"] = unit;
  serializeJson(jsonDocument, buffer);
}

void add_json_object(char *tag, float value, char *unit){
  JsonObject obj = jsonDocument.createNestedObject();
  obj["type"] = tag;
  obj["value"] = value;
  obj["unit"]  = unit;
}

void read_sensor_data(void * parameter) {
     for (;;) {
     temperature = dht.readTemperature();
     humidity = dht.readHumidity();
     }
     delay(2000);  
}

void getTemperature(){
  create_json("temperature", temperature,"°C");
  server.send(200, "application/json", buffer);
  Serial.println(temperature);
}
void getHumidity(){
  create_json("humidty", humidity, "%");
  server.send(200, "application/json", buffer);
  Serial.println(humidity);
}
void getEnv() {
  jsonDocument.clear();
  add_json_object("temperature", temperature, "°C");
  add_json_object("humidity", humidity, "%");
  serializeJson(jsonDocument, buffer);
  server.send(200, "application/json", buffer);
}

void setup() {
dht.begin();
Serial.begin(115200);
Serial.println(WiFi.localIP());  
connectToWiFi();
setup_routing();

}

void loop() {
  // put your main code here, to run repeatedly:
delay(2000);
float temperature = dht.readTemperature();
float humidty = dht.readHumidity();

}

3
  • By nothing loads you mean blank page or some sort of error ? Commented Dec 22, 2020 at 18:46
  • It loads and loads and loads but nothing is getting displayed (like you have an slow connection) Commented Dec 22, 2020 at 18:48
  • Have you tried debugging it at all? Adding a few Serial.println()'s at the right places would let you know what's being run and what's not being run. Commented Dec 22, 2020 at 18:54

1 Answer 1

2

The loop() code you posted looks like this:

void loop() {
  // put your main code here, to run repeatedly:
delay(2000);
float temperature = dht.readTemperature();
float humidty = dht.readHumidity();

}

You didn't include the necessary code to allow the web server to run. The web server needs to be given a chance to run during the loop() or else it can't service requests.

Your loop() needs to include server.handleClient().

You can see this in basic examples showing how to use the WebServer.

Once you add that code you'll need to rewrite loop() to not always delay for two seconds or else you'll be delaying the web server as well as the sensor reads.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that solution worked. My only problem now is that my sensor values are 0.
It works now, I added float temperature = dht.readTemperature(); to the void getEnv.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.