Hello all,
I am working on a school project in which I am using an MKR1000 as a high speed data acquisition device wirelessly. The application is to get sensor readings inside a tire as it is spinning.
I have gotten it to a point where I can reliably read sensors at the desired sampling rate, and then send the data to a mySQL database that runs on my computer. This has been working great, and I used a lot of the methodology given here. This has gotten me 95% of the way, but my problem is that I would like a way to control when the Arduino decides to record and send a sample. Currently I've only been getting it started running, and then just using a delay to have it run every 60 seconds or so.
I have been researching for a few hours this evening and I either am too out of my comfort zone to realize what is applicable to me, or there is not many resources out there that have done this.
A summary of the system is this:
MKR1000 running a Wifi101 client that reads sensor data, than sends it to a XAMPP server that is running on a pc in the same network. That uses a PHP script to then put the sensor data in a MySQL database.
MKR1000 Code:
#include <SPI.h>
#include <WiFi101.h>
//Wifi Connection
char ssid[] = "NathanE" ;
char pass[] = "Arduino1" ;
int status = WL_IDLE_STATUS;
IPAddress server(192, 168, 0, 103) ;
WiFiClient client;
//Sensor and Time Data
float timeSent ;
int interval = 1000 ;
int sampleSize = 2000 ;
float voltage [2000];
unsigned long timestamp [2000];
unsigned long previousTime = 0 ;
unsigned long currentTime = 0 ;
String message ;
float sensorToVoltage = (1.0 / (7.5 / (7.5 + 30.0)) * (3.3 / 4095.0)) ;
bool recordAndSend = true ;
void setup()
{
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for USB port only serial communications get rid of when running experiment
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Connected to wifi");
printWiFiStatus();
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected to server and ready to sense");
// Make a HTTP request:
}
}
//------------------------------------------------------------------------------
/* Infinite Loop */
void loop()
{
//Read the sensors locally and save readings to Arduino
read_sensors() ;
//Create packets and send to server database
Sending_To_phpmyadmindatabase();
delay(60000);
}
void read_sensors()
{
Serial.println("Beginning of read sensors") ;
for (int i = 0 ; i < sampleSize;)
{
analogReadResolution(12) ;
currentTime = micros() ;
Serial.println("In read sensors for") ;
if (currentTime - previousTime >= interval)
{
Serial.println("In the sensors if") ;
timestamp[i] = currentTime ;
voltage[i] = analogRead(A0) ;
previousTime = currentTime ;
i = i + 1 ;
}
}
}
void Sending_To_phpmyadmindatabase() //CONNECTING WITH MYSQL
{
Serial.println("In send to db") ;
if (client.connect(server, 80))
{
Serial.print("Sending to database") ;
for (int z = 0 ; z < sampleSize ; z++)
{
client.connect(server, 80) ;
voltage[z] = voltage[z] * sensorToVoltage ;
timeSent = timestamp[z] / 1000000.0 ;
message = "GET /testcode/dht.php?temperature=" + String(voltage[z], 4) + "&humidity=" + String(timeSent, 3)
+ " HTTP/1.1\r\n" + ("Host: 192.168.0.101\r\n") + ("Connection: close\r\n\r\n") ;
client.print(message) ;
Serial.println(message) ;
}
}
else
{
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void printWiFiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
PHP Code
<?php
class dht11
{
public $link='';
function __construct($temperature,$humidity)
{
$this->connect();
$this->storeInDB($temperature,$humidity);
}
function connect()
{
$this->link = mysqli_connect('localhost','root','') or die('Cannot connect to the DB');
mysqli_select_db($this->link,'tester') or die('Cannot select the DB');
}
function storeInDB($temperature, $humidity)
{
$query = "insert into test123 set Timer='".$humidity."', Test='".$temperature."'";
$result = mysqli_query($this->link,$query) or die('Errant query: '.$query);
}
}
if($_GET['temperature'] != '' and $_GET['humidity'] != '')
{
$dht11=new dht11($_GET['temperature'],$_GET['humidity']);
}
?>
Again, I have no problems currently with how the code above works, in fact it works better than I was expecting to get. What I am trying to figure out, is a way to be at my computer, and send a "flag" if you will, that tells the Arduino, okay lets take 2000 more sensor readings and send it. In my head it is simple, but I am really struggling to find a way to get the Arduino to actually read that message I'm trying to send. My base idea was this:
if (recordAndSend == true)
{
//Read the sensors locally and save readings to Arduino
read_sensors() ;
//Create packets and send to server database
Sending_To_phpmyadmindatabase();
recordAndSend = false ;
}
and then the main loop waits for something to change "recordAndSend" back to true. I just need to understand how to actually send a message that prompts the arduino to change recordAndSend. Any help is appreciated, let me know if I can provide any more information
Thanks so much for your time.