Skip to content

Commit 506d891

Browse files
committed
mqtt
1 parent 5b24e58 commit 506d891

File tree

4 files changed

+376
-0
lines changed

4 files changed

+376
-0
lines changed
194 KB
Binary file not shown.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include <Adafruit_SSD1306.h>
2+
#include <Wire.h>
3+
#include <Adafruit_GFX.h>
4+
#include <Arduino_JSON.h>
5+
#include <WiFi.h>
6+
#include <HTTPClient.h>
7+
8+
#define SCREEN_WIDTH 128 // OLED display width, in pixels
9+
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
10+
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
11+
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
12+
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
13+
14+
const char* ssid = "www.zeppelinmaker.it";
15+
const char* password = "****************";
16+
17+
String owkey = "xxxxxxxxxxxxxxxxxxxxxxxxx";
18+
String city = "Milan";
19+
String country = "IT";
20+
21+
String jsonBuffer;
22+
23+
unsigned long t1;
24+
25+
void setup() {
26+
Serial.begin(115200);
27+
28+
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
29+
Serial.println(F("SSD1306 allocation failed"));
30+
for(;;); // Don't proceed, loop forever
31+
}
32+
display.setTextSize(1); // Normal 1:1 pixel scale
33+
display.setTextColor(SSD1306_WHITE); // Draw white text
34+
display.cp437(true);
35+
display.clearDisplay();
36+
display.display();
37+
38+
display.setCursor(0,0);
39+
display.print("Connecting...");
40+
display.display();
41+
42+
WiFi.begin(ssid, password);
43+
while (WiFi.status() != WL_CONNECTED) {
44+
delay(500);
45+
Serial.print(".");
46+
}
47+
Serial.println("!");
48+
display.clearDisplay();
49+
display.setCursor(0,0);
50+
display.print("Connected!");
51+
display.display();
52+
delay(2000);
53+
54+
t1 = 599000;
55+
}
56+
57+
void loop() {
58+
if ((millis() - t1) >= 600000l) {
59+
if (WiFi.status() == WL_CONNECTED) {
60+
String addr = "http://api.openweathermap.org/data/2.5/weather?q="+city+","+country+"&APPID="+owkey;
61+
Serial.println(addr);
62+
63+
jsonBuffer = getRequest(addr.c_str());
64+
Serial.println(jsonBuffer);
65+
JSONVar obj = JSON.parse(jsonBuffer);
66+
if (JSON.typeof(obj) == "undefined") {
67+
Serial.println("Errore json");
68+
} else {
69+
Serial.print("temp: ");
70+
Serial.println(obj["main"]["temp"]);
71+
String str = JSON.stringify(obj["main"]["temp"]);
72+
float temp = str.toFloat() - 273.15f;
73+
Serial.print("temp (C): ");
74+
Serial.println(temp);
75+
Serial.print("hum: ");
76+
Serial.println(obj["main"]["humidity"]);
77+
Serial.print("press: ");
78+
Serial.println(obj["main"]["pressure"]);
79+
//aggiorno display
80+
display.clearDisplay();
81+
display.setCursor(0,0);
82+
display.print("t: ");
83+
display.print(temp);
84+
display.print(" C");
85+
display.display();
86+
}
87+
88+
} else {
89+
WiFi.disconnect();
90+
delay(1000);
91+
WiFi.reconnect();
92+
}
93+
t1 = millis();
94+
}
95+
}
96+
97+
String getRequest(const char* url) {
98+
WiFiClient client;
99+
HTTPClient http;
100+
http.begin(client, url);
101+
int code = http.GET();
102+
String payload = "{}";
103+
if (code > 0) {
104+
Serial.print("HTTP CODE: ");Serial.println(code);
105+
payload = http.getString();
106+
} else {
107+
Serial.print("HTTP ERROR: ");Serial.println(code);
108+
}
109+
http.end();
110+
return payload;
111+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include <WiFi.h>
2+
#include <PubSubClient.h>
3+
4+
const char* ssid = "nomerete";
5+
const char* password = "***********";
6+
7+
//https://www.hivemq.com/mqtt/public-mqtt-broker/
8+
const char* mqttBroker = "broker.hivemq.com";
9+
const int mqttPort = 1883;
10+
11+
const char* temperatureTopic = "client/sensor/temp";
12+
const char* humidityTopic = "client/sensor/hum";
13+
WiFiClient wifiClient;
14+
PubSubClient mqttClient(wifiClient);
15+
16+
int temp = 20;
17+
int hum = 50;
18+
19+
unsigned long t1 = 0;
20+
21+
//prototipi
22+
void callback(char *topic, byte *payload, unsigned int len);
23+
void mqtt_reconnect();
24+
void pubMsg(const char* topic, int val);
25+
26+
void setup() {
27+
Serial.begin(115200);
28+
WiFi.begin(ssid, password);
29+
while (WiFi.status() != WL_CONNECTED) {
30+
delay(500);
31+
Serial.print(".");
32+
}
33+
Serial.println("!");
34+
Serial.println("Connesso");
35+
36+
mqttClient.setServer(mqttBroker, mqttPort);
37+
mqttClient.setCallback(callback);
38+
39+
randomSeed(analogRead(1));
40+
}
41+
42+
void loop() {
43+
if (!mqttClient.connected()) {
44+
mqtt_reconnect();
45+
}
46+
47+
if ((millis() - t1) > 5000) {
48+
49+
hum += random(-3, 3);
50+
temp += random(-3, 3);
51+
52+
pubMsg(temperatureTopic, temp);
53+
pubMsg(humidityTopic, hum);
54+
55+
t1 = millis();
56+
}
57+
}
58+
59+
void callback(char *topic, byte *payload, unsigned int len) {
60+
Serial.print("MSG RX: ");
61+
Serial.println(topic);
62+
}
63+
64+
void mqtt_reconnect(){
65+
while (!mqttClient.connected()) {
66+
Serial.println("Reconnect MQTT server...");
67+
String clientid = "myLolin32-" + String(random(20000));
68+
69+
Serial.println(clientid);
70+
if (mqttClient.connect(clientid.c_str())) {
71+
Serial.println("Connected");
72+
} else {
73+
Serial.println("not connected");
74+
delay(5000);
75+
}
76+
}
77+
}
78+
79+
void pubMsg(const char* topic, int val){
80+
char msg[5];
81+
snprintf(msg, 5, "%d", val);
82+
Serial.println(msg);
83+
84+
mqttClient.publish(topic, msg);
85+
Serial.print(topic);
86+
Serial.print(":");
87+
Serial.println(val);
88+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
[
2+
{
3+
"id": "926489230d4875a2",
4+
"type": "tab",
5+
"label": "Flow 5",
6+
"disabled": false,
7+
"info": "",
8+
"env": []
9+
},
10+
{
11+
"id": "eeba134347eac607",
12+
"type": "mqtt in",
13+
"z": "926489230d4875a2",
14+
"name": "",
15+
"topic": "client/sensor/temp",
16+
"qos": "2",
17+
"datatype": "auto-detect",
18+
"broker": "952e25723d90ec5d",
19+
"nl": false,
20+
"rap": true,
21+
"rh": 0,
22+
"inputs": 0,
23+
"x": 190,
24+
"y": 220,
25+
"wires": [
26+
[
27+
"0cc0318938844a90",
28+
"12b63715a7f8c4e9",
29+
"dd457a95d3794d1d"
30+
]
31+
]
32+
},
33+
{
34+
"id": "0cc0318938844a90",
35+
"type": "ui_gauge",
36+
"z": "926489230d4875a2",
37+
"name": "",
38+
"group": "f81fb063b1f3e4a7",
39+
"order": 1,
40+
"width": 0,
41+
"height": 0,
42+
"gtype": "gage",
43+
"title": "gauge",
44+
"label": "units",
45+
"format": "{{value}}",
46+
"min": 0,
47+
"max": "100",
48+
"colors": [
49+
"#00b500",
50+
"#e6e600",
51+
"#ca3838"
52+
],
53+
"seg1": "",
54+
"seg2": "",
55+
"diff": false,
56+
"className": "",
57+
"x": 490,
58+
"y": 140,
59+
"wires": []
60+
},
61+
{
62+
"id": "dd457a95d3794d1d",
63+
"type": "ui_chart",
64+
"z": "926489230d4875a2",
65+
"name": "",
66+
"group": "f81fb063b1f3e4a7",
67+
"order": 3,
68+
"width": 0,
69+
"height": 0,
70+
"label": "chart",
71+
"chartType": "line",
72+
"legend": "false",
73+
"xformat": "HH:mm:ss",
74+
"interpolate": "linear",
75+
"nodata": "",
76+
"dot": false,
77+
"ymin": "",
78+
"ymax": "",
79+
"removeOlder": 1,
80+
"removeOlderPoints": "",
81+
"removeOlderUnit": "3600",
82+
"cutout": 0,
83+
"useOneColor": false,
84+
"useUTC": false,
85+
"colors": [
86+
"#1f77b4",
87+
"#aec7e8",
88+
"#ff7f0e",
89+
"#2ca02c",
90+
"#98df8a",
91+
"#d62728",
92+
"#ff9896",
93+
"#9467bd",
94+
"#c5b0d5"
95+
],
96+
"outputs": 1,
97+
"useDifferentColor": false,
98+
"className": "",
99+
"x": 490,
100+
"y": 260,
101+
"wires": [
102+
[]
103+
]
104+
},
105+
{
106+
"id": "12b63715a7f8c4e9",
107+
"type": "ui_text",
108+
"z": "926489230d4875a2",
109+
"group": "f81fb063b1f3e4a7",
110+
"order": 2,
111+
"width": 0,
112+
"height": 0,
113+
"name": "",
114+
"label": "text",
115+
"format": "{{msg.payload}}",
116+
"layout": "row-spread",
117+
"className": "",
118+
"style": false,
119+
"font": "",
120+
"fontSize": 16,
121+
"color": "#000000",
122+
"x": 490,
123+
"y": 200,
124+
"wires": []
125+
},
126+
{
127+
"id": "952e25723d90ec5d",
128+
"type": "mqtt-broker",
129+
"name": "hivemqtt-server",
130+
"broker": "broker.hivemq.com",
131+
"port": 1883,
132+
"clientid": "",
133+
"autoConnect": true,
134+
"usetls": false,
135+
"protocolVersion": 4,
136+
"keepalive": 60,
137+
"cleansession": true,
138+
"autoUnsubscribe": true,
139+
"birthTopic": "",
140+
"birthQos": "0",
141+
"birthRetain": "false",
142+
"birthPayload": "",
143+
"birthMsg": {},
144+
"closeTopic": "",
145+
"closeQos": "0",
146+
"closeRetain": "false",
147+
"closePayload": "",
148+
"closeMsg": {},
149+
"willTopic": "",
150+
"willQos": "0",
151+
"willRetain": "false",
152+
"willPayload": "",
153+
"willMsg": {},
154+
"userProps": "",
155+
"sessionExpiry": ""
156+
},
157+
{
158+
"id": "f81fb063b1f3e4a7",
159+
"type": "ui_group",
160+
"name": "Group 1",
161+
"tab": "7440e84da654de70",
162+
"order": 1,
163+
"disp": true,
164+
"width": 6,
165+
"collapse": false,
166+
"className": ""
167+
},
168+
{
169+
"id": "7440e84da654de70",
170+
"type": "ui_tab",
171+
"name": "Tab 1",
172+
"icon": "dashboard",
173+
"order": 1,
174+
"disabled": false,
175+
"hidden": false
176+
}
177+
]

0 commit comments

Comments
 (0)