极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10101|回复: 0

Socket方式数据上传声音传感器至machtalk.net物联网平台案例

[复制链接]
发表于 2014-4-14 16:13:46 | 显示全部楼层 |阅读模式
本帖最后由 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
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 10:40 , Processed in 0.088095 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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