Components and supplies
Arduino Mega 2560
Photo resistor
Jumper wires (generic)
DS18B20 temperature sensor
Project description
Code
Python code
python
1 2import socket 3import pygame 4import struct 5import time 6import datetime as dt 7dt.datetime.now() 8 9UDP_IP = '192.168.1.102' 10UDP_PORT = 8877 11sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 12sock.settimeout(2) 13 14 15pygame.init() 16 17 18pygame.joystick.init() 19 20joy_count = pygame.joystick.get_count() 21 22 23 24if joy_count < 1 : print ("Nincs joystick csatlakoztatva.") 25else : 26 fenymennyiseg=open("feny.csv","w") 27 fenymennyiseg.write("DATUM;FENYMENNY\ 28") 29 30 31 while True : 32 joystick = pygame.joystick.Joystick(0) 33 joystick.init() 34 35 pygame.event.pump() 36 37 y_tengely = int(joystick.get_axis(1) * 255) 38 x_tengely = int(joystick.get_axis(0) * 255) 39 gomb = int(joystick.get_button(1)) 40 valtgomb = int(joystick.get_button(3)) 41 fenygomb = int(joystick.get_button(2)) 42 43 44 45 46 47 48 49 50 sock.sendto(struct.pack("hhhhh", x_tengely, y_tengely, gomb, valtgomb, fenygomb), (UDP_IP, UDP_PORT)) 51 data, addr = sock.recvfrom(1024) 52 #print ("received from: ",addr) 53 54 55 adat=data.decode("utf-8") 56 sszesadat=adat.split() 57 print (sszesadat[0]) 58 59 60 61 if (int(joystick.get_button(2))==1) : 62 fenymennyiseg=open("feny.csv","a") 63 fenymennyiseg.write("{0};{1}\ 64".format(dt.datetime.now().strftime("%H:%M"),sszesadat[0])) 65 time.sleep(0.1) 66 fenymennyiseg.close() 67 68 69 70 71 72 73 74 75 76 if sszesadat[1] != 'ack' : 77 print (sszesadat) 78 print ("Nincs valasz") 79 break 80 81 82 83
Underwater ROV code
arduino
It's an open source code, you can modify this, or cut useful lines.
1 2#include <OneWire.h> 3#include <DallasTemperature.h> 4#include <SPI.h> 5#include <Ethernet.h> 6#include <EthernetUdp.h> 7 8 9#define ONE_WIRE_BUS 2 10OneWire oneWire(ONE_WIRE_BUS); 11DallasTemperature sensors(&oneWire); 12 13 14 15byte mac[] = { 16 0x90, 0xA2, 0xDA, 0x0D, 0x7F, 0xFC 17}; 18IPAddress ip(192, 168, 1, 102); 19 20unsigned int localPort = 8877; 21 22 23int fenymennyiseg=A1; 24char datain[UDP_TX_PACKET_MAX_SIZE]; 25char ReplyBuffer[] = "ack"; 26float temperature; 27 28 29 30 31EthernetUDP Udp; 32 33int motorA1=39; 34int motorA2=41; 35int speedA=11; //FEL LE DUPLA MOTOROK 36int motorB1=45; 37int motorB2=43; 38int speedB=9; //FEL-LE MOTOR 39int motorC1=49; 40int motorC2=47; 41int speedC=5; // BAL ELRE HTRA MOZGAT 42int motorD1=53; 43int motorD2=31; 44int speedD=3; //JOBB ELRE HTRA MOZGAT 45int LEDEK = 8; 46char ertek[10]; 47int oldButtonState = LOW; 48int regiallas = LOW; 49int x=0; 50int y=0; 51 52int fenylampa=7; 53 54 55int sdcs=4; 56int ethcs=10; 57int valtgomb=0; 58 59 60void setup() 61{ 62 63 Ethernet.begin(mac, ip); 64 Udp.begin(localPort); 65 66 Serial.begin(9600); 67 sensors.begin(); 68 pinMode(motorA1,OUTPUT); 69 pinMode(motorA2,OUTPUT); 70 pinMode(speedA,OUTPUT); 71 pinMode(motorB1,OUTPUT); 72 pinMode(motorB2,OUTPUT); 73 pinMode(speedB,OUTPUT); 74 pinMode(motorC1,OUTPUT); 75 pinMode(motorC2,OUTPUT); 76 pinMode(speedC,OUTPUT); 77 pinMode(motorD1,OUTPUT); 78 pinMode(motorD2,OUTPUT); 79 pinMode(speedD,OUTPUT); 80 pinMode(LEDEK, OUTPUT); 81 pinMode(sdcs, OUTPUT); 82 digitalWrite(sdcs, HIGH); 83 pinMode(ethcs, OUTPUT); 84 digitalWrite(ethcs, LOW); 85 digitalWrite(LEDEK,HIGH); 86 pinMode(fenylampa,OUTPUT); 87 digitalWrite(fenylampa,HIGH); 88 digitalWrite(speedC,LOW); 89 90 91 92} 93 94 95void loop() 96{ 97 String fenyallas= String(analogRead(1), DEC); 98 String valasz= String("ack "); 99 String pont= String("\ 100"); 101 sensors.requestTemperatures(); 102 temperature=sensors.getTempCByIndex(0); 103 String homerseklet=String(temperature); 104 105 106 107 108String Buffer=fenyallas+pont+valasz+pont+homerseklet; 109char cBuffer[Buffer.length()]; 110Buffer.toCharArray(cBuffer, Buffer.length()); 111Serial.println(cBuffer); 112 113 114 115 int packetSize = Udp.parsePacket(); 116 if (packetSize) 117 { 118 119 IPAddress remote = Udp.remoteIP(); 120 121 122 Udp.read(ertek, UDP_TX_PACKET_MAX_SIZE); 123 Serial.println("Contents:"); 124 for(int i = 0; i<10;i+= 2) 125 { 126 Serial.print( *(int*)(&ertek[i])); 127 Serial.print(","); 128 } 129 Serial.print("\ 130"); 131 132 133//------------------------------------------------------------------------------------ 134 if( *(int*)(&ertek[2]) <=-100 && valtgomb!=1)//ELORE-HATRA y tengely 135 { 136 analogWrite(speedC, abs(*(int*)(&ertek[2]))); 137 analogWrite(speedD, abs(*(int*)(&ertek[2]))); 138 digitalWrite(motorC1, HIGH); 139 digitalWrite(motorC2, LOW); 140 digitalWrite(motorD1, LOW); 141 digitalWrite(motorD2, HIGH); 142 143 144 } 145 else 146 if(*(int*)(&ertek[2]) >=100 && valtgomb!=1) 147 { 148 analogWrite(speedC, *(int*)(&ertek[2])); 149 analogWrite(speedD, *(int*)(&ertek[2])); 150 151 digitalWrite(motorC1, LOW); 152 digitalWrite(motorC2, HIGH); 153 digitalWrite(motorD1, HIGH); 154 digitalWrite(motorD2, LOW); 155 156 157 } 158 else 159 if(*(int*)(&ertek[2]) < 50 && *(int*)(&ertek[2]) >-50 && valtgomb!=1) 160 { 161 analogWrite(speedC, 0); 162 analogWrite(speedD, 0); 163 } 164//------------------------------------------------------------------------------------ 165if( *(int*)(&ertek[0]) >=50 && valtgomb!=1)//FEL-LE X tengely 166 { 167 analogWrite(speedA, *(int*)(&ertek[0])); 168 analogWrite(speedB, *(int*)(&ertek[0])); 169 digitalWrite(motorA1, HIGH); 170 digitalWrite(motorA2, LOW); 171 digitalWrite(motorB1, HIGH); 172 digitalWrite(motorB2, LOW); 173 } 174 else 175 if(*(int*)(&ertek[0]) <=-50 && valtgomb!=1) 176 { 177 analogWrite(speedA, abs(*(int*)(&ertek[0]))); 178 analogWrite(speedB, abs(*(int*)(&ertek[0]))); 179 digitalWrite(motorA1, LOW); 180 digitalWrite(motorA2, HIGH); 181 digitalWrite(motorB1, LOW); 182 digitalWrite(motorB2, HIGH); 183 } 184 else 185 if(*(int*)(&ertek[0]) < 50 && *(int*)(&ertek[0]) >-50 && valtgomb!=1) 186 { 187 188 analogWrite(speedA, 0); 189 analogWrite(speedB, 0); 190 } 191//------------------------------------------------------------------------------------ledek be/ki 192 193 int newButtonState = *(int*)(&ertek[4]); 194 195 196 if (newButtonState == HIGH && oldButtonState == LOW) 197 { 198 199 if (x == 0) { 200 // Toggle on 201 digitalWrite(LEDEK, HIGH); 202 x = 1; 203 204 } else { 205 // Toggle off 206 digitalWrite(LEDEK, LOW); 207 x = 0; 208 } 209 } 210 211 212 oldButtonState = newButtonState; 213//------------------------------------------------------------------------------------//fenymennyisegmero KI/BE 214 215 int gomballas = *(int*)(&ertek[8]); 216 217 218 if (gomballas == HIGH && regiallas == LOW) 219 { 220 221 if (y == 0) 222 { 223 // Toggle on 224 digitalWrite(fenylampa, HIGH); 225 y = 1; 226 227 228 } else { 229 // Toggle off 230 digitalWrite(fenylampa, LOW); 231 y = 0; 232 } 233 } 234 235 236 regiallas = gomballas; 237 238 239//------------------------------------------------------------------------------------ 240valtgomb=*(int*)(&ertek[6]); 241if(valtgomb==1) 242 { 243 if( *(int*)(&ertek[0]) >=50 && valtgomb==1)//fordulas 244 { 245 analogWrite(speedC, *(int*)(&ertek[0])); 246 analogWrite(speedD, *(int*)(&ertek[0])); 247 digitalWrite(motorC1, HIGH); 248 digitalWrite(motorC2, LOW); 249 digitalWrite(motorD1, HIGH); 250 digitalWrite(motorD2, LOW); 251 } 252 else 253 if(*(int*)(&ertek[0]) <=-50 && valtgomb==1) 254 { 255 analogWrite(speedC, abs(*(int*)(&ertek[0]))); 256 analogWrite(speedD, abs(*(int*)(&ertek[0]))); 257 digitalWrite(motorC1, HIGH); 258 digitalWrite(motorC2, LOW); 259 digitalWrite(motorD1, HIGH); 260 digitalWrite(motorD2, LOW); 261 } 262 else 263 if(*(int*)(&ertek[0]) < 50 && *(int*)(&ertek[0]) >-50 && valtgomb==1) 264 { 265 analogWrite(speedC, 0); 266 analogWrite(speedD, 0); 267 } 268 269 } 270 271 272 273//------------------------------------------------------------------------------------ 274 Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); 275 Udp.write(cBuffer); 276 Udp.endPacket(); 277 278 } 279 else 280 { 281 digitalWrite(motorA1,LOW); 282 digitalWrite(motorA2,LOW); 283 digitalWrite(motorB1,LOW); 284 digitalWrite(motorB2,LOW); 285 digitalWrite(motorC1,LOW); 286 digitalWrite(motorC2,LOW); 287 digitalWrite(motorD1,LOW); 288 digitalWrite(motorD2,LOW); 289 } 290 delay(50); 291} 292 293 294 295 296 297
Python code
python
1 2import socket 3import pygame 4import struct 5import time 6import 7 datetime as dt 8dt.datetime.now() 9 10UDP_IP = '192.168.1.102' 11UDP_PORT 12 = 8877 13sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 14sock.settimeout(2) 15 16 17pygame.init() 18 19 20pygame.joystick.init() 21 22joy_count 23 = pygame.joystick.get_count() 24 25 26 27if joy_count < 1 : print ("Nincs joystick 28 csatlakoztatva.") 29else : 30 fenymennyiseg=open("feny.csv","w") 31 fenymennyiseg.write("DATUM;FENYMENNY\ 32") 33 34 35 36 while True : 37 joystick = pygame.joystick.Joystick(0) 38 39 joystick.init() 40 41 pygame.event.pump() 42 43 44 y_tengely = int(joystick.get_axis(1) * 255) 45 x_tengely = int(joystick.get_axis(0) 46 * 255) 47 gomb = int(joystick.get_button(1)) 48 valtgomb = int(joystick.get_button(3)) 49 50 fenygomb = int(joystick.get_button(2)) 51 52 53 54 55 56 57 58 59 60 sock.sendto(struct.pack("hhhhh", x_tengely, 61 y_tengely, gomb, valtgomb, fenygomb), (UDP_IP, UDP_PORT)) 62 data, addr 63 = sock.recvfrom(1024) 64 #print ("received from: ",addr) 65 66 67 68 adat=data.decode("utf-8") 69 sszesadat=adat.split() 70 71 print (sszesadat[0]) 72 73 74 75 if (int(joystick.get_button(2))==1) 76 : 77 fenymennyiseg=open("feny.csv","a") 78 fenymennyiseg.write("{0};{1}\ 79".format(dt.datetime.now().strftime("%H:%M"),sszesadat[0])) 80 81 time.sleep(0.1) 82 fenymennyiseg.close() 83 84 85 86 87 88 89 90 91 92 93 94 if sszesadat[1] != 'ack' : 95 print (sszesadat) 96 print 97 ("Nincs valasz") 98 break 99 100 101 102
Downloadable files
DS18B20 temperature sensor example circuit
You can use another input pin
DS18B20 temperature sensor example circuit
Photo resistor example circuit
You have to use a 10k resistor and you can choose another input pin.
Photo resistor example circuit
Splitting UTP cable
Splitting UTP cable
DS18B20 temperature sensor example circuit
You can use another input pin
DS18B20 temperature sensor example circuit
Photo resistor example circuit
You have to use a 10k resistor and you can choose another input pin.
Photo resistor example circuit
Splitting UTP cable
Splitting UTP cable
Comments
Only logged in users can leave comments
Bankirobot
0 Followers
•0 Projects
Table of contents
Intro
20
0