· 5 years ago · Jun 07, 2020, 01:42 PM
1/*
2
3Problem Statement: Upload temperature data(LM35) to your channel on ThingSpeak
4Developed by: Rahul Shrivastava
5****************************************************
6Hardware Connections
7
8LM35 NODEMCU
91 (Vcc) 3v3 (3.3 Volts)
102 (OUT) A0 (Analog Input)
113 (GND) GND
12****************************************************
13*/
14
15#include <ESP8266WiFiMulti.h>// Wifi
16#include <ESP8266HTTPClient.h>// HTTP
17
18ESP8266WiFiMulti WiFiMulti;
19
20String api_key = "V73YPTI4XFBKRT3C";// write API key
21int field_no = 1;// temperature
22
23void setup() {
24 Serial.begin(115200);
25 Serial.flush();
26 pinMode(D0, OUTPUT);
27 wificonnect();
28}
29
30void loop() {
31 float tempC = sensorRead(A0);
32 GETsend(tempC);
33}
34
35void wificonnect()
36 {
37 WiFiMulti.addAP("InternetHere1", "oist@1986");// AP --> Access Point, SSID
38 Serial.println("Conneting");
39 while(WiFiMulti.run()!=WL_CONNECTED)
40 {
41 Serial.print(".");
42 digitalWrite(D0, LOW);
43 delay(100);
44 digitalWrite(D0, HIGH);
45 delay(100);
46 }
47 Serial.print("Connected to HOTSPOT..\n");// This line will execute if your device is connected to Access Pass
48 }
49
50void GETsend(float data)
51{
52 HTTPClient http; //http is the object of class HTTPClient this class defines the method to create and send HTTP requests
53 //https://api.thingspeak.com/update?api_key=V73YPTI4XFBKRT3C&field1=0
54 String url = "http://api.thingspeak.com/update?api_key=";
55 url += api_key;
56 url += "&field";
57 url += field_no;
58 url += "=";// Creating HTTP Query/GET Method
59 url += data;// passing the parameter to string, this value will reach cloud at field1
60 http.begin(url);// initiate HTTP Request to establish connection
61
62 Serial.print("HTTP REQUEST SENT, Waiting for response\n");
63
64 // start connection and send HTTP header
65 int httpCode = http.GET();// get method, returns a http code, -ve if access denied
66 // httpCode will be negative on error
67 if(httpCode > 0) {
68 // HTTP header has been send and Server response header has been handled
69 Serial.println("Sensor data uploaded sucessfully");
70 }
71 else
72 {
73 Serial.println("Failed to connect to server");
74 }
75 http.end();
76 delay(16000);// 16sec
77}
78
79float sensorRead (int sensorPin)
80{
81 float x = analogRead(sensorPin);
82 float voltage = x * 3.3 / 1023;
83 float voltage_mv = voltage*1000;
84 float temp = voltage_mv/10;
85 return temp;
86}