forked from exmonkey206/beehive_monitor
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwifi-no-batt-v1.ino
146 lines (119 loc) · 3.56 KB
/
wifi-no-batt-v1.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//Beehive monitor for wifi
//Based on v4 of the beehive monitor by Marc Curtis @exmonkey https://github.com/exmonkey206/beehive_monitor
//Importing various libraries
#include <Sleep_n0m1.h>
#include <CountingStream.h>
#include <Xively.h>
#include <XivelyClient.h>
#include <XivelyDatastream.h>
#include <XivelyFeed.h>
#include <b64.h>
#include <HttpClient.h>
#include <DHT.h>
#include <WiFi.h>
#include <SPI.h>
//Setup a DHT22 instance
#define DHTTYPE DHT22
#define DHTPIN 5
DHT dht(DHTPIN, DHTTYPE);
float temperature = 0;
float humidity = 0;
int wait = 225;
boolean notConnected = true;
unsigned long time;
int mins;
boolean working;
//sleep stuff
Sleep sleep;
unsigned long sleepTime; //how long you wna the arduino to sleep
char myTempStream[] = "temperature"; //Set stream name (need to match Xively name)
char myHumidityStream[] = "humidity"; //Set 2nd stream name
XivelyDatastream datastreams[] = {
XivelyDatastream(myTempStream, strlen(myTempStream), DATASTREAM_FLOAT),
XivelyDatastream(myHumidityStream, strlen(myHumidityStream), DATASTREAM_FLOAT),
};
#define FEED_ID 1234 //Set Xively Feed ID
char xivelyKey[] = "1234"; //Set Xively API key
XivelyFeed feed(FEED_ID, datastreams, 2); //Creating the feed, definining the two datastreams
WiFiClient client;
XivelyClient xivelyclient(client);
//connect to wifi
char ssid[] = "name"; // your network SSID (name)
char pass[] = "1234"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
void setup(void) {
// dht sensor
dht.begin();
// wifi
// initialize serial:
Serial.begin(9600);
// attempt to connect using WPA2 encryption:
Serial.println("Attempting to connect to WPA network...");
status = WiFi.begin(ssid, pass);
// if you're not connected, stop here:
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while(true);
}
// if you are connected, print out info about the connection:
else {
Serial.println("Connected to network");
}
}
void loop() {
if(working==false){
working = true;
Serial.print("allow the sensor to warm up");
delay(5000);
get_data();
}
}
// Get the data from the DHT222 sensor
void get_data(){
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
boolean gotReading = 0;
while (gotReading==0) {
humidity = dht.readHumidity();
temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature) ){
Serial.println("Failed to read from DHT sensor!");
delay(1000);
}
else{
gotReading = 1;
Serial.println("Read data from DHT sensor");
}
}
// Compute heat index
// Must send in temp in Fahrenheit!
//float hi = dht.computeHeatIndex(f, h);
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" *C ");
processData();
}
void processData(){
Serial.println(temperature);
Serial.println(humidity);
Serial.println("process data");
//add the results to the xively stream
datastreams[0].setFloat(temperature);
datastreams[1].setFloat(humidity);
//datastreams[2].setFloat(outputValue);
int ret = xivelyclient.put(feed, xivelyKey); // Send to Xively
Serial.println("Ret: ");
Serial.println(ret);
if(ret == 200){
Serial.println(ret);
delay(1000);
}
else{
delay(1000);
Serial.println(ret);
}
}