QC

Connect Sonoff Basic Switch to Arduino IoT Cloud

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

Devices and components

Jumper wires (generic)

SparkFun FTDI Base Bypass – 3.3V

5 position male connector - 1 row - Long (0.1")

Sonoff Basic

Materials and tools

Soldering iron (generic)

Software and tools

Arduino IoT Cloud

Project description

Overview

Setting up Sonoff Basic (ESP8285)

3.3V - 3.3V

GND - GND

RX - TX

TX - RX

IoT Cloud

relay

boolean

device ID

secret key

#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);

}

}

When the relay is true, enable relayPin and ledPin (write a HIGH state).

relay

true

relayPin

ledPin

When the relay is false, turn off the relayPin and ledPin (write a LOW state).

relay

relayPin

ledPin

relay

Full Code

arduino

1/*
2This sketch only works with IoT Cloud, with the right variables created inside of your Thing.
3*/
4
5#include "thingProperties.h"
6
7
8//define the outputs
9int relayPin = 12;
10int ledPin = 13;
11
12void setup() {
13 // Initialize serial and wait for port to open:
14 Serial.begin(9600);
15
16 pinMode(relayPin, OUTPUT); //define relay as output
17
18 // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
19 delay(1500);
20
21 // Defined in thingProperties.h
22 initProperties();
23
24 // Connect to Arduino IoT Cloud
25 ArduinoCloud.begin(ArduinoIoTPreferredConnection);
26
27 /*
28 The following function allows you to obtain more information
29 related to the state of network and IoT Cloud connection and errors
30 the higher number the more granular information you’ll get.
31 The default is 0 (only errors).
32 Maximum is 4
33 */
34 setDebugMessageLevel(2);
35 ArduinoCloud.printDebugInfo();
36}
37
38void loop() {
39 ArduinoCloud.update();
40 //leave the loop empty
41}
42
43void onRelayChange() {
44 // Add your code here to act upon Relay change
45 if (relay) {
46 digitalWrite(relayPin, HIGH);
47 digitalWrite(ledPin, HIGH);
48 }
49 else {
50 digitalWrite(relayPin, LOW);
51 digitalWrite(ledPin, LOW);
52 }
53}

Full Code

arduino

1/*
2This sketch only works with IoT Cloud, with the right variables
3 created inside of your Thing.
4*/
5
6#include "thingProperties.h"
7
8
9//define
10 the outputs
11int relayPin = 12;
12int ledPin = 13;
13
14void setup() {
15
16 // Initialize serial and wait for port to open:
17 Serial.begin(9600);
18
19
20 pinMode(relayPin, OUTPUT); //define relay as output
21
22 // This delay gives
23 the chance to wait for a Serial Monitor without blocking if none is found
24 delay(1500);
25
26
27 // Defined in thingProperties.h
28 initProperties();
29
30 // Connect to
31 Arduino IoT Cloud
32 ArduinoCloud.begin(ArduinoIoTPreferredConnection);
33
34
35 /*
36 The following function allows you to obtain more information
37 related
38 to the state of network and IoT Cloud connection and errors
39 the higher number
40 the more granular information you’ll get.
41 The default is 0 (only errors).
42
43 Maximum is 4
44 */
45 setDebugMessageLevel(2);
46 ArduinoCloud.printDebugInfo();
47}
48
49void
50 loop() {
51 ArduinoCloud.update();
52 //leave the loop empty
53}
54
55void
56 onRelayChange() {
57 // Add your code here to act upon Relay change
58 if (relay)
59 {
60 digitalWrite(relayPin, HIGH);
61 digitalWrite(ledPin, HIGH);
62 }
63
64 else {
65 digitalWrite(relayPin, LOW);
66 digitalWrite(ledPin, LOW);
67
68 }
69}

Downloadable files

Circuit

Circuit

Schematic

Schematic

Circuit

Circuit




Note: Content and images are from: https://projecthub.arduino.cc/, with some modifications.
If you want it removed due to copyright reasons, please leave a comment. Thank you.
I want to share this article more widely so that everyone knows about Arduino and your project.

Easy remote control

Remotely control an LED with ease. Devices and components Arduino Nano 33 BLE with headers Box 525 Resistors precision 1% - 17 values Breadb...