ESP8266 is capable to transmit and receive data via WiFi. ESP8266 can be configured or set as a basic server and access point. On this guide, we will be going to configure the ESP8266 to talk to each other without the help of the network router. As you can see the illustration there will be two devices act as INPUT and OUTPUT the INPUT will be the sensor and the OUTPUT will the OLED display module and the sensor we will going used for this demonstration is the Microchip MCP9808 temperature sensor.
The Microchip MCP9808 temperature sensor is driven by i2c that can sense temperature between -20°C ~ +100°C converted to digital signals integrated accuracy of ±0.25°C/±0.5°C (typical/maximum). The MCP9808 temperature sensor is flexible the registers featuring a selectable configuration such as shutdown or low-power modes, temperature alerts, and critical output limits when the temperature changes the limits the sensor can send an alert signal to the MCU this is feature is an advantage to managing cooling systems. For more readings about the MCP9808 please refer to the datasheet below.
Required Component for this Lab
- Arduino IDE | Atmel Studio | Energia | Processing
- ESP8266 / ESP32 Module or Board
- MCP9808 Sensor /Module
- Capacitors (See below required value)
- Resistors (See below required value)
- TTL USB UART (Optional if your using s MCU USB/UART integrated)
- PCB Designer (Circuit simulator to PCB Layout / Circuitmaker / Autodesk Eagle / Fritzing )
Wiring Guide for the Station / Client
Wiring Guide for the Access Point / Server
Source Code for the Client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
/* ESP8266 P2P with MCP9808 i2c Temperature Sensor * CONFIGURE AS CLIENT */ #include <ESP8266WiFi.h> #include <U8g2lib.h> #include <Wire.h> #include "Adafruit_MCP9808.h" Adafruit_MCP9808 tempsensor = Adafruit_MCP9808(); //Lobrary can downloaded below U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 5, /* data=*/ 4); const char *ssid = "yourssid"; const char *password = "yourpassword"; int outvalc = 0; // the value that sent to reciever void setup() { Serial.begin(115200); while(!Serial) //Wait for the serial to bigin Serial.println("14CORE | MCP9808 - P2P READING TEST") delay(1000); Serial.println("14CORE | Initializing as client") delay(1000); // Set ESP8266 as Client WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); Serial.begin(115200); // 0x18 default i2C Address if (!tempsensor.begin(0x18)){ Serial.println("Error: Please check the sensor connection..."); while(1); } Serial.println("MCP9808 SENSOR FOUND, Initializing..."); tempsensor.setResolution(3); delay(1500); Serial.println(" Initializing Display Panel...") delay(1000); u8g2.begin(); u8g2.setFont(u8g2_font_logisoso62_tn); u8g2.setFontMode(0); // Set to enable transparent mode } void loop() { tempsensor.wake();// Wake up, MCP9880 ~200 mikro Ampere ready to get the value Serial.println (tempsensor.getResolution()); float c = tempsensor.readTempC(); //float f = tempsensor.readTempF(); outvalc = map(c, 3, 1023, 0, 999); // Reading the celcius value //outvalf = map(f); char intToPrint[5]; itoa(outval, intToPrint, 10); //integer to string conversion for OLED library u8g2.firstPage(); u8g2.drawUTF8(0, 64, intToPrint); u8g2.nextPage(); // Set WiFiClient class to create TCP connection WiFiClient client; const char * host = "192.168.10.10"; //Host IP Address (192.168.10.1) const int httpPort = 80; //Host Port if (!client.connect(host, httpPort)) { Serial.println(" >>>>> Failed to Connect....."); return; } String url = "/data/"; url += "?sensor_reading="; //Value to be sent to the server url += intToPrint; // Set to send a request to the server client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + " >>>>> Closing Connection : \r\n\r\n"); unsigned long timeout = millis(); while (client.available() == 0) { if (millis() - timeout > 6000) { Serial.println(" >>>>>> Timeout......"); client.stop(); return; } } Serial.println(); Serial.println(" >>>>>>> Disconnecting....."); Serial.println(); Serial.println(); Serial.println(); delay(500); } |
Source Code for the Server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include <ESP8266WiFi.h> #include <ESP8266WebServer.h> #include <U8g2lib.h> U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 5, /* data=*/ 4); const char *ssid = "yourssid"; //Server SSI const char *password = "yourpassword"; //Server Password ESP8266WebServer server(80); // set to port 80 as server void sentvar() { if (server.hasArg("sensor_reading")) { // Value that sent from the client int readingInt = server.arg("sensor_reading").toInt(); char readingToPrint[5]; itoa(readingInt, readingToPrint, 10); //set the value to string conversion u8g2.firstPage(); u8g2.drawUTF8(0, 64, readingToPrint); u8g2.nextPage(); server.send(200, "text/html", "Value Received"); } } void setup() { Serial.println("14CORE | MCP9808 - P2P READING TEST") delay(1000); Serial.println("14CORE | Initializing as server") delay(1000); u8g2.begin(); u8g2.setFont(u8g2_font_logisoso62_tn); u8g2.setFontMode(0); //Set to enable transparent mode, which is faster WiFi.softAP(ssid, password); //Start Soft AP IPAddress myIP = WiFi.softAPIP(); server.on("/data/", HTTP_GET, sentvar); // Set to the server receives a request with /data/ in the string then run the sentvar function server.begin(); } void loop() { server.handleClient(); } |
Downloads and Datasheets
- Guide for the SSD1306 128X64 OLED Display Module
- Download the SSD1306 128X64 OLED Display Library here
- Download the MCP9808 Code Library
- Download the MCP9808 Temperature Sensor Datasheet