Hallo Arduino-Spezis,
ich bin hier ganz neu und blutiger Anfänger.
Ich habe ein Projekt vor Augen, dass ich leider nach langen Versuchen nicht alleine bewältigen kann und daher um Unterstützung bitte.
Mein Projekt:
Ich möchte eine Art "Würfel" bauen, bei der jede Zahl die oben liegt einen jeweiligen UDP-Befehl in Richtung meiner Haussteuerung schickt.
Diesen Würfel möchte ich dann zur Szenensteuerung in meinem Wohnzimmer nutzen.
Aktuelles Material:
Für das Proket habe ich mir ein Beschleunigungssensor (ADXL345) gekauft, ein Ardunino mini pro, ein NodeMCU LUA Amica V2 ESP8266 ESP-12E sowie RNF24L01 Funkmodule.
Was ist mir bei dem Projekt wichtig:
Der Würfel sollte eine lange Laufzeit haben, sodass ich die Batterie nicht ständig laden muss. Aus diesem Grund habe ich daran gedacht, dieses Projekt auf 2 Controller aufzuteilen. Der NODEMCU der die Verbindung zum Netzwerk herstellt und der Arduino mini Pro, der die eigentliche Aufgabe übernimmt.
Was habe ich bisher erreicht:
Ich habe den NODEMCU in meinem Netzwerk eingefügt und konnte per Taster einen UDP-Befehl senden.
Den Arduino Mini pro mit dem Beschleunigungssensor habe ich dazu bekommen, die Richte Lage zu erfassen und im Serial-Monitor anzuzeigen. Nach 10sek. legt sich der Würfel wieder schlafen solange bis der Würfel wieder bewegt wird.
Woran ich aktuell scheitere ist beide Controller miteinander sprechen zu lassen und die Lage des Würfels zu übermitteln.
Nun meine Frage:
Hätte jemand die Güte sich mein Projekt anzusehen und mir helfen das Projekt weiter vorran zu bringen?
Den Sketch hänge ich hier an:
Arduino Mini Pro:
Code:
/* *********************************************
* SparkFun_ADXL345_Example
* Triple Axis Accelerometer Breakout - ADXL345
* Hook Up Guide Example
*
* Utilizing Sparkfun's ADXL345 Library
* Bildr ADXL345 source file modified to support
* both I2C and SPI Communication
*
* E.Robert @ SparkFun Electronics
* Created: Jul 13, 2016
* Updated: Sep 06, 2016
*
* Development Environment Specifics:
* Arduino 1.6.11
*
* Hardware Specifications:
* SparkFun ADXL345
* Arduino Uno
* *********************************************/
#include <SparkFun_ADXL345.h> // SparkFun ADXL345 Library
#include <LowPower.h>
/*********** COMMUNICATION SELECTION ***********/
/* Comment Out The One You Are Not Using */
//ADXL345 adxl = ADXL345(10); // USE FOR SPI COMMUNICATION, ADXL345(CS_PIN);
ADXL345 adxl = ADXL345(); // USE FOR I2C COMMUNICATION
/****************** INTERRUPT ******************/
/* Uncomment If Attaching Interrupt */
//int interruptPin = 2; // Setup pin 2 to be the interrupt pin (for most Arduino Boards)
//1const byte ledPin = 13;
//1const byte interruptPin = 2;
//1volatile byte state = LOW;
const int wakeUpPin = 2; //For good accelaration return values I comment out this
void wakeUp()
{
// Just a handler for the pin interrupt.
}
/******************** SETUP ********************/
/* Configure ADXL345 Settings */
void setup(){
Serial.begin(9600); // Start the serial terminal
Serial.println("SparkFun ADXL345 Accelerometer Hook Up Guide Example");
Serial.println();
Serial.flush();
adxl.powerOn(); // Power on the ADXL345
adxl.setRangeSetting(16); // Give the range settings
// Accepted values are 2g, 4g, 8g or 16g
// Higher Values = Wider Measurement Range
// Lower Values = Greater Sensitivity
adxl.setSpiBit(0); // Configure the device to be in 4 wire SPI mode when set to '0' or 3 wire SPI mode when set to 1
// Default: Set to 1
// SPI pins on the ATMega328: 11, 12 and 13 as reference in SPI Library
adxl.setActivityXYZ(1, 0, 0); // Set to activate movement detection in the axes "adxl.setActivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF)
adxl.setActivityThreshold(75); // 62.5mg per increment // Set activity // Inactivity thresholds (0-255)
adxl.setInactivityXYZ(1, 1, 1); // Set to detect inactivity in all the axes "adxl.setInactivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF)
adxl.setInactivityThreshold(75); // 62.5mg per increment // Set inactivity // Inactivity thresholds (0-255)
adxl.setTimeInactivity(10); // How many seconds of no activity is inactive?
adxl.setTapDetectionOnXYZ(0, 0, 1); // Detect taps in the directions turned ON "adxl.setTapDetectionOnX(X, Y, Z);" (1 == ON, 0 == OFF)
// Set values for what is considered a TAP and what is a DOUBLE TAP (0-255)
adxl.setTapThreshold(50); // 62.5 mg per increment
adxl.setTapDuration(15); // 625 μs per increment
adxl.setDoubleTapLatency(80); // 1.25 ms per increment
adxl.setDoubleTapWindow(200); // 1.25 ms per increment
// Set values for what is considered FREE FALL (0-255)
adxl.setFreeFallThreshold(7); // (5 - 9) recommended - 62.5mg per increment
adxl.setFreeFallDuration(30); // (20 - 70) recommended - 5ms per increment
// Setting all interupts to take place on INT1 pin
adxl.setImportantInterruptMapping(1, 1, 1, 1, 1); // Sets "adxl.setEveryInterruptMapping(single tap, double tap, free fall, activity, inactivity);"
// Accepts only 1 or 2 values for pins INT1 and INT2. This chooses the pin on the ADXL345 to use for Interrupts.
// This library may have a problem using INT2 pin. Default to INT1 pin.
// Turn on Interrupts for each mode (1 == ON, 0 == OFF)
adxl.InactivityINT(1);
adxl.ActivityINT(1);
adxl.FreeFallINT(1);
adxl.doubleTapINT(1);
adxl.singleTapINT(1);
//attachInterrupt(digitalPinToInterrupt(interruptPin), ADXL_ISR, RISING); // Attach Interrupt
//************************************************ Jan
//1pinMode(ledPin, OUTPUT);
//1pinMode(interruptPin, INPUT_PULLUP);
//1attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
pinMode(wakeUpPin, INPUT);
//************************************************* Jan
}
/****************** MAIN CODE ******************/
/* Accelerometer Readings and Interrupt */
void loop(){
//************************************************* Jan
//1digitalWrite(ledPin, state);
//Serial.flush(); //new 2018-05-03
// Allow wake up pin to trigger interrupt on low.
attachInterrupt(0, wakeUp, RISING);
Serial.flush(); //new 2018-05-03
// Enter power down state with ADC and BOD module disabled.
// Wake up when wake up pin is low.
//adxl.InactivityINT(0);
// LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
//Serial.flush(); //new 2018-05-03
// Disable external pin interrupt on wake up pin.
//detachInterrupt(0);
// Do something here
// Example: Read sensor, data logging, data transmission.
//************************************************* Jan
// Accelerometer Readings
int x,y,z;
adxl.readAccel(&x, &y, &z); // Read the accelerometer values and store them in variables declared above x,y,z
// Output Results to Serial
/* UNCOMMENT TO VIEW X Y Z ACCELEROMETER VALUES */
// Würfel Zahl 1
if (x>= 30 && y <=2 && z<=2)
{
Serial.print("Würfel 1 ");
}
// Würfel Zahl 2
if (x<= 2 && y <=2 && z<=-25)
{
Serial.print("Würfel 2 ");
}
// Würfel Zahl 3
if (x<= 2 && y <=-25 && z<=2)
{
Serial.print("Würfel 3 ");
}
// Würfel Zahl 4
if (x<= 2 && y >=25 && z<=2)
{
Serial.print("Würfel 4 ");
}
// Würfel Zahl 5
if (x<= 2 && y <=2 && z>=25)
{
Serial.print("Würfel 5 ");
}
// Würfel Zahl 6
if (x<= -25 && y <=2 && z<=2)
{
Serial.print("Würfel 6 ");
}
ADXL_ISR();
// You may also choose to avoid using interrupts and simply run the functions within ADXL_ISR();
// and place it within the loop instead.
// This may come in handy when it doesn't matter when the action occurs.
}
/********************* ISR *********************/
/* Look for Interrupts and Triggered Action */
void ADXL_ISR() {
// getInterruptSource clears all triggered actions after returning value
// Do not call again until you need to recheck for triggered actions
byte interrupts = adxl.getInterruptSource();
// Free Fall Detection
if(adxl.triggered(interrupts, ADXL345_FREE_FALL)){
Serial.println("*** FREE FALL ***");
//add code here to do when free fall is sensed
}
// Inactivity
if(adxl.triggered(interrupts, ADXL345_INACTIVITY)){
Serial.println("*** INACTIVITY ***");
Serial.println("Going to sleep");
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(20); // wait for 20 ms
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(50); // wait for 50 ms
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(20); // wait for 20 ms
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
Serial.flush(); //new 2018-05-03
adxl.InactivityINT(0);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
}
// Activity
if(adxl.triggered(interrupts, ADXL345_ACTIVITY)){
Serial.println("*** ACTIVITY ***");
adxl.InactivityINT(1);
//add code here to do when activity is sensed
}
// Double Tap Detection
if(adxl.triggered(interrupts, ADXL345_DOUBLE_TAP)){
Serial.println("*** DOUBLE TAP ***");
adxl.InactivityINT(1);
//add code here to do when a 2X tap is sensed
}
// Tap Detection
if(adxl.triggered(interrupts, ADXL345_SINGLE_TAP)){
Serial.println("*** TAP ***");
adxl.InactivityINT(1);
}
}
//************************************************* Jan
void blink() {
//state = !state;
//digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
//delay(20); // wait for 20 ms
//digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
//delay(50); // wait for 20 ms
//digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
//delay(20); // wait for 20 ms
//digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
}
//************************************************* Jan
NODEMCU:
Code:
//________________________________________________________________________________________________Libraries
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
//________________________________________________________________________________________________Libraries_Ende//
//________________________________________________________________________________________________PIN_Definition
#define Button_1 16
#define Button_2 4
#define LED 5
//________________________________________________________________________________________________PIN_Definition_Ende//
//________________________________________________________________________________________________Netzwerk_Einstellungen
const char* ssid = "xxxx";
const char* password = "xxxxxxxx";
IPAddress meineIP(192, 168, xxx, 112);
IPAddress meinSubnet(255, 255, 255, 0);
IPAddress meinGateway(192, 168, xxx, 1);
//________________________________________________________________________________________________Netzwerk_Einstellungen_Ende//
//________________________________________________________________________________________________UDP_Port_Listento
unsigned int localPort = 2727;// localer UDP Port???
//________________________________________________________________________________________________UDP_Port_Listento_Ende//
//________________________________________________________________________________________________Netzwerk_Einstellungen_Empfänger
IPAddress remoteIP(192, 168, xxx, 62);
//________________________________________________________________________________________________Netzwerk_Einstellungen_Empfänger_Ende//
//________________________________________________________________________________________________IP & UDP_Port_Empfänger
IPAddress RecipientIP(192, 168, xxx, 58);//hierhin senden IP???
unsigned int RecipientPort = 2727;
//________________________________________________________________________________________________UDP_Port_Empfänger_Ende//
//________________________________________________________________________________________________Variablen_Deklarierung
char Buttonstate_1 = 0;
char Buttonstate_2 = 0;
char T_1 = 0;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
//________________________________________________________________________________________________Variablen_Deklarierung_Ende//
//________________________________________________________________________________________________Ethernet_Instanz_Erstellen
WiFiUDP Udp;
char incomingPacket[255];
char replyPacket_1[] = "item|nw.test.test1|1111";
char replyPacket_2[] = "item|nw.test.test1|2222";
//________________________________________________________________________________________________Ethernet_Instanz_Erstellen_Ende//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////SETUP_START
void setup() {
Serial.begin(115200);
delay(10);
// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, 0);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.config(meineIP, meinGateway, meinSubnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.println(WiFi.localIP());
//________________________________________________________________________________________________UDP_Instanz_Starten
Udp.begin(localPort);
//________________________________________________________________________________________________UDP_Instanz_Starten_Ende//
//________________________________________________________________________________________________PIN_Deklaration
pinMode(Button_1, INPUT);
pinMode(Button_2, INPUT);
digitalWrite(Button_1, HIGH);
digitalWrite(Button_2, HIGH);
pinMode(LED, OUTPUT); // LED an Pin7
//________________________________________________________________________________________________PIN_Deklaration_Ende//
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////SETUP_START_Ende//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////LOOP_START
void loop()
{
Press_Button_1();
Press_Button_2();
}
void Press_Button_1()
{
// read the state of the pushbutton value:
Buttonstate_1 = digitalRead(Button_1);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (Buttonstate_1 == HIGH && T_1 == 1 )
{
// turn LED on:
digitalWrite(LED, HIGH);
T_1 = 0;
Udp.beginPacket(RecipientIP, RecipientPort);
Udp.print(replyPacket_1);
Udp.endPacket();
Serial.println("Paket gesendet");
} else {
// turn LED off:
digitalWrite(LED, LOW);
T_1 = 1;
}
}
void Press_Button_2()
{
// read the state of the pushbutton value:
Buttonstate_2 = digitalRead(Button_2);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (Buttonstate_2 == HIGH && T_1 == 1 )
{
// turn LED on:
digitalWrite(LED, HIGH);
T_1 = 0;
Udp.beginPacket(RecipientIP, RecipientPort);
Udp.print(replyPacket_2);
Udp.endPacket();
Serial.println("Paket gesendet");
} else {
// turn LED off:
digitalWrite(LED, LOW);
T_1 = 1;
}
}
Gruß und danke Manuel