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

weijinhe 发表于 2014-8-29 10:58

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

本帖最后由 weijinhe 于 2014-10-15 12:45 编辑

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


weijinhe 发表于 2014-10-16 09:49

吼吼!!!!
页: [1]
查看完整版本: 案例四:Arduino超声波测距采集案例