“JsonServer”的版本间的差异

来自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
+
// Sample Arduino Json Web Server
// MIT License
+
// Created by Benoit Blanchon.
//
+
// Heavily inspired by "Web Server" from David A. Mellis and Tom Igoe
// Arduino JSON library
+
// https://github.com/bblanchon/ArduinoJson
+
// If you like this project, please add a star!
+
  
 +
#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 ip(192, 168, 0, 177);
 +
EthernetServer server(80);
  
void setup() {
+
bool readRequest(EthernetClient& client) {
   Serial.begin(115200);
+
   bool currentLineIsBlank = true;
  while (!Serial) {
+
  while (client.connected()) {
     // wait serial port initialization
+
    if (client.available()) {
 +
      char c = client.read();
 +
      if (c == '\n' && currentLineIsBlank) {
 +
        return true;
 +
      } else if (c == '\n') {
 +
        currentLineIsBlank = true;
 +
      } else if (c != '\r') {
 +
        currentLineIsBlank = false;
 +
      }
 +
     }
 
   }
 
   }
 +
  return false;
 +
}
  
  IndentedPrint serial(Serial);
+
JsonObject& prepareResponse(JsonBuffer& jsonBuffer) {
   serial.setTabSize(4);
+
   JsonObject& root = jsonBuffer.createObject();
  
   serial.println("This is at indentation 0");
+
   JsonArray& analogValues = root.createNestedArray("analog");
   serial.indent();
+
   for (int pin = 0; pin < 6; pin++) {
  serial.println("This is at indentation 1");
+
    int value = analogRead(pin);
  serial.println("This is also at indentation 1");
+
    analogValues.add(value);
  serial.indent();
+
   }
   serial.println("This is at indentation 2");
+
  
   serial.unindent();
+
   JsonArray& digitalValues = root.createNestedArray("digital");
   serial.unindent();
+
   for (int pin = 0; pin < 14; pin++) {
   serial.println("This is back at indentation 0");
+
    int value = digitalRead(pin);
 +
    digitalValues.add(value);
 +
   }
 +
 
 +
  return root;
 +
}
 +
 
 +
void writeResponse(EthernetClient& client, JsonObject& json) {
 +
  client.println("HTTP/1.1 200 OK");
 +
  client.println("Content-Type: application/json");
 +
  client.println("Connection: close");
 +
  client.println();
 +
 
 +
  json.prettyPrintTo(client);
 +
}
 +
 
 +
void setup() {
 +
  Ethernet.begin(mac, ip);
 +
  server.begin();
 
}
 
}
  
 
void loop() {
 
void loop() {
   // not used in this example
+
   EthernetClient client = server.available();
 +
  if (client) {
 +
    bool success = readRequest(client);
 +
    if (success) {
 +
      StaticJsonBuffer<500> jsonBuffer;
 +
      JsonObject& json = prepareResponse(jsonBuffer);
 +
      writeResponse(client, json);
 +
    }
 +
    delay(1);
 +
    client.stop();
 +
  }
 
}
 
}
 +
  
  

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

Json库



// Sample Arduino Json Web Server
// Created by Benoit Blanchon.
// Heavily inspired by "Web Server" from David A. Mellis and Tom Igoe

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

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 0, 177);
EthernetServer server(80);

bool readRequest(EthernetClient& client) {
  bool currentLineIsBlank = true;
  while (client.connected()) {
    if (client.available()) {
      char c = client.read();
      if (c == '\n' && currentLineIsBlank) {
        return true;
      } else if (c == '\n') {
        currentLineIsBlank = true;
      } else if (c != '\r') {
        currentLineIsBlank = false;
      }
    }
  }
  return false;
}

JsonObject& prepareResponse(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 writeResponse(EthernetClient& client, JsonObject& json) {
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: application/json");
  client.println("Connection: close");
  client.println();

  json.prettyPrintTo(client);
}

void setup() {
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop() {
  EthernetClient client = server.available();
  if (client) {
    bool success = readRequest(client);
    if (success) {
      StaticJsonBuffer<500> jsonBuffer;
      JsonObject& json = prepareResponse(jsonBuffer);
      writeResponse(client, json);
    }
    delay(1);
    client.stop();
  }
}






返回Arduino库菜单

返回首页

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