From b4d82b0010d746f1a0f0b4da698b26d217f1ab1f Mon Sep 17 00:00:00 2001 From: Andrea Manni Date: Sat, 9 Dec 2017 17:29:18 +0100 Subject: [PATCH] PWM correzione --- .gitignore | 3 + iot/blink_iot/blink_iot.ino | 77 ++++++++++++++++ iot/blink_iot/connessione.ino | 29 ++++++ iot/blink_iot/html.h | 34 +++++++ iot/mqtt_base/connection.ino | 46 +++++++++ iot/mqtt_base/mqtt_base.ino | 44 +++++++++ iot/mqtt_esp8266/connessione.ino | 1 + iot/mqtt_esp8266/mqtt_esp8266.ino | 128 ++++++++++++++++++++++++++ iot/mqtt_publish/connection.ino | 46 +++++++++ iot/mqtt_publish/mqtt_publish.ino | 39 ++++++++ libraries/common/examples/pwm/pwm.ino | 2 +- 11 files changed, 448 insertions(+), 1 deletion(-) create mode 100644 iot/blink_iot/blink_iot.ino create mode 100644 iot/blink_iot/connessione.ino create mode 100644 iot/blink_iot/html.h create mode 100644 iot/mqtt_base/connection.ino create mode 100644 iot/mqtt_base/mqtt_base.ino create mode 100644 iot/mqtt_esp8266/connessione.ino create mode 100644 iot/mqtt_esp8266/mqtt_esp8266.ino create mode 100644 iot/mqtt_publish/connection.ino create mode 100644 iot/mqtt_publish/mqtt_publish.ino diff --git a/.gitignore b/.gitignore index 71827a3..8abd4d1 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,6 @@ libraries/FreeRTOS/ libraries/Adafruit_HMC5883_Unified/ libraries/aero libraries/AccelStepper +libraries/PubSubClient/ +libraries/WiFiEsp/ +libraries/psw diff --git a/iot/blink_iot/blink_iot.ino b/iot/blink_iot/blink_iot.ino new file mode 100644 index 0000000..2172b9d --- /dev/null +++ b/iot/blink_iot/blink_iot.ino @@ -0,0 +1,77 @@ +/* + * Blink IoT + +This sketch is meant to run on a ESP8266 chip +(NodeMCU like), not on a AVR Arduino board. + +*/ + +#include +#include "html.h" +void connessione() ; + +//int ledPin = LED_BUILTIN; // GPIO13 +int ledPin = 2; // GPIO2 on LoLin Nodemcu +WiFiServer server(80); + +void setup() { + Serial.begin(115200); + delay(10); + + pinMode(ledPin, OUTPUT); + digitalWrite(ledPin, HIGH); // OnBoard LED is inverted + + connessione(); +} + +void loop() { + // Check if a client has connected + WiFiClient client = server.available(); + if (!client) { + return; + } + + // Wait until the client sends some data + Serial.println("new client"); + while (!client.available()) { + delay(1); + } + + // Read the first line of the request + String request = client.readStringUntil('\r'); + Serial.println(request); + client.flush(); + + // Match the request + + int value = LOW; + if (request.indexOf("/ON") != -1) { + digitalWrite(ledPin, LOW); + value = HIGH; + } + if (request.indexOf("/OFF") != -1) { + digitalWrite(ledPin, HIGH); + value = LOW; + } + + client.println(head); + client.println(body); + if (value == HIGH) { + client.print("
Led pin is now: On
"); + } else { + client.print("
Led pin is now: Off
"); + } + + + + //client.println(FPSTR(footer)); // This is to damm slow!! + // See http://arduino-esp8266.readthedocs.io/en/latest/PROGMEM.html + client.println((footer)); + + delay(1); + Serial.print("Client disonnected. Millis: "); + Serial.println(millis()); + +} + + diff --git a/iot/blink_iot/connessione.ino b/iot/blink_iot/connessione.ino new file mode 100644 index 0000000..542709b --- /dev/null +++ b/iot/blink_iot/connessione.ino @@ -0,0 +1,29 @@ +#include +void connessione() { + // Connect to WiFi network @home: TP +// const char* ssid = "ssid"; +// const char* password = "pass"; + Serial.println(); + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.println("WiFi connected"); + + // Start the server + server.begin(); + Serial.println("Server started"); + + // Print the IP address + Serial.print("Use this URL to connect: "); + Serial.print("http://"); + Serial.print(WiFi.localIP()); + Serial.println("/"); +} diff --git a/iot/blink_iot/html.h b/iot/blink_iot/html.h new file mode 100644 index 0000000..bb94365 --- /dev/null +++ b/iot/blink_iot/html.h @@ -0,0 +1,34 @@ +static const char* head = R"foo( +HTTP/1.1 200 OK +Content-Type: text/html + + +IoT Blink + + +)foo"; + +static const char* body = R"foo( + +
+
+

Arduino IoT Example

+

This WebApp turns On / Off the onboard LED with Arduino code.

+
+
+ + +

+)foo"; + +static const char footer[] = R"foo( +
+
+ + + + + + + +)foo"; diff --git a/iot/mqtt_base/connection.ino b/iot/mqtt_base/connection.ino new file mode 100644 index 0000000..eaba28d --- /dev/null +++ b/iot/mqtt_base/connection.ino @@ -0,0 +1,46 @@ +#include +void connection() { + // Connect to WiFi network @home: TP +// const char* ssid = "ssid"; +// const char* password = "pass"; + Serial.println(); + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.println("WiFi connected"); + + + // Print the IP address + Serial.print("Local IP: \t"); + Serial.println(WiFi.localIP()); + Serial.print("Web URL: \thttp://"); + Serial.print(WiFi.localIP()); + Serial.println("/"); +} + +void reconnect() { + // MQTT: Loop until we're reconnected + while (!client.connected()) { + Serial.println("Attempting MQTT connection..."); + // Attempt to connect + if (client.connect("ESP8266Client")) { + Serial.println(">> Connected <<"); + client.subscribe(input); + } else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + // Some LED status feedback would be useful + } + } +} diff --git a/iot/mqtt_base/mqtt_base.ino b/iot/mqtt_base/mqtt_base.ino new file mode 100644 index 0000000..31babaa --- /dev/null +++ b/iot/mqtt_base/mqtt_base.ino @@ -0,0 +1,44 @@ +#include +void connection() ; + +#include +const char* mqtt_server = "chrome"; +const char* input = "inTopic" ; +const char* output = "outTopic" ; + +WiFiClient espClient; // WiFi connection obj +PubSubClient client(espClient); // MQTT client obj + +const byte ledPin = 2; // GPIO2 on LoLin Nodemcu + +void setup() { + Serial.begin(115200); + delay(10); + + pinMode(ledPin, OUTPUT); + digitalWrite(ledPin, HIGH); // OnBoard LED is inverted + + connection(); + client.setServer(mqtt_server, 1883); + client.setCallback(callback); //message callback function + // called when a message arrives for a subscription created by this client. + +} + +void loop() { + +if (!client.connected()) { // Keep MQTT connection on + reconnect(); + } +client.loop(); + +client.publish(output, "Test Message"); +delay(500); +} + +void callback(char* topic, byte* payload, unsigned int length) { + + +} + + diff --git a/iot/mqtt_esp8266/connessione.ino b/iot/mqtt_esp8266/connessione.ino new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/iot/mqtt_esp8266/connessione.ino @@ -0,0 +1 @@ + diff --git a/iot/mqtt_esp8266/mqtt_esp8266.ino b/iot/mqtt_esp8266/mqtt_esp8266.ino new file mode 100644 index 0000000..cd48994 --- /dev/null +++ b/iot/mqtt_esp8266/mqtt_esp8266.ino @@ -0,0 +1,128 @@ +/* + Basic ESP8266 MQTT example + + This sketch demonstrates the capabilities of the pubsub library in combination + with the ESP8266 board/library. + + It connects to an MQTT server then: + - publishes "hello world" to the topic "outTopic" every two seconds + - subscribes to the topic "inTopic", printing out any messages + it receives. NB - it assumes the received payloads are strings not binary + - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led, + else switch it off + + It will reconnect to the server if the connection is lost using a blocking + reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to + achieve the same result without blocking the main loop. + + To install the ESP8266 board, (using Arduino 1.6.4+): + - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs": + http://arduino.esp8266.com/stable/package_esp8266com_index.json + - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266" + - Select your ESP8266 in "Tools -> Board" + +*/ + +#include +#include +#include +// Update these with values suitable for your network. + +//const char* ssid = "........"; +//const char* password = "........"; +const char* mqtt_server = "chrome"; +const char* input = "inTopic" ; +const char* output = "outTopic" ; + +WiFiClient espClient; +PubSubClient client(espClient); +long lastMsg = 0; +char msg[50]; +int value = 0; + +void setup() { + pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output + Serial.begin(115200); + setup_wifi(); + client.setServer(mqtt_server, 1883); + client.setCallback(callback); +} + +void setup_wifi() { + + delay(10); + // We start by connecting to a WiFi network + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + +void callback(char* topic, byte* payload, unsigned int length) { + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.print("] "); + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + } + Serial.println(); + + // Switch on the LED if an 1 was received as first character + if ((char)payload[0] == '1') { + digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level + // but actually the LED is on; this is because + // it is acive low on the ESP-01) + } else { + digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH + } + +} + +void reconnect() { + // Loop until we're reconnected + while (!client.connected()) { + Serial.print("Attempting MQTT connection..."); + // Attempt to connect + if (client.connect("ESP8266Client")) { + Serial.println("connected"); + // Once connected, publish an announcement... + client.publish(output, "hello world"); + // ... and resubscribe + client.subscribe(input); + } else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } + } +} +void loop() { + + if (!client.connected()) { + reconnect(); + } + client.loop(); + + long now = millis(); + if (now - lastMsg > 2000) { + lastMsg = now; + ++value; + snprintf (msg, 75, "hello world #%ld", value); + Serial.print("Publish message: "); + Serial.println(msg); + client.publish(output, msg); + } +} diff --git a/iot/mqtt_publish/connection.ino b/iot/mqtt_publish/connection.ino new file mode 100644 index 0000000..eaba28d --- /dev/null +++ b/iot/mqtt_publish/connection.ino @@ -0,0 +1,46 @@ +#include +void connection() { + // Connect to WiFi network @home: TP +// const char* ssid = "ssid"; +// const char* password = "pass"; + Serial.println(); + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.println("WiFi connected"); + + + // Print the IP address + Serial.print("Local IP: \t"); + Serial.println(WiFi.localIP()); + Serial.print("Web URL: \thttp://"); + Serial.print(WiFi.localIP()); + Serial.println("/"); +} + +void reconnect() { + // MQTT: Loop until we're reconnected + while (!client.connected()) { + Serial.println("Attempting MQTT connection..."); + // Attempt to connect + if (client.connect("ESP8266Client")) { + Serial.println(">> Connected <<"); + client.subscribe(input); + } else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + // Some LED status feedback would be useful + } + } +} diff --git a/iot/mqtt_publish/mqtt_publish.ino b/iot/mqtt_publish/mqtt_publish.ino new file mode 100644 index 0000000..dc03d00 --- /dev/null +++ b/iot/mqtt_publish/mqtt_publish.ino @@ -0,0 +1,39 @@ +#include +void connection() ; + +#include +const char* mqtt_server = "chrome"; +const char* input = "inTopic" ; +const char* output = "outTopic" ; + +WiFiClient espClient; // WiFi connection obj +PubSubClient client(espClient); // MQTT client obj + +const byte ledPin = 2; // GPIO2 on LoLin Nodemcu + +void setup() { + Serial.begin(115200); + delay(10); + + pinMode(ledPin, OUTPUT); + digitalWrite(ledPin, HIGH); // OnBoard LED is inverted + + connection(); + client.setServer(mqtt_server, 1883); +} + +void loop() { + +if (!client.connected()) { // Keep MQTT connection on + reconnect(); + } + + +char msg[50]; + snprintf (msg, 75, "Test message @millis: %ld", millis()); +client.publish(output, msg); +delay(1000); + +client.loop(); +} + diff --git a/libraries/common/examples/pwm/pwm.ino b/libraries/common/examples/pwm/pwm.ino index e3fe058..cd69bc5 100644 --- a/libraries/common/examples/pwm/pwm.ino +++ b/libraries/common/examples/pwm/pwm.ino @@ -25,7 +25,7 @@ led.Up(2000); // Aumenta la luminosita' linearmente in 2 sec // led.UD(4000); // Aumenta e poi diminuisce la luminostia' in 4 sec (coseno) // Tutti questi metodi accettano un valore opzionale per il ritardo -// led.set(100); // Imposta il valore del PWM da 0-255 +// led.Set(100); // Imposta il valore del PWM da 0-255 // led.lSet(100); // Imposta il valore del PWM con correzione luminosita' // analogWrite(lum100); // Equivalente a sopra } -- 2.39.2