|
|
本帖最后由 weijinhe 于 2014-4-15 11:33 编辑
machtal.net登录注册
1、硬件准备
Arduino uno
模拟声音传感器模块
Arduino Ethernet W5100 网络扩展板模块
网线一根
2、硬件连接
Arduino Ethernet W5100 网络扩展板模块与Arduino uno连接。
Arduino Ethernet W5100 网络扩展板模块插上网线
模拟声音传感VC GND out 分别与Arduino uno 5V GND A(0)连接
3、烧写代码
#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; // last time you connected to the server
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; //delay between updates
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
}
void loop() {
int sensorReading = analogRead(A0);
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
sendData(sensorReading);
}
lastConnected = client.connected();
}
void sendData(int thisData) {
if (client.connect(server, 10086)) {
Serial.println("connecting...");
client.println("POST /v1.0/device/7bc8abb1b3cb434499e66ba39c206aba/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+getLength(thisData);
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 {
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
lastConnectionTime = millis();
}
int getLength(int someValue) {
int digits = 1;
int dividend = someValue /10;
while (dividend > 0) {
dividend = dividend /10;
digits++;
}
return digits;
}
4、Machtalk物联网平台配置
Machtalk物联网平台配置、动作设置、触发器设置如例一所示,不再累述。案例:
machtalk.net物联网平台温度传感器采集
http://www.geek-workshop.com/thread-9318-1-1.html
www.machtalk技术交流群:300250166
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|