以下の内容はhttps://higepon.hatenablog.com/entry/20140909/1410229118より取得しました。


4. Control LED blink speed via Internet - Spark Core

Let's add more to 3. Bread Board & LED. I use exact same setting on the bread board.
Just changed code.

Here is code for accepting API request via Internet.
As you can see, we register “blink” API endpoint which will call int blink(String) function.

// Control LED blink speed via the Internet

// We name pin D0 as led
int led = D0;

// This routine runs only once upon reset
void setup()
{
  // Initialize D0 pin as output
  pinMode(led, OUTPUT);
  Spark.function("blink", blink); 
}

int blink(String command) {
    // Default is fast blink
    int ms = 50;
    if (command == "slow") {
        // slow
        ms = 300;
    }
    for (int i = 0; i < 20; i ++) {
      digitalWrite(led, HIGH);
      delay(ms);            
      digitalWrite(led, LOW);
      delay(ms);
    }
    return 0;
}

// This routine loops forever
void loop()
{
}

You can call the API using curl as follows.

# blink fast
% curl https://api.spark.io/v1/devices/YOUR_DEVICE_ID/blink \
>   -d access_token=YOUR_TOKEN
{
  "id": "YOUR_DEVICE_ID",
  "name": "mona",
  "last_app": null,
  "connected": true,
  "return_value": 0
}

# blink slow
% curl https://api.spark.io/v1/devices/YOUR_DEVICE_ID/blink 
  -d access_token=YOUR_TOKEN -d params=slow
{
  "id": "YOUR_DEVICE_ID",
  "name": "mona",
  "last_app": null,
  "connected": true,
  "return_value": 0
}

https://github.com/higepon/Spark-Core-Examples/blob/master/4.Blink%20LED%20via%20Internet.ino




以上の内容はhttps://higepon.hatenablog.com/entry/20140909/1410229118より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14