// Simple Webserver // Connect your Arduino to the Wifi network PFE // and enter http://192.168.4.1 in your browser. #include #include #include #include // use this with Arduino Mega (or similar) // with more than one serial port //#define WifiSerial Serial1 // for Arduino Uno or Intel Gailileo // with just one serial port #define WifiSerial Serial #define SEND_BUFFER_MAX 2048 #define RECV_BUFFER_MAX 1024 #define TEMPERATURE_PIN 53 #define TRIGGER_PIN 42 #define ECHO_PIN 43 /* data wire which the temperature sensor is connected to */ OneWire oneWire(TEMPERATURE_PIN); /* access to the temperature sensor */ DallasTemperature tempSensor(&oneWire); /* LCD control object */ LiquidCrystal_I2C lcd (0x27, 16, 2) ; long temperature = 0; /* current client id */ int clientId_g = 0; /* size of the send buffer */ int sendBufferSz_g = 0; /* the global send buffer */ char sendBuffer_g[SEND_BUFFER_MAX]; /* * Issue a command to the wifi module. */ bool issueCommand(String command) { WifiSerial.println(command); delay(10); if (!WifiSerial.find("OK")) { return false; } return true; } /* * Finally write the HTML output to the network. */ int HTMLFlush() { String header; int sz; header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n"; header += "Connection: close\r\nContent-Length: "; header += sendBufferSz_g; header += "\r\n\r\n"; sz = sendBufferSz_g + header.length(); WifiSerial.print("AT+CIPSEND="); WifiSerial.print(clientId_g); WifiSerial.print(","); WifiSerial.println(sz); delay(20); WifiSerial.print(header); WifiSerial.write(sendBuffer_g, sendBufferSz_g); delay(100); sendBufferSz_g = 0; } /* * Use the methods below to output your HTML site. */ void HTMLSend(const char *data, int sz) { if (sendBufferSz_g + sz >= SEND_BUFFER_MAX) { return; } memcpy(&sendBuffer_g[sendBufferSz_g], data, sz); sendBufferSz_g += sz; } void HTMLSend(String s) { HTMLSend(s.c_str(), s.length()); } void HTMLSend(int i) { String s = String(i); HTMLSend(s); } void HTMLSend(long i) { String s = String(i); HTMLSend(s); } void HTMLSend(char *str) { HTMLSend(str, strlen(str)); } void setup() { /* initialize serial connection to Wifi chip */ WifiSerial.begin(115200); WifiSerial.setTimeout(5000); /* initialize the LCD display */ lcd.init(); lcd.backlight(); lcd.clear(); bool success; /* set mode to wifi access point */ success = issueCommand("AT+CWMODE=2"); if (!success) { lcd.println("AT+CWMODE failed"); return; } /* enable wifi access point with SSID "PFE" and disabled encryption */ success = issueCommand("AT+CWSAP=\"PFE\",\"\",1,0"); if (!success) { lcd.println("AT+CWSAP failed"); return; } /* enable multiple TCP/UDP connections */ success = issueCommand("AT+CIPMUX=1"); if (!success) { lcd.println("AT+CIPMUX failed"); return; } /* enable TCP server on port 80 */ success = issueCommand("AT+CIPSERVER=1,80"); if (!success) { lcd.println("AT+CIPSERVER failed"); return; } /* configure pins */ pinMode(TRIGGER_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); /* init the temperature sensor */ // tempSensor.begin(); } void do_measurements() { // TODO: Perform the measurement here tempSensor.requestTemperatures(); temperature = tempSensor.getTempCByIndex(0); } void display_page(char * uri) { // TODO: Output your HTML site here HTMLSend("

My Arduino

\r\n"); HTMLSend("

Requested URI was: "); HTMLSend(uri); HTMLSend("

\r\n"); HTMLSend("

Temperature is: "); HTMLSend("Undefined"); // HTMLSend(temperature); HTMLSend("

\r\n"); } void loop() { char recvBuffer[RECV_BUFFER_MAX]; char *requestLine, *method, *uri; int len; do_measurements(); /* wait for connection from a client */ if (!WifiSerial.findUntil("+IPD,", "\r")) { return; } /* retrieve the current client id and the * length of the transmitted data */ clientId_g = WifiSerial.parseInt(); WifiSerial.findUntil(",", "\r"); len = WifiSerial.parseInt(); /* drop the colon */ WifiSerial.read(); if (len >= RECV_BUFFER_MAX) { /* discard the serial input */ for (int i = 0; i < len; i++) { WifiSerial.read(); } return; } else { WifiSerial.readBytes((char *) recvBuffer, len); } /* parse request line */ requestLine = (char *) &recvBuffer; method = strtok(requestLine, " "); uri = strtok(NULL, " "); if (strcmp(method, "GET") != 0) { /* we don't support other methods than GET */ return; } /* prepare page body */ HTMLSend("\r\n"); HTMLSend("My Arduino\r\n"); /* display your own page */ display_page(uri); /* finish page body */ HTMLSend("\r\n"); /* send answer */ HTMLFlush(); }