2018年2月27日 星期二

NodeMCU SPI with Arduino IDE

源自於
http://www.electronicwings.com/nodemcu/nodemcu-spi-with-arduino-ide

NodeMCU SPI with Arduino IDE

Introduction

The Serial Peripheral Interface (SPI) is a bus interface connection protocol originally started by Motorola Corp.
  • SPI Interface uses four wires for communication. Hence it is also known as four wire serial communication protocol.
  • SPI is a full duplex master-slave communication protocol. This means that only a single master and a single slave can communicate on the interface bus at the same time.
  • SPI enabled devices work in two basic modes of SPI operation i.e. SPI Master Mode and SPI Slave Mode.
  • Master Device is responsible for initiation of communication. Master Device generates Serial Clock for synchronous data transfer. Master Device can handle multiple slave devices on the bus by selecting them one by one.
NodeMCU based ESP8266 has Hardware SPI with four pins available for SPI communication. With this SPI interface, we can connect any SPI enabled device with NodeMCU and make communication possible with it.
ESP8266 has SPI pins (SD1, CMD, SD0, CLK) which are exclusively used for Quad-SPI communication with flash memory on ESP-12E, hence, they can’t be used for SPI applications. We can use Hardware SPI interface for user end applications.
Below figure shows Quad SPI interface pins that are internally used for flash. It consists quad i/o (4-bit data bus) i.e. four (SDIO_DATA0 – SDIO_DATA3) bidirectional (i/p and o/p) data signals with synchronize clock (SDIO_CLK) and chip select pin (SDIO_CMD). It is mostly used to get more bandwidth/throughput than dual i/o (2-bit data bus) interface.

NodeMCU SPI Pins

NodeMCU SPI pins
NodeMCU SPI Pins

MISO (Master In Slave Out)
                    Master receives data and slave transmits data through this pin.
MOSI (Master Out Slave In)
                    Master transmits data and slave receives data through this pin.
SCLK (Serial Clock)
                    Master generates this clock for the communication, which is used by slave.
                    Only master can initiate serial clock.
CS (Chip Select)
                    Master can select slave device through this pin to start communication with it.

Example

Let’s write Arduino sketch of SPI communication for NodeMCU. Here NodeMCU is acting as master device and we are using Arduino uno as slave device.
In this example, we are sending “Hello Slave” string with ‘\n’ as ending of string from NodeMCU Master device. Slave device receives this string and prints it on serial monitor.
NodeMCU Arduino SPI Interfacing Diagram
NodeMCU Arduino SPI Interfacing Diagram

Arduino sketch for NodeMCU Master SPI

#include<SPI.h>

char buff[]="Hello Slave\n";

void setup() {
 Serial.begin(9600); /* begin serial with 9600 baud */
 SPI.begin();  /* begin SPI */
}

void loop() {
 for(inti=0; i<sizeof buff; i++)  /* transfer buff data per second */
  SPI.transfer(buff[i]);
 delay(1000);  
}

Arduino Sketch for Arduino Uno slave SPI

#include <SPI.h>

char buff [100];
volatile byte index;
volatile bool receivedone;  /* use reception complete flag */

void setup (void)
{
  Serial.begin (9600);
  SPCR |= bit(SPE);         /* Enable SPI */
  pinMode(MISO, OUTPUT);    /* Make MISO pin as OUTPUT */
  index = 0;
  receivedone = false;
  SPI.attachInterrupt();    /* Attach SPI interrupt */
}

void loop (void)
{
  if (receivedone)          /* Check and print received buffer if any */
  {
    buff[index] = 0;
    Serial.println(buff);
    index = 0;
    receivedone = false;
  }
}

// SPI interrupt routine
ISR (SPI_STC_vect)
{
  uint8_t oldsrg = SREG;
  cli();
  char c = SPDR;
  if (index <sizeof buff)
  {
    buff [index++] = c;
    if (c == '\n'){     /* Check for newline character as end of msg */
     receivedone = true;
    }
  }
  SREG = oldsrg;
}

Slave Output Window

This output is received at slave device which is transmitted from master device.
SPI Slave Arduino Output Window

Supporting Files
Source Code
Attached File

利用 74HC595 控制 24 顆 LED 輸出


利用 74HC595 控制 24 顆 LED 輸出

源自於 http://blog.udn.com/awei0905/26967572




// Arduino 控制 24 顆 LED
//static const uint8_t D0   = 16;
//static const uint8_t D1   = 5;
//static const uint8_t D2   = 4;
//static const uint8_t D3   = 0;
//static const uint8_t D4   = 2;
//static const uint8_t D5   = 14;
//static const uint8_t D6   = 12;
//static const uint8_t D7   = 13;
//static const uint8_t D8   = 15;
//static const uint8_t D9   = 3;
//static const uint8_t D10  = 1;
// 接 74HC595 的第 12 支接腳
int latchPin = 5;
// 接 74HC595 的第 11 支接腳
int clockPin = 4;
// 接 74HC595 的第 14 支接腳
int dataPin = 16;

void setup() {
  // 初始化所有腳位
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
  long val = 1;
  for (int led = 0; led < 24; led++) {
    digitalWrite(latchPin, LOW); // 送資料前須把 latchPin 設為 LOW
    shiftOut(dataPin, clockPin, MSBFIRST, ~(val / 65536)); // 送出第三位元組給最遠的 74HC595
    shiftOut(dataPin, clockPin, MSBFIRST, ~(val / 256)); // 送出第二位元組給中間的 74HC595
    shiftOut(dataPin, clockPin, MSBFIRST, ~(val)); // 送出第一位元組給最近的 74HC595
    digitalWrite(latchPin, HIGH); // 資料送完須把 latchPin 設為 HIGH
    delay(40); // 設定延遲時間,取自可變電阻分壓後的電壓值
    val <<= 1; // 將 val 的位元左移一次
  }
}

使用兩顆 74HC595 和三支腳位控制 16 顆 LED

ESP8266 NodeMcu 與 兩顆 74HC595 和三支腳位控制 16 顆 LED

源自於
http://coopermaa2nd.blogspot.tw/2010/12/arduino-lab12-74hc595-16-led.html


// Lab12 使用2顆 74HC595 和三支腳位控制 16 顆 LED
//static const uint8_t D0   = 16;
//static const uint8_t D1   = 5;
//static const uint8_t D2   = 4;
//static const uint8_t D3   = 0;
//static const uint8_t D4   = 2;
//static const uint8_t D5   = 14;
//static const uint8_t D6   = 12;
//static const uint8_t D7   = 13;
//static const uint8_t D8   = 15;
//static const uint8_t D9   = 3;
//static const uint8_t D10  = 1;
// 接 74HC595 的 ST_CP (pin 12,latch pin)
int latchPin = 5;   
// 接 74HC595 的 SH_CP (pin 11, clock pin)
int clockPin = 4;
// 接 74HC595 的 DS (pin 14)
int dataPin = 16;

void setup() {
  // 將 latchPin, clockPin, dataPin 設置為輸出
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {     
  for (int led = 0; led < 16; led++) {
    int numberToDisplay  = 1 << led;
    byte high_Byte = ~ (highByte(numberToDisplay));     //經過反相處理
    byte low_Byte = ~ (lowByte(numberToDisplay));         //經過反相處理
   
    // 送資料前要先把 latchPin 拉成低電位
    digitalWrite(latchPin, LOW);
   
    // 先送高位元組 (Hight Byte), 給離 Arduino 較遠的那顆 74HC595
    shiftOut(dataPin, clockPin, MSBFIRST, high_Byte); 
    // 再送低位元組 (Low Byte), 給離 Arduino 較近的那顆 74HC595
    shiftOut(dataPin, clockPin, MSBFIRST, low_Byte); 

    // 送完資料後要把 latchPin 拉回成高電位
    digitalWrite(latchPin, HIGH);
 
    delay(40);
  }
}


74HC595 是一顆八位元的移位暫存器,同時可以控制八個輸出,我們可以把多顆移位暫存器串接 (Daisy chain) 在一起以擴充腳位,例如: 如果串接兩顆 74HC595 移位暫存器,便可以同時控制 16 個輸出。
74HC595 總共有 16 支接腳,底下這 16 支接腳的圖表說明:
image_thumb4 
腳位編號名稱說明
1-7, 15Q0 ~ Q7輸出腳位
8GND接地
7Q7’序列輸出 (Serial Out)
10MRMaster Reset, 清除所有資料, 低電位有效 (Active low)
11SH_CPSHift register clock pin (Clock Pin)
12ST_CPSTorage register clock pin (Latch Pin)
13OEOutput Enable, 允許輸出,低電位有效 (Active low)
14DS序列資料輸入 (Serial data input)
16Vcc供應電壓
要把兩顆 74HC595 串接起來,方法很簡單,只要把第一顆 74HC595 的 SH_CP (Clock Pin) 和 ST_CP (Latch Pin) 兩支腳位接到第二顆 74HC595 上,接著把第一顆 74HC595 的 Q7’ (序列輸出腳) 接到第二顆的 DS (序列資料輸入) 就可以了。



2018年2月10日 星期六

IOT for ESP32



http://www.iotsharing.com/2017/07/how-to-use-ble-in-arduino-esp32.html
http://www.iotsharing.com/

Introduction to ESP32

Demo 1: Blinky - a Hello World on Arduino ESP32

Demo 2: How to use multiple Serial port on Arduino ESP32

Demo 3: How to use Arduino ESP32 to read temperature/humidity from DHT11/DHT22

Demo 4: How to use Arduino ESP32 to display information on I2C LCD

Demo 5: How to use Arduino ESP32 to display information on SPI LED matrix
  
Demo 6: How to use Arduino ESP32 to display information on OLED

Demo 7: How to use Arduino ESP32 to store data to sdcard

Demo 8: How to use TCP/IP with Arduino ESP32

Demo 9: How to use mDNS to resolve host names to Arduino ESP32 IP addresses

Demo 10: How to turn the Arduino ESP32 into an Access Point

Demo 11: How to use SmartConfig on Arduino ESP32

Demo 12: How to turn the Arduino ESP32 into a Web Server

Demo 13: How to display temperature/humidity using Google Chart/Jquery and control LED through Arduino ESP32 Web Server

Demo 14: How to use MQTT and Arduino ESP32 to build a simple Smart home system

Demo 15: How to build a system to update Price Tag automatically using Arduino ESP32

Demo 16: How to update firmware OTA for a batch of Arduino ESP32

Why Finite State Machine (FSM) is important to Arduino ESP32

Demo 17: Arduino ESP32/ESP8266 WebGPIO - control GPIO from web
Arduino ESP32 FreeRTOS 1: How to create a task

Demo 19: How to use UDP/IP with Arduino ESP32

Demo 20: How to control a Servo via Arduino ESP32 Web Server

Demo 21: How to use interrupt in Arduino ESP32

How to get the IP address of a node by its mdns host name in Arduino ESP32

Demo 22: How to use Timer interrupt in Arduino ESP32

Demo 23: How to use Preferences to backup Arduino ESP32 data in main flash memory when power is off

Demo 24: How to bring ESP32 to low power-sleep mode to extend battery life

Demo 25: How to configure ESP32 Dual core - Multicore in Arduino ESP32

Demo 26: How to use Arduino ESP32 I2S to play wav music file from sdcard

Demo 27: How to use Arduino ESP32 BLE (Bluetooth Low Energy) as a GATT server

Demo 28: How to use Arduino ESP32 BLE (Bluetooth Low Energy) as a GATT client

Demo 29: How to use HTTPS in Arduino ESP32

Demo 30: How to use ESP32 MQTTS with MQTTS Mosquitto broker (TLS/SSL)

Demo 31: How to use Arduino ESP32 CAN interface

Demo 32: Simple Machine Learning - Artificial neural network demo using Arduino ESP32
 

2018年2月8日 星期四

ESP8266 SNMP + MIB Browser

ESP8266 SNMP  +  MIB Browser


#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Arduino_SNMP.h>

const char* ssid = "PTS-2F";
const char* password = "";

WiFiUDP udp;
SNMPAgent snmp = SNMPAgent("public");  // Starts an SMMPAgent instance with the community string 'public'

int changingNumber = 1;
int settableNumber1 = 0;
int settableNumber2 = 0;

void setup(){
    Serial.begin(115200);
    WiFi.begin(ssid, password);
    Serial.println("");

    // Wait for connection
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    
    // give snmp a pointer to the UDP object
    snmp.setUDP(&udp);
    snmp.begin();
    
    // add 'callback' for an OID - pointer to an integer
    snmp.addIntegerHandler(".1.3.6.1.4.1.5.0", &changingNumber);
    
    // Using your favourite snmp tool:
    // snmpget -v 1 -c public <IP> 1.3.6.1.4.1.5.0 
    
    // you can accept SET commands with a pointer to an integer (or string)
    snmp.addIntegerHandler(".1.3.6.1.4.1.2566.10.2.2.1.20.0.2", &settableNumber1, true);
    snmp.addIntegerHandler(".1.3.6.1.4.1.2566.10.2.2.1.20.0.9", &settableNumber2, true);
    //.1.3.6.1.4.1.2566.10.2.2.1.20.0.2
    //.1.3.6.1.4.1.2566.10.2.2.1.20.0.9
    // snmpset -v 1 -c public <IP> 1.3.6.1.4.1.5.0 i 99
    
}

void loop(){
    snmp.loop(); // must be called as often as possible
    if(snmp.setOccurred){
        if (settableNumber1!=0) {
           Serial.printf("Number has been set to value: %i", settableNumber1);
           settableNumber1=0 ; 
           }
        if (settableNumber2!=0) {
           Serial.printf("Number has been set to value: %i", settableNumber2); 
           settableNumber2=0 ; 
           }
        snmp.resetSetOccurred();
    }
    changingNumber++;
}







MIB Browser

iReasoning MIB browser is an indispensable tool for engineers to manage SNMP enabled network devices and applications. It allows users to load MIBs, issue SNMP requests, and receive traps. 
More...

SNMP Agent Simulator

iReasoning SNMP Agent Simulator is a Java based application that can simulate SNMPv1/v2c/v3 agents. Since it is written in pure Java, it can run on all the platforms that have Java Virtual Machine installed, including Unix, Linux, Windows, etc. It allows you to develop, test and train SNMP management applications without purchasing and maintaining expensive hardware devices. 
More...

SNMP 系列文章



SNMP 系列文章

Arduino project


Arduino


  清除項目編譯快取 執行 flutter clean 重新取得套件依賴 執行 flutter pub get 完整重新編譯並執行 flutter run 1. 新增專案套件 (pubspec.yaml) 在 Flutter 專案根目錄下的 pubspec.yaml 中,新增 ...