Stap 2: Zet de code in
De code zie je doet de volgende dingen:
-Met een wifinetwerk verbindt
-Krijgt een IP-adres
-Via TCP verbinding te maken met een server (als een client)
-Opzetten van een TCP server van onze eigen.
-Lezen en weergeven van de temperatuurmetingen van uw sensor.
-Lezen en weergeven van de huidige tijd.
-Duw de temperatuurmetingen naar de server.
-Het IP-adres van de server weergeven.
Plaats de volgende code in uw IDE, compileren en uploaden naar je arduino.
<p>/************************************************************<br>ESP8266_Shield_Demo.h SparkFun ESP8266 AT library - Demo Jim Lindblom @ SparkFun Electronics Original Creation Date: July 16, 2015 <a href="https://github.com/sparkfun/SparkFun_ESP8266_AT_Arduino_Library" rel="nofollow"> https://github.com/sparkfun/SparkFun_ESP8266_AT_A...></p><p>This example demonstrates the basics of the SparkFun ESP8266 AT library. It'll show you how to connect to a WiFi network, get an IP address, connect over TCP to a server (as a client), and set up a TCP server of our own.</p><p>Development environment specifics: IDE: Arduino 1.6.5 Hardware Platform: Arduino Uno ESP8266 WiFi Shield Version: 1.0</p><p>This code is released under the MIT license.</p><p>Distributed as-is; no warranty is given. ************************************************************</p><p>///////////////////// // Library Includes // ////////////////////// // SoftwareSerial is required (even you don't intend on // using it). #include #include </p><p>////////////////////////////// // WiFi Network Definitions // ////////////////////////////// // Replace these two character strings with the name and // password of your WiFi network. const char mySSID[] = "*****"; /Enter your SSID const char myPSK[] = "*****";</p><p> /Enter your Password ////////////////////////////// // ESP8266Server definition // ////////////////////////////// // server object used towards the end of the demo. // (This is only global because it's called in both setup() // and loop()). ESP8266Server server = ESP8266Server(80);</p><p>////////////////// // HTTP Strings // ////////////////// const char destServer[] = "tmce.io.tudelft.nl"; const String htmlHeader = "HTTP/1.1 200 OK\r\n" "Content-Type: text/html\r\n" "Connection: close\r\n\r\n" "\r\n" " \r\n";</p><p>const String httpRequest = "GET <a href="http://tmce.io.tudelft.nl/test/time.php" rel="nofollow"> <a href="http://tmce.io.tudelft.nl/test/time.php" rel="nofollow"> http://tmce.io.tudelft.nl/test/time.php </a> </a> HTTP/1.1\n" "Host: tmce.io.tudelft.nl\n" "Connection: close\n\n";</p><p>// All functions called from setup() are defined below the // loop() function. They modularized to make it easier to // copy/paste into sketches of your own. void setup() { // Serial Monitor is used to control the demo and view // debug information. { Serial.begin(9600); }</p><p> serialTrigger(F("Press any key to begin."));</p><p> // initializeESP8266() verifies communication with the WiFi // shield, and sets it up. initializeESP8266();</p><p> // connectESP8266() connects to the defined WiFi network. connectESP8266();</p><p> // displayConnectInfo prints the Shield's local IP // and the network it's connected to. displayConnectInfo();</p><p> serialTrigger(F("Press any key to connect client.")); clientDemo(); serialTrigger(F("Press any key to test server.")); serverSetup(); }</p><p>double Thermister(int RawADC) { //Function to perform the fancy math of the Steinhart-Hart equation double Temp; Temp = log(((10240000/RawADC) - 10000)); Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp ); Temp = Temp - 273.15; // Convert Kelvin to Celsius return Temp; }</p><p>void loop() { serverDemo(); int hours; // you must add these variables int minutes; // to store the time</p><p>{ // GET THE TIME clientDemoTime(&hours,&minutes); // Show it on the serial monitor. Serial.print("Server Time is: "); Serial.print(hours); Serial.print(":"); Serial.println(minutes);</p><p> delay(5000); // delay - don't oveload my server... } int val; //Create an integer variable val=analogRead(0); //Read the analog port 0 and store the value in val double temp; temp=Thermister(val); ; Serial.println("And the temperature is: "); Serial.println(temp); //Print the value to the serial port Serial.println( "degrees Celsius"); delay(1000); //Wait one second before we do it again }</p><p>void initializeESP8266() { // esp8266.begin() verifies that the ESP8266 is operational // and sets it up for the rest of the sketch. // It returns either true or false -- indicating whether // communication was successul or not. // true int test = esp8266.begin(); if (test != true) { Serial.println(F("Error talking to ESP8266.")); errorLoop(test); } Serial.println(F("ESP8266 Shield Present")); }</p><p>void connectESP8266() { // The ESP8266 can be set to one of three modes: // 1 - ESP8266_MODE_STA - Station only // 2 - ESP8266_MODE_AP - Access point only // 3 - ESP8266_MODE_STAAP - Station/AP combo // Use esp8266.getMode() to check which mode it's in: int retVal = esp8266.getMode(); if (retVal != ESP8266_MODE_STA) { // If it's not in station mode. // Use esp8266.setMode([mode]) to set it to a specified // mode. retVal = esp8266.setMode(ESP8266_MODE_STA); if (retVal < 0) { Serial.println(F("Error setting mode.")); errorLoop(retVal); } } Serial.println(F("Mode set to station"));</p><p> // esp8266.status() indicates the ESP8266's WiFi connect // status. // A return value of 1 indicates the device is already // connected. 0 indicates disconnected. (Negative values // equate to communication errors.) retVal = esp8266.status(); if (retVal <= 0) { Serial.print(F("Connecting to ")); Serial.println(mySSID); // esp8266.connect([ssid], [psk]) connects the ESP8266 // to a network. // On success the connect function returns a value >0 // On fail, the function will either return: // -1: TIMEOUT - The library has a set 30s timeout // -3: FAIL - Couldn't connect to network. retVal = esp8266.connect(mySSID, myPSK); if (retVal < 0) { Serial.println(F("Error connecting")); errorLoop(retVal); } } }</p><p>void displayConnectInfo() { char connectedSSID[24]; memset(connectedSSID, 0, 24); // esp8266.getAP() can be used to check which AP the // ESP8266 is connected to. It returns an error code. // The connected AP is returned by reference as a parameter. int retVal = esp8266.getAP(connectedSSID); if (retVal > 0) { Serial.print(F("Connected to: ")); Serial.println(connectedSSID); }</p><p> // esp8266.localIP returns an IPAddress variable with the // ESP8266's current local IP address. IPAddress myIP = esp8266.localIP(); Serial.print(F("My IP: ")); Serial.println(myIP); }</p><p>void clientDemo() { // To use the ESP8266 as a TCP client, use the // ESP8266Client class. First, create an object: ESP8266Client client;</p><p> // ESP8266Client connect([server], [port]) is used to // connect to a server (const char * or IPAddress) on // a specified port. // Returns: 1 on success, 2 on already connected, // negative on fail (-1=TIMEOUT, -3=FAIL). int retVal = client.connect(destServer, 80); if (retVal <= 0) { Serial.println(F("Failed to connect to server.")); return; }</p><p> // print and write can be used to send data to a connected // client connection. client.print(httpRequest);</p><p> // available() will return the number of characters // currently in the receive buffer. while (client.available()) Serial.write(client.read()); // read() gets the FIFO char // connected() is a boolean return value - 1 if the // connection is active, 0 if it's closed. if (client.connected()) client.stop(); // stop() closes a TCP connection. }</p><p>void serverSetup() { // begin initializes a ESP8266Server object. It will // start a server on the port specified in the object's // constructor (in global area) server.begin(); Serial.print(F("Server started! Go to ")); Serial.println(esp8266.localIP()); Serial.println(); }</p><p>void serverDemo() { // available() is an ESP8266Server function which will // return an ESP8266Client object for printing and reading. // available() has one parameter -- a timeout value. This // is the number of milliseconds the function waits, // checking for a connection. ESP8266Client client = server.available(500); if (client) { Serial.println(F("Client Connected!")); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply if (c == '\n' && currentLineIsBlank) { Serial.println(F("Sending HTML page")); // send a standard http response header: client.print(htmlHeader); String htmlBody; // output the value of each analog input pin for (int a = 0; a < 1; a++) { htmlBody += "A"; htmlBody += String(a); htmlBody += ": "; htmlBody += String(Thermister(analogRead(a)));</p><p> //htmlBody += Serial.println(temp); htmlBody += "<br>\n"; } htmlBody += "</p><p>\n"; client.print(htmlBody); break; } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); Serial.println(F("Client disconnected")); } }</p><p>// errorLoop prints an error code, then loops forever. void errorLoop(int error) { Serial.print(F("Error: ")); Serial.println(error); Serial.println(F("Looping forever.")); for (;;) ; }</p><p>// serialTrigger prints a message, then waits for something // to come in from the serial port. void serialTrigger(String message) { Serial.println(); Serial.println(message); Serial.println(); while (!Serial.available()) ; while (Serial.available()) Serial.read(); }</p>