Also check out or community

PostsWifi

Turning on LED with a press of a button through RTW

This is a guide for connecting a button to the nodemcu(esp32 board with wifi) to send a simple on or off signal to RTW(Real Time Web) and also receiving the signal with another nodemcu board to turn a LED on or off. This guide is perfect for beginners as it doesn’t involve much skills and can easily be completed by anyone as well as give you a glimpse of what you can do with RTW. This guide requires an account at NDS(Nornir Domain Service)

Hardware:

1 X Red Jumper Wires
2 X Black Jumper Wires
2 X Jumper Wires(Color of choice)
1 X DHT11 Temperature and Humidity Sensor
1 X LED
2 X MicroControllers(I used two ESP32 board with wifi capabilities, you can use whatever you want)

 

Schematics:

 

DHT11 Temprature Sensor Sensor
GND (-) GND (-)
3.3V/5V (+) Vcc (+)
pin 13 Signal pin

 

ESP32 with LED Sensor
GND (-) GND (-)
pin 13 3.3V/5V (+) Vcc (+)/Signal pin

 

Note that the pins may vary from board to board, so if you don’t have the specific board I’m using, just plug it into a digital pin and it should work. If your are using any other boards, please check if it have Ethernet capabilities because you will need that in this guide.

 

this is for turning led on when right signal is received(this is a sketch of arduino board because I couldn’t find the board I’m currently using in the fritzing software, but it usually works the same):

This is for the the device that sends the button state:

 

Source Code:

The two devices connect to each connect via RTW so get this to work, you have to have to set up an RTW account and service and downloaded the nornir library.

 


</pre>
#include <Arduino.h>
#include <Nornir_ESP32.h>

// RTW config
const String host = "http://[domain].rtw.no/[service]?objectID=[id number]&token=[your token]";

int button = 13;
int buttonState = 0;

void setup()
{
// Default baudrate for ESP8266 is 115200
Serial.begin(115200);

// Initialize signal pin
pinMode(button, INPUT);

// SSID and password for your WIFI Router ("SSID", "password");

Serial.println("Connecting Wifi...");
WiFi.begin("SSID", "password");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void loop()
{
buttonState = digitalRead(button);

Serial.println(String(buttonState));
// Check WiFi Connection
if (WiFi.status() == WL_CONNECTED)
{
if(buttonState == HIGH){

//active is the label and what behind is the value you will send to rtw
RTW_send_data(host, "active=1");
}else{
RTW_send_data(host, "active=0");
}
}
else
{
Serial.println("Error: WiFi not connected!");
delay(1000);
}
}
<pre>

This is the code for the LED device:

</pre>
#include <Arduino.h>
#include <Nornir_ESP32.h>

// RTW config
const String host = "http://[domain].rtw.no/[service]";

const String login = "objectID=[id number]&token=[your token]";

#define BUILTIN_LED 2

void setup()
{
// Default baudrate for ESP8266 is 115200
Serial.begin(115200);

// Initialize signal pin
pinMode(BUILTIN_LED, OUTPUT);

// SSID and password for your WIFI Router ("SSID", "password");

Serial.println("Connecting Wifi...");
WiFi.begin("SSID", "password");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void loop()
{
buttonState = digitalRead(button);

Serial.println(String(buttonState));
// Check WiFi Connection
if (WiFi.status() == WL_CONNECTED)
{

RTW_get_data(host, login, rtw_callback, NULL);
}
else
{
Serial.println("Error: WiFi not connected!");
delay(1000);
}

}

void rtw_callback(String received_data)
{
//Gets the message form RTW
RTW_message message(received_data);

for (int i = 0; i < message.size; i++)
{
if (message.RTW_data[i].tag == "ACTIVE")
{
if (message.RTW_data[i].data == "0")
{
digitalWrite(BUILTIN_LED, LOW);
}
if (message.RTW_data[i].data == "1")
{
digitalWrite(BUILTIN_LED, HIGH);
}
}
}
}
<pre>
Related posts
NB-IoTPosts

Connecting NB-IoT to RTW with Telenor

PostsWifi

Code used to connect to WIFI with Esp32 (Referenced as WifiCode)

PostsWifi

Turning on the lights with a clap

PostsWifi

Automatic trash can opener based on distance

Legg igjen en kommentar

Din e-postadresse vil ikke bli publisert. Obligatoriske felt er merket med *