查看: 7517|回复: 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
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

热门推荐

5分钟带你快速了解新一代开发板:M5STACK
5分钟带你快速了解新一代
一、什么是M5Stack M5Stack是一种模块化、可堆叠扩展的开发板,每个模块
创客火首发无人机编队套装,开启不一样的人工智能教育
创客火首发无人机编队套装
2017年国务院发布《新一代人工智能发展规划》,提出要广泛开展人工智能科普活动,在中
为开发板 Generic ESP8266 Module 编译时出错
为开发板 Generic ESP8266
第一次尝试Arduino UNO软串口通信,编译一直通不过,错误信息 “开发板 generic (平
请问L298N能不能驱动这种电机呀
请问L298N能不能驱动这种
小白不太敢确定orzz电机是30W,10转,12V的直流减速电机 谢谢大家了!
ws2812灯带求助,求大佬。。
ws2812灯带求助,求大佬。
刚才那个求助帖我说的可能有点模糊,所以我这次弄详细一点。小弟第一次接触这个灯带,
Copyright   ©2015-2016  Arduino中文社区  Powered by©Discuz!   
快速回复 返回顶部 返回列表