- 项目简述:以Arduino驱动传感器采集数据,将数据上传到香蕉派上,并保存在mysql数据库例,配置Tomcat,实现远程访问。
- 用到的硬件:
香蕉派 Arduino*3 紫外线传感器 非接触测温传感器 红外测距感器 游戏摇杆模块 云台 u***摄像头
一拖四USB集线器
下面开始:
- 节点1(红外测距传感器): 连线(VCC->5V GND->GND 信号->A0)
- 代码(测量代为是mm,在网页显示的时候加单位)
- #define pin A0
- void setup()
- {
- Serial.begin(9600); // 9600 bps
- pinMode (pin, INPUT);
- }
- void loop()
- {
- uint16_t value = analogRead (pin);
- uint16_t range = get_gp2d12 (value);
- if ( Serial.available())
- {
- if('s' == Serial.read())
- Serial.println(range);
- }
- }
- uint16_t get_gp2d12 (uint16_t value) {
- if (value < 30)
- value = 30;
- return ((67870.0 / (value - 3.0)) - 40.0);
- }
- 节点二(紫外线传感器): 连线(VCC->5V GND->GND 信号->A0)
- 代码(单位mW/cm^2)
- int ReadUVintensityPin = A0;
- void setup()
- {
- pinMode(ReadUVintensityPin, INPUT);
- Serial.begin(9600);
- }
- void loop()
- {
- int uvLevel = averageAnalogRead(ReadUVintensityPin);
- float outputVoltage = 5.0 * uvLevel/1024;
- float uvIntensity = mapfloat(outputVoltage, 0.99, 2.9, 0.0, 15.0);
- if ( Serial.available())
- {
- if('s' == Serial.read())
- Serial.println(uvIntensity);
- }
- delay(500);
- }
- int averageAnalogRead(int pinToRead)
- {
- byte numberOfReadings = 8;
- unsigned int runningValue = 0;
- for(int x = 0 ; x < numberOfReadings ; x++)
- runningValue += analogRead(pinToRead);
- runningValue /= numberOfReadings;
- return(runningValue);
- }
- float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
- {
- return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
- }
- 节点三(非接触温度传感器)连线(VCC->5V GND->GND SDA->A4 SCL->A5)
- 代码(這里需要一个库IR_Thermometer_Sensor_MLX90614)
- /***************************************************
- * IR Thermometer Sensor-MLX90614
- * ****************************************************
- * This example is to get the ambient temperature and object temperature by the IIC bus
-
- * [url=home.php?mod=space&uid=40524]@author[/url] jackli(Jack.li@dfrobot.com)
- * [url=home.php?mod=space&uid=644434]@version[/url] V1.0
- * @date 2016-2-2
- * A4(SDA) A5(SCL)
- * GNU Lesser General Public License.
- * See for details.
- * All above must be included in any redistribution
- * ****************************************************/
- #include
- #include
- IR_Thermometer_Sensor_MLX90614 MLX90614 = IR_Thermometer_Sensor_MLX90614();
- void setup() {
- Serial.begin(9600);
- MLX90614.begin();
- }
- void loop()
- {
- if(Serial.available())
- {
- if('s'==Serial.read())
- {
- Serial.print(MLX90614.GetObjectTemp_Celsius());
- }
- }
- }
2.使用一拖四u***集线器将三个节点连接起来
3.香蕉派读取USB口的数据
- sudo apt-get install python-serial
代码
- import serial
- def read(str1):
- ser=serial.Serial(str1,9600,timeout=1)
- flag = True
- result=0.0
- while flag:
- ser.write('s')
- response = ser.readall()
- response=response.strip()
- if len(response) and len(response.split("."))==2:
- st=response.split(".")
- result=float(st[0])+float(st[1])*(pow(0.1,len(st[1])))
- flag=False
- elif len(response) and len(response.split("."))==1:
- result=int(response)
- flag=False
- ser.close()
- return result
- def main():
- str=("/dev/ttyACM0","/dev/ttyUSB0","/dev/ttyUSB1")
- while(1):
- for i in str:
- num=read(i)
- print num
- main()
- 运行代码
待续部分(安装mysql 、tomcat)