コード例 #1
0
ファイル: senml_pack.cpp プロジェクト: osdomotics/osd-contiki
void SenMLPack::fromJson(Stream *source, SenMLStreamMethod format)
{
    JsonStreamingParser parser;
    SenMLJsonListener listener(this);
    
    parser.setListener(&listener);
    char data;
    if(format == SENML_RAW) {
        #ifdef __MBED__
            data = source->getc();
        #else
            data = source->read();
        #endif
    }
    else{
        data = readHexChar(source);
    }
        
    while(data != -1){
        parser.parse(data); 
        if(format == SENML_RAW){
            #ifdef __MBED__
                data = source->getc();
            #else
                data = source->read();
            #endif
        }   
        else
            data = readHexChar(source);
    }
    // when we get here, all the data is stored in the document and callbacks have been called.
}
コード例 #2
0
ファイル: senml_pack.cpp プロジェクト: osdomotics/osd-contiki
void SenMLPack::fromJson(const char *source)
{
    JsonStreamingParser parser;
    SenMLJsonListener listener(this);
    
    parser.setListener(&listener);
    for(int i = 0; source[i] != 0; i++){
        parser.parse(source[i]); 
    }
    // when we get here, all the data is stored in the document and callbacks have been called.
}
コード例 #3
0
void ThingspeakClient::getLastChannelItem(String channelId, String readApiKey) {
  JsonStreamingParser parser;
  parser.setListener(this);
  WiFiClient client;

  // http://api.thingspeak.com/channels/CHANNEL_ID/feeds.json?results=2&api_key=API_KEY
  const char host[] = "api.thingspeak.com";
  String url = "/channels/" + channelId +"/feeds.json?results=1&api_key=" + readApiKey;

  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }


  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");

  int retryCounter = 0;
  while(!client.available()) {
    Serial.println(".");
    delay(1000);
    retryCounter++;
    if (retryCounter > 10) {
      return;
    }
  }

  int pos = 0;
  boolean isBody = false;
  char c;

  int size = 0;
  client.setNoDelay(false);
  while(client.connected()) {
    while((size = client.available()) > 0) {
      c = client.read();
      if (c == '{' || c == '[') {
        isBody = true;
      }
      if (isBody) {
        parser.parse(c);
      }
    }
  }
}
コード例 #4
0
void WundergroundClient::doUpdate(String url) {
  JsonStreamingParser parser;
  parser.setListener(this);
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect("api.wunderground.com", httpPort)) {
    Serial.println("connection failed");
    return;
  }

  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: api.wunderground.com\r\n" +
               "Connection: close\r\n\r\n");
  int retryCounter = 0;
  while(!client.available()) {
    delay(1000);
    retryCounter++;
    if (retryCounter > 10) {
      return;
    }
  }

  int pos = 0;
  boolean isBody = false;
  char c;

  int size = 0;
  client.setNoDelay(false);
  while(client.connected()) {
    while((size = client.available()) > 0) {
      c = client.read();
      if (c == '{' || c == '[') {
        isBody = true;
      }
      if (isBody) {
        parser.parse(c);
      }
    }
  }
}