You are on page 1of 4

/*****************************************************************

* Board: NodeMCU 1.0 (ESP-12E Module) (ESP8266MOD)


* Developed by: Akshay P Daga Email: apdaga@gmail.com
*
* Description:
* In this program,
* Analog Temperature sensor (LM35) is connected to the NodeMCU 1.0
* (ESP-12E Module) (ESP8266MOD) board
* and It's output will be send to Gateway (Rasperry Pi 3)
* Temperature data will be logged on the RPi3 and
* then send further to https://thinkspeak.com server / cloud by RPi3
*
* Code For Logging temperature data in txt file and
* sending it further to thinkspeak cloud is written in python (ts.py)
* and running on RPi3 (Not included in this file)
*
*
*
* ESP8266MOD NodeMCU 1.0 (ESP-12E Module) Board is used to connect to Internet
* and send data to Raspberry Pi3
*
* Temperature data will be logged in the text file on RPi3
* and same data will be send to thinkspeak server
* where Output is shown using GRAPH on thinkspeak server.
*
*
* NOTE:
* Internal 10 bit ADC (A0) is used to read the Analog output of the Temperature
Sensor.
*
* Temperature data shown on thinkspeak server grapth will be subset of
* Logged temperature data in text file on RPi3.
* (Because free version of thinkspeak server accept data with the gap of 16 sec
interval)
*
********************************************************************/
/****************************************************************
* STEPS to Configure NodeMCU with Arduino IDE
* 1. Open Arduino IDE
* Go To File > Preferances > Put following URL to "Additional Board Manager
URL's"
* http://arduino.esp8266.com/stable/package_esp8266com_index.json
* Click OK.
* 2. Go To Tools > Board > Boards Manager > Search: Node
* Click on "esp8266"
* select version 2.3.0 > Click on Install
* 3. Go to Following Link and Install respective Drivers
* https://www.silabs.com/products/development-tools/software/usb-to-uart-
bridge-vcp-drivers
* 4. Go to System Preferences > Security & Privacy > General > Allow
* 5. Restart Computer
* 6. Disconnect all connected arduino boards
* 7. Connect NodeMCU to Laptop through MicroUSB cable
* 8. Open Arduino IDE,
* Go To Tools > select Board: "NodeMCU 1.0 (ESP-12E Module)"
* 9. Go To File > Examples > ESP8266 > Blink
* 10. Upload the Code
* 11. Done. Blue LED should Blink on ModeMCU Board With delay of 1 Sec.
* 12. Now NodeMCU is configured with your Arduino IDE.
****************************************************************/
/******************************************************************
* Physical Connections:
*
* NodeMCU Board -> LM35 (Analog Tempt Sensor)
* 3V3 -> Vcc (Left Pin - Flat side facing towards you)
* A0 -> Analog Out (Middle Pin) (10mV / degree Celcious)
* GND -> Gnd (Right Pin - Flat side facing towards you)
*
* PIN Diagram of ESP8266MOD NodeMCU 1.0 (ESP-12E Module) Board:
* +-------------------------------------------------------------------------------
+
* | o . o . o . o . o . o . o . o . o . o . o . o . o . o . o
|
* | Vin GND RST EN 3V3 GND CLK SD0 CM0 SD1 SD2 SD3 RSV RSV A0
|
* | ANTENA
|
* | Facing ur side
|
* |
|
* | 3V3 GND Tx Rx D8 D7 D6 D5 GND 3V3 D4 D3 D2 D1 D0
|
* | o . o . o . o . o . o . o . o . o . o . o . o . o . o . o
|
* +-------------------------------------------------------------------------------
+
*
*
* Serial Communication with PC through MicroUSB cable (Tx0 and Rx0)
*****************************************************************/
/****************************************************************
* STEPS:
* 1. Sign up at https://thingspeak.com
* 2. Channels > New Channel > Update "Name" and "Field 1" field names > Save
Channel
* 3. Click On API keys > Copy "Write API key"
* 4. Make all the Connections to Arduino Mega board mentioned Above.
* 5. Check ip address of RPi3 using "ifconfig" command
* 6. Change Following 3 fields in below written program.
* a. ip of RPi3 (from step 5)
* b. ssid_name (Name of you WiFi)
* c. password (Password of your WiFi)
* d. path of file name of python script running on RPi3 // Here it is: ts.py
*
* 7. Start CGI server on RPi3 using command "python -m CGIHTTPServer"
* 8. Upload Program to NodeMCU 1.0 (ESP-12E Module)
* 9. Open Arduino Serial Monitor on PC (Set Baud Rate to 115200 and set "Both NL &
CR")
* 10. Go to https://thingspeak.com and login
* Channels > My Channels > Click on Channel Name (created in step 2) > Private
View
* Here you can observe the Grapth of Temperature Vs Day.
****************************************************************/
/**************************
*CHANGE FOLLOWING THINGS:
*ip of RPi3
*ssid
*password
**************************/
#include <ESP8266WiFi.h>
#include <DHT.h>
#define DHTPIN 4 //DHT11 is connected to GPIO Pin 5 i.e D1 in node mcu
const char* host = "192.168.43.171"; // ip of RPi3
//String ApiKey = "FHLRDWDK7GXTF83S"; // Not needed
String path = "/cgi-bin/ts.py?field1="; // ts.py is the name of python script
running on RPi3
const char* ssid = "Lenovo"; // ssid_name
const char* pass = "aravinthkalai"; // password
const int httpPort = 8000;
float humi;
float temp;
DHT dht(DHTPIN, DHT11);
WiFiClient client;

//int sensor = A0; // LM35 Analog Output


void setup(void)
{ // Function to Connect to Wi-Fi
Serial.begin(115200);
Serial.println("");
dht.begin();
WiFi.begin(ssid, pass);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
humi = dht.readHumidity();
temp = dht.readTemperature();
Serial.println(temp); // display on serial monitor
if (!client.connect(host, httpPort))
{ // connect to RPi3 CGI server with port no. 8000
Serial.println("connection failed");
return;
}

// send temperature data to RPi3 CGI server


client.print(String("GET ") + path + String(temp) + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: keep-alive\r\n\r\n");
Serial.println("data send successfully");
// Format of the string for temp=25.4 is:
// GET /cgi-bin/ts.py?field1=25.4 HTTP/1.1\r\nHost: 192.168.0.35\r\nConnection:
keep-alive\r\n\r\n

delay(5000); // wait for 1 sec


}

You might also like