Skip to content

Commit 33b149a

Browse files
committed
initial commit of actual code
1 parent ef060b9 commit 33b149a

File tree

11 files changed

+690
-1
lines changed

11 files changed

+690
-1
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
Part of the "streaming water workshop given at Delft University of Technology
3+
4+
This program is used to test the communication between the student slave arduinos
5+
and the master arduino. IF this is succesful, the final program to run on the
6+
master arduino should be I2CSensorMasterTwitter, which tweets the measurements.
7+
8+
This program cycles through channels of the I2C bus. When it finds
9+
a life slave connected to an address it sends a code-word to the slave
10+
The slave repsons depends on the code-word. The code words are:
11+
name - the slave returns the name of the students working with that slave arduino
12+
var - the slave returns the type of measurement, ie. "voltage" or "rainfall"
13+
data - the slave returns the actual measurement. this is send as a string!
14+
unit - the slave returns the unit of the measurement, ie "volts" or "mm/h"
15+
16+
These values are than printen on the Serial com line, for easy debugging.
17+
18+
The master should be connected to the slaves using an I2C bus, ie. pin A4 off
19+
master and all slaves should be connected to each other, as well as pinA5.
20+
21+
For correct communication it is paramount that the master and all slaves share
22+
the same ground. This is done by disconnecting the slaves from an external
23+
power source and than connecting all gnd pins in a bus as well as all
24+
5V pins. This means that the master has to supply the power for all the slaves
25+
which will work up to about 20 slaves (depending on individual power consumption)
26+
27+
28+
*/
29+
30+
#include <Wire.h>
31+
32+
#define MAXCHANNEL 40
33+
34+
char tweet[140];
35+
36+
void setup()
37+
{
38+
delay(10000);
39+
Wire.begin(); // join i2c bus (address optional for master)
40+
Serial.begin(9600); // start serial for output
41+
Serial.println("starting");
42+
}
43+
44+
void loop()
45+
{
46+
for(int address=2;address<MAXCHANNEL;address++){
47+
Serial.println(address);
48+
Wire.beginTransmission(address);
49+
Wire.write("");
50+
byte check = Wire.endTransmission();
51+
//Serial.println(check);
52+
if(check==0){
53+
54+
String name = pullSlave(address,"name");
55+
String var = pullSlave(address,"var");
56+
String data = pullSlave(address,"data");
57+
String unit = pullSlave(address,"unit");
58+
59+
("testmeasurement " + name + " " + var + ": " + data + " " + unit).toCharArray(tweet,140);
60+
Serial.print(address);
61+
Serial.print(" ");
62+
Serial.println(tweet);
63+
64+
delay(1000);
65+
}
66+
}
67+
delay(5000);
68+
}
69+
70+
String pullSlave(int address, char command[]){
71+
char msg[30];
72+
int n=0;
73+
74+
//send command to slave
75+
Wire.beginTransmission(address);
76+
Wire.write(command);
77+
Wire.endTransmission();
78+
79+
//give slave time to construct msg
80+
delay(100);
81+
82+
//demand msg from slave
83+
Wire.requestFrom(address, 28); // request 28 bytes from slave device #2
84+
while(Wire.available()){
85+
msg[n++]=Wire.read();
86+
}
87+
String msgStr=String(msg);
88+
msgStr.trim();
89+
return msgStr;
90+
}
91+
92+
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
This program is used to finalize the "streaming water" workshop, given at Delft
3+
University of Technology. This master program tweets the name of the students,
4+
the measurement type, value and unit of all the connected slave Arduinos
5+
6+
This program cycles through channels of the I2C bus. When it finds
7+
a life slave connected to an address it sends a code-word to the slave
8+
The slave repsons depends on the code-word. The code words are:
9+
name - the slave returns the name of the students working with that slave arduino
10+
var - the slave returns the type of measurement, ie. "voltage" or "rainfall"
11+
data - the slave returns the actual measurement. this is send as a string!
12+
unit - the slave returns the unit of the measurement, ie "volts" or "mm/h"
13+
14+
The master compiles a tweet, based on the answers of the slave, which is tweeted
15+
using the Tweet Library for Arduino.
16+
17+
Connecting to twitter is done over wifi: the master arduino must be equiped
18+
with a wifi shield and the settings below must be adjusted to log into the correct
19+
network. Note that the standard twitter library is written for use with the arduino
20+
ethernet shield and here we use the wifi shield. Therefore, we have to use the
21+
twitterWifi library, which is also provided in the same repository as this code.
22+
Add the twitterWifi library to your libraries folder and restart the arduino soft-
23+
ware for this to work.
24+
25+
the following parameters should be changed below:
26+
line 57: ssid - this is the ssid (ie. name) of the wifi network to login to.
27+
line 58: pass - this is the password (wpa / wpa2) or the key (WEP) to log into the wifi
28+
network. If no pwd is needed, comment-out line 58.
29+
30+
In line 66, the twitter token associated with the twitter account should be added
31+
You can get it from http://arduino-tweet.appspot.com/
32+
33+
In line 97, the construction of the tweet should be changed to represent the
34+
wording that you want. Make sure the total tweet length can never succeed 140
35+
character.
36+
37+
The master should be connected to the slaves using an I2C bus, ie. pin SDA off
38+
master and all slaves should be connected to each other, as well as SCL. When using
39+
the Arduino Uno (as most people will be doing), keep in mind that the SDA and SCL pins
40+
are shared with the A4 and A5 analog pins. They can not be used!
41+
42+
For correct communication it is paramount that the master and all slaves share
43+
the same ground. This is done by disconnecting the slaves from an external
44+
power source and than connecting all gnd pins in a bus as well as all
45+
5V pins. This means that the master has to supply the power for all the slaves
46+
which will work up to about 20 slaves (depending on individual power consumption)
47+
48+
*/
49+
50+
#include <Wire.h>
51+
#include <SPI.h>
52+
#include <WiFi.h>
53+
#include <TwitterWifi.h>
54+
55+
#define MAXCHANNEL 40
56+
57+
char ssid[] = "YOUR WIFI SSID HERE"; // your network SSID (name)
58+
char pass[] = "YOUR WIFI PASSWORD HERE"; // your network password (use for WPA, or use as key for WEP)
59+
int keyIndex = 0; // your network key Index number (needed only for WEP)
60+
61+
int status = WL_IDLE_STATUS; // status of the wifi connection
62+
63+
char tweet[140];
64+
65+
// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
66+
Twitter twitter("YOUR TOKEN HERE");
67+
68+
69+
70+
void setup()
71+
{
72+
Wire.begin(); // join i2c bus (address optional for master)
73+
Serial.begin(9600); // start serial for output
74+
75+
WiFi.begin(ssid, pass);
76+
//WiFi.begin(ssid);
77+
delay(5000);
78+
// you're connected now, so print out the status:
79+
80+
printWifiStatus();
81+
82+
}
83+
84+
void loop()
85+
{
86+
for(int address=0;address<MAXCHANNEL;address++){
87+
Wire.beginTransmission(address);
88+
Wire.write("");
89+
byte check = Wire.endTransmission();
90+
if(check==0){
91+
92+
String name = pullSlave(address,"name");
93+
String var = pullSlave(address,"var");
94+
String data = pullSlave(address,"data");
95+
String unit = pullSlave(address,"unit");
96+
97+
("a sensor made by " + name + " measures " + var + ": " + data + " " + unit + " and tweets using software by @RolfHut").toCharArray(tweet,140);
98+
99+
Serial.println("connecting to twitter...");
100+
if (twitter.post(tweet)) {
101+
// Specify &Serial to output received response to Serial.
102+
// If no output is required, you can just omit the argument, e.g.
103+
// int status = twitter.wait();
104+
int status = twitter.wait(&Serial);
105+
if (status == 200) {
106+
Serial.println("OK.");
107+
} else {
108+
Serial.print("failed : code ");
109+
Serial.println(status);
110+
}
111+
} else {
112+
Serial.println("connection failed.");
113+
}
114+
115+
116+
}delay(0);
117+
118+
}
119+
delay(30000);
120+
}
121+
122+
String pullSlave(int address, char command[]){
123+
char msg[30];
124+
int n=0;
125+
126+
//send command to slave
127+
Wire.beginTransmission(address);
128+
Wire.write(command);
129+
Wire.endTransmission();
130+
131+
//give slave time to construct msg
132+
delay(100);
133+
134+
//demand msg from slave
135+
Wire.requestFrom(address, 28); // request 6 bytes from slave device #2
136+
while(Wire.available()){
137+
msg[n++]=Wire.read();
138+
}
139+
String msgStr=String(msg);
140+
msgStr.trim();
141+
return msgStr;
142+
}
143+
144+
void printWifiStatus() {
145+
// print the SSID of the network you're attached to:
146+
Serial.print("SSID: ");
147+
Serial.println(WiFi.SSID());
148+
149+
// print your WiFi shield's IP address:
150+
IPAddress ip = WiFi.localIP();
151+
Serial.print("IP Address: ");
152+
Serial.println(ip);
153+
154+
// print the received signal strength:
155+
long rssi = WiFi.RSSI();
156+
Serial.print("signal strength (RSSI):");
157+
Serial.print(rssi);
158+
Serial.println(" dBm");
159+
}
160+
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
I2CSensorSlaveTemplate
3+
4+
This piece of code is copy pasted below the individual codes of the students
5+
Arduinos. This allows the student arduino to communicate as a slave with the
6+
master arduino, which has a wifi shield on it. In this way all the measurements
7+
from all the student arduinos are tweeted from the master arduino.
8+
9+
After copy pasting the code below their own code, the students have to make a
10+
simple addition to their electric scheme, and the have to add some meta-data below.
11+
12+
These steps below have to be done in the correct order!
13+
14+
- make sure that the student code has a global variable called "measurement" that
15+
is of type "float". This variable should contain the actual measurement value.
16+
17+
- copy-paste this code underneath the students own code.
18+
19+
- change the variables ADDRESS, NAME, VARIABLE and UNIT below.
20+
- The address is given to the students by the teachter and should be unique and
21+
lower than 40
22+
- The name contains the name of the students like "John and Lea". Students can
23+
use their twitter name in stead of their real name, for example "@john and @lea"
24+
- The variable is the type of measurement, for example "rain" or "temperature"
25+
- The unit is the unit of the measurement, for example "mm per hour" or "degrees C"
26+
27+
- finally change the "void setup{" line in the student code to "void user_setup{"
28+
and change the "void loop{" line in the student code to "void user_loop{"
29+
30+
- compile and upload the student-code to the arduino.
31+
32+
- disconnect the arduino from the computer
33+
34+
- connect the Gnd pin of the slave to the bus that is also connected to the Gnd pin
35+
of the master
36+
- connect pin SDA of the slave to the bus that is also connected to SDA of the master
37+
- connect pin SCL of the slave to the bus that is also connected to SCL of the master
38+
- connect the 5V pin of the slave to the bus that is also connected to the 5V pin
39+
of the master
40+
41+
This example code is in the public domain. The code is written by Rolf Hut (@RolfHut)
42+
but borrows heavily on earlier code written by Adam Gleave of Cambridge University.
43+
*/
44+
45+
46+
/***************************************** GENERAL CODE ****************************/
47+
48+
49+
#define ADDRESS XX //change the XX into the adres number assigend to this arduino.
50+
#define NAME "STUDENT NAME"
51+
#define VARIABLE "VARIABLE THAT IS MEASURED" //ie. temperature, or rainfall
52+
#define UNIT "UNIT THAT IS MEASURED" //ie. degrees C, or mm per hour
53+
54+
55+
56+
#define SPACE " "
57+
#include <Wire.h>
58+
#include <stdbool.h>
59+
60+
61+
char msgVal0[8];
62+
char buf[8];
63+
int posRead = 0;
64+
int posWrite = 0;
65+
char * msgout;
66+
char tweet[32];
67+
68+
69+
/* This function SHOULD be modified */
70+
void requestEvent()
71+
{
72+
if (strcmp(buf, "?") == 0)
73+
{
74+
msgout = "HELLO";
75+
} else if (strcmp(buf, "name") == 0) {
76+
msgout = NAME;
77+
} else if (strcmp(buf, "var") == 0) {
78+
msgout = VARIABLE;
79+
} else if (strcmp(buf, "unit") == 0) {
80+
msgout = UNIT;
81+
} else if (strcmp(buf, "data") == 0) {
82+
dtostrf(measurement,2,5,msgVal0); //this line converts the students variable measurement to a string
83+
msgout = msgVal0;
84+
} else {
85+
msgout = "ERR";
86+
}
87+
88+
//Serial.print("Received ");
89+
//Serial.println(buf);
90+
posRead = 0;
91+
buf[0] = 0;
92+
(String(msgout)+String(SPACE)).toCharArray(tweet,32);
93+
Wire.write(tweet);
94+
//Serial.print("Send: ");
95+
//Serial.println(tweet);
96+
}
97+
98+
void setup()
99+
{
100+
//Serial.begin(9600);
101+
Wire.begin(ADDRESS);
102+
Wire.onRequest(requestEvent);
103+
Wire.onReceive(receiveEvent);
104+
105+
//Serial.println("Starting");
106+
user_setup();
107+
}
108+
109+
void loop()
110+
{
111+
user_loop();
112+
}
113+
114+
void receiveEvent(int howMany)
115+
{
116+
while (howMany > 0)
117+
{
118+
if (posRead < sizeof(buf)-1)
119+
{
120+
buf[posRead++] = Wire.read();
121+
buf[posRead] = 0; // maintain it as a string by NIL-terminating
122+
}
123+
else {
124+
//Serial.println("WARNING: Read too many bytes before receiving a request.");
125+
}
126+
howMany--;
127+
}
128+
//Serial.println(buf);
129+
}
130+
131+

0 commit comments

Comments
 (0)