Code Monkey home page Code Monkey logo

nodemcu's Introduction

Connecting the ESP8266 Nodemcu with BME 280 sensor.

To insert data into the database, we are going to use the ESP8266 Nodemcu and BME280 sensor so that we can insert temperature, humidity, pressure readings into our database every 30 seconds. The setup is as shown in the schematic below.

image

normalFetching_Posting

#ifdef ESP32
  #include <WiFi.h>
  #include <HTTPClient.h>
#else
  #include <ESP8266WiFi.h>
  #include <ESP8266HTTPClient.h>
  #include <WiFiClient.h>
#endif
#include <Wire.h>
#define ln D2
const char* ssid     = "Wifi name";
const char* password = "Wifi password";
WiFiClient wifiClient;
String serverName = "http://ipv4 address:3000/POST";
String apiKeyValue = "tPmAT5Ab3j7F9";
String sensorName = "BME280";
String sensorLocation = "My Room";
#define SEALEVELPRESSURE_HPA (1013.25)
void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) { 
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
  pinMode(ln, INPUT);  
}
void loop() {
  if(WiFi.status()== WL_CONNECTED){
    HTTPClient http;
    http.begin(wifiClient, serverName);
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    Serial.println(random(100));
    String httpRequestData = "&sensor=" + sensorName + "&location=" + sensorLocation + "&value1=" + digitalRead(ln);
    Serial.print("httpRequestData: ");
    Serial.println(httpRequestData);
    int httpResponseCode = http.POST(httpRequestData);
  if (httpResponseCode>0) {
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
    }
    else {
      Serial.print("Error code: ");
      Serial.println(httpResponseCode);
    }
    http.begin(wifiClient, "http://192.168.1.3:3000/");
    int httpCode = http.GET(); 
    if (httpCode > 0) {
      String payload = http.getString(); 

      Serial.println(payload);
    }
    http.end();
  }
  else {
    Serial.println("WiFi Disconnected");
  }
  delay(11500);  
}

code-with-dht11

#ifdef ESP32
  #include <WiFi.h>
  #include <HTTPClient.h>
#else
  #include <ESP8266WiFi.h>
  #include <ESP8266HTTPClient.h>
  #include <WiFiClient.h>
#endif
#include <Wire.h>
#include "DHT.h"
#define DHTPIN D2 
#define DHTTYPE DHT11
const char* ssid     = "Subhamoy";
const char* password = "PASS020302";
WiFiClient wifiClient;
String serverName = "http://192.168.1.3:3000/POST";
DHT dht(DHTPIN, DHTTYPE);
void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) { 
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
  dht.begin();  
}
void loop() {
  if(WiFi.status()== WL_CONNECTED){
    HTTPClient http;
    http.begin(wifiClient, serverName);
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    delay(1500);
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    float f = dht.readTemperature(true);
    if (isnan(h) || isnan(t) || isnan(f)) {
      Serial.println(F("Failed to read from DHT sensor!"));
      return;
  }
  float hif = dht.computeHeatIndex(f, h);
  float hic = dht.computeHeatIndex(t, h, false);
  String httpRequestData = "&Temperature=" + String(t) + "&Humidity=" + String(h) + "&Heat Index=" + String(hic)+" "+String(hif);
  int httpResponseCode = http.POST(httpRequestData);
  if (httpResponseCode>0) {
     // Serial.print("HTTP Response code: ");
      //Serial.println(httpResponseCode);
    }
    else {
      Serial.print("Error code: ");
      Serial.println(httpResponseCode);
    }
    http.begin(wifiClient, "http://192.168.1.3:3000/");
    int httpCode = http.GET(); 
    if (httpCode > 0) {
      String payload = http.getString(); 
      Serial.println(payload);
    }
    http.end();
  }
  else {
    Serial.println("WiFi Disconnected");
  }
  delay(1500);  
}

Don’t forget to replace the SSID and password corresponding to your network. Also make sure you input the correct domain name and URL path or IP address, so the ESP publishes the sensor readings to your own server. const char* serverName = "http://ip_address/sensordata/post-esp-data.php";

After uploading the above code to the ESP82665 Nodemcu you can be able to access the sensor reading via a web page using the URL; https://localhost/sensordata

image

You can also access this webpage using other devices like mobile phone and other computers using the URL: http://ip_address/sensordata

You can also view the readings in the database in the sensordata table.

image

In the above setup we were using a local host with the help of XAMPP web server to host MySQL database locally on your Windows PC. However you can use this knowledge for a real website in case you have your own domain name and hosting account as demonstrated here.

nodemcu's People

Contributors

alphacosmiccoder avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.