Skip to content

Commit edc26fc

Browse files
committed
WiFi gedeelte: over MQTT tekenpunten sturen
1 parent 430ebb3 commit edc26fc

File tree

4 files changed

+450
-0
lines changed

4 files changed

+450
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include <ESP8266WiFi.h>
2+
#include <PubSubClient.h>
3+
4+
#define wifi_ssid "Robot14"
5+
#define wifi_password "ciscocisco"
6+
7+
#define mqtt_server "192.168.137.1"
8+
#define mqtt_port 1883
9+
10+
#define in_topic "/light/in"
11+
#define in_x "/point/x"
12+
#define in_y "/point/y"
13+
#define in_z "/point/z"
14+
#define out_topic "/light/out" //output
15+
16+
// Replace by 2 if you aren't enable to use Serial Monitor... Don't forget to Rewire R1 to GPIO2!
17+
#define in_led 2
18+
19+
int lastTimeSend;
20+
21+
const int waitTime = 500;
22+
23+
WiFiClient espClient;
24+
PubSubClient client;
25+
26+
void setup() {
27+
Serial.begin(115200);
28+
pinMode(in_led, OUTPUT);
29+
digitalWrite(in_led, HIGH);
30+
setup_wifi();
31+
client.setClient(espClient);
32+
client.setServer(mqtt_server, mqtt_port);
33+
client.setCallback(callback);
34+
35+
// initialize digital pin LED_BUILTIN as an output.
36+
pinMode(in_led, OUTPUT);
37+
digitalWrite(in_led, HIGH);
38+
}
39+
40+
void setup_wifi() {
41+
delay(10);
42+
// We start by connecting to a WiFi network
43+
44+
WiFi.begin(wifi_ssid, wifi_password);
45+
46+
while (WiFi.status() != WL_CONNECTED) {
47+
delay(500);
48+
}
49+
}
50+
51+
void reconnect() {
52+
// Loop until we're reconnected
53+
while (!client.connected()) {
54+
// Attempt to connect
55+
// If you do not want to use a username and password, change next line to
56+
// if (client.connect("ESP8266Client")) {
57+
if (client.connect("ESP8266Client")) {
58+
client.subscribe(in_x);
59+
client.subscribe(in_y);
60+
client.subscribe(in_z);
61+
} else {
62+
delay(5000);
63+
}
64+
}
65+
}
66+
67+
void callback(char* topic, byte* payload, unsigned int length) {
68+
if (topic == in_x ){
69+
Serial.print("(");
70+
}
71+
for (int i = 0; i < length; i++) {
72+
char receivedChar = (char)payload[i];
73+
//Serial.print(receivedChar);
74+
if (topic == in_x || topic == in_y){
75+
Serial.print(receivedChar);
76+
}
77+
}
78+
if (topic == in_x ){
79+
Serial.print(",");
80+
}
81+
else if ( topic == in_y) {
82+
Serial.print(")");
83+
}
84+
Serial.println();
85+
}
86+
87+
void loop() {
88+
if (!client.connected()) {
89+
digitalWrite(in_led, HIGH);
90+
reconnect();
91+
}
92+
digitalWrite(in_led, LOW);
93+
client.loop();
94+
// Publishes a random 0 and 1 like someone switching off and on randomly (random(2))
95+
if(millis() > lastTimeSend + waitTime){
96+
client.publish(out_topic, "Online", true);
97+
lastTimeSend = millis();
98+
}
99+
100+
delay(100);
101+
102+
103+
//delay(1000);
104+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include <ESP8266WiFi.h>
2+
#include <PubSubClient.h>
3+
4+
#define wifi_ssid "Robot14"
5+
#define wifi_password "ciscocisco"
6+
7+
#define mqtt_server "192.168.137.1"
8+
#define mqtt_port 1883
9+
10+
#define in_topic "/light/in"
11+
#define in_x "/point/x"
12+
#define in_y "/point/y"
13+
#define in_z "/point/z"
14+
#define out_topic "/light/out" //output
15+
16+
// Replace by 2 if you aren't enable to use Serial Monitor... Don't forget to Rewire R1 to GPIO2!
17+
#define in_led 2
18+
19+
int lastTimeSend;
20+
const int waitTime = 500;
21+
22+
WiFiClient espClient;
23+
PubSubClient client;
24+
25+
void setup() {
26+
Serial.begin(115200);
27+
pinMode(in_led, OUTPUT);
28+
digitalWrite(in_led, HIGH);
29+
setup_wifi();
30+
client.setClient(espClient);
31+
client.setServer(mqtt_server, mqtt_port);
32+
client.setCallback(callback);
33+
34+
// initialize digital pin LED_BUILTIN as an output.
35+
pinMode(in_led, OUTPUT);
36+
digitalWrite(in_led, HIGH);
37+
}
38+
39+
void setup_wifi() {
40+
delay(10);
41+
// We start by connecting to a WiFi network
42+
Serial.println();
43+
Serial.print("Connecting to ");
44+
Serial.println(wifi_ssid);
45+
46+
WiFi.begin(wifi_ssid, wifi_password);
47+
48+
while (WiFi.status() != WL_CONNECTED) {
49+
delay(500);
50+
Serial.print(".");
51+
}
52+
53+
Serial.println("");
54+
Serial.println("WiFi connected");
55+
Serial.println("IP address: ");
56+
Serial.println(WiFi.localIP());
57+
}
58+
59+
void reconnect() {
60+
// Loop until we're reconnected
61+
while (!client.connected()) {
62+
Serial.print("Attempting MQTT connection...");
63+
// Attempt to connect
64+
// If you do not want to use a username and password, change next line to
65+
// if (client.connect("ESP8266Client")) {
66+
if (client.connect("ESP8266Client")) {
67+
Serial.println("connected");
68+
client.subscribe(in_x);
69+
client.subscribe(in_y);
70+
client.subscribe(in_z);
71+
} else {
72+
Serial.print("failed, rc=");
73+
Serial.print(client.state());
74+
Serial.println(" try again in 5 seconds");
75+
// Wait 5 seconds before retrying
76+
delay(5000);
77+
}
78+
}
79+
}
80+
81+
void callback(char* topic, byte* payload, unsigned int length) {
82+
Serial.print("Message arrived [");
83+
Serial.print(topic);
84+
Serial.print("] ");
85+
for (int i = 0; i < length; i++) {
86+
char receivedChar = (char)payload[i];
87+
Serial.print(receivedChar);
88+
if (topic == in_x ){
89+
Serial.print("x");
90+
}
91+
else if ( topic == in_y) {
92+
Serial.print("y");
93+
}
94+
else if ( topic == in_z ){
95+
Serial.print(" pen");
96+
}
97+
}
98+
Serial.println();
99+
}
100+
101+
void loop() {
102+
if (!client.connected()) {
103+
digitalWrite(in_led, HIGH);
104+
reconnect();
105+
}
106+
digitalWrite(in_led, LOW);
107+
client.loop();
108+
// Publishes a random 0 and 1 like someone switching off and on randomly (random(2))
109+
if(millis() > lastTimeSend + waitTime){
110+
client.publish(out_topic, "Online", true);
111+
lastTimeSend = millis();
112+
}
113+
114+
delay(100);
115+
116+
117+
//delay(1000);
118+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#pip install paho-mqtt
2+
#python3 -m pip install -U pygame --user
3+
import pygame
4+
from pygame.locals import *
5+
import sys
6+
import time
7+
import paho.mqtt.client as mqtt
8+
9+
SCREENSIZE = (400,400)
10+
11+
pygame.init()
12+
screen = pygame.display.set_mode(SCREENSIZE)
13+
14+
#variabelen definieren
15+
#kleuren
16+
BLACK = (0,0,0)
17+
BLUE = (0,0,255)
18+
RED = (255,0,0)
19+
WHITE = (255,255,255)
20+
MOUSE = (200,200,125)
21+
22+
points = []
23+
drawpoints = []
24+
25+
delay = 0.25
26+
27+
lastTime = 0
28+
29+
grid = []
30+
def plotGrid(steps):
31+
for i in range(steps,SCREENSIZE[0],steps*2):
32+
grid.append((i,-1))
33+
grid.append((i,SCREENSIZE[1]+1))
34+
grid.append((i+steps,SCREENSIZE[1]+1))
35+
grid.append((i+steps,-1))
36+
37+
grid.append((0,0))
38+
for i in range(steps,SCREENSIZE[1],steps*2):
39+
grid.append((-1,i))
40+
grid.append((SCREENSIZE[0],i))
41+
grid.append((SCREENSIZE[0],i+steps))
42+
grid.append((-1,i+steps))
43+
44+
plotGrid(50)
45+
46+
#title
47+
pygame.display.set_caption("LineDancer")
48+
49+
# The callback for when the client receives a CONNACK response from the server.
50+
def on_connect(client, userdata, flags, rc):
51+
print("Connected with result code "+str(rc))
52+
53+
# Subscribing in on_connect() means that if we lose the connection and
54+
# reconnect then subscriptions will be renewed.
55+
client.subscribe("/light/out")
56+
57+
# The callback for when a PUBLISH message is received from the server.
58+
def on_message(client, userdata, msg):
59+
print(msg.topic+" "+str(msg.payload))
60+
61+
client = mqtt.Client(client_id="PC")
62+
client.on_connect = on_connect
63+
client.on_message = on_message
64+
65+
client.connect("192.168.137.1", 1883, 60)
66+
67+
# Blocking call that processes network traffic, dispatches callbacks and
68+
# handles reconnecting.
69+
# Other loop*() functions are available that give a threaded interface and a
70+
# manual interface.
71+
client.loop_start()
72+
print("connection success")
73+
74+
def drawMyLines(points,screen,color):
75+
lineThickness = 2
76+
pygame.draw.lines(screen, color, False,
77+
points, lineThickness)
78+
79+
80+
81+
while True:
82+
screensize = (0,0,SCREENSIZE[0],SCREENSIZE[1])
83+
pygame.draw.rect(screen, WHITE,screensize)
84+
pygame.draw.lines(screen, BLACK, False, grid, 1)
85+
for event in pygame.event.get():
86+
if event.type == QUIT:
87+
pygame.quit()
88+
client.loop_stop()
89+
client.disconnect()
90+
sys.exit()
91+
elif True == pygame.mouse.get_pressed()[0]:
92+
if(time.time() > (lastTime + delay)):
93+
points.append(pygame.mouse.get_pos())
94+
print(points)
95+
client.publish("/point/x", payload=points[-1][0], qos=0, retain=False)
96+
client.publish("/point/y", payload=points[-1][1], qos=0, retain=False)
97+
client.publish("/point/z", payload=1, qos=0, retain=False)
98+
lastTime = time.time()
99+
elif False == pygame.mouse.get_pressed()[0]:
100+
if(time.time() > (lastTime + delay)):
101+
#points.append((-1,-1))
102+
print("Pen is UP")
103+
#publish("/point/x", payload=points[-1][0], qos=0, retain=False)
104+
#publish("/point/y", payload=points[-1][1], qos=0, retain=False)
105+
#publish("/point/z", payload=0, qos=0, retain=False)
106+
lastTime = time.time()
107+
108+
if len(points) > 1:
109+
drawMyLines(points, screen, BLUE)
110+
pygame.draw.circle(screen, MOUSE,
111+
pygame.mouse.get_pos(), 5, 0)
112+
pygame.display.update()

0 commit comments

Comments
 (0)