Skip to content

Instantly share code, notes, and snippets.

@pudelosha
Created November 30, 2017 15:58
Show Gist options
  • Save pudelosha/e0b2d16d3d03670736bfdfa7449d805b to your computer and use it in GitHub Desktop.
Save pudelosha/e0b2d16d3d03670736bfdfa7449d805b to your computer and use it in GitHub Desktop.
Wemos D1 R2 MySQL database meto station
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Wire.h>
#include <BH1750.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085.h>
#include <ClosedCube_HDC1080.h>
#include <SDS011.h>
const char *Webssid = "xxxxxx";
const char *Webpass = "xxxxxx";
ESP8266WebServer Webserver(80);
char ssid[] = "xxxxxx"; // Network Name
char pass[] = "xxxxxx"; // Network Password
WiFiServer server(80);
IPAddress ip(xx, xx, xx, xx);
IPAddress gateway(192, 168, 2, 1);
IPAddress subnet(255, 255, 255, 0);
WiFiClient client;
MySQL_Connection conn((Client *)&client);
char INSERT_SQL[] = "INSERT INTO xxxxxx.TBL_READINGS(ID_PLANT, AIR_HUMIDITY, AIR_TEMPERATURE_1, AIR_TEMPERATURE_2, AIR_PRESSURE, SEA_LEVEL_PRESSURE, LIGHT_LEVEL, PM25, PM10) VALUES (1, %d, %d, %d, %d, %d, %d, %d, %d)";
char query[2048];
IPAddress server_addr(xx, xx ,xx, xx); // MySQL server IP
char user[] = "xxxxxx"; // MySQL user
char password[] = "xxxxxx"; // MySQL password
BH1750 lightMeter;
ClosedCube_HDC1080 hdc1080;
Adafruit_BMP085 bmp;
float p10,p25;
int error;
SDS011 my_sds;
void setup() {
Serial.begin(9600);
lightMeter.begin(); // initialise BH1750
bmp.begin(); // initialise BMP180
hdc1080.begin(0x40); // initialise HDC1080
WiFi.softAP(Webssid,Webpass);
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, pass);
// connect to WiFi
while (WiFi.status() != WL_CONNECTED) {
delay(200);
}
// connect to database
while (conn.connect(server_addr, 3306, user, password) != true) {
delay(200);
}
my_sds.begin(D4,D5);
}
void loop() {
ReadAndInsert();
delay(10000);
Serial.println("next Loop");
}
void ReadAndInsert(){
// air quality
error = my_sds.read(&p25,&p10);
// read temp and humidity
float temp1 = hdc1080.readTemperature();
float hum = hdc1080.readHumidity();
// read data pressure and temp
float pres;
pres = bmp.readPressure() / 100;
float temp2;
temp2 = bmp.readTemperature();
float alt;
alt = bmp.readAltitude();
double slpMultiplier;
double slp;
slpMultiplier = 1 - (0.0065 * alt / (temp1 + 0.0065 * alt + 273.15));
slpMultiplier = 1 / pow(slpMultiplier, 5.257);
slp = pres * slpMultiplier;
//convert data to int
int airHumidity = (int)hum;
int airTemperature1 = (int)temp1;
int airTemperature2 = (int)temp2;
int airPressure = (int)pres;
int airPressureSea = (int)slp;
uint16_t lightLevel = lightMeter.readLightLevel();
int pm25 = (int)p25;
int pm10 = (int)p10;
sprintf(query, INSERT_SQL, airHumidity, airTemperature1, airTemperature2, airPressure, airPressureSea, lightLevel, pm25, pm10);
Serial.println(query);
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
cur_mem->execute(query);
delete cur_mem;
}
@hamzu06
Copy link

hamzu06 commented Aug 2, 2018

Hello Bro i'm working on a project i need some help. I have an Nodemcu ESP8266 and RFID RC522 which is scanning unique id of RFID card now what i need is to send that ID to my database particular table then we will match the ID in that table and if the ID found we will delete that specific row.
when i wass send card unique id on database server found this error.
error 29 = no database selected.
can you help me to sort this problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment