2018年11月25日 星期日

Interfacing Arduino With Node-RED

Interfacing Arduino With Node-RED


//Arduino 程式

/*LED_Breathing.ino Arduining.com  20 AUG 2015
Using NodeMCU Development Kit V1.0
Going beyond Blink sketch to see the blue LED breathing.
A PWM modulation is made in software because GPIO16 can't
be used with analogWrite().
*/

//#define BUILTIN_LED     D0        // Led in NodeMCU at pin GPIO16 (D0).
#define BRIGHT    350     //max led intensity (1-500)
#define INHALE    1250    //Inhalation time in milliseconds.
#define PULSE     INHALE*1000/BRIGHT
#define REST      5000    //Rest Between Inhalations.

//----- Setup function. ------------------------
void setup() {                
  pinMode(BUILTIN_LED , OUTPUT);   // LED pin as output.   
  Serial.begin(115200);
  Serial.println("Wemos D1 Build LED ON , OFF Message send to Com Port Show to Node-Red!");
  
}

//----- Loop routine. --------------------------
void loop() {
  //ramp increasing intensity, Inhalation: 
  for (int i=1;i<BRIGHT;i++){
    digitalWrite(BUILTIN_LED, LOW);          // turn the LED on.
    delayMicroseconds(i*10);         // wait
    digitalWrite(BUILTIN_LED, HIGH);         // turn the LED off.
    delayMicroseconds(PULSE-i*10);   // wait
    delay(0);                        //to prevent watchdog firing.
  }
  
  Serial.print("ON"); 
  delay(REST);                       //take a rest...
  Serial.println(""); 
  //ramp decreasing intensity, Exhalation (half time):
  for (int i=BRIGHT-1;i>0;i--){
    digitalWrite(BUILTIN_LED, LOW);          // turn the LED on.
    delayMicroseconds(i*10);          // wait
    digitalWrite(BUILTIN_LED, HIGH);         // turn the LED off.
    delayMicroseconds(PULSE-i*10);  // wait
    i--;
    delay(0);                        //to prevent watchdog firing.
  }
  
  Serial.print("OFF");
  delay(REST);                       //take a rest...
  Serial.println(""); 
}

//Node-Red Json File

[{"id":"63b28b77.c39dc4","type":"debug","z":"8e1c2d49.f884e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":330,"y":120,"wires":[]},{"id":"cb08945c.0923d8","type":"ui_text","z":"8e1c2d49.f884e","group":"dbdf585d.7c3478","order":0,"width":0,"height":0,"name":"LED Message","label":"Arduino Reading","format":"{{msg.payload}}","layout":"col-center","x":340,"y":60,"wires":[]},{"id":"dda11bb7.eba0b8","type":"serial in","z":"8e1c2d49.f884e","name":"","serial":"965dbdd7.dd54c","x":70,"y":140,"wires":[["cb08945c.0923d8","63b28b77.c39dc4","67d19b8b.e03474"]]},{"id":"59325d79.183f94","type":"function","z":"8e1c2d49.f884e","name":"Test","func":"if (msg.payload === \"ON\")\n  msg.color =\"lime\";\n\nif (msg.payload === \"OFF\")\n  msg.color =\"red\";\n  \n\n// msg.color = (msg.payload === \"ON\")?\"lime\":\"red\";\n\n\nreturn msg;","outputs":1,"noerr":0,"x":510,"y":280,"wires":[["ae83ae61.feb5d"]]},{"id":"ae83ae61.feb5d","type":"ui_text","z":"8e1c2d49.f884e","group":"dbdf585d.7c3478","order":3,"width":"4","height":"4","name":"","label":"LED","format":"<font color={{msg.color}} ><i class=\"fa fa-circle\" style=\"font-size:24px;\"></i></font>","layout":"row-center","x":650,"y":280,"wires":[]},{"id":"bab93485.9a7a58","type":"switch","z":"8e1c2d49.f884e","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"ON","vt":"str"},{"t":"cont","v":"OFF","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":230,"y":280,"wires":[["b9f8f698.b29508"],["b1a95511.629bf8"]]},{"id":"b9f8f698.b29508","type":"function","z":"8e1c2d49.f884e","name":"ON","func":"msg.payload =\"ON\";\nreturn msg;\n","outputs":1,"noerr":0,"x":370,"y":240,"wires":[["59325d79.183f94"]]},{"id":"b1a95511.629bf8","type":"function","z":"8e1c2d49.f884e","name":"OFF","func":"msg.payload=\"OFF\";\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":320,"wires":[["59325d79.183f94"]]},{"id":"67d19b8b.e03474","type":"json","z":"8e1c2d49.f884e","name":"","property":"payload","action":"str","pretty":false,"x":160,"y":360,"wires":[["bab93485.9a7a58"]]},{"id":"dbdf585d.7c3478","type":"ui_group","z":"","name":"LED Testing","tab":"89e5353f.12fef8","disp":true,"width":"4","collapse":false},{"id":"965dbdd7.dd54c","type":"serial-port","z":"8e1c2d49.f884e","serialport":"COM3","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","newline":"\\n","bin":"false","out":"char","addchar":false,"responsetimeout":""},{"id":"89e5353f.12fef8","type":"ui_tab","name":"Tab","icon":"dashboard","order":0}]

















沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...