Skip to content

Commit c5cec94

Browse files
committed
Added WiFi examples
1 parent 9cb0a29 commit c5cec94

File tree

3 files changed

+255
-0
lines changed

3 files changed

+255
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <SPI.h>
2+
#include <WiFi.h>
3+
#include <HttpClient.h>
4+
#include <Cosm.h>
5+
6+
char ssid[] = "YourNetwork"; // your network SSID (name)
7+
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
8+
int keyIndex = 0; // your network key Index number (needed only for WEP)
9+
10+
int status = WL_IDLE_STATUS;
11+
12+
// Your Cosm key to let you upload data
13+
char cosmKey[] = "YOUR_COSM_API_KEY";
14+
15+
// Define the string for our datastream ID
16+
char temperatureId[] = "temperature";
17+
18+
CosmDatastream datastreams[] = {
19+
CosmDatastream(temperatureId, strlen(temperatureId), DATASTREAM_FLOAT),
20+
};
21+
// Finally, wrap the datastreams into a feed
22+
CosmFeed feed(15552, datastreams, 1 /* number of datastreams */);
23+
24+
WiFiClient client;
25+
CosmClient cosmclient(client);
26+
27+
void setup() {
28+
// put your setup code here, to run once:
29+
Serial.begin(9600);
30+
31+
Serial.println("Reading from Cosm example");
32+
Serial.println();
33+
34+
// attempt to connect to Wifi network:
35+
while ( status != WL_CONNECTED) {
36+
Serial.print("Attempting to connect to SSID: ");
37+
Serial.println(ssid);
38+
status = WiFi.begin(ssid, pass);
39+
// wait 10 seconds for connection:
40+
delay(10000);
41+
}
42+
Serial.println("Connected to wifi");
43+
printWifiStatus();
44+
}
45+
46+
void loop() {
47+
int ret = cosmclient.get(feed, cosmKey);
48+
Serial.print("cosmclient.get returned ");
49+
Serial.println(ret);
50+
51+
Serial.println("Datastream is...");
52+
Serial.println(feed[0]);
53+
54+
Serial.print("Temperature is: ");
55+
Serial.println(feed[0].getFloat());
56+
57+
Serial.println();
58+
delay(15000UL);
59+
}
60+
61+
void printWifiStatus() {
62+
// print the SSID of the network you're attached to:
63+
Serial.print("SSID: ");
64+
Serial.println(WiFi.SSID());
65+
66+
// print your WiFi shield's IP address:
67+
IPAddress ip = WiFi.localIP();
68+
Serial.print("IP Address: ");
69+
Serial.println(ip);
70+
71+
// print the received signal strength:
72+
long rssi = WiFi.RSSI();
73+
Serial.print("signal strength (RSSI):");
74+
Serial.print(rssi);
75+
Serial.println(" dBm");
76+
}
77+
78+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <SPI.h>
2+
#include <WiFi.h>
3+
#include <HttpClient.h>
4+
#include <Cosm.h>
5+
6+
char ssid[] = "YourNetwork"; // your network SSID (name)
7+
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
8+
int keyIndex = 0; // your network key Index number (needed only for WEP)
9+
10+
int status = WL_IDLE_STATUS;
11+
12+
// Your Cosm key to let you upload data
13+
char cosmKey[] = "YOUR_API_KEY";
14+
15+
// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
16+
int sensorPin = 2;
17+
18+
// Define the strings for our datastream IDs
19+
char sensorId[] = "sensor_reading";
20+
CosmDatastream datastreams[] = {
21+
CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
22+
};
23+
// Finally, wrap the datastreams into a feed
24+
CosmFeed feed(15552, datastreams, 1 /* number of datastreams */);
25+
26+
WiFiClient client;
27+
CosmClient cosmclient(client);
28+
29+
void setup() {
30+
// put your setup code here, to run once:
31+
Serial.begin(9600);
32+
33+
Serial.println("Starting single datastream upload to Cosm...");
34+
Serial.println();
35+
36+
// attempt to connect to Wifi network:
37+
while ( status != WL_CONNECTED) {
38+
Serial.print("Attempting to connect to SSID: ");
39+
Serial.println(ssid);
40+
status = WiFi.begin(ssid, pass);
41+
// wait 10 seconds for connection:
42+
delay(10000);
43+
}
44+
Serial.println("Connected to wifi");
45+
printWifiStatus();
46+
}
47+
48+
void loop() {
49+
int sensorValue = analogRead(sensorPin);
50+
datastreams[0].setFloat(sensorValue);
51+
52+
Serial.print("Read sensor value ");
53+
Serial.println(datastreams[0].getFloat());
54+
55+
Serial.println("Uploading it to Cosm");
56+
int ret = cosmclient.put(feed, cosmKey);
57+
Serial.print("cosmclient.put returned ");
58+
Serial.println(ret);
59+
60+
Serial.println();
61+
delay(15000);
62+
}
63+
64+
void printWifiStatus() {
65+
// print the SSID of the network you're attached to:
66+
Serial.print("SSID: ");
67+
Serial.println(WiFi.SSID());
68+
69+
// print your WiFi shield's IP address:
70+
IPAddress ip = WiFi.localIP();
71+
Serial.print("IP Address: ");
72+
Serial.println(ip);
73+
74+
// print the received signal strength:
75+
long rssi = WiFi.RSSI();
76+
Serial.print("signal strength (RSSI):");
77+
Serial.print(rssi);
78+
Serial.println(" dBm");
79+
}
80+
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <SPI.h>
2+
#include <WiFi.h>
3+
#include <HttpClient.h>
4+
#include <Cosm.h>
5+
6+
char ssid[] = "YourNetwork"; // your network SSID (name)
7+
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
8+
int keyIndex = 0; // your network key Index number (needed only for WEP)
9+
10+
int status = WL_IDLE_STATUS;
11+
12+
// Your Cosm key to let you upload data
13+
char cosmKey[] = "YOUR_API_KEY";
14+
15+
// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
16+
int sensorPin = 2;
17+
18+
// Define the strings for our datastream IDs
19+
char sensorId[] = "sensor_reading";
20+
char bufferId[] = "info_message";
21+
String stringId("random_string");
22+
const int bufferSize = 140;
23+
char bufferValue[bufferSize]; // enough space to store the string we're going to send
24+
CosmDatastream datastreams[] = {
25+
CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
26+
CosmDatastream(bufferId, strlen(bufferId), DATASTREAM_BUFFER, bufferValue, bufferSize),
27+
CosmDatastream(stringId, DATASTREAM_STRING)
28+
};
29+
// Finally, wrap the datastreams into a feed
30+
CosmFeed feed(15552, datastreams, 3 /* number of datastreams */);
31+
32+
WiFiClient client;
33+
CosmClient cosmclient(client);
34+
35+
void setup() {
36+
// put your setup code here, to run once:
37+
Serial.begin(9600);
38+
39+
Serial.println("Starting multiple datastream upload to Cosm...");
40+
Serial.println();
41+
42+
// attempt to connect to Wifi network:
43+
while ( status != WL_CONNECTED) {
44+
Serial.print("Attempting to connect to SSID: ");
45+
Serial.println(ssid);
46+
status = WiFi.begin(ssid, pass);
47+
// wait 10 seconds for connection:
48+
delay(10000);
49+
}
50+
Serial.println("Connected to wifi");
51+
printWifiStatus();
52+
}
53+
54+
void loop() {
55+
int sensorValue = analogRead(sensorPin);
56+
datastreams[0].setFloat(sensorValue);
57+
58+
Serial.print("Read sensor value ");
59+
Serial.println(datastreams[0].getFloat());
60+
61+
datastreams[1].setBuffer("a message to upload");
62+
Serial.print("Setting buffer value to:\n ");
63+
Serial.println(datastreams[1].getBuffer());
64+
65+
// Pick a random number to send up in a string
66+
String stringValue(random(100));
67+
stringValue += " is a random number";
68+
datastreams[2].setString(stringValue);
69+
Serial.print("Setting string value to:\n ");
70+
Serial.println(datastreams[2].getString());
71+
72+
Serial.println("Uploading it to Cosm");
73+
int ret = cosmclient.put(feed, cosmKey);
74+
Serial.print("cosmclient.put returned ");
75+
Serial.println(ret);
76+
77+
Serial.println();
78+
delay(15000);
79+
}
80+
81+
void printWifiStatus() {
82+
// print the SSID of the network you're attached to:
83+
Serial.print("SSID: ");
84+
Serial.println(WiFi.SSID());
85+
86+
// print your WiFi shield's IP address:
87+
IPAddress ip = WiFi.localIP();
88+
Serial.print("IP Address: ");
89+
Serial.println(ip);
90+
91+
// print the received signal strength:
92+
long rssi = WiFi.RSSI();
93+
Serial.print("signal strength (RSSI):");
94+
Serial.print(rssi);
95+
Serial.println(" dBm");
96+
}
97+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy