This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
arduino:arduino [2020/07/27 21:44] – [MQTT] tmade | arduino:arduino [Unknown date] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | =====Arduino===== | ||
- | The Arduino IDE you can get here: | ||
- | |||
- | https:// | ||
- | |||
- | Setup esp8266: | ||
- | |||
- | https:// | ||
- | |||
- | ==== Irrigation System ==== | ||
- | |||
- | Enhanced code to control a waterpump, a display and an " | ||
- | |||
- | *Display shows a diagramm in % | ||
- | *Sensor values | ||
- | *dry or wet | ||
- | *% value | ||
- | |||
- | <code sh Irrigation.ino> | ||
- | // | ||
- | //version 1.2 | ||
- | //by tmade | ||
- | |||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | |||
- | #define SCREEN_WIDTH 128 // OLED display width, in pixels | ||
- | #define SCREEN_HEIGHT 64 // OLED display height, in pixels | ||
- | |||
- | // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) | ||
- | #define OLED_RESET | ||
- | Adafruit_SSD1306 display(SCREEN_WIDTH, | ||
- | // | ||
- | |||
- | //variables | ||
- | int SLEEP = 1; | ||
- | const int DRY = 694; | ||
- | const int WET = 344; | ||
- | int sensorValue = 0; | ||
- | int rectangle = 0; | ||
- | const int X = 10; | ||
- | const int Y = 1; | ||
- | const int WIDTH = 35; | ||
- | const int HEIGHT = 63; | ||
- | |||
- | void setup() { | ||
- | // put your setup code here, to run once: | ||
- | Serial.begin(9600); | ||
- | //to debug set to 115200 !! | ||
- | // | ||
- | | ||
- | digitalWrite(0, | ||
- | pinMode (2, OUTPUT); | ||
- | digitalWrite(2, | ||
- | |||
- | // initialize with the I2C addr 0x3C | ||
- | display.begin(SSD1306_SWITCHCAPVCC, | ||
- | |||
- | // Clear the buffer. | ||
- | display.clearDisplay(); | ||
- | display.setTextSize(2); | ||
- | display.setTextColor(WHITE); | ||
- | //Define the position where the text starts using the setCursor(x, | ||
- | display.setCursor(0, | ||
- | display.println(" | ||
- | display.display(); | ||
- | delay(4000); | ||
- | } | ||
- | |||
- | void loop() { | ||
- | // put your main code here, to run repeatedly: | ||
- | display.clearDisplay(); | ||
- | | ||
- | int sensorValue=analogRead(A0); | ||
- | int val = analogRead(0); | ||
- | val = map(val, DRY, WET, 0, 100); | ||
- | delay(200); | ||
- | int rectangle = analogRead(0); | ||
- | rectangle = map(rectangle, | ||
- | delay(200); | ||
- | |||
- | // debugging & examples | ||
- | /* | ||
- | Serial.print(" | ||
- | Serial.println(rectangle); | ||
- | |||
- | analogWrite(9, | ||
- | |||
- | Serial.print(" | ||
- | Serial.println(WET); | ||
- | Serial.print(" | ||
- | Serial.println(WET); | ||
- | delay(200); | ||
- | */ | ||
- | |||
- | SLEEP = SLEEP-1; | ||
- | if ((sensorValue > 570) && (SLEEP < 2)) | ||
- | { | ||
- | display.clearDisplay(); | ||
- | // drawRect(x, y, width, height, color) | ||
- | display.drawRoundRect(X, | ||
- | display.fillRoundRect(X, | ||
- | display.setTextSize(2); | ||
- | display.setTextColor(WHITE); | ||
- | display.setCursor(60, | ||
- | display.println(" | ||
- | display.setCursor(60, | ||
- | display.setTextSize(2); | ||
- | display.println(sensorValue); | ||
- | display.setCursor(60, | ||
- | display.setTextSize(2); | ||
- | display.print(val); | ||
- | display.println(" | ||
- | // write the buffer to the display | ||
- | display.display(); | ||
- | delay(200); | ||
- | |||
- | Serial.print(" | ||
- | Serial.println(sensorValue); | ||
- | | ||
- | Serial.println(" | ||
- | // 0 = LOW = ON | ||
- | digitalWrite(2, | ||
- | //set pump duration per loop in milliseconds | ||
- | delay(5000); | ||
- | // 1 = HIGH = OFF | ||
- | digitalWrite(2, | ||
- | Serial.println(" | ||
- | delay(200); | ||
- | //set wait until next check | ||
- | SLEEP = 60; | ||
- | } | ||
- | |||
- | else if (sensorValue < 571) | ||
- | { | ||
- | Serial.print(" | ||
- | Serial.println(sensorValue); | ||
- | Serial.print(" | ||
- | Serial.print(val); | ||
- | Serial.println(" | ||
- | Serial.println(" | ||
- | delay(1000); | ||
- | // Display | ||
- | display.clearDisplay(); | ||
- | // drawRect(x, y, width, height, color) | ||
- | display.drawRoundRect(X, | ||
- | display.fillRoundRect(X, | ||
- | display.setTextSize(2); | ||
- | display.setTextColor(WHITE); | ||
- | display.setCursor(60, | ||
- | display.println(" | ||
- | display.setCursor(60, | ||
- | display.setTextSize(2); | ||
- | display.println(sensorValue); | ||
- | display.setCursor(60, | ||
- | display.setTextSize(2); | ||
- | display.print(val); | ||
- | display.println(" | ||
- | // write the buffer to the display | ||
- | display.display(); | ||
- | delay(1000); | ||
- | } | ||
- | |||
- | else if (SLEEP > 2) | ||
- | { | ||
- | display.clearDisplay(); | ||
- | // drawRect(x, y, width, height, color) | ||
- | display.drawRoundRect(X, | ||
- | display.fillRoundRect(X, | ||
- | display.setTextSize(2); | ||
- | display.setTextColor(WHITE); | ||
- | display.setCursor(60, | ||
- | display.println(" | ||
- | display.setCursor(60, | ||
- | display.setTextSize(2); | ||
- | display.println(sensorValue); | ||
- | display.setCursor(60, | ||
- | display.setTextSize(2); | ||
- | display.print(val); | ||
- | display.println(" | ||
- | // write the buffer to the display | ||
- | display.display(); | ||
- | delay(200); | ||
- | |||
- | Serial.print(" | ||
- | Serial.println(sensorValue); | ||
- | Serial.print(" | ||
- | Serial.print(val); | ||
- | Serial.println(" | ||
- | Serial.println(" | ||
- | Serial.print(" | ||
- | Serial.print(SLEEP); | ||
- | Serial.println(" | ||
- | delay(200); | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | ==== Alles & nichts ==== | ||
- | |||
- | Code to control a waterpump, a display and an " | ||
- | |||
- | Many thanks to "Alles & nichts" | ||
- | |||
- | Have also a look on: | ||
- | |||
- | https:// | ||
- | |||
- | |||
- | < | ||
- | /* | ||
- | VCC = 5 Volt cap sens | ||
- | VCC = 3,3 Volt Relais | ||
- | |||
- | -------------------------------------------------------------*/ | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | |||
- | Adafruit_SSD1306 display(-1); | ||
- | |||
- | /* | ||
- | const unsigned char Smiley_lachen [] PROGMEM = { | ||
- | |||
- | }; | ||
- | |||
- | const unsigned char Smiley_middle [] PROGMEM = { | ||
- | |||
- | }; | ||
- | |||
- | const unsigned char Smiley_traurig [] PROGMEM = { | ||
- | |||
- | };*/ | ||
- | |||
- | /*const int waterValue= 400; | ||
- | const int moistValue= 600; | ||
- | const int dryValue = 850; | ||
- | const int pumpON = 660;*/ | ||
- | |||
- | int val = 1; | ||
- | |||
- | void setup() { | ||
- | // put your setup code here, to run once: | ||
- | Serial.begin(9600); | ||
- | digitalWrite(0, | ||
- | pinMode (2, OUTPUT); | ||
- | digitalWrite(2, | ||
- | |||
- | // initialize with the I2C addr 0x3C | ||
- | display.begin(SSD1306_SWITCHCAPVCC, | ||
- | |||
- | // Clear the buffer. | ||
- | display.clearDisplay(); | ||
- | // | ||
- | |||
- | // symbol_leer | ||
- | |||
- | display.drawRoundRect(0, | ||
- | display.display(); | ||
- | delay(2000); | ||
- | |||
- | // symbol_viertelvoll | ||
- | |||
- | display.fillRoundRect(0, | ||
- | display.display(); | ||
- | delay(2000); | ||
- | |||
- | // symbol_halbvoll | ||
- | |||
- | display.fillRoundRect(0, | ||
- | display.display(); | ||
- | delay(2000); | ||
- | |||
- | // symbol_dreiviertelvoll | ||
- | |||
- | display.fillRoundRect(0, | ||
- | display.display(); | ||
- | delay(2000); | ||
- | |||
- | // symbol_voll | ||
- | |||
- | display.fillRoundRect(0, | ||
- | display.display(); | ||
- | delay(2000); | ||
- | |||
- | } | ||
- | |||
- | void loop() { | ||
- | // put your main code here, to run repeatedly: | ||
- | display.clearDisplay(); | ||
- | int sensorValue=analogRead(A0); | ||
- | |||
- | Serial.println(sensorValue); | ||
- | delay(200); | ||
- | |||
- | // | ||
- | |||
- | if (sensorValue > 570) | ||
- | { | ||
- | //val=!val; | ||
- | display.drawRoundRect(0, | ||
- | display.display(); | ||
- | digitalWrite(2, | ||
- | } | ||
- | |||
- | else if ((sensorValue < 550) && (sensorValue > 500)) | ||
- | { | ||
- | display.fillRoundRect(0, | ||
- | display.display(); | ||
- | } | ||
- | |||
- | else if ((sensorValue < 500) && (sensorValue > 450)) | ||
- | { | ||
- | display.fillRoundRect(0, | ||
- | display.display(); | ||
- | } | ||
- | |||
- | else if (sensorValue < 450) | ||
- | { | ||
- | display.fillRoundRect(0, | ||
- | display.display(); | ||
- | digitalWrite(2, | ||
- | } | ||
- | display.setTextSize(2); | ||
- | display.setTextColor(WHITE); | ||
- | display.setCursor(50, | ||
- | display.println(sensorValue); | ||
- | | ||
- | display.display(); | ||
- | delay(200); | ||
- | } | ||
- | |||
- | </ | ||
- | |||
- | ==== Another example ==== | ||
- | |||
- | http:// | ||
- | |||
- | < | ||
- | void setup() { | ||
- | | ||
- | | ||
- | | ||
- | } | ||
- | |||
- | void loop() { | ||
- | |||
- | int sensor = analogRead(A1); | ||
- | | ||
- | |||
- | // | ||
- | |||
- | // | ||
- | |||
- | | ||
- | |||
- | if (sensor < 430 ) { | ||
- | |||
- | //Relais einschalten | ||
- | |||
- | digitalWrite(A0, | ||
- | Serial.println(" | ||
- | |||
- | //2 Sekunden anlassen | ||
- | |||
- | delay(2000); | ||
- | |||
- | //Relais ausschalten | ||
- | |||
- | digitalWrite(A0, | ||
- | |||
- | | ||
- | |||
- | |||
- | //10 Sekunden warten | ||
- | |||
- | delay(10000); | ||
- | |||
- | } | ||
- | } | ||
- | </ | ||
- | ====Display==== | ||
- | |||
- | https:// | ||
- | |||
- | To check HEX-adress of your display - which has to be referenced - please run this sketch: | ||
- | |||
- | <code sh oled-test.ino> | ||
- | /********* | ||
- | Rui Santos | ||
- | Complete project details at https:// | ||
- | *********/ | ||
- | |||
- | #include < | ||
- | |||
- | void setup() { | ||
- | Wire.begin(); | ||
- | Serial.begin(115200); | ||
- | Serial.println(" | ||
- | } | ||
- | |||
- | void loop() { | ||
- | byte error, address; | ||
- | int nDevices; | ||
- | Serial.println(" | ||
- | nDevices = 0; | ||
- | for(address = 1; address < 127; address++ ) { | ||
- | Wire.beginTransmission(address); | ||
- | error = Wire.endTransmission(); | ||
- | if (error == 0) { | ||
- | Serial.print(" | ||
- | if (address< | ||
- | Serial.print(" | ||
- | } | ||
- | Serial.println(address, | ||
- | nDevices++; | ||
- | } | ||
- | else if (error==4) { | ||
- | Serial.print(" | ||
- | if (address< | ||
- | Serial.print(" | ||
- | } | ||
- | Serial.println(address, | ||
- | } | ||
- | } | ||
- | if (nDevices == 0) { | ||
- | Serial.println(" | ||
- | } | ||
- | else { | ||
- | Serial.println(" | ||
- | } | ||
- | delay(200); | ||
- | } | ||
- | </ | ||
- | |||
- | **Note:** You have to change baud rate in serial monitor to " | ||
- | |||
- | ====MQTT==== | ||
- | |||
- | To send a message to a mqtt server: | ||
- | |||
- | https:// | ||
- | |||
- | https:// |