Hello,
I have Arduino Uno Wifi Rev2 and I'm trying to get temperature sensor to send results to MySQL.
I have followed this tutorial: [Part 1]Temperature sensor DS18b20 with Arduino – PiGreek's Blog
and part 1 went very well. Now I'm in part 2: [Part 2]Temperature sensor DS18b20 with Arduino – MySQL – PiGreek's Blog
It takes temperature but it won't send it anywhere.
I had to change code little bit, tutorial was made for esp8266wifi.
Here is my code:
#include <SPI.h>
#include <WiFiNINA.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFiUdp.h>
#include <WiFiClient.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS; // the Wifi radio's status
#define ONE_WIRE_BUS 4 // DS18B20 Data Pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// MySQL
IPAddress server_addr(192,168,43,219); // MySQL SERVER
char user[] = "testi"; // MySQL USERNAME
char password[] = "testi123"; // MySQL PASSWORD
char INSERT_DATA[] = "INSERT INTO sensors.room (value, created) VALUES (%s, NOW() + INTERVAL 1 HOUR)";
char query[128];
char temperature[10];
WiFiClient client;
MySQL_Connection conn((Client *)&client);
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < "1.0.0") {
Serial.println("Please upgrade the firmware");
}
// 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 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
Serial.begin(115200);
WiFi.begin(ssid, pass);
sensors.begin();
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
Serial.println ( "" );
Serial.print ( "Connected to " );
Serial.println ( ssid );
Serial.print ( "IP address: " );
Serial.println ( WiFi.localIP() );
Serial.println("DB - Connecting...");
while (conn.connect(server_addr, 3306, user, password) != true) {
delay(500);
Serial.print ( "." );
}
}
void saveTempData() {
sensors.requestTemperatures();
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
Serial.print("Temp: ");
Serial.println(sensors.getTempCByIndex(0));
Serial.print("Query: ");
dtostrf(sensors.getTempCByIndex(0), 2, 2, temperature);
sprintf(query, INSERT_DATA, temperature);
cur_mem->execute(query);
Serial.println(query);
delete cur_mem;
Serial.println("Data stored!");
delay(2000);
}
void loop() {
saveTempData();
// check the network connection once every 10 seconds:
delay(10000);
printCurrentNet();
}
void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
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();
}
and my Arduino board looks like this:
I have installed mysql-database to my Raspberry. Phpmyadmin works well and I created database normally like in tutorial.