“JsonUdpBeacon”的版本间的差异

来自YFRobotwiki
跳转至: 导航搜索
(以“ Json库 <pre> // Copyright Benoit Blanchon 2014-2016 // MIT License // // Arduino JSON library // https://github.com/bblanchon/ArduinoJson // If you like thi...”为内容创建页面)
 
 
第5行: 第5行:
 
<pre>
 
<pre>
  
// Copyright Benoit Blanchon 2014-2016
+
// Send a JSON object on UDP at regular interval
// MIT License
+
 
//
 
//
// Arduino JSON library
+
// You can easily test this program with netcat:
// https://github.com/bblanchon/ArduinoJson
+
// $ nc -ulp 8888
// If you like this project, please add a star!
+
//
 +
// by Benoit Blanchon, MIT License 2015-2016
  
 +
#include <SPI.h>
 +
#include <Ethernet.h>
 
#include <ArduinoJson.h>
 
#include <ArduinoJson.h>
  
using namespace ArduinoJson::Internals;
+
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
 +
IPAddress localIp(192, 168, 0, 177);
 +
IPAddress remoteIp(192, 168, 0, 109);
 +
unsigned int remotePort = 8888;
 +
unsigned localPort = 8888;
 +
EthernetUDP udp;
  
void setup() {
+
JsonObject& buildJson(JsonBuffer& jsonBuffer) {
   Serial.begin(115200);
+
   JsonObject& root = jsonBuffer.createObject();
   while (!Serial) {
+
 
     // wait serial port initialization
+
   JsonArray& analogValues = root.createNestedArray("analog");
 +
  for (int pin = 0; pin < 6; pin++) {
 +
     int value = analogRead(pin);
 +
    analogValues.add(value);
 
   }
 
   }
  
   IndentedPrint serial(Serial);
+
   JsonArray& digitalValues = root.createNestedArray("digital");
   serial.setTabSize(4);
+
   for (int pin = 0; pin < 14; pin++) {
 +
    int value = digitalRead(pin);
 +
    digitalValues.add(value);
 +
  }
  
   serial.println("This is at indentation 0");
+
   return root;
  serial.indent();
+
}
  serial.println("This is at indentation 1");
+
  serial.println("This is also at indentation 1");
+
  serial.indent();
+
  serial.println("This is at indentation 2");
+
  
   serial.unindent();
+
void sendJson(JsonObject& json) {
   serial.unindent();
+
   udp.beginPacket(remoteIp, remotePort);
   serial.println("This is back at indentation 0");
+
   json.printTo(udp);
 +
   udp.println();
 +
  udp.endPacket();
 +
}
 +
 
 +
void setup() {
 +
  Ethernet.begin(mac, localIp);
 +
  udp.begin(localPort);
 
}
 
}
  
 
void loop() {
 
void loop() {
   // not used in this example
+
   delay(1000);
 +
 
 +
  StaticJsonBuffer<300> jsonBuffer;
 +
  JsonObject& json = buildJson(jsonBuffer);
 +
  sendJson(json);
 
}
 
}
 +
  
  

2016年8月22日 (一) 15:42的最后版本

Json库



// Send a JSON object on UDP at regular interval
//
// You can easily test this program with netcat:
// $ nc -ulp 8888
//
// by Benoit Blanchon, MIT License 2015-2016

#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoJson.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress localIp(192, 168, 0, 177);
IPAddress remoteIp(192, 168, 0, 109);
unsigned int remotePort = 8888;
unsigned localPort = 8888;
EthernetUDP udp;

JsonObject& buildJson(JsonBuffer& jsonBuffer) {
  JsonObject& root = jsonBuffer.createObject();

  JsonArray& analogValues = root.createNestedArray("analog");
  for (int pin = 0; pin < 6; pin++) {
    int value = analogRead(pin);
    analogValues.add(value);
  }

  JsonArray& digitalValues = root.createNestedArray("digital");
  for (int pin = 0; pin < 14; pin++) {
    int value = digitalRead(pin);
    digitalValues.add(value);
  }

  return root;
}

void sendJson(JsonObject& json) {
  udp.beginPacket(remoteIp, remotePort);
  json.printTo(udp);
  udp.println();
  udp.endPacket();
}

void setup() {
  Ethernet.begin(mac, localIp);
  udp.begin(localPort);
}

void loop() {
  delay(1000);

  StaticJsonBuffer<300> jsonBuffer;
  JsonObject& json = buildJson(jsonBuffer);
  sendJson(json);
}






返回Arduino库菜单

返回首页

更多建议和问题欢迎反馈至 YFRobot论坛