Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- python script
- #!/usr/bin/python
- import MySQLdb
- import time
- import RPi.GPIO as GPIO ## Import GPIO library
- #import datetime from datetime
- GPIO.setwarnings(False)
- GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
- GPIO.setup(13, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
- # Open database connection
- #clock
- # prepare a cursor object using cursor() method
- # Prepare SQL query to INSERT a record into the database.
- second = 55
- minute = 58
- hour = 21
- while True:
- hourStartCheapElecticity = 22
- hourEndCheapElecticity = 6
- db = MySQLdb.connect("localhost","root","root","proba" )
- cursor = db.cursor()
- sql = "SELECT * FROM tabela "
- time.sleep(1)
- #update clock
- second+= 1
- if second == 60:
- minute+= 1
- second = 0
- if minute == 60:
- hour+= 1
- minute = 0
- if hour == 24:
- hour = 0
- #
- results = 0
- #print currentHour
- try:
- # Execute the SQL command
- cursor.execute(sql)
- # Fetch all the rows in a list of lists.
- results = cursor.fetchall()
- print results;
- except:
- print "Error: unable to fecth data"
- if hour >= hourStartCheapElecticity or hour <= hourEndCheapElecticity:
- GPIO.output(13,True)
- elif len(results) == 1:
- GPIO.output(13,True)
- else:
- GPIO.output(13,False)
- db.close();
- print "sec" + str(second)
- print "minute" + str(minute)
- print "hour" + str(hour)
- # disconnect from server
- db.close();
- GPIO.cleanup()
- php script
- <html>
- <body>
- <?php
- if (isset($_GET['buttonStart'])) {
- print "start";
- $servername = "localhost";
- $username = "root";
- $password = "root";
- $dbname = "proba";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname$
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sql = "INSERT INTO `tabela`(`pin13`) VALUES (1)";
- $sql = "INSERT INTO `tabela`(`pin13`) VALUES (1)";
- if ($conn->query($sql) === TRUE) {
- echo "New record created successfully";
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- $conn->close();
- }
- if (isset($_GET['buttonStop'])) {
- print "stop";
- $servername = "localhost";
- $username = "root";
- $password = "root";
- $dbname = "proba";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname$
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- // sql to delete a record
- $sql = "DELETE FROM `tabela`";
- if ($conn->query($sql) === TRUE) {
- echo "Record deleted successfully";
- } else {
- echo "Error deleting record: " . $conn->error;
- }
- $conn->close();
- }
- ?>
- <br>
- <br>
- <form method="get">
- <input type="submit" name="buttonStart" value = "start">
- <br>
- <input type="submit" name="buttonStop" value = "stop">
- </form>
- </body>
- android app
- package com.example.probahttprequest;
- import java.io.IOException;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import android.support.v7.app.ActionBarActivity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends ActionBarActivity {
- Button bStart;
- Button bStop;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- bStart = (Button) findViewById(R.id.buttonStart);
- bStop = (Button) findViewById(R.id.buttonStop);
- bStop.setBackgroundColor(Color.GRAY);
- bStart.setBackgroundColor(Color.GRAY);
- }
- public void buttonStart(View view){
- CatalogClient client = new CatalogClient();
- client.execute("http://192.168.1.2/?buttonStart=start");
- bStart.setBackgroundColor(Color.GREEN);
- bStop.setBackgroundColor(Color.GRAY);
- }
- public void buttonStop(View view){
- CatalogClient client = new CatalogClient();
- client.execute("http://192.168.1.2/?buttonStop=stop");
- bStop.setBackgroundColor(Color.RED);
- bStart.setBackgroundColor(Color.GRAY);
- }
- }
- package com.example.probahttprequest;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.net.URL;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpStatus;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.json.JSONArray;
- import android.os.AsyncTask;
- import android.util.Log;
- public class CatalogClient extends AsyncTask<String, String, JSONArray> {
- @Override
- protected JSONArray doInBackground(String... params) {
- URL url;
- HttpURLConnection urlConnection = null;
- JSONArray response = new JSONArray();
- HttpClient httpclient = new DefaultHttpClient();
- HttpGet request = new HttpGet();
- URI website ;
- try {
- website = new URI(params[0]);
- request.setURI(website);
- HttpResponse resp = httpclient.execute(request);
- } catch (IOException | URISyntaxException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return response;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement