AVR单片机程序
作者的设计用到旋钮,所以采用OneNETEDP通讯。 有一个辅助软件可以帮你更快编程和调试,使用后电脑相当于下位机。 提示:OneNET支持多种不同的接入协议,很多都是基于TCP/UDP的上层协议,其分别对应的域名、IP地址和端口号都各有不同:
EDP 域名:jjfaedp.hedevice.com
IP地址:183.230.40.33/39/40
端口号:876/29876 HTTP
域名:api.heclouds.com或者jjfarfapi.heclouds.com
IP地址:183.230.40.33/34 端口号 80
通过软件,输入IP地址、端口、设备ID、Api-Key生成代码,生成代码你就可以连接网上服务端,资料下载地址:
//OneNet平台上设备建立连接
void connection(){ unsigned char hexdata[49] ={0X10,0X2F,0X00,0X03,0X45,0X44,0X50, 0X01,0X40,0X00,0X80,0X00,0X06,0X35, 0X36,0X35,0X30,0X32,0X37,0X00,0X1C, 0X6C,0X65,0X76,0X71,0X6D,0X43,0X62, 0X54,0X69,0X54,0X36,0X47,0X62,0X45, 0X6D,0X33,0X66,0X45,0X5A,0X6A,0X6C, 0X38,0X5A,0X52,0X53,0X66,0X63,0X3D}; Serial1.write(hexdata, 49);}
//上传数据OneNet
void postData1(unsigned char Data,unsigned char SENSORID1,unsigned char SENSORID2){ unsigned char hexdata[8] = {0x80,0x0a,0x00,0x05,0x00,0x06,0x2c,0x3b}; Serial1.write(hexdata, 8); Serial1.write(Data); Serial1.write(0x2c); Serial1.write(SENSORID1);}
发送的数据格式: 0a、06是发送信息长度。 31是数据流名称为1
31 38发送数值为18 EDP格式定义文件下载地址:http://open.iot.10086.cn/download
//心跳,掉线重连接
void heart(){ unsigned char data[2] = {0xC0,0x00}; //心跳 Serial1.flush(); if(millis()-time>=2400){ Serial1.write(data, 2); } while(Serial1.available()) //重新连接 { char c = Serial1.read(); //Serial.print(c); if (c == 0X6E) //没有连接上 connection(); }}
//检测光线
float ajust(){ //自动调节亮度 float x; x=analogRead(pin)*0.00488; if(x<=constant) //constant暗和亮临界值 t=(5-x)*51.2*0.1; return t;}
这个是用到开发板的AD转化IO,A0-A5, AD转化精度为1/1024,这个可以参看一下这网址,都有说的很详细。 http://www.arduino.cn/thread-1205-1-1.html
//心跳//音乐模式//心跳//音乐模式
void heartbeat(){ //红色led for(int n=0;n<200;n++){ analogWrite(redled, n); analogWrite(whiteled, 200-n); delay(2); } for (int a=200; a>=0;a--){ //循环语句,控制PWM亮度减小 analogWrite(redled,a); analogWrite(whiteled, 200-a); delay(2); //当前亮度的维持的时间,单位毫秒 } //蓝色led for(int n=0;n<200;n++){ analogWrite(blueled, n); analogWrite(redled, 200-n); delay(2); } for (int a=200; a>=0;a--){ //循环语句,控制PWM亮度减小 analogWrite(blueled,a); analogWrite(redled, 200-a); delay(2); //当前亮度的维持的时间,单位毫秒 } //绿色led for(int n=0;n<200;n++){ analogWrite(greenled, n); analogWrite(blueled, 200-n); delay(2); } for (int a=200; a>=0;a--){ //循环语句,控制PWM亮度减小 analogWrite(greenled,a); analogWrite(blueled, 200-a); delay(2); //当前亮度的维持的时间,单位毫秒 }}
PCB板最终图:
组装成型图:
(
请忽略那质朴的外壳)
最后是作者应用展示的视频:
http://www.tudou.com/programs/view/LqjhtB-mbbw/