added projects

This commit is contained in:
mrbesen 2018-11-04 18:21:40 +01:00
parent 48531bb1bd
commit 34bd2301da
2 changed files with 221 additions and 0 deletions

View File

@ -0,0 +1,112 @@
/*
* worked on: arduino1.8.3 - arduino nano 328
* libs:
* https://github.com/avishorp/TM1637
* https://github.com/UIPEthernet/UIPEthernet
*
*/
#define l 3
#define r 4
#define CLK 5
#define DIO 6
#include <UIPEthernet.h>
#include <TM1637Display.h>
EthernetServer server = EthernetServer(1234);
TM1637Display display(CLK, DIO);
unsigned char step = 0;
const uint8_t empty[] = {0x00,0x00,0x00,0x00};
const uint8_t connected[] = {
SEG_D | SEG_E | SEG_G, //c
SEG_C | SEG_D | SEG_E | SEG_G,//o
SEG_C | SEG_E | SEG_G, //n
SEG_C | SEG_E | SEG_G //n
};
void setup() {
display.setBrightness(0, false); // Turn off
display.setSegments(empty);
Serial.begin(9600);
uint8_t mac[6] = {0x24, 0xb6, 0xfd, 0x20, 0x69, 0xed };
Ethernet.begin(mac);
pinMode(l, INPUT);
pinMode(r, INPUT);
Serial.println(Ethernet.localIP());
server.begin();
}
void loop() {
size_t size;
Serial.println("Listening port 1234 ");
Serial.println(Ethernet.localIP());
if (EthernetClient client = server.available()) {
client.write('h');
Serial.println("Connected");
bool leftp = digitalRead(l) == HIGH;
bool rightp = digitalRead(r) == HIGH;
bool conn = true;
while(client.connected()) {
if(conn) {
conn = false;
display.setBrightness(2, true);
display.setSegments(connected);
}
if(client.available() > 0) {
char data = client.read();
Serial.print(data);
if(data == 'd') {
Serial.println("Disconnect");
break;
}
}
if(digitalRead(l) == HIGH) {
if(!leftp) {
leftp = true;
client.write('l');
client.write('<');
client.flush();
}
} else if(leftp) {
leftp = false;
client.write('l');
client.write('>');
client.flush();
}
if(digitalRead(r) == HIGH) {
if(!rightp) {
rightp = true;
client.write('r');
client.write('<');
client.flush();
}
} else if(rightp) {
rightp = false;
client.write('r');
client.write('>');
client.flush();
}
delay(2);
//Serial.print(digitalRead(l));
//Serial.println(digitalRead(r));
}
client.stop();
}
delay(1000);
if(digitalRead(l) == HIGH && digitalRead(r) == HIGH) { // both pressed
display.setBrightness(7, true); // Turn on
display.showNumberDec(Ethernet.localIP()[step++], step == 0);//to make the first byte have leading zeros
step%=4;
} else {
display.setBrightness(0, false); // Turn off
display.setSegments(empty);
step = 0;
}
}

View File

@ -0,0 +1,109 @@
/*
Repeating Web client
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Sensor mit 4,7K Ohm auf Data <->5V auf pin 9
*/
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define host "10.188.17.6"
#define port 1234
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// initialize the library instance:
EthernetClient client;
OneWire oneWire(9);
EthernetUDP Udp;
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
char server[] = host;
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10000L; // delay between updates, in milliseconds
// the "L" is needed to use long type numbers
void setup() {
Serial.begin(9600);
while (!Serial);
// give the ethernet module time to boot up:
delay(1000);
// start the Ethernet connection using a fixed IP address and DNS server:
Ethernet.begin(mac);
// print the Ethernet board/shield's IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
sensors.begin();
Udp.begin(1234);
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.write(c);
}
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
delay(10);
} else {
delay(20);
}
}
void httpRequest() {
Serial.println("Sending");
//client.stop();
sensors.requestTemperatures();
Udp.beginPacket(server, port);
int temp = sensors.getTempCByIndex(0)*100;
Serial.println(temp);
char out[] = {
(char) ((temp & 0XFF000000) >> 24) ,
(char) ((temp & 0X00FF0000) >> 16),
(char) ((temp & 0X0000FF00) >> 8),
(char) (temp & 0X000000FF)
};
Udp.write(out , 4);
Udp.endPacket();
Serial.println(out[0], HEX);
Serial.println(out[1], HEX);
Serial.println(out[2], HEX);
Serial.println(out[3], HEX);
lastConnectionTime = millis();
/*if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP GET request:
client.print("POST /temp/");
client.print(sensors.getTempCByIndex(0));
client.println("HTTP/1.1");
client.print("Host: ");
client.print(host);
client.println("User-Agent: BOT");
client.println("Connection: close");
client.println();
client.println();
lastConnectionTime = millis();
} else {
Serial.println("connection failed");
}*/
}