Merge "Add oneM2M arduino clients" into dev
diff --git a/org.eclipse.om2m.client.arduino/onem2m-arduino-light.in b/org.eclipse.om2m.client.arduino/onem2m-arduino-light.in
new file mode 100644
index 0000000..a92af32
--- /dev/null
+++ b/org.eclipse.om2m.client.arduino/onem2m-arduino-light.in
@@ -0,0 +1,159 @@
+#include <ESP8266WiFi.h>
+ 
+// WIFI network
+const char* ssid = "Pi3-AP";
+const char* password = "raspberry";
+ 
+// Target CSE
+const char* host = "172.24.1.1";
+const int httpPort = 8282;
+ 
+// AE-ID
+const char* origin   = "Clight_ae1";
+ 
+// AE listening port
+WiFiServer server(80);
+ 
+void setup() {
+ 
+  Serial.begin(115200);
+  delay(10);
+ 
+  // Configure pin 5 for LED control
+  pinMode(5, OUTPUT);
+  digitalWrite(5, 0);
+ 
+  Serial.println();
+  Serial.println();
+ 
+  // Connect to WIFI network
+  Serial.print("Connecting to ");
+  Serial.println(ssid);
+ 
+  WiFi.persistent(false);
+ 
+  WiFi.begin(ssid, password);
+ 
+  while (WiFi.status() != WL_CONNECTED) {
+    delay(500);
+    Serial.print(".");
+  }
+  Serial.println();
+  Serial.println("WiFi connected");  
+  Serial.println("IP address: ");
+  Serial.println(WiFi.localIP());
+ 
+  // Start HTTP server
+  server.begin();
+  Serial.println("Server started");
+ 
+  // Create AE resource
+  String resulat = send("/~/gateway-1/gateway-1",2,"{\"m2m:ae\":{\"rn\":\"light_ae1\",\"acpi\":[\"/gateway-1/gateway-1/MN-CSEAcp\"],\"api\":\"light.company.com\",\"rr\":\"true\",\"poa\":[\"http://"+WiFi.localIP().toString()+":80\"]}}");
+ 
+  if(resulat=="HTTP/1.1 201 Created"){
+    // Create Container resource
+    send("/~/gateway-1/gateway-1/light_ae1",3,"{\"m2m:cnt\":{\"rn\":\"light\"}}");
+ 
+    // Create ContentInstance resource
+    send("/~/gateway-1/gateway-1/light_ae1/light",4,"{\"m2m:cin\":{\"con\":\"OFF\"}}");
+ 
+    // Create Subscription resource
+    send("/~/gateway-1/gateway-1/light_ae1/light",23,"{\"m2m:sub\":{\"rn\":\"lightstate_sub\",\"nu\":[\"Clight_ae1\"],\"nct\":1}}");
+  }
+}
+ 
+// Method in charge of receiving event from the CSE
+void loop(){
+  // Check if a client is connected
+  WiFiClient client = server.available();
+  if (!client) {
+    return;
+  }
+ 
+  // Wait until the client sends some data
+  Serial.println("new client");
+  while(!client.available()){
+    delay(1);
+  }
+ 
+  // Read the request
+  String req = client.readString();
+  Serial.println(req);
+  client.flush();
+ 
+  // Switch the LED state according to the received value
+  if (req.indexOf("ON") != -1){
+    digitalWrite(5, 1);
+  }else if (req.indexOf("OFF") != -1){
+    digitalWrite(5, 0);
+  }else{
+    Serial.println("invalid request");
+    client.stop();
+    return;
+  }
+ 
+  client.flush();
+ 
+  // Send HTTP response to the client
+  String s = "HTTP/1.1 200 OK\r\n";
+  client.print(s);
+  delay(1);
+  Serial.println("Client disonnected");
+ 
+}
+ 
+ 
+// Method in charge of sending request to the CSE
+String send(String url,int ty, String rep) {
+ 
+  // Connect to the CSE address
+  Serial.print("connecting to ");
+  Serial.println(host);
+ 
+  WiFiClient client;
+ 
+  if (!client.connect(host, httpPort)) {
+    Serial.println("connection failed");
+    return "error";
+  }
+ 
+ 
+  // prepare the HTTP request
+  String req = String()+"POST " + url + " HTTP/1.1\r\n" +
+               "Host: " + host + "\r\n" +
+               "X-M2M-Origin: " + origin + "\r\n" +
+               "Content-Type: application/json;ty="+ty+"\r\n" +
+               "Content-Length: "+ rep.length()+"\r\n"
+               "Connection: close\r\n\n" + 
+               rep;
+ 
+  Serial.println(req+"\n");
+ 
+  // Send the HTTP request
+  client.print(req);
+ 
+  unsigned long timeout = millis();
+  while (client.available() == 0) {
+    if (millis() - timeout > 5000) {
+      Serial.println(">>> Client Timeout !");
+      client.stop();
+      return "error";
+    }
+  }
+ 
+  // Read the HTTP response
+  String res="";
+  if(client.available()){
+    res = client.readStringUntil('\r');
+    Serial.print(res);
+  }
+  while(client.available()){
+    String line = client.readStringUntil('\r');
+    Serial.print(line);
+  }
+ 
+  Serial.println();
+  Serial.println("closing connection");
+  Serial.println();
+  return res;
+}
diff --git a/org.eclipse.om2m.client.arduino/onem2m-client2-dht.ino b/org.eclipse.om2m.client.arduino/onem2m-client2-dht.ino
new file mode 100644
index 0000000..b70d4b8
--- /dev/null
+++ b/org.eclipse.om2m.client.arduino/onem2m-client2-dht.ino
@@ -0,0 +1,217 @@
+#include <ESP8266WiFi.h>
+#include <SimpleDHT.h>
+#include "Timer.h"
+
+Timer t;
+int pinDHT11 = 2;
+SimpleDHT11 dht11;
+
+const char* ssid = "Pi3-AP";
+const char* password = "raspberry";
+
+
+const char* host = "192.168.0.2";
+const int httpPort = 8080;
+const char* origin   = "Cae-nodemcu";
+
+WiFiServer server(80);
+
+void setup() {
+
+  Serial.begin(115200);
+  delay(10);
+
+ // prepare GPIO2
+  pinMode(5, OUTPUT);
+  digitalWrite(5, 0);
+  pinMode(4, OUTPUT);
+  digitalWrite(4, 0);
+  
+  // We start by connecting to a WiFi network
+  randomSeed(analogRead(0));
+
+  Serial.println();
+  Serial.println();
+  Serial.print("Connecting to ");
+  Serial.println(ssid);
+  
+  WiFi.begin(ssid, password);
+  
+  while (WiFi.status() != WL_CONNECTED) {
+    delay(500);
+    Serial.print(".");
+  }
+
+  Serial.println("");
+  Serial.println("WiFi connected");  
+  
+  server.begin();
+  Serial.println("Server started");
+  
+  Serial.println("IP address: ");
+  Serial.println(WiFi.localIP());
+  
+  String resulat = send("/~/server/server",2,"{\"ae\":{\"rn\":\"MYDEVICE\",\"api\":\"app-test\",\"rr\":\"true\",\"poa\":[\"http://"+WiFi.localIP().toString()+":80\"]}}");
+  
+  if(resulat=="HTTP/1.1 201 Created"){
+    send("/~/server/server/MYDEVICE",3,"{\"cnt\":{\"rn\":\"TEMPERATURE\"}}");
+    send("/~/server/server/MYDEVICE/TEMPERATURE",3,"{\"cnt\":{\"rn\":\"DATA\"}}");
+
+    send("/~/server/server/MYDEVICE",3,"{\"cnt\":{\"rn\":\"HUMIDITY\"}}");
+    send("/~/server/server/MYDEVICE/HUMIDITY",3,"{\"cnt\":{\"rn\":\"DATA\"}}");
+
+    send("/~/server/server/MYDEVICE",3,"{\"cnt\":{\"rn\":\"LED\"}}");
+    send("/~/server/server/MYDEVICE/LED",3,"{\"cnt\":{\"rn\":\"DATA\"}}");
+
+    push();
+    
+    String dataled = "{\"cin\":{\"con\":\"<obj><str val='Led' name='type'/><bool val='false' name='value'/></obj>\"}}";
+    send("/~/server/server/MYDEVICE/LED/DATA",4,dataled);
+
+  }
+  t.every(1000*60, push);
+}
+
+void loop(){
+  t.update();
+    // Check if a client has connected
+  WiFiClient client = server.available();
+  if (!client) {
+    return;
+  }
+  
+  // Wait until the client sends some data
+  Serial.println("new client");
+  while(!client.available()){
+    delay(1);
+  }
+  
+  // Read the first line of the request
+  String req = client.readStringUntil('\r');
+  Serial.println(req);
+  client.flush();
+  
+  // Match the request
+  int val;
+  int gpio;
+  String state;
+  String type;
+  String cnt;
+  if (req.indexOf("false") != -1){
+    gpio= 5;
+    val = 0;
+    state="false";
+    type="Led";
+    cnt="LED";
+  }else if (req.indexOf("true") != -1){
+    gpio= 5;
+    val = 1;
+    state="true";
+    type="Led";
+    cnt="LED";
+  }else {
+    Serial.println("invalid request");
+    client.stop();
+    return;
+  }
+
+  // Set GPIO2 according to the request
+  digitalWrite(gpio, val);
+
+  String data = String()+"{\"cin\":{\"con\":\"<obj><str val='"+type+"' name='type'/><bool val='"+state+"' name='value'/></obj>\"}}";
+  send(String()+"/~/server/server/MYDEVICE/"+cnt+"/DATA",4,data);
+  
+  client.flush();
+
+  // Prepare the response
+  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
+  s += (val)?"high":"low";
+  s += "</html>\n";
+
+  // Send the response to the client
+  client.print(s);
+  delay(1);
+  Serial.println("Client disonnected");
+  
+}
+
+
+void push(){
+  byte temperature = 0;
+  byte humidity = 0;
+
+  if (dht11.read(pinDHT11, &temperature, &humidity, NULL)) {
+    Serial.println("Read DHT11 failed.");
+    delay(1000);
+    return;
+  }
+  Serial.print((int)temperature); Serial.print(" *C, "); 
+  Serial.print((int)humidity); Serial.println(" %");
+
+  String datat,datah;
+  datat = String()+"{\"cin\":{\"con\":\"<obj><str val='Temperature' name='type'/><int val='"+(int)temperature+"' name='value'/><str val='Celsius' name='unit'/></obj>\"}}";
+  datah = String()+"{\"cin\":{\"con\":\"<obj><str val='Humidity' name='type'/><int val='"+(int)humidity+"' name='value'/><str val='%' name='unit'/></obj>\"}}";
+  send("/~/server/server/MYDEVICE/TEMPERATURE/DATA",4,datat);
+  send("/~/server/server/MYDEVICE/HUMIDITY/DATA",4,datah);
+
+}
+
+String send(String url,int ty, String rep) {
+
+  Serial.print("connecting to ");
+  Serial.println(host);
+  
+  // Use WiFiClient class to create TCP connections
+  WiFiClient client;
+  
+  if (!client.connect(host, httpPort)) {
+    Serial.println("connection failed");
+    return "error";
+  }
+ 
+  
+  //Serial.print("Requesting URL: ");
+  //Serial.println(url);
+  
+  // This will send the request to the server
+
+  String req = String()+"POST " + url + " HTTP/1.1\r\n" +
+               "Host: " + host + "\r\n" +
+               "X-M2M-Origin: " + origin + "\r\n" +
+               "Content-Type: application/json;ty="+ty+"\r\n" +
+               "Content-Length: "+ rep.length()+"\r\n"
+               "Connection: close\r\n\n" + 
+               rep;
+
+  Serial.println(req+"\n");
+  
+  client.print(req);
+               
+  unsigned long timeout = millis();
+  while (client.available() == 0) {
+    if (millis() - timeout > 5000) {
+      Serial.println(">>> Client Timeout !");
+      client.stop();
+      return "error";
+    }
+  }
+  
+  // Read all the lines of the reply from server and print them to Serial
+  String res="";
+  if(client.available()){
+    res = client.readStringUntil('\r');
+    Serial.print(res);
+  }
+  while(client.available()){
+    String line = client.readStringUntil('\r');
+    Serial.print(line);
+  }
+  
+  Serial.println();
+  Serial.println("closing connection");
+  Serial.println();
+  return res;
+}
+
+
+