Read & Store Sensor Data From Arduino UNO to MySql

Hi,
Maybe you've thought about how to reading sensor data and display it to a web page in realtime with or without jQuery?
For communication from Arduino UNO to PC, i use VBS (Visual Basic Script) This is an Example Script to store data into MySQL:

  ' The MySQL query
  Set objShell = CreateObject( "WScript.Shell" )
  ' Store Data to MySql, make sure the mysql path you are using is correct
  Return = objShell.Run( "C:\wamp64\bin\mysql\mysql8.0.21\bin\mysql -hlocalhost -uroot arduino -e " _
  & chr(34) & "INSERT INTO `dht11` (`humi`, `tempc`, `tempf`) VALUES ('10', '20', '30')", 1, false )

  wscript.Sleep 500

And here is the complete script (Visual Basic Script) to Read & Store data into MySql ( 'readSerial.vbs' )

on error resume next
Const ForReading = 1
Const ForWriting = 2

dim port, data
' get COM port number from the user
port = InputBox( "Enter COM Port Number (e.g: COM1, COM2, COM3)" )

Set wshell = CreateObject( "WScript.Shell" )
Set fso = CreateObject( "Scripting.FileSystemObject" )
Set com = fso.OpenTextFile( port & ":9600,N,8,1", ForReading )

MsgBox( "Start to read data from " & port )

Do While com.AtEndOfStream <> True
	' Humidity & Temperature Data 
	data = com.ReadLine
	' Store Data to MySql, make sure the mysql path you are using is correct
	Return = objShell.Run( "C:\wamp64\bin\mysql\mysql8.0.21\bin\mysql -hlocalhost -uroot arduino -e " _
							& chr(34) & "INSERT INTO `dht11` (`humi`, `tempc`, `tempf`) VALUES (" & data & ")" & chr(34), 1, false )
	WScript.Sleep( 200 )
Loop

com.Close()

Schematics or circuit diagrams:

And this is the Scatch that you need to upload to Arduino UNO:

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-temperature-humidity-sensor
 */

#include "DHT.h"
#define DHTPIN 3
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

const int buzzer = 2; // buzzer to arduino pin 2
float oHumi, humi, oTempC, tempC, oTempF, tempF;

void setup() {
  Serial.begin(9600);
  // Serial.println("DHT sensor test!");
  // Serial.println("================");
  dht.begin(); // initialize the sensor
}

void loop() {
  
  // wait a few seconds between measurements.
  delay(2000);

  // read humidity
  humi  = dht.readHumidity();
  // read temperature as Celsius
  tempC = dht.readTemperature();
  // read temperature as Fahrenheit
  tempF = dht.readTemperature(true);

  // compare value 
  if ( oHumi != humi || oTempC != tempC || oTempF != tempF ) tempChanged();
  
}

void playTone() {
  tone(buzzer, 600);  // Send 1KHz sound signal...
  delay(100);         // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(100);         // ...for 1sec
}

void tempChanged() {
  
  // check if any reads failed
  if (isnan(humi) || isnan(tempC) || isnan(tempF)) {
    Serial.println("Failed to read from DHT sensor!");
    
  } else {
    
    String data = (String) "'" + round(humi) + "', '" + round(tempC) + "', '" + round(tempF) + "'";
    Serial.println( data );
    playTone();
    
  }
  
  // Save old results
  oHumi = humi;
  oTempC = tempC;
  oTempF = tempF;
}

Visit https://github.com/arduino-uno for more .. :smiley:

dht11.ino (1.54 KB)

webdemo.zip (2.81 KB)

readSerial.vbs.txt (829 Bytes)