Webserver bearbeitet Anfragen des 'NetIO Controller' clients
und gsteuert Homeautomation und VRC Monitor (Heizungssteuerung)
Siehe zugehöriges NetIO iOS App
Projekt
Sketch - Code des Webservers :
/* Project: AF104 Homeautomation Author: Michael Gries Creation: 2013-11-22 Modified: 2014-02-09 some code segments derived from folowing Arduino examples - Arduino Yun Bridge - SoftwareSerial uses Bridge library to access the digital and analog pins on the board through REST calls. It demonstrates how you can create your own API when using REST style calls through the browser. via DNS http://gries.spdns.de WIFI: 192.168.178.10 Possible REST commands created in this shetch: * "192.168.178.10/arduino/digital/13" -> digitalRead(13) * "192.168.178.10/arduino/digital/13/1" -> digitalWrite(13, HIGH) * "192.168.178.10/arduino/analog/2/123" -> analogWrite(2, 123) * "192.168.178.10/arduino/analog/2" -> analogRead(2) * "192.168.178.10/arduino/mode/13/input" -> pinMode(13, INPUT) * "192.168.178.10/arduino/mode/13/output" -> pinMode(13, OUTPUT) * "192.168.178.10/arduino/serial/2" -> returns Buad rate from mySerial * "192.168.178.10/arduino/serial/2/read" -> returns received data from rxPin buffer * "192.168.178.10/alive/0" -> returns "lamp_on" for "Icon_lamp_on.png" in NetIO Controller App Note: Commands like 'alive' can only be processed if and additional '/' followed by one or more digits are typed in Test REST commands: http://gries.spdns.de/arduino/help/0 http://gries.spdns.de/arduino/alive/0 http://gries.spdns.de/arduino/digital/13/1 http://gries.spdns.de/arduino/serial/2 for further information see: Bridge-Tutorial http://arduino.cc/en/Tutorial/Bridge Bridge-Library http://arduino.cc/en/Reference/YunBridgeLibrary SoftwareSerial-Library http://arduino.cc/en/Reference/SoftwareSerial */ #define VERSION "14.2.9" #include#include #include #include #define rxPin 11 #define txPin 10 #define mySerialBaud 19200 #define commandVCRdebugBegin "DDD" #define commandVCRdebugEnd "d" #define commandVCRtraceBegin "TTT" #define commandVCRtraceEnd "t" SoftwareSerial mySerial(rxPin,txPin); // RX, TX // Listen on http port:80, the webserver on the Yun // will forward there all the HTTP requests for us. YunServer server; String sConsoleRead; String sConsoleWrite; String sSerial2Read; String sSerial2Write; void setup() { // Bridge startup pinMode(rxPin, INPUT); pinMode(txPin, OUTPUT); pinMode(8,OUTPUT); pinMode(9,OUTPUT); pinMode(13,OUTPUT); digitalWrite(8, LOW); digitalWrite(9, LOW); 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(); // Bridge interface (Console) copied from ConsoleRead example Console.begin(); // Wait for Console port to connect //while (!Console); Console.println(">>>: Console active ..."); // Open serial communications and wait for port to open: /* Serial.begin(115200); while (!Serial) {; // wait for serial port to connect. } */ // set the data rate for the SoftwareSerial port mySerial.begin(mySerialBaud); mySerial.print("mySerial set to "); mySerial.print(mySerialBaud); mySerial.println(" Baud"); sSerial2Read = "12345678901234567890123456789012345678901234567890", sSerial2Write = "12345678901234567890123456789012345678901234567890", sSerial2Write = commandVCRdebugBegin; mySerial.println(sSerial2Write); } 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. mySerial.listen(); int bytesRec =0; bytesRec = mySerial.available(); if (bytesRec > 0) { //mySerial.print(sSerial2Read); //delay(4000); sSerial2Read = bytesRec + "> "; } while (mySerial.available() > 0) { char ch = mySerial.read(); sSerial2Read += ch; } client.stop(); } delay(50); // Poll every 50ms // Loop test /* if (mySerial.available()){ //mySerial.write(mySerial.read()); char c = mySerial.read(); mySerial.println(c); } */ } void process(YunClient client) { // read the command String command = client.readStringUntil('/'); // until timeout (1000ms) sConsoleRead = command; /* for debug reasons only client.println(">>>>"); client.println(command); client.println("<<<<"); */ // is "help" command? if (command == "help") { helpCommand(client); } // is "version" command? if (command == "version") { versionCommand(client); } // is "alive" command? if (command == "alive") { aliveCommand(client); } // is "toggle" command? if (command == "toggle") { toggleCommand(client); } // is "digital" command? if (command == "digital") { digitalCommand(client); } // is "analog" command? if (command == "analog") { analogCommand(client); } // is "mode" command? if (command == "mode") { modeCommand(client); } // is "serial" command? if (command == "serial") { serialCommand(client); } } void helpCommand(YunClient client) { client.println(F(" /arduino/help/0")); client.println(F(" /arduino/version/0")); client.println(F(" /arduino/alive/0")); client.println(F(" /arduino/toggle/13")); client.println(F(" /arduino/digital/13")); client.println(F(" /arduino/serial/2")); } void versionCommand(YunClient client) { client.println(VERSION); } void aliveCommand(YunClient client) { // Send feedback to client sConsoleWrite = "lamp_on"; client.print(sConsoleWrite); processConsoleTx(sConsoleWrite); } void serialCommand(YunClient client) { int port, value; // Read port number port = client.parseInt(); sConsoleRead += "/"; sConsoleRead += port; // If the next character is a '/' it means we have an URL // with a value like: "/serial/2/read" if (client.read() == '/') { String com = client.readStringUntil('\r'); if (com == "baud") { // Send feedback to client client.println(F("19200")); return; } if (com == "read") { // Send feedback to client client.println(sSerial2Read); return; } if (com == "write") { mySerial.println(sSerial2Write); // Send feedback to client client.println(sSerial2Write); return; } if (com == "help") { mySerial.println("?"); // Send feedback to client client.println("?"); return; } if (com == "debug") { mySerial.println("DDD"); // Send feedback to client client.println("DDD"); return; } if (com == "undebug") { mySerial.println("d"); // Send feedback to client client.println("d"); return; } if (com == "trace") { mySerial.println("TTT"); // Send feedback to client client.println("TTT"); return; } if (com == "untrace") { mySerial.println("t"); // Send feedback to client client.println("t"); return; } } else { // Wenn kein weiterer parameter dann Ausgabe Baud rate client.print("mySerial set to "); client.print(mySerialBaud); client.println(" Baud"); return; } } void toggleCommand(YunClient client) { int pin, value, toggleValue; // Read pin number pin = client.parseInt(); // get pin value value = digitalRead(pin); if (value == 0) { toggleValue = 1; digitalWrite(pin, toggleValue); } else { toggleValue = 0; digitalWrite(pin, toggleValue); } // Send feedback to client client.print(F("Pin D")); client.print(pin); client.print(F(" set to analog ")); client.println(toggleValue); sConsoleWrite = "Digital Port Pin "; sConsoleWrite += pin; sConsoleWrite += " set to "; sConsoleWrite += toggleValue; // Update datastore key with the current pin value String key = "D"; key += pin; Bridge.put(key, String(toggleValue)); processConsoleTx(sConsoleWrite); } void digitalCommand(YunClient client) { int pin, value; // Read pin number pin = client.parseInt(); sConsoleRead += "/"; sConsoleRead += pin; // If the next character is a '/' it means we have an URL // with a value like: "/digital/13/1" if (client.read() == '/') { value = client.parseInt(); digitalWrite(pin, value); sConsoleRead += "/"; sConsoleRead += value; } else { value = digitalRead(pin); } // Send feedback to client if (value == 0) { sConsoleWrite = "lamp_off"; mySerial.listen(); mySerial.println("lamp_off"); if (mySerial.isListening()) { //digitalWrite(13,1); } else { digitalWrite(13,0); } /* //digitalWrite(13,0); int bytesReceived =0; sSerial2Read = ">>> "; bytesReceived = mySerial.available(); while (mySerial.available() > 0) { char c = mySerial.read(); sSerial2Read += c; } if (bytesReceived > 0) { digitalWrite(13,1); mySerial.println(F(">")); mySerial.print(sSerial2Read); delay(4000); digitalWrite(13,1); //mySerial.write(mySerial.read()); } */ /* while (mySerial.available() > 0) { char inByte = mySerial.read(); } */ //char c = mySerial.read(); //int bytesSent = mySerial.write(48+bytesReceived); //bytesReceived = bytesReceived+48; //mySerial.println(bytesReceived); //mySerial.println(bytesSent); //client.print(F("lamp_off")); } else { sConsoleWrite = "lamp_on"; mySerial.println("lamp_on"); //client.print(F("lamp_on")); } // Update datastore key with the current pin value String key = "D"; key += pin; Bridge.put(key, String(value)); client.print(sConsoleWrite); processConsoleTx(sConsoleWrite); } void analogCommand(YunClient client) { int pin, value; // Read pin number pin = client.parseInt(); // If the next character is a '/' it means we have an URL // with a value like: "/analog/5/120" if (client.read() == '/') { // Read value and execute command value = client.parseInt(); analogWrite(pin, value); // Send feedback to client client.print(F("Pin A")); client.print(pin); client.print(F(" set to analog ")); client.println(value); // Update datastore key with the current pin value String key = "A"; key += pin; Bridge.put(key, String(value)); } else { // Read analog pin value = analogRead(pin); // Send feedback to client client.print(F("Pin A")); client.print(pin); client.print(F(" reads analog ")); client.println(value); // Update datastore key with the current pin value String key = "A"; key += pin; Bridge.put(key, String(value)); } } void modeCommand(YunClient client) { int pin; // Read pin number pin = client.parseInt(); // If the next character is not a '/' we have a malformed URL if (client.read() != '/') { client.println(F("error")); return; } String mode = client.readStringUntil('\r'); if (mode == "input") { pinMode(pin, INPUT); // Send feedback to client client.print(F("Pin D")); client.print(pin); client.print(F(" configured as INPUT!")); return; } if (mode == "output") { pinMode(pin, OUTPUT); // Send feedback to client client.print(F("Pin D")); client.print(pin); client.print(F(" configured as OUTPUT!")); return; } client.print(F("error: invalid mode ")); client.print(mode); } void processConsoleRx(String ascii) { if (Console.available() > 0) { Console.print("<<<: "); Console.println(ascii); } } void processConsoleTx(String ascii) { if (Console.available() > 0) { processConsoleRx(sConsoleRead); Console.print(">>>: "); Console.println(sConsoleWrite); } }