Hi all, currently I am trying to create a code to simulate how an Arduino Sensor will transmit a float array of 1 X 32 values to MySQL Database for it to store it via POST.
Below is the Arduino code for reference:
#include <SPI.h>
#include <WiFi.h>
const char* ssid = "Router";
const char* pass = "PASSWORD";
int status = WL_IDLE_STATUS;
IPAddress server(192,164,0,100); // Localhost
long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000; // READING INTERVAL
int i;
//float my_array[32];
String data[32];
// Initialize the client library
WiFiClient client;
void setup() {
Serial.begin(115200);
Serial.println("Attempting to connect to WPA network...");
Serial.print("SSID: ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to wifi");
Serial.println("\nStarting connection...");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("Gateway: ");
Serial.println(WiFi.gatewayIP());
Serial.print("Signal strength: ");
Serial.print(WiFi.RSSI());
Serial.println(" dBm");
}
void loop() {
for (i = 0; i < 32; i++) {
data[i]=String(random(200,400));
}
if (client.connect(server,80)) { // REPLACE WITH YOUR SERVER ADDRESS
client.println("POST /newfolder/test.php HTTP/1.1");
client.println("Host: 192.188.0.204"); // SERVER ADDRESS HERE TOO
client.println("User-Agent: ESP32");
client.println("Connection: Keep-Alive");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
//client.println(data.length());
client.println();
client.println(data);
client.println();
Serial.println(data);
} else{
Serial.println("Not working");
}
if (client.connected()) {
client.stop(); // DISCONNECT FROM THE SERVER
}
delay(10000);
}
- So far, is it possible to send a string array of 32 values to the MySQL ? If not, do I have to use a string variable instead ? Like this:
#include <SPI.h>
#include <WiFi.h>
const char* ssid = "TP-LINK_9BB34C";
const char* pass = "18645378";
int status = WL_IDLE_STATUS;
IPAddress server(192,168,0,100); // Localhost
long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000; // READING INTERVAL
int i;
float my_array[32];
String data="";
// Initialize the client library
WiFiClient client;
void setup() {
Serial.begin(115200);
}
void loop() {
for (i = 0; i < 32; i++) {
my_array[i]=random(200,400);
}
for (i = 0; i < 32; i++) {
data=data + String(my_array[i]) + " ";
}
// if (client.connect(server,80)) { // REPLACE WITH YOUR SERVER ADDRESS
client.println("POST /server_check/add.php HTTP/1.1");
client.println("Host: 192.180.0.100"); // SERVER ADDRESS HERE TOO
client.println("User-Agent: ESP32");
client.println("Connection: Keep-Alive");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
Serial.println(data.length());
client.println();
client.print(data);
client.println();
Serial.println(data);
data="\0";
delay(5000);
}
- If I send it as a string to the MySQL, how do I actually write the query to actually update the tables with the 32 values into 32 columns? Would it be similar to the solution in this link sql - How to get only Digits from String in mysql? - Stack Overflow