|
|
本帖最后由 weijinhe 于 2014-4-15 08:54 编辑
www.machtalk.net
1、硬件准备
与例二相同。machtalk.net 声音传感器数据采集案例
http://www.geek-workshop.com/thread-9326-1-1.html
2、硬件连接
与例二相同。
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);
//char server[] = "api.machtalk.net";
unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
const unsigned long postingInterval = 10*1000;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
}
void loop() {
// read the analog sensor:
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 there's a successful connection:
if (client.connect(server, 7777)) {
Serial.println("connecting...");
// socket:
client.println("{\"cmd\":\"create\",\"data\": {\"APIKey\":\"7a19bd7874a541a6b4c50a831ea0b3b2\",\"device_id\":\"7bc8abb1b3cb434499e66ba39c206aba\",\"device_value_id\":\"1\",\"device_type_id\":\"1\"}}");
client.print("{\"cmd\":\"post\",\"data\":{\"value\":");
client.print(thisData);
client.println("}}");
client.println("{\"cmd\":\"close\"}");
}
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();
}
平台配置
平台配置如案例二类似。
machtalk.net物联网平台技术交流群:300250166 |
|