QuasarARM การกลับมาแก้ตัวอีกครั้ง #5
- ไฟ VCC 3.3V ของ UART บัดกรีขั้วมาลงบอร์ด
- GND จาก UART กับ GND ของ board
- เชื่อม TX ฝั่ง UART เข้ากับ RX ของ board
- RX ฝั่ง UART เข้ากับ TX ของ board
void GetExternalIP()
{
WiFiClient client;
if (!client.connect(“api.ipify.org”, 80)) {
Serial.println(“Failed to connect with ‘api.ipify.org’ !”);
}
else {
int timeout = millis() + 5000;
client.print(“GET /?format=json HTTP/1.1\r\nHost: api.ipify.org\r\n\r\n”);
while (client.available() == 0) {
if (timeout – millis() < 0) {
Serial.println(“>>> Client Timeout !”);
client.stop();
return;
}
}
int size;
while ((size = client.available()) > 0) {
uint8_t* msg = (uint8_t*)malloc(size);
size = client.read(msg, size);
Serial.write(msg, size);
free(msg);
}
}
}
credit:martinayotte
source:https://www.esp8266.com/viewtopic.php?f=6&t=12782
ใส่ code นี้ตอนกำหนด parameter
IPAddress staticIP(192, 168, xx, xx); //ESP static IP address
IPAddress gateway(192, 168, xx, 1); //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0); //Subnet mask
IPAddress primaryDNS(8, 8, 8, 8); //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optionalDNS
แทรก code ถัดจาก void setup()
// Configures static IP address
if (!WiFi.config(staticIP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println(“STA Failed to configure”);
}
// create 04-dec-2019
#include<WiFi.h>
#include “ThingSpeak.h”
//—————- Fill in your credentails thingspeak 2019 ———————
char ssid[] = “XXXX”; // your network SSID (name)
char pass[] = “xxxxxx”; // your network password
unsigned long myChannelNumber = xxxxxx; // Replace the 0 with your channel number
const char * myWriteAPIKey = “xxxxxxxxxxxx”; // Paste your ThingSpeak Write API Key between the quotes
String myStatus = “”;
//——————————————————————
WiFiClient client;
int number = 0;
//Static IP address configuration
IPAddress staticIP(192, 168, xx,xx); //ESP static IP address
IPAddress gateway(192, 168, xx, 1); //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0); //Subnet mask
IPAddress primaryDNS(8, 8, 8, 8); //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optionalDNS
//wind speed parameter
const int m_time = 5; //Measure time in Seconds
int wind_ct = 0;
float windkm = 0.0;
float windms = 0.0;
unsigned long ttime = 0;
void setup()
{
Serial.begin(115200);
// Configures static IP address
if (!WiFi.config(staticIP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println(“STA Failed to configure”);
}
// connect wifi
Connect_to_Wifi();
// time parameter
ttime = millis();
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop()
{
meassure();
Serial.print(“ความเร็วลม: “);
Serial.print(windkm); //Speed in Km/h
Serial.print(” km/h – “);
Serial.print(windms); //Speed in m/s
Serial.println(” m/s”);
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
// set the fields with the values
ThingSpeak.setField(1, windkm);
ThingSpeak.setField(2, windms);
// set the status
ThingSpeak.setStatus(myStatus);
//int x = ThingSpeak.writeField(myChannelNumber, 1, windkm, myWriteAPIKey);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
// Check the return code
if(x == 200) {
Serial.println(“ThingSpeak Channel update successful.”);
}
else{
Serial.println(“Problem updating channel. HTTP error code ” + String(x));
}
number++;
if(number > 99){
number = 0;
}
delay(20000); // Wait 20 seconds before sending a new value
delay(20000);
}
void countWind() {
wind_ct ++;
}
void meassure() {
wind_ct = 0;
ttime = millis();
attachInterrupt(16, countWind, RISING);
delay(1000 * m_time);
detachInterrupt(1);
windkm = (float)wind_ct / (float)m_time * 2.4;
windms = windkm/3.6;
}
void Connect_to_Wifi()
{
// We start by connecting to a WiFi network
Serial.println(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, pass);
//Serial.println();
Serial.println();
Serial.print(“Wait for WiFi… “);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(“.”);
delay(500);
}
Serial.println(“”);
Serial.println(“WiFi connected”);
Serial.print(“IP address: “);
Serial.println(WiFi.localIP());
}
source:https://github.com/mathworks/thingspeak-arduino
ใช้ Interrupt ที่ขา GPIO
attachInterrupt(GPIOPin, ISR, Mode);
โดยมีรูปแบบ การตรวจจับการ Interrupt 5 Mode
LOW : เกิดเมื่อสัญญาณขา Interrupt เป็น Low
HIGH : เกิดเมื่อสัญญาณขา Interrupt เป็น High
CHANGE : เกิดเมื่อสัญญาณขา Interrupt มีการเปลี่ยนแปลงค่าแรงดัน เช่น เปลี่ยนจาก 0 เป็น 3.3V หรือจาก 3.3 เป็น 0V
FALLING : เกิดเมื่อสัญญาณขา Interrupt เปลี่ยนจาก High เป็น Lowfrom HIGH to LOW.
RISING : เกิดเมื่อสัญญาณขา Interrupt เปลี่ยนจาก Low เป็น High
จากวงจรต่อ กับ ขา GPIO16
//bosblog.cz
const int m_time = 5; //Meassure time in Seconds
int wind_ct = 0;
float wind = 0.0;
unsigned long ttime = 0;
void setup()
{
Serial.begin(9600);
ttime = millis();
}
void loop()
{
meassure();
Serial.print(“ความเร็วลม: “);
Serial.print(wind); //Speed in Km/h
Serial.print(” km/h – “);
Serial.print(wind / 3.6); //Speed in m/s
Serial.println(” m/s”);
}
void countWind() {
wind_ct ++;
}
void meassure() {
wind_ct = 0;
ttime = millis();
attachInterrupt(16, countWind, RISING);
delay(1000 * m_time);
detachInterrupt(1);
wind = (float)wind_ct / (float)m_time * 2.4;
}
credit:http://www.bosblog.cz/93-2/
fritzing 0.93b
https://fritzing.org/media/downloads/fritzing.0.9.3b.64.pc.zip
fritzing 0.94
https://fritzing.org/media/downloads/fritzing.0.9.4.64.pc.zip
Arduino IDE
File> Preferences
เลือก Additional Board Manager URLs
https://dl.espressif.com/dl/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json
Tools > Board > Boards Manager
Tools > Board > Boards Manager…
กดหา ESP32 เลือก
ESP32 by Espressif Systems
กด ติดตั้ง
ที่มา:https://randomnerdtutorials.com/installing-the-esp32-board-in-arduino-ide-windows-instructions/