Hello, i am breaking my head about my code.
I have seperate codes working. But when i combine code it doesnt parse variables.
I have a wemos D1 mini set up with LoRa. it sends message like: AT+SEND=3,1,2
on the second wemos d1 mini i recieve this message
the code breaks it up in channel_id = 3
length of message = 1
and message = 3 (this last number is important)
i have to recieve the number and convert it into a string (1 for Door, 2 for kitchen, 3 for Bar)
then i want to send that text to the oled screen.
it doesnt remember the message?!?
cant figure out why.
here is my receive code:
/***************************************************************************/
// Deze werkt tot nu toe
/***************************************************************************/
#include <SoftwareSerial.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Arduino.h>
#include <EEPROM.h>
int address = 0; // EEPROM address to store the value
// ^^^^^^^^^^^^^^^^^^^ dont use this yet ^^^^^^^^
#include <Fonts/FreeSerifBold9pt7b.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int buttonPin = 0; // Definieer de pin waarop de knop is aangesloten
// connect switch between pin D3 (GPIO0) en GND
#define RXD2 14 //pin D6 op de Wemos D1 mini
#define TXD2 12 //pin D5 op de Wemos D1 mini
SoftwareSerial mySerial(RXD2, TXD2);
#define TARGET_ID 3 // dit DEVICE
String Message = "DIT IS DE ONTVANGER OP ADDRESS 3";
String content = "";
String lora_band = "868500000"; // setting LoRa Band frequency
String incomming = "";
String channel_ID = "";
String message = ""; // Declare 'message' as a global variable
void setup() {
pinMode(buttonPin, INPUT); // Stel de knop in als invoer
Serial.begin(9600);
mySerial.begin(115200);
delay(1000);
mySerial.println("AT+BAND=" + lora_band);
delay(1000);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
display.setFont(&FreeSerifBold9pt7b); // Set custom Font (built in GFX lib)
// just for fun (small arial)
display.drawLine(5, 5, 15, 15, SSD1306_WHITE);
display.drawLine(15, 15,25,5, SSD1306_WHITE);
display.drawLine(15, 5,15,30, SSD1306_WHITE);
// Show the display buffer on the screen. You MUST call display.display() after
// drawing commands to make them visible on screen!
display.display();
delay(2000);
// display.display() is NOT necessary after every single drawing command,
// unless that's what you want...rather, you can batch up a bunch of
// drawing operations and then update the screen all at once by calling
// display.display(). These examples demonstrate both approaches...
}
void loop() {
if (Serial.available()){
Serial.println("Writing");
content = Serial.readString();
content.trim();
Serial.println();
content = content + "\r\n";
char* bufc = (char*) malloc(sizeof(char) * content.length() + 1);
content.toCharArray(bufc, content.length() + 1);
mySerial.write(bufc);
free(bufc);
}
if (mySerial.available()) {
String incomming = mySerial.readString();
if (incomming.length() <= 10){ // kleiner of gelijk aan....
digitalWrite(2, LOW);
delay(2000); // light led for 2 seconds
digitalWrite(2, HIGH);
Serial.println(incomming); // voor seriele monitor input
}else {
String channel_ID = incomming.substring(incomming.indexOf('=') + 1, incomming.indexOf(','));
Serial.println("Channel ID : " + channel_ID);
String str = incomming.substring(incomming.indexOf(',') + 1);
String msgLength = str.substring(0, str.indexOf(','));
Serial.println("Message Length : " + msgLength);
String str2 = str.substring(str.indexOf(',') + 1);
String message = str2.substring(0, str2.indexOf(','));
Serial.println("Message : >" + message+"<"); // dit word geprint
testtekst(message);
}
}
/* Controleren of de knop is ingedrukt */
if (digitalRead(buttonPin) == LOW) {
//message = "3";
testtekst(message); // send message to Oled
delay(100);
}
}
//--------------------------------------------------------------------------------------------------------------------------------
void testtekst(String message) { // sub routine
//message.trim(); // Trim the message in place
if (message == "1") {
Serial.println("recieved >" + message +"<");
display.clearDisplay(); // wis buffer voor de nieuwe text (lijkt me eigenlijk overbodig)
display.setTextSize(2); // text size 2
display.setTextColor(SSD1306_WHITE); // neem witte text kleur
display.setCursor(20, 24); // Start positie cursor
display.println(F("Deur")); // vul buffer met text (Deur)
display.display(); // geef buffer inhoud weer op het scherm (Deur)
delay(2000); // pause 2 seconden
display.clearDisplay(); // wis buffer inhoud (maar niet van de display)
display.display(); // geef buffer inhoud op het oled weer (in dit geval dus leeg scherm)
} else if (message == "2") {
Serial.println("recieved " + message);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(1, 24);
display.println(F("Keuken"));
display.display();
delay(2000);
display.clearDisplay();
display.display();
} else if (message == "3") {
Serial.println("recieved " + message);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(40, 24);
display.println(F("Bar"));
display.display();
delay(2000);
display.clearDisplay();
display.display();
} else {
Serial.println("Not Valid : >" + message + "<");
}
}
}
can sombody help me ?
this is what i recive in serial monitor:
01:54:09.148 -> Channel ID : 3
01:54:09.148 -> Message Length : 1
01:54:09.148 -> Message : >1<
01:54:09.148 -> recieved >1<
this got send:
01:55:50.610 -> BUTTON PRESSED
01:55:50.610 -> AT+SEND=4,1,1
01:55:50.610 ->
01:55:53.734 -> +OK
01:55:53.734 ->