Hi there, I am trying to check whether the Serial Buffer contains a specific string, if so i would like to turn on one LED, but for some reason i can read an print out the Serial Buffer but i cannot check whether it is the sentence i want it to be. Btw, i am using an ESP32 connected to an Arduino Mega.
This is the code for Arduino Mega:
//Code for Arduino MEGA
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
uint8_t leds_cycle[] = { 5, 6, 7};
uint8_t leds_control[] = {0, 2, 3, 4};
int quant = 20;
int led_passed = 0;
int led_delay = 1000;
int current_led = 0;
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
lcd.begin();
lcd.backlight();
lcd.clear();
lcd.setCursor(4,0);
Serial.begin(115200);
}
void loop() {
led_passed += quant;
if (Serial.available()){
//Serial.println("It is working in the Serial available");
String Incoming = Serial.readString();
Incoming.trim();
String Incoming1 = Serial.readString();
Incoming1.trim();
Serial.println(Incoming);
Serial.println(Incoming1);
delay(1000);
if(Incoming == "Switch 1"){
Serial.println("It is coming in the if statement");
if(Incoming1 == "1")
digitalWrite(2, HIGH);
digitalWrite(2,LOW);
}
else if(Incoming.toInt() == "3"){
String State = Serial.readString();
Serial.print("Setting LED ");
Serial.print(Incoming);
Serial.print(" to state ");
Serial.println(State);
if(State == "true" || State == "TRUE"){
digitalWrite(3, HIGH);
}
digitalWrite(3, LOW);
}
else if(Incoming.toInt() == "4"){
String State = Serial.readString();
Serial.print("Setting LED ");
Serial.print(Incoming);
Serial.print(" to state ");
Serial.println(State);
if(State == "true" || State == "TRUE"){
digitalWrite(4, HIGH);
}
digitalWrite(4, LOW);
}
else {
led_delay = Incoming.toInt();
}
}
if (led_passed > led_delay) {
digitalWrite(leds_cycle[current_led], LOW);
led_passed = 0;
current_led = current_led >= 2 ? 0 : (current_led + 1);
// Turn on next LED in a row
digitalWrite(leds_cycle[current_led], HIGH);
delay(3000);
/* Serial.print("Current LED: ");
Serial.println(current_led);
Serial.print("led_passed: ");
Serial.println(led_passed);
Serial.print("led_delay: ");
Serial.println(led_delay);
Serial.println('\n');*/
}
}
This is the code for ESP32:
//Code for ESP32
#include <WiFi.h>
#include <DHTesp.h>
#include <ThingsBoard.h>
#include <HardwareSerial.h>
#define COUNT_OF(x)((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
#define WIFI_USER "YOURSSID"
#define WIFI_PASS "YOURPASS"
#define TOKEN "YOURTOKEN"
#define THINGSBOARD_SERVER "YOURTHINGSBOARDSERVER"
#define echoPin 13
#define trigPin 12
#define DHT_PIN 14
HardwareSerial SerialPort(2);
WiFiClient espClient;
ThingsBoard tb(espClient);
int status = WL_IDLE_STATUS;
uint8_t leds_cycle[] = {5, 6, 7};
uint8_t leds_control[] = {0, 2, 3, 4};
DHTesp dht;
int quant = 20;
int led_delay = 1000;
int send_delay = 2000;
int led_passed = 0;
int send_passed = 0;
int current_led = 0;
bool subscribed = false;
long duration;
int distance;
RPC_Response processDelayChange(const RPC_Data &data)
{
led_delay = data;
SerialPort.print(led_delay);
return RPC_Response(NULL, led_delay);
}
RPC_Response processGetDelay(const RPC_Data &data)
{
return RPC_Response(NULL, led_delay);
}
RPC_Response processSetSwitch1(const RPC_Data &data)
{
Serial.println("Received the set GPIO RPC method");
bool onoff = data;
SerialPort.println("Switch 1");
SerialPort.println(onoff);
return RPC_Response(NULL, onoff);
}
RPC_Response processSetSwitch2(const RPC_Data &data)
{
Serial.println("Received the set GPIO RPC method");
bool onoff = data;
SerialPort.print("Switch data: ");
SerialPort.println(onoff);
return RPC_Response(NULL, onoff);
}
RPC_Response processSetSwitch3(const RPC_Data &data)
{
Serial.println("Received the set GPIO RPC method");
bool onoff = data;
SerialPort.print("Switch data: ");
SerialPort.println(onoff);
return RPC_Response(NULL, onoff);
}
RPC_Callback callbacks[] = {
{ "setValue", processDelayChange },
{ "getValue", processGetDelay },
{ "setSwitch1", processSetSwitch1 },
{ "setSwitch2", processSetSwitch2 },
{ "setSwitch3", processSetSwitch3 },
};
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_USER, WIFI_PASS);
Serial.begin(115200);
SerialPort.begin(115200, SERIAL_8N1, 16, 17);
InitiateWiFi();
dht.setup(DHT_PIN, DHTesp::DHT22);
}
void loop() {
delay(quant);
led_passed += quant;
send_passed += quant;
// Reconnect to WiFi, if needed
if (WiFi.status() != WL_CONNECTED) {
reconnect();
return;
}
if (!tb.connected()) {
subscribed = false;
if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) {
return;
}
}
// Subscribe for RPC, if needed
if (!subscribed) {
if (!tb.RPC_Subscribe(callbacks, COUNT_OF(callbacks))) {
return;
}
subscribed = true;
}
// Check if it is a time to send DHT22 temperature and humidity
if (send_passed > send_delay) {
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
TempAndHumidity lastValues = dht.getTempAndHumidity();
if (isnan(lastValues.humidity) || isnan(lastValues.temperature)) {
} else {
tb.sendTelemetryFloat("temperature", lastValues.temperature);
tb.sendTelemetryFloat("humidity", lastValues.humidity);
}
tb.sendTelemetryFloat("Distance", distance);
send_passed = 0;
}
// Process messages
tb.loop();
}
void InitiateWiFi(){
while (WiFi.status() != WL_CONNECTED){
delay(500);
}
}
void reconnect(){
status = WiFi.status();
if (status != WL_CONNECTED){
WiFi.begin(WIFI_USER, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED){
delay(500);
}
}
}