|
| 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 | + |
0 commit comments