RemoteStation Arduino Sketch

This sketch demonstrates Internet-based remote communication using a companion ‘bridge’ program running on the attached host which sends and receives messages via an online MQTT server. This allows connecting one Arduino to another over arbitrary distances, which we can use as a starting point for building collaborative telepresence.

Full Source Code

The full code is all in one file RemoteStation.ino.

 1// RemoteStation.ino : demonstrate communication across the Internet using a Python-based MQTT bridge and MQTT server
 2
 3// This example sends and receives data over the host serial port representing
 4// I/O messages.  The arduino_mqtt_bridge.py Python application can be run on
 5// the attached host to relay these messages to a remote system using an MQTT
 6// server as a messaging hub.
 7
 8//================================================================
 9
10const int switchPin = 6;       // Specify a pin for user switch input.
11const long BAUD_RATE = 115200;  // The baud rate is the number of bits per second transmitted over the serial port.
12
13//================================================================
14
15// This function is called once after reset to initialize the program.
16void setup()
17{
18  // Initialize the Serial port for host communication.
19  Serial.begin(BAUD_RATE);
20
21  // Initialize the digital input/output pins.
22  pinMode(switchPin, INPUT);
23  pinMode(LED_BUILTIN, OUTPUT);
24}
25
26//================================================================
27// Polling function to process messages received over the serial port from the
28// remote Arduino.  Each message is a line of text containing a single integer
29// as text.
30
31void serial_input_poll(void)
32{
33  while (Serial.available()) {
34    // read the text and convert to an integer
35    int value = Serial.parseInt();
36
37    // drive the LED to indicate the value
38    if (value) {
39      digitalWrite(LED_BUILTIN, HIGH);
40    } else {
41      digitalWrite(LED_BUILTIN, LOW);
42    }
43    
44    // flush any remaining characters until the line end
45    Serial.find('\n');
46  }
47}
48//================================================================
49// Polling function to transmit data both periodically and whenever the input
50// changes.
51
52void hardware_input_poll(void)
53{
54  static int last_switch_input = 0;
55      
56  const unsigned long message_timeout = 2000;  // send every 2 seconds at minimum
57  static unsigned long last_time = 0;
58
59  // capture the current state
60  int switch_input = digitalRead(switchPin);  
61  unsigned long now = millis();
62
63  // if the state changed or the timer expired, send data
64  if ((switch_input != last_switch_input) || ((now - last_time) > message_timeout)) {
65    last_time = now;
66    last_switch_input = switch_input;
67
68    // send the message as a single integer on a text line
69    Serial.println(switch_input);
70  }
71}
72  
73/****************************************************************/
74/// Standard Arduino polling function to handle all I/O and periodic processing.
75/// This loop should never be allowed to stall or block so that all tasks can be
76/// constantly serviced.
77void loop()
78{
79  serial_input_poll();
80  hardware_input_poll();
81}
82/****************************************************************/