-
Notifications
You must be signed in to change notification settings - Fork 1
/
LM35_data_to_thingspeak.ino
130 lines (97 loc) · 4.02 KB
/
LM35_data_to_thingspeak.ino
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <LM35.h>
// Code to use SoftwareSerial
#include <SoftwareSerial.h>
SoftwareSerial espSerial = SoftwareSerial(2,3); // arduino RX pin=2 arduino TX pin=3 connect the arduino RX pin to esp8266 module TX pin - connect the arduino TX pin to esp8266 module RX pin
LM35 temp(A0);
int sensorpin = 0;
String apiKey = "your api key"; // replace with your channel's thingspeak WRITE API key
String ssid="ssid"; // Wifi network SSID
String password ="password"; // Wifi network password
boolean DEBUG=true;
//======================================================================== showResponce
void showResponse(int waitTime){
long t=millis();
char c;
while (t+waitTime>millis()){
if (espSerial.available()){
c=espSerial.read();
if (DEBUG) Serial.print(c);
}
}
}
//========================================================================
boolean thingSpeakWrite(float value1){
String cmd = "AT+CIPSTART=\"TCP\",\""; // TCP connection
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "\",80";
espSerial.println(cmd);
if (DEBUG) Serial.println(cmd);
if(espSerial.find("Error")){
if (DEBUG) Serial.println("AT+CIPSTART error");
return false;
}
//making tcp connections
String getStr = "GET /update?api_key="; // prepare GET string
getStr += apiKey;
getStr +="&field1=";
getStr += String(value1);
// getStr +="&field3=";
// getStr += String(value3);
// ...
getStr += "\r\n";
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
espSerial.println(cmd);
if (DEBUG) Serial.println(cmd);
delay(100);
if(espSerial.find(">")){
espSerial.print(getStr); //request to the server
if (DEBUG) Serial.print(getStr);
}
else{
espSerial.println("AT+CIPCLOSE");
// alert user
if (DEBUG) Serial.println("AT+CIPCLOSE");
return false;
}
return true;
}
//================================================================================ setup
void setup() {
DEBUG=true; // enable debug serial
Serial.begin(9600);
espSerial.begin(9600); // enable software serial
// Your esp8266 module's speed is probably at 115200.
// For this reason the first time set the speed to 115200 or to your esp8266 configured speed
// and upload. Then change to 9600 and upload again
espSerial.println("AT+RST"); // Enable this line to reset the module;
showResponse(1000);
espSerial.println("AT+UART_CUR=9600,8,1,0,0"); // Enable this line to set esp8266 serial speed to 9600 bps
//showResponse(1000);
espSerial.println("AT+CWMODE=1"); // set esp8266 as client
showResponse(1000);
espSerial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\""); // set your home router SSID and password
showResponse(5000);
if (DEBUG) Serial.println("Setup completed");
}
// ====================================================================== loop
void loop() {
// Read sensor values
int val = analogRead(sensorpin); // i have just written different methods to find temp
//any of them can be used
float mv = val* (5000/1024);
float celcious =( 500 - mv)/10;
float q = celcious;
float t = temp.cel();
if (isnan(t) ) {
if (DEBUG) Serial.println("Failed to read from LM35");
}
else {
if (DEBUG) Serial.println("Temp="+String(t)+" *C");
thingSpeakWrite(t); // Write values to thingspeak
}
// thingspeak needs 15 sec delay between updates,
delay(6000);
}
// reference link : https://elementztechblog.wordpress.com/2015/05/13/esp8266-based-temperature-data-logger-using-arduino/