Hello everyone!
I'm trying to make temperature station.
I successfully made temperature sensor, which sends temperature information to Thingspeak.
Now I'm trying to send that data to MySQL.
So there are two methods:
- Php to MySQL
- Straight to to the MySQL.
I tried PHP to MySQL with this guide: https://www.instructables.com/id/PART-1-Send-Arduino-data-to-the-Web-PHP-MySQL-D3js/
But It didn't work.
Second method:
I have Raspberry Pi and I have installed Apache, MySQL and Phpmyadmin. I can contact to them correctfully but I can't connect to it with my Raspberry. It just doesn't work.
What is best method to send my data to MySQL and do you have any guides?
Here is my code to ThingSpeak, which works correctfully:
#include <WiFiNINA.h>
#define analogPin A0 //the thermistor attach to
#define beta 4090 //the beta of the thermistor
#define resistance 10 //the value of the pull-up resistor
////////////////////////////////////////////////////////////////////////////////////////////////////////////
String apiKey = "XXXXXXXXXXXXXXX"; //API key from ThingSpeak channel
const char* ssid = "ZyXEL"; //SSID of your wifi
const char* password = "testi123"; //password of your wifi
int duration=5;//delay between each data measure and uploading
////////////////////////////////////////////////////////////////////////////////////////////////////////////
const char* server = "api.thingspeak.com";
WiFiClient client; //Start clinet
void setup()
{
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop()
{
//read thermistor value
long a =analogRead(analogPin);
//the calculating formula of temperature
float tempC = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0;
float tempF = 1.8*tempC + 32.0;
delay(200); //wait for 100 milliseconds
if (client.connect(server, 80)) {
String postStr = apiKey;
postStr += "&field1=";
postStr += String(tempC);
postStr += "&field2=";
postStr += String(tempF);
postStr += "\r\n\r\n";
//Uplad the postSting with temperature and Humidity information every
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.print(" degrees Celcius Humidity: ");
Serial.print(tempF);
Serial.println("% send to Thingspeak");
}
client.stop();
Serial.println("Waiting…");
// thingspeak needs minimum 15 sec delay between updates
delay(duration*1000);
}