ESP8266 NodeMCU + дачик HTU21D + датчик BH1750+ магнитометр HMC5883L(3 датчика соединяются паралелльно к шине I2C)

C Sharp > ESP8266 NodeMCU + дачик HTU21D + датчик BH1750+ магнитометр HMC5883L(3 датчика соединяются паралелльно к шине I2C)
02.04.2018 15:40:55


Наиболее часто встречающиеся слова в статье:

[println] [client2] [azimuth] [myHTU21D] [dtostrf] [available] [include] [temperatureFString] [humidityString] [pressureString]


Статья:

ESP8266 NodeMCU + дачик HTU21D + датчик BH1750+ магнитометр HMC5883L

[скачать arduino проект]

/*********
  Project: 
  VIN--3.3V
GND--GND
SCL--5
SDA--4
*********/
 
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <C:\Users\iocsha\Documents\Arduino\NodeMCU\HTU21D.h>
#include <C:\Users\iocsha\Documents\Arduino\NodeMCU\BH1750.h>
#define addr 0x0D //I2C Address for The HMC5883
BH1750 lightMeter;
HTU21D myHTU21D(HTU21D_RES_RH12_TEMP14);
// Replace with your network details
const char* ssid = "iocsha";
const char* password = "1122";
float h, t, p, pin, dp;
char temperatureFString[6];
char dpString[6];
char humidityString[6];
char pressureString[7];
char pressureInchString[6];
char *mess;
uint16_t lux;
int x, y, z; //triple axis data
byte mac[6]; 
 
// Web Server on port 80
WiFiServer server(80);
WiFiServer tcpserver(23);
 
// only runs once on boot
void setup() {
  // Initializing serial port for debugging purposes
  Serial.begin(57600);
  delay(10);
  //SDA/SCL default to pins 4 & 5 but any two pins can be assigned as SDA/SCL using Wire.begin(SDA,SCL)  по умолчанию 4(SDA) и 5(SCL).
 
  // Connecting to WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Starting the web server
  server.begin();
  tcpserver.begin();
  Serial.println("Web server running. Waiting for the ESP IP...");
  delay(10000);
 
  // Printing the ESP IP address
  Serial.println(WiFi.localIP());
 // temperature datsik
  while (myHTU21D.begin(D4,D5) != true)
  {
    Serial.println(F("HTU21D, SHT21 sensor is faild or not connected"));
    delay(5000);
  }
  Serial.println(F("HTU21D, SHT21 sensor is active"));
  Serial.println("Humidity,Compensated Humidity,Temperature");
   delay(3000);
   lightMeter.begin();
  Serial.println(F("BH1750 begin"));
  delay(3000);
  // magnitometr
  Serial.println(F("magnitometr begin"));
  Wire.beginTransmission(addr); //start talking
  Wire.write(0x0B); // Tell the HMC5883 to Continuously Measure
  Wire.write(0x01); // Set the Register
  Wire.endTransmission();
  Wire.beginTransmission(addr); //start talking
  Wire.write(0x09); // Tell the HMC5883 to Continuously Measure
  Wire.write(0x1D); // Set the Register
  Wire.endTransmission();
  delay(3000);
}
 
float azimuth(int a, int b){
  float azimuth = atan2((int)a,(int)b) * 180.0/PI;
  return azimuth < 0?360 + azimuth:azimuth;
}
 
void getWeather() {
    h = myHTU21D.readHumidity();
    t = myHTU21D.readTemperature();
    dp=myHTU21D.readTemperature();
    lux = lightMeter.readLightLevel();   
    p = myHTU21D.readCompensatedHumidity();
    dtostrf(t, 5, 1, temperatureFString);
    dtostrf(h, 5, 1, humidityString);
    dtostrf(p, 6, 1, pressureString);
    dtostrf(p, 5, 2, pressureInchString);
    dtostrf(dp, 5, 1, dpString);
    delay(100);
    // magnitometr
     Wire.beginTransmission(addr);
  Wire.write(0x00); //start with register 3.
  Wire.endTransmission();
 
  //Read the data.. 2 bytes for each axis.. 6 total bytes
  Wire.requestFrom(addr, 6);
  if (6 <= Wire.available()) {
    x = Wire.read(); //MSB  x
    x |= Wire.read() << 8; //LSB  x
    z = Wire.read(); //MSB  z
    z |= Wire.read() << 8; //LSB z
    y = Wire.read(); //MSB y
    y |= Wire.read() << 8; //LSB y
  }
}
 
 
// runs over and over again
void loop() {
 // TCP-шная часть для сервера tcpserver
   WiFiClient client2 = tcpserver.available();
 if (client2) {
    Serial.println("New TCP client");
    while (client2.connected()) {
      if (client2.available()) {
        getWeather();
        client2.print(temperatureFString);
        client2.print(",");
        client2.print(humidityString);   
        client2.print(",");
        client2.print(pressureString); 
        client2.print(",");
        client2.print(lux); 
        client2.print(",");
        client2.print(azimuth(y,x)); 
        client2.print(",");
        client2.print(x); 
        client2.print(",");
        client2.print(y); 
            // closing the client connection
    delay(5000);
    client2.stop();
    Serial.println("Client TCP disconnected.");  
    break;   
      }
    }
  }
 
  // Listenning for new clients www
  WiFiClient client = server.available();
 
  if (client) {
    Serial.println("New client");
    // bolean to locate when the http request ends
    boolean blank_line = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
 
        if (c == ''\n'' && blank_line) {
            getWeather();
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connection: close");
            client.println();
            // your actual web page that displays temperature
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
            client.println("<head><META HTTP-EQUIV=\"refresh\" CONTENT=\"30\"></head>");
            client.println("<body><h1>ESP8266 Weather Web Server</h1>");
            client.println("<table border=\"2\" width=\"456\" cellpadding=\"10\"><tbody><tr><td>");
            client.println("<h3>Temperature = ");
            client.println(temperatureFString);
            client.println("&deg;</h3><h3>Humidity = ");
            client.println(humidityString);
            client.println("%</h3>");
              client.println("<h3>Lux = ");
            client.println(lux);
            client.println(" lux</h3>");
            client.println("<h3>Azimuth = ");
            client.println(azimuth(y,x));
            client.println(" &deg;</h3>");
            client.println("<h3>Magnito X= ");
            client.println(x);
            client.println("</h3>");
            client.println("<h3>Magnito Y= ");
            client.println(y);
            client.println("</h3>");
            client.println("<h3>CompensatedHumidity = ");
            client.println(pressureString);
            client.println("%</h3><h3>Free memory = ");
            client.println(ESP.getFreeHeap());
            client.println("byte</h3><h3>Wifi status:");
              client.print("SSID: ");
             client.println(WiFi.SSID());
 
             // print your WiFi shield''s IP address:
            IPAddress ip = WiFi.localIP();
            client.print("IP Address: ");
             client.println(ip);
          // print the received signal strength:
            long rssi = WiFi.RSSI();
            client.print("signal strength (RSSI):");
            client.print(rssi);
            client.println(" dBm");
            client.println("</h3></td></tr></tbody></table><h3>@2018 Ioksha S.</h3></body></html>");
            break;
        }
        if (c == ''\n'') {
          // when starts reading a new line
          blank_line = true;
        }
        else if (c != ''\r'') {
          // when finds a character on the current line
          blank_line = false;
        }
      }
    }
    // closing the client connection
    delay(1);
    client.stop();
    Serial.println("Client disconnected.");
  }
}