arduino osc code (pins need to be adjusted))

main
louisafriederike 7 months ago
parent 4e694724af
commit b6da3ccc1e

@ -1,55 +0,0 @@
#include <SPI.h>
#include <LoRa.h>
#define LORA_SS_PIN 10
#define LORA_RST_PIN 9
#define LORA_DI0_PIN 2
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(LED_BUILTIN, OUTPUT);
// Initialize LoRa module
LoRa.setPins(LORA_SS_PIN, LORA_RST_PIN, LORA_DI0_PIN);
if (!LoRa.begin(433E6)) {
Serial.println("LoRa initialization failed. Check your wiring!");
while (true);
}
// Connect to WiFi or Ethernet here
// Connect to Socket.IO server
// Replace <SERVER_ADDRESS> with the actual server address
// e.g., http://localhost:3000
// Replace <TOKEN> with an authentication token if required
// e.g., "?token=abcd1234"
//socketIO.connect("<SERVER_ADDRESS><TOKEN>");
}
void loop() {
// Handle Socket.IO events or other tasks here
// Check for incoming LoRa messages
int packetSize = LoRa.parsePacket();
if (packetSize) {
while (LoRa.available()) {
String message = LoRa.readString();
Serial.println("Received message: " + message);
// Process the received message and control the LED
if (message == "led_on") {
digitalWrite(LED_BUILTIN, HIGH); // Turn on the LED
LoRa.beginPacket();
LoRa.print("ack");
LoRa.endPacket();
} else if (message == "led_off") {
digitalWrite(LED_BUILTIN, LOW); // Turn off the LED
LoRa.beginPacket();
LoRa.print("ack");
LoRa.endPacket();
}
}
}
}

@ -1,57 +0,0 @@
#include <LoRa.h>
#include "boards.h"
void setup()
{
initBoard();
// When the power is turned on, a delay is required.
delay(1500);
Serial.println("LoRa Receiver");
LoRa.setPins(RADIO_CS_PIN, RADIO_RST_PIN, RADIO_DI0_PIN);
if (!LoRa.begin(LoRa_frequency)) {
Serial.println("Starting LoRa failed!");
while (1);
}
}
void loop()
{
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
// Serial.print("Received packet '");
String recv = "";
// read packet
while (LoRa.available()) {
recv += (char)LoRa.read();
}
Serial.println(recv);
// // print RSSI of packet
// Serial.print("' with RSSI ");
// Serial.println(LoRa.packetRssi());
////
#ifdef HAS_DISPLAY
if (u8g2) {
u8g2->clearBuffer();
char buf[256];
u8g2->drawStr(0, 12, "Received OK!");
u8g2->drawStr(0, 26, recv.c_str());
snprintf(buf, sizeof(buf), "RSSI:%i", LoRa.packetRssi());
u8g2->drawStr(0, 40, buf);
snprintf(buf, sizeof(buf), "SNR:%.1f", LoRa.packetSnr());
u8g2->drawStr(0, 56, buf);
u8g2->sendBuffer();
}
#endif
}
}

@ -1,85 +0,0 @@
#include <LoRa.h>
#include "boards.h"
#include <SafeString.h>
#include <millisDelay.h>
//elapsedMillis timeElapsed;
//unsigned int interval = 20000;
millisDelay codetimer;
int counter = 0;
int percent = 0;
int prevPercent = 0;
long futuretime = 0;
long timetowait = 1000 * 3; //thelast number is the seconds
int secretcode = 21;
int addedRandom = 0;
void setup()
{
Serial.begin( 9600 );
initBoard();
// When the power is turned on, a delay is required.
delay(1500);
Serial.println("LoRa Sender");
LoRa.setPins(RADIO_CS_PIN, RADIO_RST_PIN, RADIO_DI0_PIN);
if (!LoRa.begin(LoRa_frequency)) {
Serial.println("Starting LoRa failed!");
while (1);
}
}
void loop(){
percent = round(analogRead(2) / 4095.00 * 100 + addedRandom);
if(percent != prevPercent) {
Serial.println(percent);
LoRa.beginPacket();
LoRa.print("node4 ");
LoRa.print(percent);
LoRa.endPacket();
prevPercent = percent;
futuretime = millis() + timetowait; // current time plus the timetowait
}
// delay(100);
// Serial.print("Sending packet: ");
// Serial.println(percent);
// send packet
#ifdef HAS_DISPLAY
if (u8g2) {
char buf[256];
dtostrf(percent,3,0,buf);
u8g2->clearBuffer();
String message = String(percent);
u8g2->drawStr(0, 20, " Gateway code:");
// snprintf(buf, sizeof(buf), "Sending: %d", counter);
u8g2->drawStr(0, 40, buf);
if(futuretime > millis()){ // checks the current time with the future time -> count down
u8g2->drawStr(0, 60, " connecting...");
}
if(percent == secretcode && futuretime+10000 < millis()){
u8g2->drawStr(0, 60, " connecting...");
addedRandom = random(0,10);
}
else if(percent == secretcode && futuretime < millis()) {
u8g2->drawStr(0, 60, " PORT: 20.15.18");
}
// u8g2->print(percent);
u8g2->sendBuffer();
}
#endif
counter++;
}

@ -1,169 +0,0 @@
#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include "utilities.h"
#ifdef HAS_SDCARD
#include <SD.h>
#include <FS.h>
#endif
#ifdef HAS_DISPLAY
#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C *u8g2 = nullptr;
#endif
#if defined(LILYGO_TBeam_V1_0) || defined(LILYGO_TBeam_V1_1)
#include <axp20x.h>
AXP20X_Class PMU;
bool initPMU()
{
if (PMU.begin(Wire, AXP192_SLAVE_ADDRESS) == AXP_FAIL) {
return false;
}
/*
* The charging indicator can be turned on or off
* * * */
// PMU.setChgLEDMode(LED_BLINK_4HZ);
/*
* The default ESP32 power supply has been turned on,
* no need to set, please do not set it, if it is turned off,
* it will not be able to program
*
* PMU.setDCDC3Voltage(3300);
* PMU.setPowerOutPut(AXP192_DCDC3, AXP202_ON);
*
* * * */
/*
* Turn off unused power sources to save power
* **/
PMU.setPowerOutPut(AXP192_DCDC1, AXP202_OFF);
PMU.setPowerOutPut(AXP192_DCDC2, AXP202_OFF);
PMU.setPowerOutPut(AXP192_LDO2, AXP202_OFF);
PMU.setPowerOutPut(AXP192_LDO3, AXP202_OFF);
PMU.setPowerOutPut(AXP192_EXTEN, AXP202_OFF);
/*
* Set the power of LoRa and GPS module to 3.3V
**/
PMU.setLDO2Voltage(3300); //LoRa VDD
PMU.setLDO3Voltage(3300); //GPS VDD
PMU.setDCDC1Voltage(3300); //3.3V Pin next to 21 and 22 is controlled by DCDC1
PMU.setPowerOutPut(AXP192_DCDC1, AXP202_ON);
PMU.setPowerOutPut(AXP192_LDO2, AXP202_ON);
PMU.setPowerOutPut(AXP192_LDO3, AXP202_ON);
pinMode(PMU_IRQ, INPUT_PULLUP);
attachInterrupt(PMU_IRQ, [] {
// pmu_irq = true;
}, FALLING);
PMU.adc1Enable(AXP202_VBUS_VOL_ADC1 |
AXP202_VBUS_CUR_ADC1 |
AXP202_BATT_CUR_ADC1 |
AXP202_BATT_VOL_ADC1,
AXP202_ON);
PMU.enableIRQ(AXP202_VBUS_REMOVED_IRQ |
AXP202_VBUS_CONNECT_IRQ |
AXP202_BATT_REMOVED_IRQ |
AXP202_BATT_CONNECT_IRQ,
AXP202_ON);
PMU.clearIRQ();
return true;
}
void disablePeripherals()
{
PMU.setPowerOutPut(AXP192_DCDC1, AXP202_OFF);
PMU.setPowerOutPut(AXP192_LDO2, AXP202_OFF);
PMU.setPowerOutPut(AXP192_LDO3, AXP202_OFF);
}
#else
#define initPMU()
#define disablePeripherals()
#endif
SPIClass SDSPI(HSPI);
void initBoard()
{
Serial.begin(115200);
Serial.println("initBoard");
SPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN);
Wire.begin(I2C_SDA, I2C_SCL);
#ifdef HAS_GPS
Serial1.begin(GPS_BAUD_RATE, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
#endif
#if OLED_RST
pinMode(OLED_RST, OUTPUT);
digitalWrite(OLED_RST, HIGH); delay(20);
digitalWrite(OLED_RST, LOW); delay(20);
digitalWrite(OLED_RST, HIGH); delay(20);
#endif
initPMU();
#ifdef HAS_SDCARD
SDSPI.begin(SDCARD_SCLK, SDCARD_MISO, SDCARD_MOSI, SDCARD_CS);
if (!SD.begin(SDCARD_CS, SDSPI)) {
Serial.println("setupSDCard FAIL");
} else {
uint32_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.print("setupSDCard PASS . SIZE = ");
Serial.print(cardSize);
Serial.println(" MB");
}
#endif
#ifdef BOARD_LED
/*
* T-BeamV1.0, V1.1 LED defaults to low level as trun on,
* so it needs to be forced to pull up
* * * * */
#if LED_ON == LOW
gpio_hold_dis(GPIO_NUM_4);
#endif
pinMode(BOARD_LED, OUTPUT);
digitalWrite(BOARD_LED, LED_ON);
#endif
#ifdef HAS_DISPLAY
Wire.beginTransmission(0x3C);
if (Wire.endTransmission() == 0) {
Serial.println("Started OLED");
u8g2 = new U8G2_SSD1306_128X64_NONAME_F_HW_I2C(U8G2_R0, U8X8_PIN_NONE);
u8g2->begin();
u8g2->clearBuffer();
u8g2->setFlipMode(0);
u8g2->setFontMode(1); // Transparent
u8g2->setDrawColor(1);
u8g2->setFontDirection(0);
u8g2->firstPage();
do {
u8g2->setFont(u8g2_font_inb19_mr);
u8g2->drawStr(0, 30, "LilyGo");
u8g2->drawHLine(2, 35, 47);
u8g2->drawHLine(3, 36, 47);
u8g2->drawVLine(45, 32, 12);
u8g2->drawVLine(46, 33, 12);
u8g2->setFont(u8g2_font_inb19_mf);
u8g2->drawStr(58, 60, "LoRa");
} while ( u8g2->nextPage() );
u8g2->sendBuffer();
u8g2->setFont(u8g2_font_fur11_tf);
delay(5000);
}
#endif
}

@ -1,100 +0,0 @@
#include <LoRa.h>
#include <Wire.h>
#include "boards.h"
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);
String tagId = "None";
String cleanString = "";
void setup()
{
Serial.begin(9600);
Serial.println("System initialized");
nfc.begin();
initLoRa();
}
void loop()
{
readNFC();
if (!cleanString.isEmpty()) {
sendLoRaData(cleanString);
}
}
void initLoRa()
{
Serial.println("Initializing LoRa...");
LoRa.setPins(RADIO_CS_PIN, RADIO_RST_PIN, RADIO_DI0_PIN);
if (!LoRa.begin(LoRa_frequency))
{
Serial.println("Starting LoRa failed!");
while (1);
}
Serial.println("LoRa initialized successfully!");
}
void sendLoRaData(String data)
{
String message = "node3 " + data; // Concatenate "node3 " with the value of cleanString
LoRa.beginPacket();
LoRa.print(message);
LoRa.endPacket();
}
void readNFC()
{
if (nfc.tagPresent())
{
NfcTag tag = nfc.read();
tagId = tag.getUidString();
Serial.print("Tag ID: ");
Serial.println(tagId);
if (tag.hasNdefMessage())
{
NdefMessage message = tag.getNdefMessage();
int recordCount = message.getRecordCount();
Serial.print("Number of NDEF records: ");
Serial.println(recordCount);
for (int i = 0; i < recordCount; i++)
{
NdefRecord record = message.getRecord(i);
int payloadLength = record.getPayloadLength();
byte payload[payloadLength];
record.getPayload(payload);
String payloadAsString = "";
for (int c = 0; c < payloadLength; c++)
{
payloadAsString += (char)payload[c];
}
cleanString = payloadAsString;
cleanString.remove(0, 3);
Serial.print("Payload: ");
Serial.println(cleanString);
String uid = record.getId();
if (uid != "")
{
Serial.print("Record ID: ");
Serial.println(uid);
}
}
}
else
{
Serial.println("No NDEF message found on the tag.");
}
delay(2000);
}
}

@ -1,167 +0,0 @@
#pragma once
/*
* arduinoLoRa Library just only support SX1276/Sx1278,Not support SX1262
* */
// #define LILYGO_TBeam_V0_7
#define LILYGO_TBeam_V1_0
// #define LILYGO_TBeam_V1_1
// #define LILYGO_T3_V1_0
// #define LILYGO_T3_V1_6
// #define LILYGO_T3_V2_0
// #define LILYGO_T95_V1_0
/*
* if you need to change it,
* please open this note and change to the frequency you need to test
* Option: 433E6,470E6,868E6,915E6
* */
#define LoRa_frequency 868E6
#define UNUSE_PIN (0)
#if defined(LILYGO_TBeam_V0_7)
#define GPS_RX_PIN 12
#define GPS_TX_PIN 15
#define BUTTON_PIN 39
#define BUTTON_PIN_MASK GPIO_SEL_39
#define I2C_SDA 21
#define I2C_SCL 22
#define RADIO_SCLK_PIN 5
#define RADIO_MISO_PIN 19
#define RADIO_MOSI_PIN 27
#define RADIO_CS_PIN 18
#define RADIO_DI0_PIN 26
#define RADIO_RST_PIN 23
#define RADIO_DIO1_PIN 33
#define RADIO_BUSY_PIN 32
#define BOARD_LED 14
#define LED_ON HIGH
#define LED_OFF LOW
#define GPS_BAUD_RATE 9600
#define HAS_GPS
#define HAS_DISPLAY //Optional, bring your own board, no OLED !!
#elif defined(LILYGO_TBeam_V1_0) || defined(LILYGO_TBeam_V1_1)
#define GPS_RX_PIN 34
#define GPS_TX_PIN 12
#define BUTTON_PIN 38
#define BUTTON_PIN_MASK GPIO_SEL_38
#define I2C_SDA 21
#define I2C_SCL 22
#define PMU_IRQ 35
#define RADIO_SCLK_PIN 5
#define RADIO_MISO_PIN 19
#define RADIO_MOSI_PIN 27
#define RADIO_CS_PIN 18
#define RADIO_DI0_PIN 26
#define RADIO_RST_PIN 23
#define RADIO_DIO1_PIN 33
#define RADIO_BUSY_PIN 32
#define BOARD_LED 4
#define LED_ON LOW
#define LED_OFF HIGH
#define GPS_BAUD_RATE 9600
#define HAS_GPS
#define HAS_DISPLAY //Optional, bring your own board, no OLED !!
#elif defined(LILYGO_T3_V1_0)
#define I2C_SDA 4
#define I2C_SCL 15
#define OLED_RST 16
#define RADIO_SCLK_PIN 5
#define RADIO_MISO_PIN 19
#define RADIO_MOSI_PIN 27
#define RADIO_CS_PIN 18
#define RADIO_DI0_PIN 26
#define RADIO_RST_PIN 14
#define RADIO_DIO1_PIN 33
#define RADIO_BUSY_PIN 32
#define HAS_DISPLAY
#elif defined(LILYGO_T3_V1_6)
#define I2C_SDA 21
#define I2C_SCL 22
#define OLED_RST UNUSE_PIN
#define RADIO_SCLK_PIN 5
#define RADIO_MISO_PIN 19
#define RADIO_MOSI_PIN 27
#define RADIO_CS_PIN 18
#define RADIO_DI0_PIN 26
#define RADIO_RST_PIN 23
#define RADIO_DIO1_PIN 33
#define RADIO_BUSY_PIN 32
#define SDCARD_MOSI 15
#define SDCARD_MISO 2
#define SDCARD_SCLK 14
#define SDCARD_CS 13
#define BOARD_LED 25
#define LED_ON HIGH
#define ADC_PIN 35
#define HAS_SDCARD
#define HAS_DISPLAY
#elif defined(LILYGO_T3_V2_0)
#define I2C_SDA 21
#define I2C_SCL 22
#define OLED_RST UNUSE_PIN
#define RADIO_SCLK_PIN 5
#define RADIO_MISO_PIN 19
#define RADIO_MOSI_PIN 27
#define RADIO_CS_PIN 18
#define RADIO_DI0_PIN 26
#define RADIO_RST_PIN 14
#define RADIO_DIO1_PIN UNUSE_PIN
#define RADIO_BUSY_PIN UNUSE_PIN
#define SDCARD_MOSI 15
#define SDCARD_MISO 2
#define SDCARD_SCLK 14
#define SDCARD_CS 13
#define BOARD_LED 0
#define LED_ON LOW
#define HAS_DISPLAY
#define HAS_SDCARD
#elif defined(LILYGO_T95_V1_0)
#define I2C_SDA 21
#define I2C_SCL 22
#define OLED_RST UNUSE_PIN
#define RADIO_SCLK_PIN 18
#define RADIO_MISO_PIN 19
#define RADIO_MOSI_PIN 23
#define RADIO_CS_PIN 5
#define RADIO_DI0_PIN 26
#define RADIO_RST_PIN 4
#define RADIO_DIO1_PIN 33
#define RADIO_DIO2_PIN 32
#define RADIO_BUSY_PIN UNUSE_PIN
#define ADC_PIN 35
#define HAS_DISPLAY
#else
#error "Please select the version you purchased in utilities.h"
#endif

@ -0,0 +1,93 @@
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
const char* ssid = ""; // EditThis: The name of your WiFi access point.
const char* password = "";
const int ledPin = LED_BUILTIN; // EditThis: Pin for LED control, use the built-in LED pin or specify your own pin.
WiFiUDP Udp; // Create a UDP object for OSC communication
const int localPort = 8080; // EditThis: The password of your WiFi access point.
// setup executes once after booting. It configures the underlying hardware for
// use in the main loop.
void setup() {
Serial.begin(9600);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
Serial.print("Connecting to WiFi ");
WiFi.mode(WIFI_STA);
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());
Serial.println("Done!");
Udp.begin(localPort);
Serial.print("Listening for OSC messages on port ");
Serial.println(localPort);
}
void loop() {
HTTPClient http;
// Create a WiFiClient object
WiFiClient client;
// Specify the WiFi client and the request destination URL
http.begin(client, "http://127.0.0.1:8000/hello");
http.GET();
http.end();
delay(500);
if (Udp.parsePacket()) {
OSCMessage oscMsg;
while (Udp.available()) {
oscMsg.fill(Udp.read());
}
if (!oscMsg.hasError()) {
String address = oscMsg.getAddress();
if (address == "/led1") {
digitalWrite(D1, HIGH);
Serial.println("LED 1 turned ON");
delay(1000);
digitalWrite(D1, LOW);
Serial.println("LED 1 turned OFF");
} else if (address == "/led2") {
digitalWrite(D2, HIGH);
Serial.println("LED 2 turned ON");
delay(1000);
digitalWrite(D2, LOW);
Serial.println("LED 2 turned OFF");
} else if (address == "/led3") {
digitalWrite(D3, HIGH);
Serial.println("LED 3 turned ON");
delay(1000);
digitalWrite(D3, LOW);
Serial.println("LED 3 turned OFF");
}
} else {
Serial.print("Error parsing OSC message: ");
Serial.println(oscMsg.getError());
}
oscMsg.empty();
}
}
Loading…
Cancel
Save