Shivani Kannan - Working with Networking
Made by Shivani Kannan
Made by Shivani Kannan
Developing paired devices and networked interactions with Particle.publish
Created: December 3rd, 2024
Developing paired devices and networked interactions with Particle.publish
Components: Breadboard, jumper wires, touch sensors, LED, resistor, Particle photon
1. Creating a circuit with touch sensor, LED, and resistor on the same board
2. Writing code for the simple circuit with a touch sensor to test the circuit out & get familiar with it.
3. Creating new circuits: Touch sensor on photon 1 and LED on photon 2
4. Writing code
5. Debugging to realize we need to use webhooks
6. Learning about webhooks
7. Integrating webhook
8. Using perplexity for code
9. Code doesn't work
10. Repromting... repromting... repromting
11. Restarting from scratch & asking an ECE friend to help
12. Trying other ways of integration: using JSON instead of Webform
14. Trying 'Things Speak' instead of Webhook
15. Restarting from scratch
1
#define TOUCH_SENSOR_PIN D2 // Pin connected to the capacitive touch sensor
bool wasTouched = false; // Tracks the previous state of the sensor
void setup() {
pinMode(TOUCH_SENSOR_PIN, INPUT); // Set the sensor pin as input
Serial.begin(9600); // Optional: Debugging
}
void loop() {
// Read the current state of the touch sensor
int sensorValue = digitalRead(TOUCH_SENSOR_PIN);
if (sensorValue == HIGH && !wasTouched) {
// If the sensor is touched and it wasn't previously touched
wasTouched = true; // Update the state
Serial.println("Touch detected! Publishing event.");
Particle.publish("toggle_led", "on", PRIVATE); // Publish the event
}
else if (sensorValue == LOW && wasTouched) {
// If the sensor is not touched anymore, reset the state
wasTouched = false;
Serial.println("Touch released.");
}
delay(50); // Small delay for stability
}
Click to Expand
void setup() {
Particle.subscribe("toggle_led", eventHandler);//, MY_DEVICES);
pinMode(D7, OUTPUT); // LED pin
Serial.begin(9600); // Optional: Debugging
}
void eventHandler(const char *event, const char *data) {
Serial.printlnf("Received event: %s, data: %s", event, data);
if (strcmp(data, "on") == 0) {
digitalWrite(D7, HIGH); // Turn on the LED
delay(1000); // Keep the LED on for 5 seconds
digitalWrite(D7, LOW); // Turn off the LED
}
}
void loop() {
// Continuously listen for events
}
Click to Expand
The process was quite challenging. Our team spent a lot of time (8hrs over 2 days) trying to get our particle boards to communicate, to the point where we also got a friend from ECE to help out. It was, however, still not working: while the event was getting published on Board 1's console, Board 2 was unable to listen to the event.
Developing paired devices and networked interactions with Particle.publish