Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
IAP_Sensor_03 - added millis() timer
NOTES:
Consider using http://playground.arduino.cc/Code/Timer
This example connects to an unencrypted Wifi network.
Then it prints the MAC address of the Wifi BoosterPack / LaunchPad,
the IP address obtained, and other network details.
Circuit:
* CC3200 WiFi LaunchPad or CC3100 WiFi BoosterPack
with TM4C or MSP430 LaunchPad
created 13 July 2010 by dlf (Metodo2 srl)
modified 31 May 2012 by Tom Igoe
modified 2 July 2014 by Noah Luskey
modified and repurposed 14 Jan 2015 by Brian DeLacey
*/
#ifndef __CC3200R1M1RGC__
// Do not include SPI for CC3200 LaunchPad
#include <SPI.h>
#endif
#include <WiFi.h>
String buzzerStateString = "0";
//// >>> Change this to match server address in class
char ssid[] = "MIT GUEST"; // your access point
char password[] = ""; // your network password
//// >>> Change three lines to match server address in class
char server[] = "10.189.35.148"; // your hub messaging server
String destinationServer = "10.189.35.148:8008";
String localServerString = "10.189.35.148:8008";
//// >>> Change this information for each sensorThing to team#
String deviceName = "Team1";
///////////////// NO MORE CHANGES NEEDED ////////////////
int serverPort = 8008; // numeric port on your server
char portChar[] = ":8008";
long lastConfigurationCheck= 0; // the last time the device checked in with the mother ship
long configurationDelay = 10000;// the millisecond delay between configuration checks
boolean magnetic_sensor_installed = false;
boolean light_sensor_installed = false; // true on MIT1
boolean big_button_installed = true;
boolean pir_sensor_installed = false;
// most launchpads have a red LED
#define LED RED_LED
#define MAGNO_SWITCH 5
#define LIGHT_SENSOR 6 // analog
#define BIG_BUTTON 7
#define PIR_SENSOR 8
#define BUZZER 9
WiFiClient client;
int val = 0;
void countdown(int looplimit) {
// give the sensors a bit of time to warm up and settle
for (int counter = 0; counter < looplimit; counter++) {
// Serial.println("counting down timer ...");
digitalWrite(LED, LOW); // initialize the LED off by making the voltage LOW
delay(250);
digitalWrite(LED, HIGH); // initialize the LED off by making the voltage LOW
delay(250);
digitalWrite(LED, LOW); // initialize the LED off by making the voltage LOW
};
}
void buzzerOn() {
// analogWrite(BUZZER, 255);
// digitalWrite(BUZZER, HIGH);
// only play buzzer if configuration says to - otherwise it gets annoying!
if (buzzerStateString == "1") {
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(BUZZER, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
// actually also play it for MIT IAP all the time even if it gets annoying!
if (buzzerStateString == "0") {
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(BUZZER, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
// Serial.println("Attempting buzzerOn() ");
}
void buzzerOff() {
// analogWrite(BUZZER, 0);
// digitalWrite(BUZZER, LOW);
analogWrite(BUZZER, 0);
// Serial.println("Attempting buzzerOff() ");
}
void toggleBuzzer(){
buzzerOn();
countdown(0);
buzzerOff();
}
void toggleLED(){
digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
countdown(1);
digitalWrite(LED, LOW);
}
void checkClientHTTP() { // if there are incoming bytes available
// from the server, read them and print them
String responseString = "";
String copyString = "";
bool copyMode = false;
char leftBracket[] = "[";
char rightBracket[] = "]";
while (client.available()) {
char c = client.read();
if (c==leftBracket[0]) {
copyMode = true;
}
if (copyMode) {
responseString += c; //
// Serial.println(">>>>>> " + responseString);
if (c == rightBracket[0]) {
// Serial.println(buzzerStateString);
// Serial.println("<<<<<<< buzzerStateString");
copyMode = false;
}
if (responseString.charAt(1) == 'b') {
buzzerStateString = responseString.substring(7,8);
Serial.println("<<<<<<< buzzerStateString >>>>>>>> " + buzzerStateString);
}
if (responseString.charAt(1) == 'e') {
int rLength = responseString.length();
int stringLength = rLength - 5;
String serverString = responseString.substring(5,stringLength);
Serial.println("<<<<<<< ethernet address server String >>>>>>>> " + serverString);
}
}
Serial.write(c);
// responseString[0]= c;
// Serial.println(responseString);
}
}
void pnsNameServerFind() {
IPAddress ghost_server;
char buf[] = "1234";
int len = 4;
ghost_server = WiFi.localIP();
// Serial.println(ghost_server);
for (int node = 1; node < 256; node++) {
// String(node).toCharArray(buf, len);
ghost_server[3] = node;
Serial.println(ghost_server);
/*
if (client.connect(ghost_server, serverPort)) {
// if (client.connect("192.168.1.4", serverPort)) {
Serial.println("********* connected to pnsNameServerFind ********* ");
Serial.println(ghost_server);
Serial.println(serverPort);
Serial.println("========= connected to pnsNameServerFind ========= ");
// client.println("GET /hello.html HTTP/1.1");
client.println("GET /resolve HTTP/1.1");
client.println("Host: " + String(ghost_server) + String(portChar));
client.println("Connection: close");
client.println();
// checkClientHTTP(); // if there are incoming bytes available
Serial.println(">>>>>>>>>> connected to pnsNameServerFind <<<<<<<<< ");
//Serial.println("192.168.1." + String(node));
}
*/
}
}
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
Serial.print("Begin Setup ");
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW); // initialize the LED off by making the voltage LOW
pinMode(BUZZER, OUTPUT);
pinMode(MAGNO_SWITCH, INPUT);
pinMode(PIR_SENSOR, INPUT);
pinMode(BIG_BUTTON, INPUT);
pinMode(LIGHT_SENSOR, INPUT);
toggleBuzzer();
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to Network named: ");
// print the network name (SSID);
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
WiFi.begin(ssid); // begin
while ( WiFi.status() != WL_CONNECTED) {
// print dots while we wait to connect
Serial.print(".");
delay(300);
}
Serial.println("\nYou're connected to the network");
Serial.println("Waiting for an ip address");
while (WiFi.localIP() == INADDR_NONE) {
// print dots while we wait for an ip addresss
Serial.print(".");
delay(300);
}
Serial.println("\nIP Address obtained");
// by now we have a WiFi address - WiFi.localIP()
// search within last octet of our sub-network
// pnsNameServerFind(); TBD - work on this to auto-select server
if (client.connect(server, serverPort)) {
Serial.println("connected to server");
Serial.println(server);
Serial.println(serverPort);
Serial.println("SETUP CONNECTed to server below");
Serial.println("SETUP CONNECTed to server above");
// Make a HTTP request:
client.println("GET /hello.html HTTP/1.1");
// client.println("GET /alert HTTP/1.1");
// client.println("Host: 192.168.1.18:8008");
// client.println("Host: " + localServerString);
client.println("Connection: close");
client.println();
}
else {
Serial.println("SETUP CANNOT FIND server below");
printCurrentNet();
printWifiData();
Serial.println("SETUP CANNOT FIND server above");
}
lastConfigurationCheck = millis(); // initialize
// Print out the status
printCurrentNet();
printWifiData();
}
void loop() {
// printCurrentNet();
// printWifiData();
checkClientHTTP(); // if there are incoming bytes available
checkConfiguration(); // run through and check configuration and sensors
if (big_button_installed)
checkBUTTON();
if (light_sensor_installed)
checkLightSensor();
if (magnetic_sensor_installed)
checkMagnet();
if (pir_sensor_installed)
checkPIR();
}
void sendMsgViaHTTP(String myString) {
Serial.println("sendAlertViaHTTP ...");
if (client.connect(server, serverPort)) {
Serial.println("connected to server");
// Make a HTTP request:
// client.println("GET /hello.html HTTP/1.1");
if (myString == "pir") {
// client.println("GET /alert/pir/office HTTP/1.1");
client.println("GET /alert/pir/" + deviceName + " HTTP/1.1");
}
else
if (myString == "magnet") {
// client.println("GET /alert/magnet/office HTTP/1.1");
client.println("GET /alert/magnet/" + deviceName + " HTTP/1.1");
}
else
if (myString == "button") {
// client.println("GET /alert/button/office HTTP/1.1");
// This really should be a button message / but server not set
client.println("GET /alert/button/" + deviceName + " HTTP/1.1");
}
else
if (myString == "configure") {
// client.println("GET /alert/configure HTTP/1.1");
client.println("GET /alert/configure/" + deviceName + " HTTP/1.1");
}
else // really should never hit this case ...
client.println("GET /hello.html HTTP/1.1");
client.println("Host: " + localServerString);
client.println("Connection: close");
client.println();
}
else {
// Serial.println("LOST SERVER below");
// printCurrentNet();
// printWifiData();
// Serial.println("LOST SERVER above");
}
countdown(3); // give sensors time to settle down
}
void printPIR(int val) {
Serial.print("printPIR: ");
Serial.println(val);
}
void checkPIR() {
String alertType = "pir";
val = digitalRead(PIR_SENSOR);
if (val == HIGH) {
toggleBuzzer();
toggleLED();
sendMsgViaHTTP(alertType);
}
// printPIR(val);
}
void checkBUTTON() {
String alertType = "button";
val = digitalRead(BIG_BUTTON);
if (val == LOW) {
toggleBuzzer();
toggleLED();
sendMsgViaHTTP(alertType);
}
// printPIR(val);
}
void printMagnet(int val) {
Serial.print("printMagnet: ");
Serial.println(val);
}
void checkMagnet() {
String alertType = "magnet";
val = digitalRead(MAGNO_SWITCH);
if (val == LOW) {
toggleBuzzer();
toggleLED();
sendMsgViaHTTP(alertType);
}
// printMagnet(val);
}
void checkLightSensor(){
String alertType = "lights";
val = analogRead(LIGHT_SENSOR);
/*
if (val> 4000)
Serial.println(val);
if (val< 1500)
Serial.println(val);
*/
/*
if (val == LOW) {
toggleBuzzer();
toggleLED();
sendMsgViaHTTP(alertType);
}
*/
}
void checkConfiguration() {
long currentTimeMillis;
currentTimeMillis = millis();
// Serial.print("time is " + currentTimeMillis);
if ((currentTimeMillis - lastConfigurationCheck) > configurationDelay) {
sendMsgViaHTTP("configure"); // String alertType = "configure" ;
Serial.println("$$$ sent onfiguration check message $$$$ ");
lastConfigurationCheck = currentTimeMillis;
}
}
void printWifiData() {
// print your WiFi IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
Serial.print(mac[5], HEX);
Serial.print(":");
Serial.print(mac[4], HEX);
Serial.print(":");
Serial.print(mac[3], HEX);
Serial.print(":");
Serial.print(mac[2], HEX);
Serial.print(":");
Serial.print(mac[1], HEX);
Serial.print(":");
Serial.println(mac[0], HEX);
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
Serial.print(bssid[5], HEX);
Serial.print(":");
Serial.print(bssid[4], HEX);
Serial.print(":");
Serial.print(bssid[3], HEX);
Serial.print(":");
Serial.print(bssid[2], HEX);
Serial.print(":");
Serial.print(bssid[1], HEX);
Serial.print(":");
Serial.println(bssid[0], HEX);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment