查看: 6980|回复: 1

案例四:Arduino超声波测距采集案例

[复制链接]

该用户从未签到

发表于 2014-8-29 10:58 | 显示全部楼层 |阅读模式
本帖最后由 weijinhe 于 2014-10-15 12:45 编辑

1、硬件准备
(1)Arduino uno
(2)超声波测距传感器模块
(3)Arduino Ethernet W5100 网络扩展板模块
(4)网线一根

2、硬件连接(1)Arduino Ethernet W5100 网络扩展板模块与Arduino uno连接。(2)Arduino Ethernet W5100 网络扩展板模块插上网线(3)超声波传感器VC GND Trgpin EchoPin 分别与Arduino uno 5V GND 数字量2 3连接。
3、烧写代码

kittenblock中小学创客名师推荐的图形化编程软件

const int TrigPin = 2; 
const int EchoPin = 3; 
float cm; 
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192,168,0,12);
EthernetClient client;
IPAddress server(60,211,253,162);   
unsigned long lastConnectionTime = 0;         
boolean lastConnected = false;                
const unsigned long postingInterval = 10*1000; 
void setup() {
  Serial.begin(9600);
   while (!Serial) {
    ; 
  }
pinMode(TrigPin, OUTPUT); 
pinMode(EchoPin, INPUT); 
pinMode(8,OUTPUT);
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }
}
void loop() {
digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin 
delayMicroseconds(2); 
digitalWrite(TrigPin, HIGH); 
delayMicroseconds(10); 
digitalWrite(TrigPin, LOW); 
cm = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm 
cm = (int(cm * 100.0)) / 100.0; //保留两位小数 
//Serial.println(cm);  
delay(100);
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  if (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
  // if you're not connected, and ten seconds have passed since
  // your last connection, then connect again and send data:
  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
    sendData(cm);
  }
  // store the state of the connection for next time through
  // the loop:
  lastConnected = client.connected();
}

// this method makes a HTTP connection to the server:
void sendData(float thisData) {
  // if there's a successful connection:
  if (client.connect(server, 10086)) {
    Serial.println("connecting...");
 char tempStr3[20];
 floatToString(cm,tempStr3);
 String data;
 data+="";
 data+=String(tempStr3);
 int t=data.length();
  client.println("POST /v1.0/device/9c12597e7756453389068fdff7ad93f9/1/1/datapoints/add HTTP/1.1"); 
  client.println("Host: api.machtalk.net");
  client.println("APIKey:7a19bd7874a541a6b4c50a831ea0b3b2");
  client.print("Accept: *");
  client.print("/");   
  client.println("*"); 
  client.print("Content-Length: "); 
  int thislength=17+t;
  client.println(thislength);
  client.println("Content-Type: application/x-www-form-urlencoded");
  client.println("Connection: close");   
  client.println();
  client.print("params={\"value\":");
  client.print(thisData);
  client.println("}");
  } 
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
   // note the time that the connection was made or attempted:
  lastConnectionTime = millis();
}
void floatToString(float in,char* out){
  uint16_t Tc_100 = in*100;
  uint8_t whole, fract;
  whole = Tc_100/10 ;  // separate off the whole and fractional portions
  fract = Tc_100 % 100;
  sprintf(out,"%d.%d",whole,fract); 
}
4Machtalk物联网平台配置Machtalk物联网平台配置、动作设置、触发器设置如http://machtalk.net/intro/regist所示,不再累述。www.machtalk.net技术交流群:300250166


超声波测距传感器.jpg
w5100.jpg
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

热门推荐

[限时福利]5分钟带你快速了解新一代开发板:M5STACK
[限时福利]5分钟带你快速
一、什么是M5Stack M5Stack是一种模块化、可堆叠扩展的开发板,每个模块
ESP-01M上传问题
ESP-01M上传问题
求告诉,如何解决!
Arduino四则混合运算计算器
Arduino四则混合运算计算
#由于时间问题,再加上东西刚刚做出来过于兴奋导致发帖太着急,帖子暂时没写完
【干货分享】mega2560原理图PCB图纸altium designer18
【干货分享】mega2560原理
分享一下mega2560的板子 AD版本 **** 本内容被作者隐藏 **** ergo
M5StickC平衡机器人
M5StickC平衡机器人
M5StickC内置了SH200Q/MPU6886,也就是加速度计和陀螺仪 ,添加驱动板和小电机就可以
Copyright   ©2015-2016  Arduino中文社区  Powered by©Discuz!   
快速回复 返回顶部 返回列表