]> git.piffa.net Git - sketchbook_andrea/commitdiff
PWM correzione
authorAndrea Manni <andrea@piffa.net>
Sat, 9 Dec 2017 16:29:18 +0000 (17:29 +0100)
committerAndrea Manni <andrea@piffa.net>
Sat, 9 Dec 2017 16:29:18 +0000 (17:29 +0100)
.gitignore
iot/blink_iot/blink_iot.ino [new file with mode: 0644]
iot/blink_iot/connessione.ino [new file with mode: 0644]
iot/blink_iot/html.h [new file with mode: 0644]
iot/mqtt_base/connection.ino [new file with mode: 0644]
iot/mqtt_base/mqtt_base.ino [new file with mode: 0644]
iot/mqtt_esp8266/connessione.ino [new file with mode: 0644]
iot/mqtt_esp8266/mqtt_esp8266.ino [new file with mode: 0644]
iot/mqtt_publish/connection.ino [new file with mode: 0644]
iot/mqtt_publish/mqtt_publish.ino [new file with mode: 0644]
libraries/common/examples/pwm/pwm.ino

index 71827a30dd0c2b6cb8247191edd02592b96cdc14..8abd4d1370208639e27685258343a17e08d5f333 100644 (file)
@@ -9,3 +9,6 @@ libraries/FreeRTOS/
 libraries/Adafruit_HMC5883_Unified/
 libraries/aero
 libraries/AccelStepper
 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 (file)
index 0000000..2172b9d
--- /dev/null
@@ -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 <ESP8266WiFi.h>
+#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("<div class=\"alert alert-info\">Led pin is now: <strong>On </strong></div>");
+  } else {
+    client.print("<div class=\"alert alert-warning\">Led pin is now: <strong>Off </strong></div>");
+  }
+
+
+
+  //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 (file)
index 0000000..542709b
--- /dev/null
@@ -0,0 +1,29 @@
+#include <psw.h>
+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 (file)
index 0000000..bb94365
--- /dev/null
@@ -0,0 +1,34 @@
+static const char* head = R"foo(
+HTTP/1.1 200 OK
+Content-Type: text/html
+
+<head>
+<title>IoT Blink</title>
+<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" >
+</head>
+)foo";
+
+static const char* body = R"foo(
+<body>
+  <div class="container">
+  <div class="jumbotron">
+    <h1>Arduino IoT Example</h1>      
+    <p>This WebApp turns On / Off the onboard LED with Arduino code.</p>
+  </div>
+  <br><center>
+<a href="/ON""><button type="button" class="btn btn-primary btn-lg">Turn On </button></a>
+<a href="/OFF""><button type="button" class="btn btn-secondary btn-lg">Turn Off </button></a>
+<br/><br/>
+)foo";
+  
+static const char footer[]   = R"foo(
+</center>
+</div>
+<!-- Optional JavaScript -->
+    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
+    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
+    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" ></script>
+    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" ></script>
+</body>
+</html>
+)foo";
diff --git a/iot/mqtt_base/connection.ino b/iot/mqtt_base/connection.ino
new file mode 100644 (file)
index 0000000..eaba28d
--- /dev/null
@@ -0,0 +1,46 @@
+#include <psw.h>
+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 (file)
index 0000000..31babaa
--- /dev/null
@@ -0,0 +1,44 @@
+#include <ESP8266WiFi.h>
+void connection() ;
+
+#include <PubSubClient.h>
+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 (file)
index 0000000..8b13789
--- /dev/null
@@ -0,0 +1 @@
+
diff --git a/iot/mqtt_esp8266/mqtt_esp8266.ino b/iot/mqtt_esp8266/mqtt_esp8266.ino
new file mode 100644 (file)
index 0000000..cd48994
--- /dev/null
@@ -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 <ESP8266WiFi.h>
+#include <PubSubClient.h>
+#include <psw.h>
+// 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 (file)
index 0000000..eaba28d
--- /dev/null
@@ -0,0 +1,46 @@
+#include <psw.h>
+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 (file)
index 0000000..dc03d00
--- /dev/null
@@ -0,0 +1,39 @@
+#include <ESP8266WiFi.h>
+void connection() ;
+
+#include <PubSubClient.h>
+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();
+}
+
index e3fe058ae19ba793c67c36db14338ef80fe8ef4a..cd69bc570f781824280cb02983f2321bab6ad3d4 100644 (file)
@@ -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.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
 }
 // led.lSet(100);   // Imposta il valore del PWM con correzione luminosita' 
 // analogWrite(lum100); // Equivalente a sopra
 }