get WAN IP arduino

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

 

Arduino Fix IP wifi

ใส่ 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”);
}

 

ESP32 wind Speed (Anemometer) thingspeak+fix IP wifi

// 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

ที่วัดความเร็วลม Wind Speed (Anemometer) กับ ESP32

ใช้ 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.94

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

 

esp32 esp8266 Arduino IDE

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/

 

 

Arduino Nano +DHT22

#include “DHT.h”
#define DHTPIN 2 // what pin we’re connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE, 6);

void setup() {
Serial.begin(9600);
Serial.println(“DHTxx test!”);

dht.begin();
}

void loop() {
// Wait a few seconds between measurements.
delay(2000);

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Read temperature as Fahrenheit
float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(“Failed to read from DHT sensor!”);
return;
}

// Compute heat index
// Must send in temp in Fahrenheit!
float hi = dht.computeHeatIndex(f, h);

Serial.print(“Humidity: “);
Serial.print(h);
Serial.print(” %\t”);
Serial.print(“Temperature: “);
Serial.print(t);
Serial.print(” *C “);
Serial.print(f);
Serial.print(” *F\t”);
Serial.print(“Heat index: “);
Serial.print(hi);
Serial.println(” *F”);

}

output

DHTxx test!
Humidity: 93.10 % Temperature: 29.90 *C 85.82 *F Heat index: 106.62 *F
Humidity: 93.20 % Temperature: 29.90 *C 85.82 *F Heat index: 106.68 *F
Humidity: 93.30 % Temperature: 29.90 *C 85.82 *F Heat index: 106.74 *F
Humidity: 93.30 % Temperature: 29.90 *C 85.82 *F Heat index: 106.74 *F
Humidity: 93.20 % Temperature: 29.90 *C 85.82 *F Heat index: 106.68 *F

remark ตัว DHT22 ที่ทดสอบ ไม่ค่อยดีนัก

credit:http://arduino-er.blogspot.com/2015/05/arduino-nano-dht11-temperature-humidity.html

nodmcu V3 (Lolin) DHT11 with DeepSleep

การหลับ Deepsleep จะเป็นหน่วย microsecond

#include <ESP8266WiFi.h>
#include “DHT.h”
//deep sleep  wire GPIO16 (D0) to RST Pin
#define DHTPIN D4 // ขา Out ของ Sensor ต่อเข้าขา D4 ของ Esp8266
#define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
dht.begin();
Serial.begin(9600);
Serial.println(“Humidity and temperature\n\n”);
delay(700);
}

void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print(“Current humidity = “);
Serial.print(h);
Serial.print(“% “);
Serial.print(“temperature = “);
Serial.print(t);
Serial.println(“C “);
//ESP.deepSleep(30e6); // Deep sleep mode for 30 seconds
//ESP.deepSleep(60e6); // Deep sleep mode for 60 seconds
ESP.deepSleep(300e6); // Deep sleep mode for (~5 min) 300 seconds
delay(800);
}

credit:https://roboindia.com/tutorials/DHT11-NodeMCU-arduino

 

nodmcu V3 (Lolin) DHT11 #1

nodmcu-dht11-d4

#include “DHT.h”

#define DHTPIN D4 // ขา Out ของ Sensor ต่อเข้าขา D4 ของ Esp8266
#define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
dht.begin();
Serial.begin(9600);
Serial.println(“Humidity and temperature\n\n”);
delay(700);
}

void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print(“Current humidity = “);
Serial.print(h);
Serial.print(“% “);
Serial.print(“temperature = “);
Serial.print(t);
Serial.println(“C “);
delay(800);
}

nodmcu-dht11-d4-output

credit:https://roboindia.com/tutorials/DHT11-NodeMCU-arduino

 

nodemcu 0.9 am2305 ds18b20 thingspeak

// www.arduinesp.com
//
// Plot DTH11 data on thingspeak.com using an ESP8266
// April 11 2015
// Author: Jeroen Beemster
// Website: www.arduinesp.com
// ใช้กับ กล่อง วัดอุณหภูมิที่บ้าน
// 20-3-2017 ใช้งานครั้งแรก DHT22
// 19-5-2017 เพิ่ม DS18B20
// 18-12-2017 ซ่อมครั้งที่ 1 เปลี่ยน DHT22 เป็น AM2305
// 26-2-2018 เปลี่ยน accesspoint ใหม่ ตั้งรหัสใหม่ เปลี่ยน thingspeak acc ใหม่
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include “ThingSpeak.h”

// replace with your channel’s thingspeak API key,
unsigned long myChannelNumber = xxxxxx;
const char * myWriteAPIKey = “xxxxxxxxxxxxxx”;

const char* ssid = “SMART_xxx”;
const char* password = “xxxxxxxxxxx”;

#define DHTPIN1 D1 // d0 dht22 ext
// #define DHTPIN2 D2 // d1 dht22 mushroom
DHT dht1(DHTPIN1,DHT22,15);
// DHT dht2(DHTPIN2,DHT22,15);
#define sensorPin D2 // Data wire is plugged into pin D3 on NodeMCU
OneWire oneWire(sensorPin); // Setup the oneWire Sensor
DallasTemperature DS18B20(&oneWire); // Pass reference to Dallas Temperature.
WiFiClient client;

ADC_MODE(ADC_VCC);
// int vcc;
void setup() {
Serial.begin(115200);
delay(10);
dht1.begin();
//dht2.begin();
WiFi.begin(ssid, password);

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”);
Serial.println(“IP address: “);
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
}

void loop() {

float h1 = dht1.readHumidity();
float t1 = dht1.readTemperature();
float vcc = 0.00f;
vcc = ESP.getVcc()/1024.00f;
// uint32_t vcc = ESP.getVcc()/1000.0;
DS18B20.requestTemperatures();
float t2 = DS18B20.getTempCByIndex(0);
//if (isnan(h1) || isnan(t1) ||isnan(h2) || isnan(t2)) {
if (isnan(h1) || isnan(t1) ) {
Serial.println(“Failed to read from EXT DHT sensor 1 !”);
//return;
t1=0;
h1=0;
}
else if (isnan(t2) ) {
Serial.println(“Failed to read from DS18B20 mushroom sensor 2 !”);
//return;
t2=0;
//h2=0;
}
Serial.print(“Temperature 1: “);
Serial.print(t1);
Serial.print(” C Humidity 1: “);
Serial.print(h1);
Serial.print(” % Voltage : “);
Serial.print(vcc);
Serial.print(” V MushRoom Temperature 2: “);
Serial.print(t2);
Serial.println(” C”);
//Serial.print(” C MushRoom Humidity 2: “);
//Serial.print(h2);
//Serial.println(“% send to Thingspeak”);

ThingSpeak.setField(1,t1);
ThingSpeak.setField(2,h1);
ThingSpeak.setField(3,vcc);
ThingSpeak.setField(4,t2);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
Serial.println(“Waiting”);
delay(20000);
// ThingSpeak will only accept updates every 15 seconds.
// delay 1 minitue delay 1000=1sec

client.stop();

Serial.println(“Wifi Disconnect”);
WiFi.disconnect();
//delay(60000);
delay(100);
//โดย 1,000,000 microseconds = 1 second)
Serial.println(“”);
Serial.println(“Deep Sleep 5 min”);
// deep sleep 1 min
//ESP.deepSleep(60 * 1000000, WAKE_RF_DEFAULT);
//deep sleep 5 min
ESP.deepSleep(360 * 1000000, WAKE_RF_DEFAULT);
//ESP.deepSleep(5*60 * 1000000);
//node.dsleep(60 * 1000000);
delay(500); // wait for deep sleep to happen
}