python/Lecture
[arduino] DS18B20 Digital Sensor 연결 및 wifi 통신 with MariaDB
jinozpersona
2023. 11. 22. 18:01
Arduino UNO R4 Wifi : DS18B20 연결도
Client Side WiFi
Server Side : 집에 공유기에 설치된 R-pi server 사용
# Raspberry-Pi Ubuntu Server 만들기 : https://jinozblog.tistory.com/119
# Ubuntu Server Docker install : https://jinozpersona.github.io/posts/ubuntu-docker-install/
# Ubuntu Server Docker MariaDB install : https://jinozpersona.github.io/posts/mariadb-install-on-docker/
[Arduino IDE 사용] / [Visual Studio Code사용]
Arduino IDE or VS code 초기 셋팅 및 설정 참고
https://jinozblog.tistory.com/207
[arduino] LM35 / TMP36 Sensor 연결도 : arduino IDE or VScode 설정 및 모니터링
Arduino UNO R3 : LM35 Sensor 연결도(TMP36 동일) 및 전달함수식 LM35 Analog Output이므로 A0 ~ A5에 연결 : 필자는 A0에 연결 range : 온도 / 출력(전송)전압, -55ºC ~ 150ºC : -550mV ~ 1500mV LM35 Transfer Function Arduino AO resol
jinozblog.tistory.com
[Code 작성 및 시리얼모니터링]
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFiS3.h>
#include "arduino_secrets.h"
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
int status = WL_IDLE_STATUS;
WiFiClient client;
MySQL_Connection conn((Client *)&client);
char cur_mem[255];
// API-Server personaHome IP Address:
IPAddress api_server_ip(000, 000, 000, 000); //Your Server IP
char hostname[] = "YourHostname"; //Your Server Hostname
char user[] = "YourID"; //Your Server ID
char password[] = "YourPASS"; //Your Server PASS
// YourDatabases Name, YourTables Name
char INSERT_SQL[] = "INSERT INTO YourDatabases.YourTables (Count, value) VALUES (%d,%s);";
char DELETE_SQL[] = "DELETE FROM YourDatabases.YourTables WHERE Count <= 100";
char query[255];
#define ONE_WIRE_BUS 13
OneWire Wire(ONE_WIRE_BUS);
DallasTemperature sensor(&Wire);
const int baudrate = 9600;
char value[8];
int loopcnt = 0;
void setup() {
Serial.begin(baudrate);
sensor.begin();
while (!Serial) {
}
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 5 seconds for connection:
delay(5000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
// api-server
if (conn.connect(api_server_ip, 9886, user, password)) {
delay(1000);
Serial.println("API-Server Connection : OK.");
} else {
Serial.println("API-Server Connection : Failed.");
}
}
void loop() {
sensor.requestTemperatures();
dtostrf(sensor.getTempCByIndex(0) , 5, 2, value);
Serial.println(value);
if (loopcnt == 10) {
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
sprintf(query, DELETE_SQL);
cur_mem->execute(query);
delete cur_mem;
loopcnt = 1;
if (loopcnt == 1) {
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
sprintf(query, INSERT_SQL, loopcnt, value);
cur_mem->execute(query);
delete cur_mem;
// loopcnt++;
}
} else {
loopcnt++;
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
sprintf(query, INSERT_SQL, loopcnt, value);
cur_mem->execute(query);
delete cur_mem;
}
MySQL_Cursor(&conn).close();
delay(2000);
}
void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}
void printMacAddress(byte mac[]) {
for (int i = 5; i >= 0; i--) {
if (mac[i] < 16) {
Serial.print("0");
}
Serial.print(mac[i], HEX);
if (i > 0) {
Serial.print(":");
}
}
Serial.println();
}
wifi SSID, PASS header file(동일 폴더에 두고 업로드)
arduino_secrets.h
//arduino_secrets.h header file
#define SECRET_SSID "TP-LINK_C71232"
#define SECRET_PASS "YourWIFI_PASS"
DBeaver
DataBase 확인
반응형