Nexus 7 and the Arduino.
by Computothought in Circuits > Arduino
32787 Views, 103 Favorites, 0 Comments
Nexus 7 and the Arduino.
Will show several ways to show how to use an Arduino as a sensor using The Nexus 7 for a terminal and to do development. This can be very important for protecting your electronic equipment especially servers.
Note 1: All connections are to a standard Arduino board. This instructable is for users very familiar with the Arduino boards. If you are a novice, you may want to get additional help.
Note 2: With android 5.1, you may not be able to direct connect the devices, but using a router that has a dhcp server can be a temporary fix. Android 5.0x does not seem to support ethernet connections.
Wigh android 5.1.1and the latest version of the software, there is no way to fix the baud rate so uploads do not work.
Working With the Nexus 7.
Have fun.
Code to test the connect from the Arduino to the Nexus 7.
<code>
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("This is a test");
delay(1000);
}
</code>
Developing for the Arduino on the Nexus 7
Several Examples to Work With.
1. EMF (Electomotive force) field reader
2. Capacitive reader
3. Temperature reading
4. Flood detection.
Magnetic Field
<code>
// EMF Detector for LED Bargraph v1.0
// 5.12.2009
// original code/project by Aaron ALAI - aaronalai1@gmail.com
#define NUMREADINGS 15 // raise this number to increase data smoothing
int senseLimit = 15; // raise this number to decrease sensitivity (up to 1023 max)
int probePin = 5; // analog 5
int val = 0; // reading from probePin
// variables for smoothing
int readings[NUMREADINGS]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // final average of the probe reading
void setup() {
Serial.begin(9600); // initiate serial connection for debugging/etc
for (int i = 0; i < NUMREADINGS; i++)
readings[i] = 0; // initialize all the readings to 0
}
void loop() {
val = analogRead(probePin); // take a reading from the probe
if(val >= 1){ // if the reading isn't zero, proceed
val = constrain(val, 1, senseLimit); // turn any reading higher than the senseLimit value into the senseLimit value
val = map(val, 1, senseLimit, 1, 1023); // remap the constrained value within a 1 to 1023 range
total -= readings[index]; // subtract the last reading
readings[index] = val; // read from the sensor
total += readings[index]; // add the reading to the total
index = (index + 1); // advance to the next index
if (index >= NUMREADINGS) // if we're at the end of the array...
index = 0; // ...wrap around to the beginning
average = total / NUMREADINGS; // calculate the average
Serial.println(val); // use output to aid in calibrating
}
}
</code>
Capacitive Sense.
<code>
#include <CapacitiveSensor.h> /* * CapitiveSense Library Demo Sketch * Paul Badger 2008 * Uses a high value resistor e.g. 10 megohm between send pin and receive pin * Resistor effects sensitivity, experiment with values, 50 kilohm - 50 megohm. Larger resistor values yield larger sensor values. * Receive pin is the sensor pin - try different amounts of foil/metal on this pin * Best results are obtained if sensor foil and wire is covered with an insulator such as paper or plastic sheet */ CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2); // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil CapacitiveSensor cs_4_5 = CapacitiveSensor(4,5); // 10 megohm resistor between pins 4 & 6, pin 6 is sensor pin, add wire, foil CapacitiveSensor cs_4_8 = CapacitiveSensor(4,8); // 10 megohm resistor between pins 4 & 8, pin 8 is sensor pin, add wire, foil void setup() { cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example Serial.begin(9600); } void loop() { long start = millis(); long total1 = cs_4_2.capacitiveSensor(30); long total2 = cs_4_5.capacitiveSensor(30); long total3 = cs_4_8.capacitiveSensor(30); Serial.print(millis() - start); // check on performance in milliseconds Serial.print("\t"); // tab character for debug windown spacing Serial.print(total1); // print sensor output 1 Serial.print("\t"); Serial.print(total2); // print sensor output 2 Serial.print("\t"); Serial.println(total3); // print sensor output 3 delay(10); // arbitrary delay to limit data to serial port }
</code>
Temp Sensor.
<code.>
//declare variables
float tempC;
int tempPin = 0;
void setup()
{
Serial.begin(9600); //opens serial port, sets data rate to 9600 bps
}
void loop()
{
tempC = analogRead(tempPin); //read the value from the sensor
tempC = (5.0 * tempC * 100.0)/1024.0; //convert the analog data to temperature
Serial.print((byte)tempC); //send the data to the computer
delay(1000); //wait one second before sending new data
}
</code>
Flood Sensor.
<code>
/* Flood Sensor
This sketch Display message when water (anything conductive) bridges the gap in the sensor.
created 02/09/09
by n00b
*/
const int floodSensors = 2; // the number of the Flood Sensor pin
// variables will change:
int floodSensorState = 0; // variable for reading the floodSensors status
void setup() {
// initialize the flood Sensor pin as an input:
pinMode(floodSensors, INPUT);
Serial.begin(9600);
}
void loop(){
// read the state of the flood Sensor value:
floodSensorState = digitalRead(floodSensors);
// check if the flood Sensor is wet.
// if it is, the floodSensorState is HIGH:
if (floodSensorState == HIGH) {
// turn LED on:
Serial.println(floodSensorState);
Serial.println("Warning there is a flood");
}
else {
// Not flood:
// Serial.println(floodSensorState);
// Serial.println(floodSensorState);
}
}
</code>
Check Out Web Page From the Arduino.
You will need to copy your ip setting somewhere so that you can restore them when you finnish.
Then set new ip adress settings so the two can talk with each other.
Nexus 7
IPadress: 192.168.1.20
Netmask 255.255.255.0
Gateway 192.1168.1.20
Arduino
IPadress: 192.168.1.17
Netmask 255.255.255.0
Gateway 192.1168.1.20
<code>
Web Server
A simple web server
Circuit:
* Ethernet shield attached to pins oA, 0B 0C, 0D
*/
//——————————————————————————————————-
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0×90, 0xA2, 0xDA, 0x0D, 0×48, 0xD3 };
// assign an IP address for the controller:
IPAddress gateway(192,168,1,1);
IPAddress subnet(255, 255, 255, 0);
EthernetServer server(80);
String readString;
//——————————————————————————————————-
//————————————————-
// Any extra codes for Declaration :
int led = 8;
//——————————————————————————————————-
void setup()
{
//————————————————-
pinMode(led, OUTPUT); //pin selected to control
//——————————————————————————————————-
//enable serial data print
Serial.begin(9600);
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.print(“Server is at “);
Serial.println(Ethernet.localIP());
Serial.println(“LED Controller Test 1.0″);
}
//——————————————————————————————————-
//——————————————————————————————————-
{
// listen for incoming clients
EthernetClient client = server.available();
if (client)
Serial.println(“new client”);
{
if (client.available())
char c = client.read();
if (readString.length() < 100)
readString += c;
//Serial.print(c);
Serial.write(c);
// if you’ve gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
//if HTTP request has ended
if (c == ‘\n’) {
Serial.println(readString); //print to serial monitor for debuging
//——————————————————————————————————–
// Needed to Display Site:
client.println(“HTTP/1.1 200 OK”); //send new page
client.println(“Content-Type: text/html”);
client.println();
client.println(“<HTML>”);
client.println(“<HEAD>”);
//————————————————-
client.println(“<center>”);
client.println(“</HEAD>”);
client.println(“<BODY>”);
client.println(“<H1>Home Automation</H1>”);
client.println(“<hr />”);
client.println(“<center>”);
client.println(“<br />”);
client.println(“<br />”);
client.println(“<a href=\”/?lightoff\”\”>Turn Off Light</a><br />”);
client.println(“</HTML>”);
//stopping client
client.stop();
// Code which needs to be Implemented:
if(readString.indexOf(“?lighton”) >0)//checks for on
{
digitalWrite(8, HIGH); // set pin 4 high
Serial.println(“Led On”);
}
else{
if(readString.indexOf(“?lightoff”) >0)//checks for off
{
digitalWrite(8, LOW); // set pin 4 low
Serial.println(“Led Off”);
}
}
//clearing string for next read
readString=”";
delay(1);
// close the connection:
client.stop();
Serial.println(“client disonnected”);
}
}
}
}
}