Stap 3: REST Protocol en Code
Het protocol van de REST betekent dat het bericht ziet eruit als de /arduino/message/this is mijn boodschap
Als je arduino arduino heette en gelegen op IP-adres 192.168.1.34 was dan zou de volledige REST:
http: //192.168.1.14/arduino/message/I got zulks depêche via Wifi :-)
Opmerking: geen spaties of speciale tekens zal worden vervangen door hun Hex-codes door de browser van de Client voordat naar de Yun. Bijvoorbeeld:
192.168.1.14/arduino/message/I%20got%20this%20%20%20message%20over%20Wifi%20:-%29
Hier is de code. Zoals u kunt zien is het een vrij eenvoudig mod van het voorbeeld van de brug en de Adafruit TFT-voorbeeld:
/* Adapted from: Arduino Yun Bridge example http://arduino.cc/en/Tutorial/Bridge Adapted from example sketch for the Adafruit 2.2" SPI display written by Limor Fried/Ladyada for Adafruit Industries. This library works with the Adafruit 2.2" TFT Breakout w/SD card ----> http://www.adafruit.com/products/1480 */ #include "Bridge.h"#include "YunServer.h" #include "YunClient.h" #include "SPI.h" #include "Adafruit_GFX.h" #include "Adafruit_ILI9340.h" #if defined(__SAM3X8E__) #undef __FlashStringHelper::F(string_literal) #define F(string_literal) string_literal #endif // These are the pins used for the UNO // for Due/Mega/Leonardo use the hardware SPI pins (which are different) #define _sclk 13 #define _miso 12 #define _mosi 11 #define _cs 10 #define _rst 9 #define _dc 8 Adafruit_ILI9340 tft = Adafruit_ILI9340(_cs, _dc, _rst); // Listen on default port 5555, the webserver on the Yun // will forward there all the HTTP requests for us. YunServer server; void setup() { // Bridge startup pinMode(13,OUTPUT); digitalWrite(13, LOW); Bridge.begin(); digitalWrite(13, HIGH); // Listen for incoming connection only from localhost // (no one from the external network could connect) server.listenOnLocalhost(); server.begin(); tft.begin(); tft.fillScreen(ILI9340_BLACK); tft.setRotation(1); } void loop() { // Get clients coming from server YunClient client = server.accept(); // There is a new client? if (client) { // Process request process(client); // Close connection and free resources. client.stop(); } delay(50); // Poll every 50ms } void process(YunClient client) { // read the command String command = client.readStringUntil('/'); // is "message" command? if (command == "message") { messageCommand(client); } } void messageCommand(YunClient client) { String message = client.readStringUntil('/'); // Send feedback to client client.print(F("Message")); client.print(F(" set to ")); client.println(message); // Update datastore key with the current message value String key = "Message"; Bridge.put(key, message); // Send to the Screen tft.fillScreen(ILI9340_WHITE); tft.setCursor(5, 5); tft.setTextColor(ILI9340_BLACK); tft.setTextSize(4); tft.println(message); }