Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. python script
  2.  
  3. #!/usr/bin/python
  4.  
  5. import MySQLdb
  6. import time
  7. import RPi.GPIO as GPIO ## Import GPIO library
  8. #import datetime from datetime
  9. GPIO.setwarnings(False)
  10. GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
  11. GPIO.setup(13, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
  12.  
  13. # Open database connection
  14.  
  15. #clock
  16.  
  17. # prepare a cursor object using cursor() method
  18.  
  19.  
  20. # Prepare SQL query to INSERT a record into the database.
  21. second = 55
  22. minute = 58
  23. hour = 21
  24.  
  25. while True:
  26. hourStartCheapElecticity = 22
  27. hourEndCheapElecticity = 6
  28.  
  29. db = MySQLdb.connect("localhost","root","root","proba" )
  30. cursor = db.cursor()
  31. sql = "SELECT * FROM tabela "
  32. time.sleep(1)
  33. #update clock
  34. second+= 1
  35. if second == 60:
  36. minute+= 1
  37. second = 0
  38. if minute == 60:
  39. hour+= 1
  40. minute = 0
  41. if hour == 24:
  42. hour = 0
  43. #
  44. results = 0
  45.  
  46. #print currentHour
  47. try:
  48. # Execute the SQL command
  49. cursor.execute(sql)
  50. # Fetch all the rows in a list of lists.
  51. results = cursor.fetchall()
  52. print results;
  53. except:
  54. print "Error: unable to fecth data"
  55. if hour >= hourStartCheapElecticity or hour <= hourEndCheapElecticity:
  56. GPIO.output(13,True)
  57. elif len(results) == 1:
  58. GPIO.output(13,True)
  59. else:
  60. GPIO.output(13,False)
  61. db.close();
  62. print "sec" + str(second)
  63. print "minute" + str(minute)
  64. print "hour" + str(hour)
  65.  
  66. # disconnect from server
  67. db.close();
  68. GPIO.cleanup()
  69.  
  70.  
  71. php script
  72.  
  73. <html>
  74. <body>
  75. <?php
  76. if (isset($_GET['buttonStart'])) {
  77. print "start";
  78. $servername = "localhost";
  79. $username = "root";
  80. $password = "root";
  81. $dbname = "proba";
  82.  
  83. // Create connection
  84. $conn = new mysqli($servername, $username, $password, $dbname$
  85. // Check connection
  86. if ($conn->connect_error) {
  87. die("Connection failed: " . $conn->connect_error);
  88. }
  89.  
  90. $sql = "INSERT INTO `tabela`(`pin13`) VALUES (1)";
  91. $sql = "INSERT INTO `tabela`(`pin13`) VALUES (1)";
  92.  
  93. if ($conn->query($sql) === TRUE) {
  94. echo "New record created successfully";
  95. } else {
  96. echo "Error: " . $sql . "<br>" . $conn->error;
  97. }
  98.  
  99. $conn->close();
  100. }
  101. if (isset($_GET['buttonStop'])) {
  102. print "stop";
  103.  
  104. $servername = "localhost";
  105. $username = "root";
  106. $password = "root";
  107. $dbname = "proba";
  108.  
  109. // Create connection
  110. $conn = new mysqli($servername, $username, $password, $dbname$
  111. // Check connection
  112. if ($conn->connect_error) {
  113. die("Connection failed: " . $conn->connect_error);
  114. }
  115.  
  116. // sql to delete a record
  117. $sql = "DELETE FROM `tabela`";
  118.  
  119. if ($conn->query($sql) === TRUE) {
  120. echo "Record deleted successfully";
  121. } else {
  122. echo "Error deleting record: " . $conn->error;
  123. }
  124.  
  125. $conn->close();
  126. }
  127. ?>
  128. <br>
  129. <br>
  130.  
  131. <form method="get">
  132. <input type="submit" name="buttonStart" value = "start">
  133. <br>
  134. <input type="submit" name="buttonStop" value = "stop">
  135. </form>
  136.  
  137. </body>
  138.  
  139.  
  140. android app
  141.  
  142. package com.example.probahttprequest;
  143.  
  144. import java.io.IOException;
  145.  
  146. import org.apache.http.HttpResponse;
  147. import org.apache.http.client.ClientProtocolException;
  148. import org.apache.http.client.HttpClient;
  149. import org.apache.http.client.methods.HttpGet;
  150. import org.apache.http.client.methods.HttpPost;
  151. import org.apache.http.impl.client.DefaultHttpClient;
  152.  
  153. import android.support.v7.app.ActionBarActivity;
  154. import android.graphics.Color;
  155. import android.os.Bundle;
  156. import android.view.Menu;
  157. import android.view.MenuItem;
  158. import android.view.View;
  159. import android.widget.Button;
  160. import android.widget.TextView;
  161. import android.widget.Toast;
  162.  
  163. public class MainActivity extends ActionBarActivity {
  164.  
  165. Button bStart;
  166. Button bStop;
  167. @Override
  168. protected void onCreate(Bundle savedInstanceState) {
  169. super.onCreate(savedInstanceState);
  170. setContentView(R.layout.activity_main);
  171. bStart = (Button) findViewById(R.id.buttonStart);
  172. bStop = (Button) findViewById(R.id.buttonStop);
  173. bStop.setBackgroundColor(Color.GRAY);
  174. bStart.setBackgroundColor(Color.GRAY);
  175. }
  176.  
  177.  
  178. public void buttonStart(View view){
  179.  
  180. CatalogClient client = new CatalogClient();
  181. client.execute("http://192.168.1.2/?buttonStart=start");
  182.  
  183. bStart.setBackgroundColor(Color.GREEN);
  184. bStop.setBackgroundColor(Color.GRAY);
  185.  
  186. }
  187.  
  188. public void buttonStop(View view){
  189.  
  190. CatalogClient client = new CatalogClient();
  191. client.execute("http://192.168.1.2/?buttonStop=stop");
  192. bStop.setBackgroundColor(Color.RED);
  193. bStart.setBackgroundColor(Color.GRAY);
  194.  
  195.  
  196. }
  197.  
  198. }
  199.  
  200. package com.example.probahttprequest;
  201.  
  202. import java.io.BufferedReader;
  203. import java.io.IOException;
  204. import java.io.InputStream;
  205. import java.io.InputStreamReader;
  206. import java.net.HttpURLConnection;
  207. import java.net.URI;
  208. import java.net.URISyntaxException;
  209. import java.net.URL;
  210.  
  211. import org.apache.http.HttpResponse;
  212. import org.apache.http.HttpStatus;
  213. import org.apache.http.client.ClientProtocolException;
  214. import org.apache.http.client.HttpClient;
  215. import org.apache.http.client.methods.HttpGet;
  216. import org.apache.http.impl.client.DefaultHttpClient;
  217. import org.json.JSONArray;
  218.  
  219. import android.os.AsyncTask;
  220. import android.util.Log;
  221.  
  222. public class CatalogClient extends AsyncTask<String, String, JSONArray> {
  223.  
  224. @Override
  225. protected JSONArray doInBackground(String... params) {
  226. URL url;
  227. HttpURLConnection urlConnection = null;
  228. JSONArray response = new JSONArray();
  229.  
  230.  
  231. HttpClient httpclient = new DefaultHttpClient();
  232.  
  233. HttpGet request = new HttpGet();
  234.  
  235. URI website ;
  236. try {
  237. website = new URI(params[0]);
  238. request.setURI(website);
  239. HttpResponse resp = httpclient.execute(request);
  240.  
  241. } catch (IOException | URISyntaxException e) {
  242. // TODO Auto-generated catch block
  243. e.printStackTrace();
  244. }
  245.  
  246. return response;
  247. }
  248.  
  249.  
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement