Intel物联网开发者专区
直播中

dvd1478

12年用户 586经验值
擅长:可编程逻辑 电源/新能源 MEMS/传感技术 测量仪表 嵌入式技术 制造/封装 模拟技术 连接器 EMC/EMI设计 光电显示 存储技术 EDA/IC设计 处理器/DSP 接口/总线/驱动 控制/MCU RF/无线
私信 关注
[经验]

【Intel Edison试用体验】网络版园林守护者(结项)

本帖最后由 dvd1478 于 2016-9-10 21:41 编辑

概述
利用Edison实现对园林关键参数的实时更数,通过以太网实现webserver,远程通过手机或者PC电脑,只要能上网使用浏览器就可以进行的观看数据与相应的操作。
涉及技术:
       开发语言: Arduino
       网络框架:webserver
       使用模块: Grove LCD RGB Backlight、  Grove - Temperature Sensor 、Grove - Light Sensor  Grove - Moisture Sensor 、Grove LED

系统架框
园林守护者柜架图.png
效果图:
P60909-171509.jpg
远程开灯
P60909-171516.jpg
远程关灯
P60909-171522.jpg
效果视频:
[media]http://v.youku.com/v_show/id_XMTcxOTE4OTE0MA[/media]

调试经验
下载程序
FG1.png
测试是否PING通
FG2.png
网页远程开灯
FG3 灯开.png
远程关灯
FG4 灯关.png
串口在浏览器访问Edison时反馈的数据
FG5 brower.png
FG6 brower.png

源代码分享

  1. #include
  2. #include
  3. #include "rgb_lcd.h"


  4. char ssid[] = "TP-LINK_7DE1DA"; // your network SSID (name)
  5. char pass[] = "lyp53d80ncftmt"; // your network password (use for WPA, or use as key for WEP)

  6. int keyIndex = 0; // your network key Index number (needed only for WEP)

  7. int status = WL_IDLE_STATUS;

  8. WiFiServer server(2000);





  9. //Define the lcd
  10. rgb_lcd lcd;

  11. const int colorR = 128;
  12. const int colorG = 128;
  13. const int colorB = 128;


  14. // Define the pin to which the temperature sensor is connected.
  15. const int pinTemp = A0;//Grove - Temperature Sensor
  16. const int pinLight = A1;//Grove - Light Sensor
  17. const int pinMoisture = A3;//Grove - Moisture Sensor
  18. const int pinLed = 3;//Grove - Led
  19. // Define the B-value of the thermistor.
  20. // This value is a property of the thermistor used in the Grove - Temperature Sensor,
  21. // and used to convert from the analog value it measures and a temperature value.
  22. const int B = 4250;//3975;

  23. //cutoff freq and time constant of the offset calibration LPF
  24. #define WC_CAL 0.2
  25. #define TC_CAL 1/WC_CAL
  26. float T = 0.2; // Samping period (sec)
  27. float cal_filt_gain = T / (T + TC_CAL);
  28. float temperature_last = 32.0;


  29. int analogLightValue = 0;

  30. int analogHumidityValue = 0;



  31. long lastReadingTime = 0;




  32. void setup() {
  33. float temp = 0 ;

  34. //Initialize serial and wait for port to open:
  35. Serial.begin(115200);
  36. while (!Serial) {
  37. ; // wait for serial port to connect. Needed for Leonardo only
  38. }

  39. // check for the presence of the shield:
  40. if (WiFi.status() == WL_NO_SHIELD) {
  41. Serial.println("WiFi shield not present");
  42. // don't continue:
  43. while (true);
  44. }

  45. String fv = WiFi.firmwareVersion();
  46. if ( fv != "1.1.0" )
  47. Serial.println("Please upgrade the firmware");

  48. // attempt to connect to Wifi network:
  49. while ( status != WL_CONNECTED) {
  50. Serial.print("Attempting to connect to SSID: ");
  51. Serial.println(ssid);
  52. // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  53. status = WiFi.begin(ssid, pass);

  54. // wait 10 seconds for connection:
  55. delay(10000);
  56. }
  57. server.begin();
  58. // you're connected now, so print out the status:
  59. printWifiStatus();


  60. // set up the LCD's number of columns and rows:
  61. lcd.begin(16, 2);
  62. lcd.setRGB(colorR, colorG, colorB);
  63. lcd.clear();// clear the screen
  64. // set the cursor to column 0, line 1
  65. // (note: line 1 is the second row, since counting begins with 0):
  66. lcd.setCursor(0, 0);
  67. // Print a message to the LCD.
  68. lcd.print("Flower Guardian!");

  69. // Configure the sound sensor's pin for input.
  70. pinMode(pinTemp, INPUT);
  71. pinMode(pinLight, INPUT);
  72. pinMode(pinMoisture, INPUT);

  73. //init data
  74. temp = 0 ;
  75. for (int i = 0 ; i < 16; i++)
  76. {
  77. temp += getTemperature();
  78. delay(200);
  79. }
  80. temperature_last = temp / 16;

  81. // Configure the LED's pin for output signals.
  82. pinMode(pinLed, OUTPUT);
  83. //digitalWrite(pinLed, HIGH);//on
  84. digitalWrite(pinLed, LOW);//off

  85. }


  86. void loop() {
  87. // check for a reading no more than once a second.
  88. if (millis() - lastReadingTime > 1000) {
  89. // if there's a reading ready, read it:
  90. // don't do anything until the data ready pin is high:
  91. {
  92. getData();
  93. // timestamp the last time you got a reading:
  94. lastReadingTime = millis();
  95. }
  96. }

  97. // listen for incoming Ethernet connections:
  98. listenForEthernetClients();
  99. }

  100. float getTemperature() {
  101. // Get the (raw) value of the temperature sensor.
  102. int analogValueTemp = analogRead(pinTemp);

  103. // Determine the current resistance of the thermistor based on the sensor value.
  104. float resistance = (float)(1023 - analogValueTemp) * 100000 / analogValueTemp; //串联电阻分压公式求出,注意,AD的参考电压 等于 串联电阻的总电压

  105. // Calculate the temperature based on the resistance value.
  106. float temperature_now = 1 / (log(resistance / 100000) / B + 1 / 298.15) - 273.15;
  107. //1°K=°C+273.15 298.15K = 25
  108. return temperature_now;
  109. }

  110. void getData() {
  111. //Serial.println("Getting reading");

  112. //LPF
  113. float temperature = getTemperature() - temperature_last;
  114. temperature_last = temperature * cal_filt_gain + temperature_last;

  115. // Read the value of the light sensor. The light sensor is an analog sensor.
  116. analogLightValue = 1024 -analogRead(pinLight);//光强

  117. // Read the value of the Moisture Sensor. The Moisture Sensor is an analog sensor.
  118. analogHumidityValue = 1024 - analogRead(pinMoisture); //湿度



  119. lcd.clear();// clear the screen

  120. lcd.setCursor(0, 0);
  121. lcd.print("T:");//LCD显示当前温度
  122. lcd.print(temperature_last, 1);
  123. lcd.print("C");

  124. //lcd.print(" ");
  125. lcd.setCursor(8, 0);
  126. lcd.print("H:");//LCD现实当前湿度
  127. lcd.print(analogHumidityValue);

  128. lcd.setCursor(0, 1);
  129. lcd.print("L:");//LCD现实当前光照情况
  130. lcd.print(analogLightValue);

  131. //显示当前土壤情况
  132. lcd.setCursor(8, 1); //光标移动到第二行,第8个字符
  133. if (analogHumidityValue < 300) {
  134. lcd.print("S:Dry ");
  135. }
  136. else if (analogHumidityValue >= 300 && analogHumidityValue < 700) {
  137. lcd.print("S:Humid");
  138. }
  139. else {
  140. lcd.print("S:Water");
  141. }

  142. }



  143. void listenForEthernetClients() {
  144. // listen for incoming clients
  145. WiFiClient client = server.available();
  146. if (client) {// if you get a client,
  147. Serial.println("new client"); // print a message out the serial port
  148. // an http request ends with a blank line
  149. boolean currentLineIsBlank = true;
  150. String currentLine = ""; // make a String to hold incoming data from the client
  151. while (client.connected()) { // loop while the client's connected
  152. if (client.available()) { // if there's bytes to read from the client,
  153. char c = client.read(); // read a byte, then
  154. Serial.write(c); // print it out the serial monitor
  155. // if you've gotten to the end of the line (received a newline
  156. // character) and the line is blank, the http request has ended,
  157. // so you can send a reply
  158. //if (c == 'n' && currentLineIsBlank) { // if the byte is a newline character
  159. if ( c == 'n' && currentLine.length() == 0 ) {
  160. // if the current line is blank, you got two newline characters in a row.
  161. // that's the end of the client HTTP request, so send a response:

  162. // send a standard http response header
  163. client.println("HTTP/1.1 200 OK");
  164. client.println("Content-Type: text/html");
  165. client.println("Connection: close"); // the connection will be closed after completion of the response
  166. client.println("Refresh: 5"); // refresh the page automatically every 5 sec
  167. client.println();
  168. client.println("");
  169. client.println("");
  170. // output the value of each analog input pin
  171. /*
  172. for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
  173. int sensorReading = analogRead(analogChannel);
  174. client.print("analog input ");
  175. client.print(analogChannel);
  176. client.print(" is ");
  177. client.print(sensorReading);
  178. client.println("
    ");
  179. }*/
  180. client.print("Temperature: ");
  181. client.print(temperature_last);
  182. client.print(" degrees C");
  183. client.print("
    ");
  184. client.print("Light : " + String(analogLightValue));
  185. client.print(" lu");
  186. client.print("
    ");
  187. client.print("Humidity : " + String(analogHumidityValue));
  188. client.print(" %rh");
  189. client.print("
    ");

  190. // the content of the HTTP response follows the header:
  191. client.print("Click here turn the LED on
    ");
  192. client.print("Click here turn the LED off
    ");


  193. client.println("");

  194. // The HTTP response ends with another blank line:
  195. client.println();
  196. break;
  197. }
  198. if (c == 'n') {
  199. //currentLineIsBlank = true; // you're starting a new line
  200. currentLine = "";// if you got a newline, then clear currentLine:
  201. }
  202. else if (c != 'r') {// if you got anything else but a carriage return character,
  203. //currentLineIsBlank = false;// you've gotten a character on the current line
  204. currentLine += c; // add it to the end of the currentLine
  205. }

  206. // Check to see if the client request was "GET /H" or "GET /L":
  207. if (currentLine.endsWith("GET /H")) {
  208. digitalWrite(pinLed, HIGH); // GET /H turns the LED on
  209. }
  210. if (currentLine.endsWith("GET /L")) {
  211. digitalWrite(pinLed, LOW); // GET /L turns the LED off
  212. }
  213. }
  214. }

  215. // give the web browser time to receive the data
  216. delay(1);

  217. // close the connection:
  218. client.stop();
  219. Serial.println("client disonnected");
  220. }
  221. }


  222. void printWifiStatus() {
  223. // print the SSID of the network you're attached to:
  224. Serial.print("SSID: ");
  225. Serial.println(WiFi.SSID());

  226. // print your WiFi shield's IP address:
  227. IPAddress ip = WiFi.localIP();
  228. Serial.print("IP Address: ");
  229. Serial.println(ip);

  230. // print the received signal strength:
  231. long rssi = WiFi.RSSI();
  232. Serial.print("signal strength (RSSI):");
  233. Serial.print(rssi);
  234. Serial.println(" dBm");
  235. // print where to go in a browser:
  236. Serial.print("To see this Flower guardian page in action, open a browser to http://");
  237. Serial.println(ip);
  238. }

总结:
       使用Edison真的很方便,加上众多的模块,加速了原型的开发。本来做几个项目练练手的,没有想到,越做越深越广,快到结项时间,就把以前的练练手的项目综合起来,做个结项贴子,日后还会用Edison做更多的创意。












更多回帖

发帖
×
20
完善资料,
赚取积分