Data From Domotic Sensors to Cloud and Real Time View by a Web Application
by DavideMancino4 in Circuits > Sensors
852 Views, 7 Favorites, 0 Comments
Data From Domotic Sensors to Cloud and Real Time View by a Web Application
This Intel Edison with domotic sensors is meant to be a home domotic station that you can place anywhere and have it read the current surrounding that send in cloud and display them back on a web application or mobile application.
IDE: Arduino Language: javascript, HTML, PHP, Arduino.
Parts List
This application of Intel Edison is an all-in-one device. It uses many sensors belonging in Grove System.
Parts of sensors and components:
- 1 Intel Edison board
- 1 Grove base shield
- 1 Grove Gas MQ2 Sensor
- 1 Grove Flame Sensor
- 1 Grove Temperature Sensor
-1 Grove Moisture Sensor
Flashing the Intel Edison Board
For a right use of the board, you will need to update the firmware on your Intel Edison board. The instructions for this may be found at:
https://software.intel.com/en-us/articles/flash-tool-lite-user-manual
Assembly
The final layout looks like the second image.
First plug the grove base shield into the Intel Edison board. Then you may begin plugging the sensors into the base shield. Look on the base shield for identifiers such as A0 and D4.
These will be used for placement below:
- 1 Grove Gas MQ2 Sensor (A2)
- 1 Grove Flame Sensor (D4)
- 1 Grove Temperature Sensor (A0)
-1 Grove Moisture Sensor (A1)
Code and Explanation of Cloud and Web Application
The code that interfaces the sensor of Intel Edison and the cloud (that is for now emulate by another PC) is:
#include
#include
char ssid[] = "LUISS_ENLABS"; // your network SSID (name)
char pass[] = "luissenlabs"; // your network password
float temperature;
float humidity;
float gas;
int fire;
int B=3975;
int temp; String strURL = "";
int status = WL_IDLE_STATUS;
IPAddress server(172,16,37,156);
// Initialize the client library WiFiClient client;
void setup() { Serial.begin(9600); Serial.println("Attempting to connect to WPA network..."); Serial.print("SSID: "); Serial.println(ssid);
status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); // don't do anything else: while(true); } }
void loop(){
Serial.println("Entro nel looop");
UpdateTemp();
Serial.println("Esco da UpdateTemp");
while(client.available()){ char c = client.read(); Serial.print(c); }
if (!client.connected()){ Serial.println(); Serial.println("Disconnesso.");
client.stop(); } //esegui la richiesta ogni 10 secondi
delay(1000); }
void UpdateTemp(){
Serial.println("Connessione...");
while (client.connect(server, 80)){
Serial.println("Connesso e sono in while");
temperature = getTempValue();
Serial.print("\nTemp: ---> ");
Serial.println(temperature);
humidity = getHumidityValue();
Serial.print("Hum: ---> ");
Serial.println(humidity);
gas = getGasValue();
Serial.print("Gas: ---> ");
Serial.println(gas);
fire = getFireValue();
Serial.print("Fire: ---> ");
Serial.println(fire);
Serial.print("String: ---> "); //creo l'url utilizzando una stringa
strURL = getURLString();
Serial.println(strURL); //invio la richiesta al server
client.println(strURL);
client.println("Host: 172.16.37.156");
Serial.println("Host: localhost");
client.println("Connection: close");
client.println();
client.stop();
Serial.println("Stop");
delay(2500); }
if (client.connect(server, 80)){ Serial.println("Errore Connessione"); } }
float getTempValue(){
float temp = analogRead(A0);
float resistance = (1023-temp)*10000/temp; //get the resistance of the sensor;
float temperature = 1/(log(resistance/10000)/B+1/298.15)-273.15;//convert to temperature via datasheet ; return temperature; }
float getHumidityValue(){ float humidity = analogRead(A1); return humidity; }
float getGasValue(){ gas = analogRead(A3); gas = gas/1024*5.0; return gas; }
int getFireValue(){ fire = digitalRead(4); return !fire; }
String getURLString(){
strURL = "GET /mypet/index.php?temperatura=";
strURL += (int)temperature;
strURL += "&gas=";
strURL += (int)gas; strURL += "&umidita=";
strURL += (int)humidity; strURL += "&fuoco=";
strURL += (int)fire; return strURL; }
This code read the value of all the sensors on the board, then send they in the cloud (cloud = 172.16.37.156).
Moreover, the sensors' data in the cloud are readable in a web application and We send the value of the sensors in Intel Dashboard Analytics.
Code Web Application
Now this is the code that inferface the web application with Intel Edison Board:
$mysqli = new mysqli("localhost", "root", "", "mypet");
// Temperature $query = "SELECT valore FROM `temperatura` ORDER BY id DESC LIMIT 1";
$stmt = $mysqli->prepare($query); $stmt->execute();
$stmt->bind_result($temp);
while($stmt->fetch()){ }
// Gas $query = "SELECT valore FROM `gas` ORDER BY id DESC LIMIT 1";
$stmt = $mysqli->prepare($query); $stmt->execute();
$stmt->bind_result($gas);
while($stmt->fetch()){ }
// Fire $query = "SELECT valore FROM `fuoco` ORDER BY id DESC LIMIT 1";
$stmt = $mysqli->prepare($query); $stmt->execute();
$stmt->bind_result($fire);
while($stmt->fetch()){ }
// Humidity $query = "SELECT valore FROM `umidita` ORDER BY id DESC LIMIT 1";
$stmt = $mysqli->prepare($query); $stmt->execute();
$stmt->bind_result($humidity);
while($stmt->fetch()){ }
$mysqli->close(); ?>
At the end, this is the code that can allow us to read the last value readable by the Intel Edison:
if(isset($_GET['temperatura']) && isset($_GET['gas']) && isset($_GET['umidita']) && isset($_GET['fuoco'])){ $temperatura = $_GET['temperatura']; $gas = $_GET['gas']; $umidita = $_GET['umidita']; $fuoco = $_GET['fuoco']; $link = mysql_connect('localhost', 'root', ''); if (!$link) {die('Impossibile connettersi: ' . mysql_error());} mysql_select_db("mypet") or die( "Impossibile selezionare il database.");
//Query 1 $sql = "INSERT INTO temperatura (valore) VALUES( " .$temperatura. ");"; $retval = mysql_query( $sql, $link ); if(! $retval ){die('Impossibile eseguire la query: 1 ' . mysql_error());}
//Query 2 $sql = "INSERT INTO gas (valore) VALUES( " .$gas. ");"; $retval = mysql_query( $sql, $link ); if(! $retval ){die('Impossibile eseguire la query: 2 ' . mysql_error());}
//Query 1 $sql = "INSERT INTO umidita (valore) VALUES( " .$umidita. ");"; $retval = mysql_query( $sql, $link ); if(! $retval ){die('Impossibile eseguire la query: 3 ' . mysql_error());}
//Query 1 $sql = "INSERT INTO fuoco (valore) VALUES( " .$fuoco. ");"; $retval = mysql_query( $sql, $link ); if(! $retval ){die('Impossibile eseguire la query: 1 ' . mysql_error());}
mysql_close($link); }
?>
?>