Code
Open Sketch
After installing all of the dependencies, navigate to the example sketch for this project:
Navigate to the adafruitio_08_analog_in sketch by opening the File -> Examples -> Adafruit IO Arduino menu.
Configure Sketch
Within Arduino IDE, click the config.h tab. Set the IO_USERNAME field to your Adafruit IO username. Then, set the IO_KEY field to the value of your Active Key.
By default, WiFi is enabled in example sketches. Within the config.h file, set WIFI_SSID to the name of your wireless network. Then, set WIFI_PASS to the password for your WiFi network.
Upload Sketch
In the Arduino IDE, select the board you are using from the Tools -> Board menu.
Then, using the Tools->Port menu, select the proper COM port on Windows, or USB device on macOS or Linux.
Click the arrow icon to upload the example sketch to your board.
It might take a while, but you should see an Upload Complete message at the bottom of the window when the process has finished.
You can now click the serial monitor icon (magnifying glass) to view the output of the sketch.
Your board should connect to Adafruit IO.
You can now cover the photocell with your hand, and you should see the changing values being sent to Adafruit IO.
If not, check your WiFi and Adafruit IO credentials in config.h and try uploading your sketch again using the process above.
The adafruitio_08_analog_in example uses pin A0 by default on all boards, and that can be modified if needed by changing the PHOTOCELL_PIN define.
/************************ Example Starts Here *******************************/ // analog pin 0 #define PHOTOCELL_PIN A0
The next chunk of code sets up two integer variables to track the state of the photocell and an Adafruit IO Feed instance for a feed named analog.
// photocell state
int current = 0;
int last = -1;
// set up the 'analog' feed
AdafruitIO_Feed *analog = io.feed("analog");
The setup function connects your feather to Adafruit IO. The code will wait until you have a valid connection to Adafruit IO before continuing with the sketch. If you have any issues connecting, check config.h for any typos in your username or key.
void setup() {
// start the serial connection
Serial.begin(115200);
// wait for serial monitor to open
while(! Serial);
// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();
// wait for a connection
while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}
// we are connected
Serial.println();
Serial.println(io.statusText());
}
Next, we have the main loop() function. The first line of the loop function calls io.run(); this line will need to be present at the top of your loop in every sketch. It helps keep your device connected to Adafruit IO and processes any incoming data.
void loop() {
// io.run(); is required for all sketches.
// it should always be present at the top of your loop
// function. it keeps the client connected to
// io.adafruit.com, and processes any incoming data.
io.run();
The next chunk of code inside the loop() checks the current value of the photocell, and saves the value of the photocell in the current variable.
We then check if the current photocell value is equal to the last photocell value. If it is equal, we will return early and not continue with the rest of the loop.
// grab the current state of the photocell current = analogRead(PHOTOCELL_PIN); // return if the value hasn't changed if(current == last) return;
The final chunk of the loop() function prints the current value to the Arduino Serial Monitor, and sends the current value to the analog feed on Adafruit IO. We also set last = current; so we can tell if the value of the photocell has changed in the next run of the loop.
// save the current state to the analog feed
Serial.print("sending -> ");
Serial.println(current);
analog->save(current);
// store last photocell state
last = current;
// wait one second (1000 milliseconds == 1 second)
delay(1000);
}
Page last edited
Text editor powered by tinymce.