peterwestvillage
Published © CC BY

Connect Sonoff Basic Switch to the Arduino IoT Cloud

Learn how to control real-life devices via the Sonoff Basic Switch, by programming and connecting it to the Arduino IoT Cloud.

IntermediateFull instructions provided6,676
Connect Sonoff Basic Switch to the Arduino IoT Cloud

Things used in this project

Hardware components

Sonoff Basic
Itead Sonoff Basic
×1
SparkFun FTDI Basic Breakout - 3.3V
×1
Jumper wires (generic)
Jumper wires (generic)
×4
Male-Header 5 Position- 1 Row- Long (0.1")
Male-Header 5 Position- 1 Row- Long (0.1")
×1

Software apps and online services

Arduino IoT Cloud
Arduino IoT Cloud

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Circuit

Schematic

Code

Full Code

Arduino
/* 
This sketch only works with IoT Cloud, with the right variables created inside of your Thing.
*/

#include "thingProperties.h"


//define the outputs
int relayPin = 12;
int ledPin = 13;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);

  pinMode(relayPin, OUTPUT); //define relay as output

  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
  */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  //leave the loop empty
}

void onRelayChange()  {
  // Add your code here to act upon Relay change
  if (relay) {
    digitalWrite(relayPin, HIGH);
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(relayPin, LOW);
    digitalWrite(ledPin, LOW);
  }
}

Credits

peterwestvillage
1 project • 4 followers
Contact

Comments

Please log in or sign up to comment.