Stap 2: Broncode van toepassing
Op de ingesloten kant ik gewend energia platform ontwikkelen toepassingscodes. Zoals u weet of niet "Energia is een open-source elektronica prototyping platform gestart door Robert Wessels in januari van 2012 met als doel om de bedrading en Arduino kader aan de Texas instrumenten MSP430 gebaseerd LaunchPad." Het is dus arduino schets gebaseerd, gratis en eenvoudig te gebruiken platform. Energia bijna volledig compatibel met arduino voorbeelden. In een korte tijd kunt u uw kandidaatstelling van Arduino TI Launchpads. U kunt controleren hier ondersteund kits, voorbeelden en meer informatie.
Hoe dan ook, zoals ik al zei, ik schreef mijn codes energia. Het is gebaseerd op voorbeelden codes. U kunt gemakkelijk te begrijpen en veranderen wat je wilt. U ziet hieronder codes.
<p style="margin-left: 20.0px;">/***************************************************************************<br>* Designer: Erhan YILMAZ * * Application: Tiva Connected Launchpad Web Server Application * * Date: 19-08-2015 * * Version: 1.0 * * Description: Web server application with Tiva connected * * launchpad. Codes writen on energia * * *************************************************************************/</p><p>#define LED1 PN_1 #define LED2 PN_0 #define LED3 PF_4 #define LED4 PF_0</p><p>#include "Ethernet.h" // Prototypes void printEthernetData(); void GetSwitchState(EthernetClient); void SetLEDStates(EthernetClient, String);</p><p>EthernetServer server(80); int statusConfig = 0;</p><p>void setup() { Serial.begin(115200); </p><p> pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT); pinMode(LED3, OUTPUT); pinMode(LED4, OUTPUT); pinMode(PUSH1, INPUT_PULLUP); pinMode(PUSH2, INPUT_PULLUP);</p><p> Serial.println("Connecting to Ethernet...."); </p><p> Ethernet.begin(0); // For static ip use below code /* IPAddress ip = IPAddress(10,2,7,158); IPAddress dns = IPAddress(10,2,7,200); IPAddress gw = IPAddress(10,2,7,254); IPAddress mask = IPAddress(255,255,0,0); Ethernet.begin(0, ip, dns, gw); */ server.begin(); printEthernetData(); }</p><p>EthernetClient client; String HTTP_req; // stores the HTTP request void loop() { client = server.available();</p><p> if (client) { // if you get a client, Serial.print("new client on port "); // print a message out the serial port Serial.println(client.port()); String currentLine = ""; // make a String to hold incoming data from the client boolean newConnection = true; // flag for new connections unsigned long connectionActiveTimer; // will hold the connection start time</p><p> boolean currentLineIsBlank = true; while (client.connected()) { // loop while the client's connected if (newConnection) { // it's a new connection, so connectionActiveTimer = millis(); // log when the connection started newConnection = false; // not a new connection anymore } if (!newConnection && connectionActiveTimer + 1000 < millis()) { // if this while loop is still active 1000ms after a web client connected, something is wrong break; // leave the while loop, something bad happened }</p><p> if (client.available()) { // if there's bytes to read from the client, char c = client.read(); // read a byte, then // This lockup is because the recv function is blocking. Serial.print(c); HTTP_req += c; // save the HTTP request 1 char at a time // last line of client request is blank and ends with \n // respond to client only after last line received if (c == '\n' && currentLineIsBlank) { // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: keep-alive"); client.println(); // AJAX request for switch state if (HTTP_req.indexOf("ajax_switch") > -1) { // read switch state and send appropriate paragraph text GetSwitchState(client); SetLEDStates(client, HTTP_req);</p><p> } else { // HTTP request for web page // send web page - contains JavaScript with AJAX calls client.println(""); client.println(" "); client.println(" "); client.println(" </p><p>"); client.println(" </p><p>"); client.println("</p><p>"); client.println(" </p><center>"); client.println(" <h1>Erhan YILMAZ AJAX Test Application</h1>"); client.println(" "); client.println(""); client.println(" "); client.println(""); client.println(" <p><button>LED1</button> "); client.println("<button>LED2</button> "); client.println("<button>LED3</button> "); client.println("<button>LED4</button> </p><p>"); client.println(" </p><center>"); client.println(""); client.println(""); client.println(""); client.println(""); client.println("</center><p>"); client.println("</p></center><p>"); client.println("</p><p>"); } // display received HTTP request on serial port Serial.print(HTTP_req); HTTP_req = ""; // finished with request, empty string break; } // every line of text received from the client ends with \r\n if (c == '\n') { // last character on line of received text // starting new line with next character read currentLineIsBlank = true; } else if (c != '\r') { // a text character was received from client currentLineIsBlank = false; } } } // close the connection: client.stop(); Serial.println("client disonnected"); } }</p><p>// send the state of the switch to the web browser void GetSwitchState(EthernetClient cl) { char x=0;</p><p> if (!digitalRead(PUSH1)) x+=1; if (!digitalRead(PUSH2)) x+=2; switch(x) { case 0: cl.println("S1:OFF S2:OFF"); break;</p><p> case 1: cl.println("S1:ON S2:OFF"); break;</p><p> case 2: cl.println("S1:OFF S2:ON"); break;</p><p> default: cl.println("S1:ON S2:ON"); break; } }</p><p>void SetLEDStates(EthernetClient cl, String HTTP_req) {</p><p> if (HTTP_req.indexOf("LED1ON") > -1) { digitalWrite(LED1, HIGH); cl.println("LED1ON"); } else { digitalWrite(LED1, LOW); cl.println("LED1OFF"); }</p><p> if (HTTP_req.indexOf("LED2ON") > -1) { digitalWrite(LED2, HIGH); cl.println("LED2ON"); } else { digitalWrite(LED2, LOW); cl.println("LED2OFF"); }</p><p> if (HTTP_req.indexOf("LED3ON") > -1) { digitalWrite(LED3, HIGH); cl.println("LED3ON"); } else { digitalWrite(LED3, LOW); cl.println("LED3OFF"); }</p><p> if (HTTP_req.indexOf("LED4ON") > -1) { digitalWrite(LED4, HIGH); cl.println("LED4ON"); } else { digitalWrite(LED4, LOW); cl.println("LED4OFF"); } }</p><p>void serialEvent() { while (Serial.available()) { // get the new byte: char inChar = (char)Serial.read(); } }</p><p>void printEthernetData() { // print your IP address: Serial.println(); Serial.println("IP Address Information:"); IPAddress ip = Ethernet.localIP(); Serial.print("IP Address:\t"); Serial.println(ip);</p><p> // print your MAC address:</p><p> IPAddress subnet = Ethernet.subnetMask(); Serial.print("NetMask:\t"); Serial.println(subnet);</p><p> // print your gateway address: IPAddress gateway = Ethernet.gatewayIP(); Serial.print("Gateway:\t"); Serial.println(gateway);</p><p> // print your gateway address: IPAddress dns = Ethernet.dnsServerIP(); Serial.print("DNS:\t\t"); Serial.println(dns);</p><p>}</p>