-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode
More file actions
117 lines (102 loc) · 2.28 KB
/
code
File metadata and controls
117 lines (102 loc) · 2.28 KB
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
# Machine-maintenance-report-generation-
#include <Adafruit_ESP8266.h>
#include <SoftwareSerial.h>
#include <stdlib.h>
#define DEBUG true
#define SSID "vivo 1816" // "SSID-WiFiname"
#define PASS "devi2708" // "password"
#define IP "184.106.153.149" // thingspeak.com ip
String msg = "GET /update?key=NVGHRP96XUW6A5DT";
SoftwareSerial esp8266(7,8);
#define TempPin A0
float temp;
int hum;
String tempC;
int error;
void setup()
{
Serial.begin(115200); //or use default 115200.
esp8266.begin(115200);
Serial.println("AT");
esp8266.println("AT");
delay(5000);
if(esp8266.find("OK"))
{
connectWiFi();
}
}
void loop()
{
//Read temperature and humidity values from DHT sensor:
start: //label
error=0;
int TempCel = analogRead(TempPin); // Getting LM35 value and saving it in variable
Serial.print("SENSOR VALUE = "); //Displaying temperature in Celsius
Serial.println(TempCel);
char buffer[10];
// there is a useful c function called dtostrf() which will convert a float to a char array
//so it can then be printed easily. The format is: dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);
tempC = dtostrf(TempCel, 4, 1, buffer);
updateTemp();
//Resend if transmission is not completed
if (error==1)
{
goto start; //go to label "start"
}
delay(3600);
}
void updateTemp()
{
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
Serial.println(cmd);
esp8266.println(cmd);
delay(2000);
if(esp8266.find("Error"))
{
return;
}
cmd = msg ;
cmd += "&field1="; //field 1 for temperature
cmd += tempC;
cmd += "\r\n";
Serial.print("AT+CIPSEND=");
esp8266.print("AT+CIPSEND=");
Serial.println(cmd.length());
esp8266.println(cmd.length());
if(esp8266.find(">"))
{
Serial.print(cmd);
esp8266.print(cmd);
}
else
{
Serial.println("AT+CIPCLOSE");
esp8266.println("AT+CIPCLOSE");
//Resend...
error=1;
}
}
boolean connectWiFi()
{
Serial.println("AT+CWMODE=1");
esp8266.println("AT+CWMODE=1");
delay(2000);
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
Serial.println(cmd);
esp8266.println(cmd);
delay(5000);
if(esp8266.find("OK"))
{
return true;
}
else
{
return false;
}
}