So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Markius
#59590 Ok so first post, looking for a bit of direction.

I'm using the Arduino Huzzah with DHT11 (and a few other) sensors. Coding in Arduino IDE.

I'd like to have distributed sensor sitting on a wifi network, that update an SQL and my skill level is probably too low on all the steps required to accomplish.

I have the huzzah connected to a DHT11 and reporting temp/humidity on Serial, while also connecting to a wifi network and posting plain text.

I can't seem to find enough documentation on how to update the server, it seems easy enough but i'm not familiar with all the functions.

My thought is if i can have each sensor responding to querys it would be safer than trying to hard code access into the SQL. Pulling the data appears safer than me trying to open up so they can push it.

My code below.... most of it is just examples sewn together.

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include "DHT.h" //Sensor Library
 

#define DHTTYPE DHT11   // DHT 11 Sensor
#define DHTPIN 2     // what digital pin we're connected to
DHT dht(DHTPIN, DHTTYPE);  // Initialize DHT Sensor

const char* ssid = "T86Q5";  //WIFI SSID
const char* password = "Z6DYFBFKYLG4RTJ2"; // WIFI PW

ESP8266WebServer server(80);

const int led = 13;

void handleRoot() {
   Serial.begin(9600);
  digitalWrite(led, 1);
  server.send(200, "text/plain", "hello from esp8266!");
  digitalWrite(led, 0);
}

void handleNotFound(){
   Serial.begin(9600);
  digitalWrite(led, 1);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
  digitalWrite(led, 0);
}

void setup(void){
   Serial.begin(9600);
  pinMode(led, OUTPUT);
  digitalWrite(led, 0);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);

  server.on("/inline", [](){
    server.send(200, "text/plain", "this works as well");
  });

  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTP server started");


  //Sensor Code
   Serial.begin(9600);
  Serial.println("DHTxx test!");
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  dht.begin();
}//end setup

void loop(void){

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

    server.handleClient();
  server.send(200, "text/plain", "hello from esp8266! !!!);
}