-
Notifications
You must be signed in to change notification settings - Fork 35
/
Node_Red_espWithUptime.ino
123 lines (103 loc) · 4.03 KB
/
Node_Red_espWithUptime.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
/*
ESP32 Version, just replace esp8266 library with WiFi.h library
*/
//#include "WiFi.h" //needed for the ESP32
#include "ESP8266WiFi.h" //needed for the ESP8266
//-----------------------------------------------
//This sketch is combined from Adafruit DHT sensor and tdicola for dht.h library
//by Stephen Borsay www.embedded-IoT.net
//#include "DHT.h" uncomment if using a DHT sensor with Adafruit DHT library
//#define DHTPIN 2 // what digital pin we're connected to pin2 to D4 on esp board
// Uncomment whatever DHT sensor type you're using!
//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21
//#define DHTTYPE DHT22 // DHT 22
//DHT dht(DHTPIN,DHTTYPE);
unsigned long uptime;
long randomNumberT, randomNumberH; //randoms variables for temperature and humidity for testing
const char WEBSITE[] = "YOUR_STATIC_IP_NAME.mybluemix.net"; //incoming HTTP Node URL/Topic at Node Red do not use http or https prefix
const char* MY_SSID = "YOUR_SSID";
const char* MY_PWD = "YOUR_PWD";
void setup()
{
Serial.begin(115200);
// dht.begin();
Serial.print("Connecting to "+*MY_SSID);
WiFi.begin(MY_SSID, MY_PWD);
Serial.println("going into wl connect");
while (WiFi.status() != WL_CONNECTED) //not connected, ...waiting to connect
{
delay(1000);
Serial.print(".");
}
Serial.println("wl connected");
Serial.println("");
Serial.println("Credentials accepted! Connected to wifi\n ");
Serial.println("");
}
void loop()
{
//Change loop delay as you see fit
delay(5000); //5 seconds, adjust as you like relative to sampling rate vs. service call quota
/*
float humidityData = dht.readHumidity();
// Read temperature as Celsius (the default)
float celData = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float fehrData = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(humidityData) || isnan(celData) || isnan(fehrData))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Celsius (isFahreheit = false)
float hicData = dht.computeHeatIndex(celData, humidityData, false);
// Compute heat index in Fahrenheit (the default)
float hifData = dht.computeHeatIndex(fehrData, humidityData);
//Print to Serial monitor or Terminal of your chocice at 115200 Baud
Serial.print("Humidity: ");
Serial.print(humidityData);
Serial.print(" %\t");
Serial.print("Temperature in Cel: ");
Serial.print(celData);
Serial.print(" *C ");
Serial.print("Temperature in Fehr: ");
Serial.print(fehrData);
Serial.print(" *F\t");
Serial.print("Heat index in Cel: ");
Serial.print(hicData);
Serial.print(" *C ");
Serial.print("Heat index in Fehr: ");
Serial.print(hifData);
Serial.print(" *F\n");
*/
WiFiClient client; //instantiate WiFi object, can scope from here or Globally
if (client.connect(WEBSITE, 80))
{
Serial.print("Time: ");
uptime = millis();
Serial.println(uptime); //prints time since program started
randomNumberT = random(10, 90);
randomNumberH = random(5, 70);
Serial.print(" sending data package now \n");
//http URl format is ---> ?variable1=value&variable2=value2
client.print("GET /mytopic?Temperature=" + (String)randomNumberT
+ "&Humidity=" + (String) randomNumberH
+ "&Uptime=" + uptime
/* + "&celData=" + (String) celData
+ "&fehrData=" + (String) fehrData
+ "&hicData=" + (String) hicData
+ "&hifData=" + (String) hifData */
);
// HTTP 1.1 provides a persistent connection, allowing multiple requests to be batched
// or pipelined to an output buffer. Careful when altering headers, they arnt forgiving!
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(WEBSITE);
client.println("User-Agent: ESP32/1.0");
client.println("Connection: close");
client.println();
Serial.print(" finished sending http headers \n");
}
}