本帖最后由 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
系统架框
效果图:
远程开灯
远程关灯
效果视频:
调试经验
下载程序
测试是否PING通
网页远程开灯
远程关灯
串口在浏览器访问Edison时反馈的数据
源代码分享
- #include
- #include
- #include "rgb_lcd.h"
- char ssid[] = "TP-LINK_7DE1DA"; // your network SSID (name)
- char pass[] = "lyp53d80ncftmt"; // your network password (use for WPA, or use as key for WEP)
- int keyIndex = 0; // your network key Index number (needed only for WEP)
- int status = WL_IDLE_STATUS;
- WiFiServer server(2000);
- //Define the lcd
- rgb_lcd lcd;
- const int colorR = 128;
- const int colorG = 128;
- const int colorB = 128;
- // Define the pin to which the temperature sensor is connected.
- const int pinTemp = A0;//Grove - Temperature Sensor
- const int pinLight = A1;//Grove - Light Sensor
- const int pinMoisture = A3;//Grove - Moisture Sensor
- const int pinLed = 3;//Grove - Led
- // Define the B-value of the thermistor.
- // This value is a property of the thermistor used in the Grove - Temperature Sensor,
- // and used to convert from the analog value it measures and a temperature value.
- const int B = 4250;//3975;
- //cutoff freq and time constant of the offset calibration LPF
- #define WC_CAL 0.2
- #define TC_CAL 1/WC_CAL
- float T = 0.2; // Samping period (sec)
- float cal_filt_gain = T / (T + TC_CAL);
- float temperature_last = 32.0;
- int analogLightValue = 0;
- int analogHumidityValue = 0;
- long lastReadingTime = 0;
- void setup() {
- float temp = 0 ;
- //Initialize serial and wait for port to open:
- Serial.begin(115200);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- // check for the presence of the shield:
- if (WiFi.status() == WL_NO_SHIELD) {
- Serial.println("WiFi shield not present");
- // don't continue:
- while (true);
- }
- String fv = WiFi.firmwareVersion();
- if ( fv != "1.1.0" )
- Serial.println("Please upgrade the firmware");
- // 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);
- }
- server.begin();
- // you're connected now, so print out the status:
- printWifiStatus();
- // set up the LCD's number of columns and rows:
- lcd.begin(16, 2);
- lcd.setRGB(colorR, colorG, colorB);
- lcd.clear();// clear the screen
- // set the cursor to column 0, line 1
- // (note: line 1 is the second row, since counting begins with 0):
- lcd.setCursor(0, 0);
- // Print a message to the LCD.
- lcd.print("Flower Guardian!");
- // Configure the sound sensor's pin for input.
- pinMode(pinTemp, INPUT);
- pinMode(pinLight, INPUT);
- pinMode(pinMoisture, INPUT);
- //init data
- temp = 0 ;
- for (int i = 0 ; i < 16; i++)
- {
- temp += getTemperature();
- delay(200);
- }
- temperature_last = temp / 16;
- // Configure the LED's pin for output signals.
- pinMode(pinLed, OUTPUT);
- //digitalWrite(pinLed, HIGH);//on
- digitalWrite(pinLed, LOW);//off
- }
- void loop() {
- // check for a reading no more than once a second.
- if (millis() - lastReadingTime > 1000) {
- // if there's a reading ready, read it:
- // don't do anything until the data ready pin is high:
- {
- getData();
- // timestamp the last time you got a reading:
- lastReadingTime = millis();
- }
- }
- // listen for incoming Ethernet connections:
- listenForEthernetClients();
- }
- float getTemperature() {
- // Get the (raw) value of the temperature sensor.
- int analogValueTemp = analogRead(pinTemp);
- // Determine the current resistance of the thermistor based on the sensor value.
- float resistance = (float)(1023 - analogValueTemp) * 100000 / analogValueTemp; //串联电阻分压公式求出,注意,AD的参考电压 等于 串联电阻的总电压
- // Calculate the temperature based on the resistance value.
- float temperature_now = 1 / (log(resistance / 100000) / B + 1 / 298.15) - 273.15;
- //1°K=°C+273.15 298.15K = 25
- return temperature_now;
- }
- void getData() {
- //Serial.println("Getting reading");
- //LPF
- float temperature = getTemperature() - temperature_last;
- temperature_last = temperature * cal_filt_gain + temperature_last;
- // Read the value of the light sensor. The light sensor is an analog sensor.
- analogLightValue = 1024 -analogRead(pinLight);//光强
- // Read the value of the Moisture Sensor. The Moisture Sensor is an analog sensor.
- analogHumidityValue = 1024 - analogRead(pinMoisture); //湿度
- lcd.clear();// clear the screen
- lcd.setCursor(0, 0);
- lcd.print("T:");//LCD显示当前温度
- lcd.print(temperature_last, 1);
- lcd.print("C");
- //lcd.print(" ");
- lcd.setCursor(8, 0);
- lcd.print("H:");//LCD现实当前湿度
- lcd.print(analogHumidityValue);
- lcd.setCursor(0, 1);
- lcd.print("L:");//LCD现实当前光照情况
- lcd.print(analogLightValue);
- //显示当前土壤情况
- lcd.setCursor(8, 1); //光标移动到第二行,第8个字符
- if (analogHumidityValue < 300) {
- lcd.print("S:Dry ");
- }
- else if (analogHumidityValue >= 300 && analogHumidityValue < 700) {
- lcd.print("S:Humid");
- }
- else {
- lcd.print("S:Water");
- }
- }
- void listenForEthernetClients() {
- // listen for incoming clients
- WiFiClient client = server.available();
- if (client) {// if you get a client,
- Serial.println("new client"); // print a message out the serial port
- // an http request ends with a blank line
- boolean currentLineIsBlank = true;
- String currentLine = ""; // make a String to hold incoming data from the client
- while (client.connected()) { // loop while the client's connected
- if (client.available()) { // if there's bytes to read from the client,
- char c = client.read(); // read a byte, then
- Serial.write(c); // print it out the serial monitor
- // if you've gotten to the end of the line (received a newline
- // character) and the line is blank, the http request has ended,
- // so you can send a reply
- //if (c == 'n' && currentLineIsBlank) { // if the byte is a newline character
- if ( c == 'n' && currentLine.length() == 0 ) {
- // if the current line is blank, you got two newline characters in a row.
- // that's the end of the client HTTP request, so send a response:
- // send a standard http response header
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println("Connection: close"); // the connection will be closed after completion of the response
- client.println("Refresh: 5"); // refresh the page automatically every 5 sec
- client.println();
- client.println("");
- client.println("");
- // output the value of each analog input pin
- /*
- for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
- int sensorReading = analogRead(analogChannel);
- client.print("analog input ");
- client.print(analogChannel);
- client.print(" is ");
- client.print(sensorReading);
- client.println("
");
- }*/
- client.print("Temperature: ");
- client.print(temperature_last);
- client.print(" degrees C");
- client.print("
");
- client.print("Light : " + String(analogLightValue));
- client.print(" lu");
- client.print("
");
- client.print("Humidity : " + String(analogHumidityValue));
- client.print(" %rh");
- client.print("
");
- // the content of the HTTP response follows the header:
- client.print("Click here turn the LED on
");
- client.print("Click here turn the LED off
");
- client.println("");
- // The HTTP response ends with another blank line:
- client.println();
- break;
- }
- if (c == 'n') {
- //currentLineIsBlank = true; // you're starting a new line
- currentLine = "";// if you got a newline, then clear currentLine:
- }
- else if (c != 'r') {// if you got anything else but a carriage return character,
- //currentLineIsBlank = false;// you've gotten a character on the current line
- currentLine += c; // add it to the end of the currentLine
- }
- // Check to see if the client request was "GET /H" or "GET /L":
- if (currentLine.endsWith("GET /H")) {
- digitalWrite(pinLed, HIGH); // GET /H turns the LED on
- }
- if (currentLine.endsWith("GET /L")) {
- digitalWrite(pinLed, LOW); // GET /L turns the LED off
- }
- }
- }
- // give the web browser time to receive the data
- delay(1);
- // close the connection:
- client.stop();
- Serial.println("client disonnected");
- }
- }
- 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");
- // print where to go in a browser:
- Serial.print("To see this Flower guardian page in action, open a browser to http://");
- Serial.println(ip);
- }
复制代码
总结:
使用Edison真的很方便,加上众多的模块,加速了原型的开发。本来做几个项目练练手的,没有想到,越做越深越广,快到结项时间,就把以前的练练手的项目综合起来,做个结项贴子,日后还会用Edison做更多的创意。
|