案例四:Arduino超声波测距采集案例-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 8837|回复: 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、烧写代码[mw_shl_code=c,true]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);
}[/mw_shl_code]
4Machtalk物联网平台配置Machtalk物联网平台配置、动作设置、触发器设置如http://machtalk.net/intro/regist所示,不再累述。www.machtalk.net技术交流群:300250166


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

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 10:40 , Processed in 0.365960 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表