README adn project files

0 parents
Showing with 4860 additions and 0 deletions
soft/ESP_soft soft/programmer
soft/
Для поддержки ESP8266 в ARDUINO IDE необходимо включить поддержку этого модуля согласно инструкции.
Например: https://arduinoplus.ru/programmnoe-obespechenie-esp8266-na-arduino-ide/#__ESP8266__Arduino
или http://wiki.amperka.ru/arduino-ide:boards-manager - пользовался этой инструкцией
!!! В Arduino IDE в разделе НАСТРОЙКИ (в строке ДОПОЛНИТЕЛЬНЫЕ ССЫЛКИ ДЛЯ МЕНЕДЖЕРА ПЛАТ:) необходимо указать правильный путь:
http://arduino.esp8266.com/stable/package_esp8266com_index.json - это рабочий путь
\ No newline at end of file
2914 0 D7 0 1153 000430 5 1ms TYPE: PCR800 RAIN RATE: 17 litr/m TOTAL RAIN : 3 mm BAT: F ID: D7
2914 0 D7 1 3393 977430 5 0ms TYPE: PCR800 RAIN RATE: 138 inch/h TOTAL RAIN : 54 inch BAT: F ID: D7
2914 0 D7 1 3393 977430 5 1ms TYPE: PCR800 RAIN RATE: 147 inch/h TOTAL RAIN : 57 inch BAT: F ID: D7
2914 0 D7 0 3200 268430 0 1ms TYPE: PCR800 RAIN RATE: 0 inch/h TOTAL RAIN : 0 inch BAT: F ID:
2914 0 D7 0 1100 853840 2 1ms TYPE: PCR800 RAIN RATE: 1 inch/h TOTAL RAIN : 12 inch BAT: F
2914 0 D7 1 3393 416840 E 1ms TYPE: PCR800 RAIN RATE: 93 inch/h TOTAL RAIN : 13 inch BAT: F
2914 0 D7 1 3393 416840 E 1ms TYPE: PCR800 RAIN RATE: 14 inch/h TOTAL RAIN : 13 inch BAT: F ID: D7
2914 0 D7 1 3393 416840 E 1ms TYPE: PCR800 RAIN RATE: 5 inch/h TOTAL RAIN : 13 inch BAT: F ID: D7
2914 0 D7 1 3393 416840 E 1ms TYPE: PCR800, RAIN RATE: 110011 inch/h, TOTAL RAIN: 10000011 inch, BAT: F ID: D7
2914 0 D7 0 9100 756840 C 1ms TYPE: PCR800, RAIN RATE: 11001 inch/h, TOTAL RAIN: 1000111 inch, BAT: F ID: D7
2914 0 D7 1 3393 550250 8 0ms TYPE: PCR800, RAIN RATE: 110011 inch/h, TOTAL RAIN: 1010101 inch, BAT: F ID: D7
2914 0 D7 1 3393 787250 4 1ms TYPE: PCR800, RAIN RATE: 48 | 48 inch/h, TOTAL RAIN: 82 | 82 inch, BAT:
2914 0 D7 1 3393 453350 B 1ms TYPE: PCR800, RAIN RATE: 147 | 147 inch/h, TOTAL RAIN: 84 | 84 inch, BAT: F ID:
2914 0 BB 0 6543 869000 F 0ms TYPE: PCR800, RAIN RATE: 7 | 7 inch/h, TOTAL RAIN: 8 | 8 inch, BAT: F ID: BB
2914 0 BB 1 3393 022230 2 0ms TYPE: PCR800, RAIN RATE: 1.20 | 1.2 inch/h, TOTAL RAIN: 0.30
2914 0 BB 0 4270 069130 6 0ms TYPE: PCR800, RAIN RATE: 0.90 | 0.9 inch/h, TOTAL RAIN: 0.30
2914 0 BB 0 5611 924230 7 1ms TYPE: PCR800, RAIN RATE: 0.70 | 0.7 inch/h, TOTAL RAIN: 0.30
2914 0 BB 1 3393 186430 F 0ms TYPE: PCR800, RAIN RATE: 1.20 | 1.2 inch/h, TOTAL RAIN: 0.30
2914 0 BB 1 3393 955630 5 0ms TYPE: PCR800, RAIN RATE: 1.20 | 1.2 inch/h, TOTAL RAIN: 0.30
2914 0 BB 1 3393 643830 1 1ms TYPE: PCR800, RAIN RATE: 1.20 | 1.2 inch/h, TOTAL RAIN: 0.30
2914 0 BB 1 3393 628830 4 1ms TYPE: PCR800, RAIN RATE: 1.20 | 1.2 inch/h, TOTAL RAIN: 0.30
2914 0 BB 1 3393 628830 4 1ms TYPE: PCR800, RAIN RATE: 1.20 | 1.2 inch/h, TOTAL RAIN: 0.30
2914 0 BB 1 3393 691340 0 0ms TYPE: PCR800, RAIN RATE: 1.20 | 1.2 inch/h, TOTAL RAIN: 0.40
2914 0 BB 1 3393 033340 6 1ms TYPE: PCR800, RAIN RATE: 1.20 | 1.2 inch/h, TOTAL RAIN: 0.40
2914 0 BB 0 2320 073340 E 0ms TYPE: PCR800, RAIN RATE: 0.20 | 0.2 inch/h, TOTAL RAIN: 0.40
2914 0 BB 0 7510 314340 2 0ms TYPE: PCR800, RAIN RATE: 0.10 | 0.1 inch/h, TOTAL RAIN: 0.40
2914 0 BB 0 7510 314340 2 0ms TYPE: PCR800, RAIN RATE: 0.10 | 0.1 inch/h, TOTAL RAIN: 0.40
2914 0 BB 0 7510 314340 2 0ms TYPE: PCR800, RAIN RATE: 0.10 | 0.1 inch/h, TOTAL RAIN: 0.40
2914 0 BB 0 7200 254340 1 1ms TYPE: PCR800, RAIN RATE: 0.00 | 0.0 inch/h, TOTAL RAIN: 0.40
2914 0 BB 1 3393 345440 D 1ms TYPE: PCR800, RAIN RATE: 1.20 | 1.2 inch/h, TOTAL RAIN: 0.40
2914 0 BB 1 3393 308370 E 1ms TYPE: PCR800, RAIN RATE: 1.20 | 1.2 inch/h, TOTAL RAIN: 0.70
2914 0 BB 1 3393 663280 2 0ms TYPE: PCR800, RAIN RATE: 1.20 | 1.2 inch/h, TOTAL RAIN: 0.80
2914 0 BB 1 3393 291890 6 1ms TYPE: PCR800, RAIN RATE: 1.20 | 1.2 inch/h, TOTAL RAIN: 0.90
2914 0 BB 1 3393 910990 5 1ms TYPE: PCR800, RAIN RATE: 1.20 | 1.2 inch/h, TOTAL RAIN: 0.90
2914 0 BB 1 3393 679001 0 0ms TYPE: PCR800, RAIN RATE: 1.20 | 1.2 inch/h, TOTAL RAIN: 0.00
2914 0 BB 1 3393 910101 5 0ms TYPE: PCR800, RAIN RATE: 1.20 | 1.2 inch/h, TOTAL RAIN: 1.60
2914 0 BB 0 5720 659531 1 1ms TYPE: PCR800, RAIN RATE: 0.20 | 0.2 inch/h, TOTAL RAIN: 1.36 | 1.3595597744
2914 0 BB 0 1500 699531 D 0ms TYPE: PCR800, RAIN RATE: 0.00 | 0.0 inch/h, TOTAL RAIN: 1.36 | 1.3599598408
2914 0 BB 0 1500 699531 D 1ms TYPE: PCR800, RAIN RATE: 0.05 | 0.1 inch/h, TOTAL RAIN: 1.36 | 1.3599598408
rate total
8-11 12--17
rain_rate 11 .. 8
total_rain 12 .. 17
B9A4 2 21 C 820B 050000 0
2914 0 BB 0 0000 221000 D
F824 1 0B 1 8020 37ED47 6
ID: D7
1984 0 72 0 D0C00000083 101ms TYPE: WGR800 AVG WS: 0.0m/s MAX WS: 0.0m/s WDIR: 13 BAT: F ID: 72
1984 0 72 0 30C 000 000 E2 0ms TYPE: WGR800 AVG WS: 0.0m/s MAX WS: 0.0m/s WDIR: 3 BAT: F
1984 0 72 0 E0C 000 000 93 1ms TYPE: WGR800 AVG WS: 0.0m/s MAX WS: 0.0m/s WDIR: 14
1984 0 72 0 F0C 110 000 C3 0ms TYPE: WGR800 AVG WS: 0.0m/s MAX WS: 1.7m/s WDIR: 15 BAT: F ID: 72
1984 0 72 0 30C 600 000 43 0ms TYPE: WGR800 AVG WS: 0.0m/s MAX WS: 0.6m/s WDIR: 3 BAT: F ID: 72
1984 0 72 0 10C 530 000 43 1ms TYPE: WGR800 AVG WS: 0.0m/s MAX WS: 5.3m/s WDIR: 1 BAT: F ID: 72
1984 0 72 0 30C 410 400 73 0ms TYPE: WGR800 AVG WS: 0.4m/s MAX WS: 2.0m/s WDIR: 3 BAT: F ID: 72
1984 0 72 0 50C 530 510 E3 1ms TYPE: WGR800 AVG WS: 2.1m/s MAX WS: 5.3m/s WDIR: 5 BAT: F ID: 72
1984 0 72 0 50C 910 020 C3 0ms TYPE: WGR800 AVG WS: 3.2m/s MAX WS: 2.5m/s WDIR: 5 BAT: F ID: 72
1984 0 72 0 70C 910 220 04 0ms TYPE: WGR800 AVG WS: 3.4m/s MAX WS: 2.5m/s WDIR: 7 BAT: F ID: 72
1984 0 72 0 70C 710 810 34 0ms TYPE: WGR800 AVG WS: 2.4m/s MAX WS: 2.3m/s WDIR: 7 BAT: F ID: 72
1984 0 72 0 70C 720 920 64 0ms TYPE: WGR800 AVG WS: 4.1m/s MAX WS: 3.9m/s WDIR: 7 BAT: F ID: 72
1984 0 72 0 90C 910 230 34 0ms TYPE: WGR800 AVG WS: 5.0m/s MAX WS: 2.5m/s WDIR: 9 BAT: F ID: 72
1984 0 72 0 90C 360 520 44 0ms TYPE: WGR800 AVG WS: 3.7m/s MAX WS: 9.9m/s WDIR: 9 BAT: F ID: 72
dir max avg cs
0123 4 56 7 8-- 11- 14-
########################################################################################################################################################################
SCOPE1 07 07 05 05 05 05 05 05 05 05 05 05 00 75 00 85 05 00 50 50 85 00 50 85 00 40 50 85 00 50 85 00 50 50 50 50 85 05 05 00 50 85 00 50 50 50 50 50 85 05 00 85 00 50 50 50 50 40 85 04 00 50 50 50 40 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 85 05 00 85 05 00 50 50 85 00 85 05 05 05 05 00
16:10:22.557 -> BEFORE OI OI OI OI OI OI OI OI OI OI OI OI OO II OO II OI OO IO IO II OO IO II OO OO IO II OO IO II OO IO IO IO IO II OI OI OO IO II OO IO IO IO IO IO II OI OO II OO IO IO IO IO OO II OO OO IO IO IO OO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO II OI OO II OI OO IO IO II OO II OI OI OI OI OO
16:10:22.557 -> AFTER OI OI OI OI OI OI OI OI OI OI OI OI OO II OO II OI OO IO IO II OO IO II OO IO IO II OO IO II OO IO IO IO IO II OI OI OO IO II OO IO IO IO IO IO II OI OO II OO IO IO IO IO IO II OO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO II OI OO II OI OO IO IO II OO II OI OI OI OI OO
16:10:22.604 -> RESULT I I I I I I I I I I I I I O I O I I O O O I O O I O O O I O O I O O O O O I I I O O I O O O O O O I I O I O O O O O O I O O O O O O O O O O O O O O O O O O O O O O O O O O I I O I I O O O I O I I I I I O OSV:3 SYN:13 TIME:5494
16:10:22.604 -> 5494.9s 3 19840720B0400000063 100ms
SCOPE1 07 07 05 05 05 05 05 05 00 85 00 85 05 00 50 50 85 00 50 85 00 50 50 75 00 50 85 00 50 50 50 50 85 05 05 00 40 85 00 50 50 50 50 50 85 00 51 85 00 50 50 50 50 50 85 05 00 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 85 00 85 05 00 50 40 85 05 05 05 05 00 50 60 00
16:12:28.532 -> BEFORE OI OI OI OI OI OI OI OI OO II OO II OI OO IO IO II OO IO II OO IO IO II OO IO II OO IO IO IO IO II OI OI OO OO II OO IO IO IO IO IO II OO IO II OO IO IO IO IO IO II OI OO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO II OO II OI OO IO OO II OI OI OI OI OO IO IO OO
16:12:28.579 -> AFTER OI OI OI OI OI OI OI OI OO II OO II OI OO IO IO II OO IO II OO IO IO II OO IO II OO IO IO IO IO II OI OI OO IO II OO IO IO IO IO IO II OO IO II OO IO IO IO IO IO II OI OO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO IO II OO II OI OO IO IO II OI OI OI OI OO IO IO __
16:12:28.579 -> RESULT I I I I I I I I I O I O I I O O O I O O I O O O I O O I O O O O O I I I O O I O O O O O O I O O I O O O O O O I I O O O O O O O O O O O O O O O O O O O O O O O O O O I O I I O O O I I I I I O O O . OSV:3 SYN:9 TIME:5620
16:12:28.632 -> 5620.9s 3 1984072090C00000043 98ms TYPE: WGR800 AVG WS: 0.0m/s MAX WS: 0.0m/s WDIR: 9 BAT: F ID: 72
29140BB00000221000B
I I I I I I I I I I I I O I O I O I O O I O O I I O O O O O I O O O O O I I O I I I O I O O O O O O O O O O O O O O O O O O O O O I O O O I O O I O O O O O O O O O O O O O O O I I O I O I O O I O O I O O I I O
\ No newline at end of file
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
The MIT License (MIT)
Copyright (c) 2015 ekstrand
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# SerialESP8266wifi
A simple ESP8266 Arduino library with built in re-connect functionality.
* The ESP8266 is a dirtcheap wifimodule. I got mine for about 2.50 US including shipping at Aliexpress. Read about it here: https://nurdspace.nl/ESP8266
* An AT command reference can be found here: https://github.com/espressif/esp8266_at/wiki/AT_Description
* Contact me if you have ideas for changes or new features, found a bug or just want to buy a beer at jonas[AT]inspirativ[DOT]se
## Memory footprint and more
* Tested on an Arduino Nano v3 ATMega 328, Arduino IDE 1.60, ESP8266 module with firmware version 0.9.2.4
* approx 3.5kB of program storage
* approx 285 bytes or RAM
## Install
* Download the library as a zip from https://github.com/ekstrand/SerialESP8266wifi/archive/master.zip
* Unzip and place in ARDUINO_HOME/libraries/ directory as SerialESP8266wifi
* Restart the Arduino IDE
* In your sketch do a `#include <SerialESP8266wifi.h>`
* To set up a simple server for testing, I like to use SocketTest http://sourceforge.net/projects/sockettest/
## Constructor
**SerialESP8266wifi(Stream serialIn, Stream serialOut, byte resetPin)**
* **serialIn** this object is used to read from the ESP8266, you can use either hardware or software serial
* **serialOut** this object is used to write to the ESP8266, you can use either hardware or software serial
* **resetPin** this pin will be pulled low then high to reset the ESP8266. It is assumed that a the CH_PD pin is connected the this pin. See pin out and more at: http://www.electrodragon.com/w/ESP8266#Module_Pin_Description
* **Example:** ```SerialESP8266wifi wifi(swSerial, swSerial, 10);```
**SerialESP8266wifi(Stream serialIn, Stream serialOut, byte resetPin, Stream debugSerial)**
* **serialIn** this object is used to read from the ESP8266, you can use either hardware or software serial
* **serialOut** this object is used to write to the ESP8266, you can use either hardware or software serial
* **resetPin** this pin will be pulled low then high to reset the ESP8266. It is assumed that a the CH_PD pin is connected the this pin. See pin out and more at: http://www.electrodragon.com/w/
ESP8266#Module_Pin_Description
* **debugSerial** enables wifi debug and local echo to Serial (could be hw or sw)
* **Example:** ```SerialESP8266wifi wifi(swSerial, swSerial, 10, Serial);```
## Starting the module
**boolean begin()** calling this method will do a hw reset on the ESP8266 and set basic parameters
* **return** will return a true or false depending if the module was properly initiated
* **Example:** `boolean esp8266started = wifi.begin();`
## Connecting to an access point
**boolean connectToAP(char * ssid, char* password)** tells the ESP8266 to connect to an accesspoint
* **ssid** the ssid (station name) to be used. Note that this method uses char arrays as input. See http://arduino.cc/en/Reference/StringToCharArray for how to convert an arduino string object to a char array (max 15 chars)
* **password** the access point password wpa/wpa2 is assumed (max 15 chars)
* **return** will return a true if a valid IP was received within the time limit (15 seconds)
* **Example:** `boolean apConnected = wifi.connectToAP("myaccesspoint", "password123");`
**boolean isConnectedToAP()** checks if the module is connected with a valid IP
* **return** will return a true if the module has an valid IP address
* **Example:** `boolean apConnected = wifi.isConnectedToAP();`
## Connecting to a server
**boolean connectToServer(char* ip, char* port)** tells the ESP8266 to open a connection to a server
* **ip** the IP-address of the server to connect to
* **port** the port number to be used
* **return** true if connection is established within 5 seconds
* **Example:** `boolean serverConnected = wifi.connectToServer("192.168.5.123", "2121");`
**boolean isConnectedToServer()** checks if a server is connected
* **return** will return a true if we are connected to a server
* **Example:** `boolean serverConnected = wifi.isConnectedToServer();`
**setTransportToTCP() AND setTransportToUDP()** tells the ESP8266 which transport to use when connecting to a server. Default is TCP.
## Disconnecting from a server
**disconnectFromServer()** tells the ESP8266 to close the server connection
* **Example:** `wifi.disconnectFromServer();`
## Sending a message
**boolean send(char channel, char * message)** sends a message - alias for send(char channel, char * message, true)
* **channel** Set to **SERVER** if you want to send to server. If we are the server, the value can be between '1'-'3'
* **message** a character array, max 25 characters long.
* **return** true if the message was sent
* **Example:** `boolean sendOk = wifi.send(SERVER, "Hello World!");`
**boolean send(char channel, char * message, boolean sendNow)** sends or queues a message for later sending
* **channel** Set to **SERVER** if you want to send to server. If we are the server, the value can be between '1'-'3'
* **message** a character array, max 25 characters long.
* **sendNow** if false, the message is appended to a buffer, if true the message is sent right away
* **return** true if the message was sent
* **Example:**
```
wifi.send(SERVER, "You", false);
wifi.send(SERVER, " are ", false);
wifi.send(SERVER, "fantastic!", true); // ie wifi.send(SERVER, "fantastic!");
```
**endSendWithNewline(bool endSendWithNewline)** by default all messages are sent with newline and carrage return (println), you can disable this
* **endSendWithNewline** sent messages with print instead of println
* **Example:** `wifi.endSendWithNewline(false);`
## Checking Client Connections
**boolean checkConnections(&connections)** - Updates pre-initialised pointer to
WifiConnection \*connections.
* **return** true if client is connected
* Updated pointer is array of 3 connections:
* **boolean connected** true if connected.
* **char channel** channel number, can be passed to `send`.
* **Example:**
```
WifiConnection *connections;
wifi.checkConnections(&connections);
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (connections[i].connected) {
// See if there is a message
WifiMessage msg = wifi.getIncomingMessage();
// Check message is there
if (msg.hasData) {
processCommand(msg);
}
}
}
```
## Check Connection
**boolean isConnection(void)** - Returns true if client is connected,
otherwise false. Use as above without WifiConnection pointer if not
bothered about multi-client.
## Get Incoming Message From Connected Client
**WifiMessage getIncomingMessage(void)** - checks serial buffer for messages.
Return is WifiMessage type as below. See example Check Client Connection
example for usage.
## Receiving messages
**WifiMessage listenForIncomingMessage(int timeoutMillis)** will listen for new messages up to timeoutMillis milliseconds. Call this method as often as possible and with as large timeoutMillis as possible to be able to catch as many messages as possible..
* **timeoutMillis** the maximum number of milliseconds to look for a new incoming message
* **return** WifiMessage contains:
* **boolean hasData** true if a message was received
* **char channel** tells you if the message was received from the server (channel == SERVER) or another source
* **char * message** the message as a character array (up to the first 25 characters)
* **Example:**
```
void loop(){
WifiMessage in = wifi.listenForIncomingMessage(6000);
if (in.hasData) {
Serial.print("Incoming message:");
Serial.println(in.message);
if(in.channel == SERVER)
Serial.println("From server");
else{
Serial.print("From channel:");
Serial.println(in.channel);
}
}
// Do other stuff
}
```
## Local access point and local server
**boolean startLocalAPAndServer(char* ssid, char* password, char* channel, char* port)** will create an local access point and start a local server
* **ssid** the name for your access point, max 15 characters
* **password** the password for your access point, max 15 characters
* **channel** the channel for your access point
* **port** the port for your local server, TCP only
* **return** true if the local access point and server was configured and started
* **Example:** `boolean localAPAndServerStarted = wifi.startLocalAPAndServer("my_ap", "secret_pwd", "5", "2121");`
**boolean stopLocalAPAndServer()** disable the accesspoint (the server will not be stopped, since a restart is needed)
* **return** true if the local access point was stopped
* **Example:** `boolean localAPAndServerStopped = wifi.stopLocalAPAndServer();`
**boolean isLocalAPAndServerRunning()** check if local access point and server is running
* **return** true if the local access point and local server is running
* **Example:** `boolean localAPAndServerRunning = wifi.isLocalAPAndServerRunning();`
## Re-connect functionality
Everytime send(...) and listenForIncomingMessage(..) is called a watchdog checks that the configured access point, server and local access point and server is still running, if not they will be restarted or re-connected. The same thing happens if the ESP8266 should reset.
Note: It is really only the send method that can detect a lost connection to the server. To be sure you are connected, do a send once in a while..
## Avanced configuration
In SerialESP8266wifi.h you can change some stuff:
* **HW_RESET_RETRIES 3** - is the maximum number of times begin() will try to start the ESP8266 module
* **SERVER_CONNECT_RETRIES_BEFORE_HW_RESET 30** - is the nr of time the watchdog will try to establish connection to a server before a hardware reset of the ESP8266 is performed
* The maximum number of characters for incoming and outgoing messages can be changes by editing:
* char msgOut[26];
* char msgIn[26];
* If the limit for ssid and password length does not suite you, please change:
* char _ssid[16];
* char _password[16];
* char _localAPSSID[16];
* char _localAPPassword[16];
//
// SerialESP8266wifi.h
//
//
// Created by Jonas Ekstrand on 2015-02-20.
// ESP8266 AT cmd ref from https://github.com/espressif/esp8266_at/wiki/CIPSERVER
//
//
#ifndef SerialESP8266wifi_h
#define SerialESP8266wifi_h
#define HW_RESET_RETRIES 3
#define SERVER_CONNECT_RETRIES_BEFORE_HW_RESET 3
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <inttypes.h>
#if defined(SerialESP8266)
#include <pgmspace.h>
#else
#include <avr/pgmspace.h>
#endif
#include "HardwareSerial.h"
#define SERVER '4'
#define MAX_CONNECTIONS 3
#define MSG_BUFFER_MAX 128
struct WifiMessage{
public:
bool hasData:1;
char channel;
char * message;
};
struct WifiConnection{
public:
char channel;
bool connected:1;
};
struct Flags // 1 byte value (on a system where 8 bits is a byte
{
bool started:1,
echoOnOff:1,
debug:1,
serverConfigured:1, // true if a connection to a remote server is configured
connectedToServer:1, // true if a connection to a remote server is established
apConfigured:1, // true if the module is configured as a client station
localApConfigured:1,
localServerConfigured:1,
localApRunning:1,
localServerRunning:1,
endSendWithNewline:1,
connectToServerUsingTCP:1;
};
class SerialESP8266wifi
{
public:
/*
* Will pull resetPin low then high to reset esp8266, connect this pin to CHPD pin
*/
SerialESP8266wifi(Stream &serialIn, Stream &serialOut, byte resetPin);
/*
* Will pull resetPin low then high to reset esp8266, connect this pin to CHPD pin
*/
SerialESP8266wifi(Stream &serialIn, Stream &serialOut, byte resetPin, Stream &dbgSerial);
/*
* Will do hw reset and set inital configuration, will try this HW_RESET_RETRIES times.
*/
bool begin(); // reset and set echo and other stuff
bool isStarted();
/*
* Connect to AP using wpa encryption
* (reconnect logic is applied, if conn lost or not established, or esp8266 restarted)
*/
bool connectToAP(String& ssid, String& password);
bool connectToAP(const char* ssid, const char* password);
bool isConnectedToAP();
char* getIP();
char* getMAC();
/*
* Evaluate the connection and perform reconnects if needed. Eventually perform reset and restart.
*
*/
bool watchdog();
/*
* Connecting with TCP to server
* (reconnect logic is applied, if conn lost or not established, or esp8266 restarted)
*/
void setTransportToUDP();
//Default..
void setTransportToTCP();
bool connectToServer(String& ip, String& port);
bool connectToServer(const char* ip, const char* port);
void disconnectFromServer();
bool isConnectedToServer();
/*
* Starting local AP and local TCP-server
* (reconnect logic is applied, if conn lost or not established, or esp8266 restarted)
*/
bool startLocalAPAndServer(const char* ssid, const char* password, const char* channel,const char* port);
bool startLocalAP(const char* ssid, const char* password, const char* channel);
bool startLocalServer(const char* port);
bool stopLocalAPAndServer();
bool stopLocalAP();
bool stopLocalServer();
bool isLocalAPAndServerRunning();
/*
* Send string (if channel is connected of course)
*/
bool send(char channel, String& message, bool sendNow = true);
bool send(char channel, const char * message, bool sendNow = true);
/*
* Default is true.
*/
void endSendWithNewline(bool endSendWithNewline);
/*
* Scan for incoming message, do this as often and as long as you can (use as sleep in loop)
*/
WifiMessage listenForIncomingMessage(int timeoutMillis);
WifiMessage getIncomingMessage(void);
bool isConnection(void);
bool checkConnections(WifiConnection **pConnections);
private:
Stream* _serialIn;
Stream* _serialOut;
byte _resetPin;
Flags flags;
bool connectToServer();
char _ip[16];
char _port[6];
bool connectToAP();
char _ssid[16];
char _password[16];
bool startLocalAp();
bool startLocalServer();
char _localAPSSID[16];
char _localAPPassword[16];
char _localAPChannel[3];
char _localServerPort[6];
WifiConnection _connections[MAX_CONNECTIONS];
bool restart();
byte serverRetries;
char msgOut[MSG_BUFFER_MAX];//buffer for send method
char msgIn[MSG_BUFFER_MAX]; //buffer for listen method = limit of incoming message..
void writeCommand(const char* text1, const char* text2 = NULL);
byte readCommand(int timeout, const char* text1 = NULL, const char* text2 = NULL);
//byte readCommand(const char* text1, const char* text2);
byte readBuffer(char* buf, byte count, char delim = '\0');
char readChar();
Stream* _dbgSerial;
};
#endif
#include <SoftwareSerial.h>
#include <SerialESP8266wifi.h>
#define sw_serial_rx_pin 4 // Connect this pin to TX on the esp8266
#define sw_serial_tx_pin 6 // Connect this pin to RX on the esp8266
#define esp8266_reset_pin 5 // Connect this pin to CH_PD on the esp8266, not reset. (let reset be unconnected)
SoftwareSerial swSerial(sw_serial_rx_pin, sw_serial_tx_pin);
// the last parameter sets the local echo option for the ESP8266 module..
SerialESP8266wifi wifi(swSerial, swSerial, esp8266_reset_pin, Serial);//adding Serial enabled local echo and wifi debug
String inputString;
boolean stringComplete = false;
unsigned long nextPing = 0;
void setup() {
inputString.reserve(20);
swSerial.begin(9600);
Serial.begin(9600);
while (!Serial)
;
Serial.println("Starting wifi");
wifi.setTransportToTCP();// this is also default
// wifi.setTransportToUDP();//Will use UDP when connecting to server, default is TCP
wifi.endSendWithNewline(true); // Will end all transmissions with a newline and carrage return ie println.. default is true
wifi.begin();
//Turn on local ap and server (TCP)
wifi.startLocalAPAndServer("MY_CONFIG_AP", "password", "5", "2121");
wifi.connectToAP("wifissid", "wifipass");
wifi.connectToServer("192.168.0.28", "2121");
wifi.send(SERVER, "ESP8266 test app started");
}
void loop() {
//Make sure the esp8266 is started..
if (!wifi.isStarted())
wifi.begin();
//Send what you typed in the arduino console to the server
static char buf[20];
if (stringComplete) {
inputString.toCharArray(buf, sizeof buf);
wifi.send(SERVER, buf);
inputString = "";
stringComplete = false;
}
//Send a ping once in a while..
if (millis() > nextPing) {
wifi.send(SERVER, "Ping ping..");
nextPing = millis() + 10000;
}
//Listen for incoming messages and echo back, will wait until a message is received, or max 6000ms..
WifiMessage in = wifi.listenForIncomingMessage(6000);
if (in.hasData) {
if (in.channel == SERVER)
Serial.println("Message from the server:");
else
Serial.println("Message a local client:");
Serial.println(in.message);
//Echo back;
wifi.send(in.channel, "Echo:", false);
wifi.send(in.channel, in.message);
nextPing = millis() + 10000;
}
//If you want do disconnect from the server use:
// wifi.disconnectFromServer();
}
//Listen for serial input from the console
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}
#include <SoftwareSerial.h>
#include <SerialESP8266wifi.h>
/* TCP server/client example, that manages client connections, checks for messages
* when client is connected and parses commands. Connect to the ESP8266 IP using
* a TCP client such as telnet, eg: telnet 192.168.0.X 2121
*
* ESP8266 should be AT firmware based on 1.5 SDK or later
*
* 2016 - J.Whittington - engineer.john-whittington.co.uk
*/
#define sw_serial_rx_pin 4 // Connect this pin to TX on the esp8266
#define sw_serial_tx_pin 6 // Connect this pin to RX on the esp8266
#define esp8266_reset_pin 5 // Connect this pin to CH_PD on the esp8266, not reset. (let reset be unconnected)
#define SERVER_PORT "2121"
#define SSID "YourSSID"
#define PASSWORD "YourPassword"
SoftwareSerial swSerial(sw_serial_rx_pin, sw_serial_tx_pin);
// the last parameter sets the local echo option for the ESP8266 module..
SerialESP8266wifi wifi(Serial, Serial, esp8266_reset_pin, swSerial);
void processCommand(WifiMessage msg);
uint8_t wifi_started = false;
// TCP Commands
const char RST[] PROGMEM = "RST";
const char IDN[] PROGMEM = "*IDN?";
void setup() {
// start debug serial
swSerial.begin(9600);
// start HW serial for ESP8266 (change baud depending on firmware)
Serial.begin(115200);
while (!Serial)
;
Serial.println("Starting wifi");
swSerial.println("Starting wifi");
wifi.setTransportToTCP();// this is also default
wifi.endSendWithNewline(false); // Will end all transmissions with a newline and carrage return ie println.. default is true
wifi_started = wifi.begin();
if (wifi_started) {
wifi.connectToAP(SSID, PASSWORD);
wifi.startLocalServer(SERVER_PORT);
} else {
// ESP8266 isn't working..
}
}
void loop() {
static WifiConnection *connections;
// check connections if the ESP8266 is there
if (wifi_started)
wifi.checkConnections(&connections);
// check for messages if there is a connection
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (connections[i].connected) {
// See if there is a message
WifiMessage msg = wifi.getIncomingMessage();
// Check message is there
if (msg.hasData) {
// process the command
processCommand(msg);
}
}
}
}
void processCommand(WifiMessage msg) {
// return buffer
char espBuf[MSG_BUFFER_MAX];
// scanf holders
int set;
char str[16];
// Get command and setting
sscanf(msg.message,"%15s %d",str,&set);
/* swSerial.print(str);*/
/* swSerial.println(set);*/
if ( !strcmp_P(str,IDN) ) {
wifi.send(msg.channel,"ESP8266wifi Example");
}
// Reset system by temp enable watchdog
else if ( !strcmp_P(str,RST) ) {
wifi.send(msg.channel,"SYSTEM RESET...");
// soft reset by reseting PC
asm volatile (" jmp 0");
}
// Unknown command
else {
wifi.send(msg.channel,"ERR");
}
}
{
"name": "SerialESP8266wifi",
"keywords": "wifi, wi-fi, http, web, server, client",
"description": "ESP8266 Arduino library with built in reconnect functionality",
"repository":
{
"type": "git",
"url": "https://github.com/ekstrand/ESP8266wifi.git"
},
"frameworks": "arduino",
"platforms": "atmelavr"
}
[![Logo_Iottweet_official.png](https://www.iottweet.com/img/Logo_Iottweet_official.png)]
# IoTtweet
IoTtweet library for https://www.iottweet.com dashboard using. Internet of Things cloud data storage and control
### What is IoTtweet ?
- IoTtweet.com is web APIs for storage and control your Internet of Things over network. Anywhere, anytime in the world.
### How to starting ?</br>
<b>[step 1.]</b> </br>
Signup account with https://www.iottweet.com/signup/register.php</br>
<b>[step 2.]</b></br>
Prepare your IoT device and install 2 library as below into your computer.</br>
2.1) ESP8266WiFi.h library</br>
&nbsp;&nbsp;&nbsp;&nbsp;- Open Arduino IDE on your computer.</br>
&nbsp;&nbsp;&nbsp;&nbsp;- Go to <b>preferences</b>.</br>
&nbsp;&nbsp;&nbsp;&nbsp;- Add link "http://arduino.esp8266.com/stable/package_esp8266com_index.json" into <b>Board Manager URLs</b>.</br>
&nbsp;&nbsp;&nbsp;&nbsp;- Save and re-start Arduino IDE.</br>
&nbsp;&nbsp;&nbsp;&nbsp;- Go to <b>Tool > Boards > Boards Manager</b>.</br>
&nbsp;&nbsp;&nbsp;&nbsp;- Scroll down and find <i>"esp8266 by ESP8266 community"</i>. This is contained a library of ESP8266WiFi.h.</br>
&nbsp;&nbsp;&nbsp;&nbsp;- Click install, and after installed complete, click close.</br>
&nbsp;&nbsp;&nbsp;&nbsp;- Restart Arduino IDE again.</br>
&nbsp;&nbsp;&nbsp;&nbsp;- Open example, you will see some examples code of ESP8266WiFi.h. And now ready to coding ESP8266.</br>
2.2) ArduinoJson.h library</br>
&nbsp;&nbsp;&nbsp;&nbsp;- Open Arduino IDE on your computer.</br>
&nbsp;&nbsp;&nbsp;&nbsp;- Go to Sketch> Include Libraries > Manage Libraries </br>
&nbsp;&nbsp;&nbsp;&nbsp;- Search "ArduinoJson" in search box, and make install it.</br>
&nbsp;&nbsp;&nbsp;&nbsp;- Restart Arduino IDE again.</br>
2.3) IoTtweet.h library (we provided in this GITHUB)</br>
&nbsp;&nbsp;&nbsp;&nbsp;- Download this repository into your computer.</br>
&nbsp;&nbsp;&nbsp;&nbsp;- Extract files.</br>
&nbsp;&nbsp;&nbsp;&nbsp;- Copy folder name <b>IoTtweet</b> which contained 2 files of <b><u>IoTtweet.h</u></b> and <b><u>IoTtweet.cpp</u></b>, paste this folder at <b>Arduino > libraries</b>.</br>
&nbsp;&nbsp;&nbsp;&nbsp;- Restart Arduino IDE, ready to use IoTtweet command.</br>
<b>[step 3.]</b></br>
Upload example code in this repository and change some parameter to your parameter.</br>
<code><i>const char *userid = <b>"your_IoTtweet_userID"</b>;</i></code>
<code><i>const char *key = <b>"your_registered_IoT_device_key"</b>;</i></code>
<code><i>const char *ssid = <b>"your_wifi_ssid"</b>;</i></code>
<code><i>const char *password = <b>"your_wifi_password"</b>;</i></code>
<code><i>float <b>data0, data1, data2, data3</b>;</i></code>
<code><i>const char *private_tweet = <b>"Hello%20World"</b>;</i></code>
<code><i>const char *public_tweet = <b>"I%20am%20Internet%20of%20Things"</b>;</i></code></br>
:warning: :warning: Library this version is not supported "blank space" between each string to send tweet.
Please replace "blank space" with %20
<b>[step 4.]</b></br>
Let's tweet !!</br>
See data on your dashboard and control your IoT from there.</br></br>
[![dash v3 iottweet.png](https://s16.postimg.org/ohp5o1bh1/dash_v3_iottweet.png)](https://postimg.org/image/lnm0al9ap/)
<b>Find out more at</b> : https://www.iottweet.com/getstarted.php</br>
<b>IoTtweet support team</b> :wolf:</br>
mailto:support@iottweet.com</br>
FB : www.facebook.com/IoTtweet</br>
FB group talk :https://www.facebook.com/groups/191156967967490/
/*
* This example demonstrate how to write data from your "Internet of Things" to IoTtweet dashboard
* coding from IoTtweet.com
* Created : 2016.Sep.25
* Updated : 2016.Oct.4,PM
* By Isaranu Janthong
* IoTtweet Founder.
* Visit us at : www.iottweet.com
*/
#include <ESP8266WiFi.h>
#include <IoTtweet.h>
const char *userid = "your-user-id"; /*IoTtweet account user ID (6 digits, included zeor pre-fix)*/
const char *key = "your-user-key"; /*IoTtweet registered device key in "MY IOT Garage"*/
const char *ssid = "your-ssid"; /*Your-WiFi-router-SSID*/
const char *password = "your-wifi-password"; /*Your-WiFi-password*/
IoTtweet myiot; /*naming your devices*/
void setup() {
Serial.begin(115200);
/*Get IoTtweet Library version*/
String libvers = myiot.getVersion();
Serial.println("IoTtweet Library vesion : " + String(libvers));
/*Connect WiFi*/
Serial.println("\nConnect wifi...");
bool conn = myiot.begin(ssid,password);
if(!conn)
{
Serial.println("WiFi connection failed.");
}else
{
Serial.println("WiFi connected !");
}
}
void loop() {
/*Read control panel status (Switch and Slider)*/
String control_panel_status = myiot.ReadControlPanel(userid,key);
/*Print out result: Output is JSON format*/
Serial.println(control_panel_status);
/*Delay interval time for read control status*/
delay(1000);
}
/*
* This example demonstrate how to write data from your "Internet of Things" to IoTtweet dashboard
* coding from IoTtweet.com
* Created : 2016.Oct.4
* Updated : 2016.Oct.4, PM
* By Isaranu Janthong
* IoTtweet Founder.
* Visit us at : www.iottweet.com
*/
#include <ESP8266WiFi.h>
#include <IoTtweet.h>
const char *userid = "your_IoTtweet_userID"; /*IoTtweet account user ID (6 digits, included zeor pre-fix)*/
const char *key = "your_registered_IoT_device_key"; /*IoTtweet registered device key in "MY IOT Garage"*/
const char *ssid = "your_wifi_ssid"; /*Your-WiFi-router-SSID*/
const char *password = "your_wifi_password"; /*Your-WiFi-password*/
IoTtweet myiot; /*naming your devices*/
void setup() {
Serial.begin(115200);
/*Get IoTtweet Library version*/
String libvers = myiot.getVersion();
Serial.println("IoTtweet Library vesion : " + String(libvers));
/*Connect WiFi*/
Serial.println("\nConnect wifi...");
bool conn = myiot.begin(ssid,password);
if(!conn)
{
Serial.println("WiFi connection failed.");
}else
{
Serial.println("WiFi connected !");
}
}
void loop() {
/*Example : Read status slider no.1 on dashboard.
* This command will return data type as float to you
* command parameter : ReadAnalogSlider(userid,key,slider number);
*/
float slider1_read = myiot.ReadAnalogSlider(userid,key,1);
/*Print output to serial monitor*/
Serial.println();
Serial.print("Analog Slider 1 value is ");
Serial.print(slider1_read);
delay(10);
}
/*
* This example demonstrate how to write data from your "Internet of Things" to IoTtweet dashboard
* coding from IoTtweet.com
* Created : 2016.Oct.4
* Updated : 2016.Oct.4, PM
* By Isaranu Janthong
* IoTtweet Founder.
* Visit us at : www.iottweet.com
*/
#include <ESP8266WiFi.h>
#include <IoTtweet.h>
const char *userid = "your-user-id"; /*IoTtweet account user ID (6 digits, included zeor pre-fix)*/
const char *key = "your-user-key"; /*IoTtweet registered device key in "MY IOT Garage"*/
const char *ssid = "your-ssid"; /*Your-WiFi-router-SSID*/
const char *password = "your-wifi-password"; /*Your-WiFi-password*/
IoTtweet myiot; /*naming your devices*/
void setup() {
Serial.begin(115200);
/*Set output pin of LED (Built-in LED of NodeMCU is IO no. 16)*/
pinMode(LED_BUILTIN,OUTPUT);
/*Get IoTtweet Library version*/
String libvers = myiot.getVersion();
Serial.println("IoTtweet Library vesion : " + String(libvers));
/*Connect WiFi*/
Serial.println("\nConnect wifi...");
bool conn = myiot.begin(ssid,password);
if(!conn)
{
Serial.println("WiFi connection failed.");
}else
{
Serial.println("WiFi connected !");
}
}
void loop() {
/*Example : Read status switch no.1 on dashboard.
* This command will return "ON" or "OFF" string to you
* command parameter : ReadDigitalSwitch(userid,ket,switch number);
*/
String sw1_read = myiot.ReadDigitalSwitch(userid,key,1);
delay(10);
/*Drive to built-in LED*/
if(sw1_read == "ON")
{
digitalWrite(LED_BUILTIN,LOW);
}else
{
digitalWrite(LED_BUILTIN,HIGH);
}
}
/*
* This example demonstrate how to write data from your "Internet of Things" to IoTtweet dashboard
* coding from IoTtweet.com
* Created : 2018.Apr.27
* By Isaranu Janthong
* IoTtweet Founder.
* Visit us at : www.iottweet.com
*/
#include <ESP8266WiFi.h>
#include <IoTtweet.h>
#include "DHT.h"
const char *userid = "your_IoTtweet_userID"; //IoTtweet account user ID (6 digits, included zero pre-fix)
const char *key = "your_registered_IoT_device_key"; //IoTtweet registered device key in "MY IOT Garage"
const char *ssid = "your_wifi_ssid"; //Your-WiFi-router-SSID
const char *password = "your_wifi_password"; //Your-WiFi-password
float data0, data1, data2, data3; //Your sending data variable.
String private_tweet = "Simple DHT"; //Your private tweet meassage to dashboard
String public_tweet = "send to IoTtweet"; //Your public tweet message to dashboard
#define DHTPIN 4 // GPIO4 pin
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float t,h;
IoTtweet myiot; //naming your devices
void setup() {
Serial.begin(115200);
dht.begin();
//Get IoTtweet Library version
String libvers = myiot.getVersion();
Serial.println("IoTtweet Library vesion : " + String(libvers));
//Connect WiFi
Serial.println("\nConnect wifi...");
bool conn = myiot.begin(ssid,password);
if(!conn)
{
Serial.println("WiFi connection failed.");
}else
{
Serial.println("WiFi connected !");
}
}
void loop() {
/* - DHT sensor reading - */
t = dht.readTemperature();
h = dht.readHumidity();
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.println("Temperature is " + String(t) + " celcuis");
Serial.println("Humidity is " + String(h) + " %RH");
Serial.println("----------------------------------------");
//Send data from your iot to Dashboard
String response = myiot.WriteDashboard(userid,key,t,h,0,0,private_tweet,public_tweet);
Serial.println(response); //Show response JSON from www.iottweet.com
//Waiting storage data on IoTtweet cloud 15 sec.
delay(15000);
}
/*
* This example demonstrate how to write data from your "Internet of Things" to IoTtweet dashboard
* coding from IoTtweet.com
* Created : 2016.Sep.25
* By Isaranu Janthong
* IoTtweet Founder.
* Visit us at : www.iottweet.com
*/
#include <ESP8266WiFi.h>
#include <IoTtweet.h>
const char *userid = "your_IoTtweet_userID"; //IoTtweet account user ID (6 digits, included zero pre-fix)
const char *key = "your_registered_IoT_device_key"; //IoTtweet registered device key in "MY IOT Garage"
const char *ssid = "your_wifi_ssid"; //Your-WiFi-router-SSID
const char *password = "your_wifi_password"; //Your-WiFi-password
float data0, data1, data2, data3; //Your sending data variable.
String private_tweet = "Hello World"; //Your private tweet meassage to dashboard
String public_tweet = "I am Internet of Things"; //Your public tweet message to dashboard
IoTtweet myiot; //naming your devices
void setup() {
Serial.begin(115200);
//Get IoTtweet Library version
String libvers = myiot.getVersion();
Serial.println("IoTtweet Library vesion : " + String(libvers));
//Connect WiFi
Serial.println("\nConnect wifi...");
bool conn = myiot.begin(ssid,password);
if(!conn)
{
Serial.println("WiFi connection failed.");
}else
{
Serial.println("WiFi connected !");
}
}
void loop() {
//Example data generating
data0 = random(20,80);
data1 = random(30,70);
data2 = random(40,60);
data3 = random(50,55);
//Send data from your iot to Dashboard
String response = myiot.WriteDashboard(userid,key,data0,data1,data2,data3,private_tweet,public_tweet);
Serial.println(response); //Show response JSON from www.iottweet.com
//Waiting storage data on IoTtweet cloud 15 sec.
delay(15000);
}
Example of IoTtweet library using
Have fun :)
IoTtweet Team
name=IoTtweet
version=1.0.0
author=Isaranu Janthong <isaranujanthong@gmail.com>
maintainer=Isaranu Janthong <isaranujanthong@gmail.com>
sentence=A library that makes Internet of Things send data and control on IoTtweet.com.
paragraph=Support ESP8266WiFi.h.
category=Communication
url=http://www.iottweet.com/
architectures=esp8266
includes=IoTtweet.h
/*
library for IoTtweet.com, using via api.iottweet.com
Created by : IoTtweet tech. team
Date : 2016.Sep.22
Updated : 2016.Sep.23, 01:35
Updated : 2016.Sep.23, 16:50
Updated : 2016.Sep.24, 20:40
Updated : 2016.Sep.25, 12:03
>> Release GITHUB : v0.1.0 on 25.Sep, 12:04
Updated : 2016.Oct.3, 23:00
>> Release GITHUB : v0.2.0 on 3.Oct, 23:06
>> Features - Read status of specified switch name
- Read status of specified slider name
Updated : 2016.Dec.17, 17:31
>> Release GITHUB : v0.2.9
Updated : 2017.Jan.15, 22:01
>> Able sending tweet as string type.
>> Able tweet by have whitespace string.
>> Release GITHUB : v0.3.0
Updated : 2019.Mar.14, 17:42
>> Modified code to compatible with ArduinoJson.h version 6+
>> Release GITHUB : v1.0.0
*/
#include "IoTtweet.h"
#define IoTtweet_HOST "api.iottweet.com"
#define IoTtweet_PORT 80
#define IoTtweet_libVersion "v1.0.0"
//Connect WiFi router
bool IoTtweet::begin(const char *ssid, const char *passw)
{
//Transfer variable
_ssid = ssid;
_passw = passw;
int _cnt=0, _retry=30;
bool _conn = false;
//Connect WiFi
WiFi.begin(_ssid,_passw);
Serial.print("IoTweet wifi start connection...");
//Connecting WiFi
while ((WiFi.status() != WL_CONNECTED) && (_cnt <= _retry))
{
delay(200);
_cnt++;
}
if(WiFi.status() == WL_CONNECTED)
{
_conn = true;
Serial.println("\nWiFi for IoTtweet Connected !");
}else{
_conn = false;
}
return _conn;
}
//Connect to api.iottweet.com & send data to dashboard
String IoTtweet::WriteDashboard(const char *userid, const char *key, float slot0, float slot1, float slot2, float slot3, String tw, String twpb)
{
//Transfer variable
_userid = userid;
_key = key;
_slot0 = slot0;
_slot1 = slot1;
_slot2 = slot2;
_slot3 = slot3;
_tw = tw;
_twpb = twpb;
//whitespace string replace to %20
_tw.replace(" ","%20");
_twpb.replace(" ","%20");
//Create connection TCP to api.iottweet.com
WiFiClient client;
if (client.connect(IoTtweet_HOST, IoTtweet_PORT))
{
//Start sending data package
_str = "GET /?userid=";
_str += _userid;
_str += "&key=";
_str += _key;
_str += "&slot0=";
_str += _slot0;
_str += "&slot1=";
_str += _slot1;
_str += "&slot2=";
_str += _slot2;
_str += "&slot3=";
_str += _slot3;
_str += "&tw=";
_str += _tw;
_str += "&twpb=";
_str += _twpb;
_str += " HTTP/1.1\r\n";
_str += "Host: api.iottweet.com\r\n";
_str += "Connection: keep-alive\r\n\r\n";
//Push data to api.iottweet.com
client.print(_str);
//Check response back
while(client.available()){
_response = client.readStringUntil('\r');
}
return _response;
}
}
//Connect to dashboard and read status of all switch and slide
String IoTtweet::ReadControlPanel(const char *userid, const char *key)
{
//Transfer variable
_userid = userid;
_key = key;
//Create connection TCP to api.iottweet.com
WiFiClient client;
if (client.connect(IoTtweet_HOST, IoTtweet_PORT))
{
//Start sending data package
_str = "GET /read/?userid=";
_str += _userid;
_str += "&key=";
_str += _key;
_str += " HTTP/1.1\r\n";
_str += "Host: api.iottweet.com\r\n";
_str += "Connection: keep-alive\r\n\r\n";
//Push data to api.iottweet.com
client.print(_str);
delay(50);
//Check response back
while(client.available()){
_controlpanelstatus = client.readStringUntil('\r');
}
//--------- Start parse json message ---------------
DynamicJsonDocument doc(1024);
auto error = deserializeJson(doc, _controlpanelstatus);
if (error) {
Serial.print(F("deserializeJson() failed with code "));
Serial.println(error.c_str());
return "deserializeJson() failed with code ";
}
_sw1status = doc["sw1"];
_sw2status = doc["sw2"];
_sw3status = doc["sw3"];
_sw4status = doc["sw4"];
_sw5status = doc["sw5"];
_sl1status = doc["slide1"];
f_sl1status = String(_sl1status).toFloat();
_sl2status = doc["slide2"];
f_sl2status = String(_sl2status).toFloat();
_sl3status = doc["slide3"];
f_sl3status = String(_sl3status).toFloat();
_allcontrol = "{";
_allcontrol += "\"sw1\":\"";
_allcontrol += _sw1status;
_allcontrol += "\",";
_allcontrol += "\"sw2\":\"";
_allcontrol += _sw2status;
_allcontrol += "\",";
_allcontrol += "\"sw3\":\"";
_allcontrol += _sw3status;
_allcontrol += "\",";
_allcontrol += "\"sw4\":\"";
_allcontrol += _sw4status;
_allcontrol += "\",";
_allcontrol += "\"sw5\":\"";
_allcontrol += _sw5status;
_allcontrol += "\",";
_allcontrol += "\"slider1\":\"";
_allcontrol += f_sl1status;
_allcontrol += "\",";
_allcontrol += "\"slider2\":\"";
_allcontrol += f_sl2status;
_allcontrol += "\",";
_allcontrol += "\"slider3\":\"";
_allcontrol += f_sl3status;
_allcontrol += "\"}";
return _allcontrol;
}
}
//Connect to dashboard and read status of specified number of switch
String IoTtweet::ReadDigitalSwitch(const char *userid, const char *key, uint8_t sw)
{
//Transfer variable
_userid = userid;
_key = key;
_sw = sw;
//Create connection TCP to api.iottweet.com
WiFiClient client;
if (client.connect(IoTtweet_HOST, IoTtweet_PORT))
{
//Start sending data package
_str = "GET /read/?userid=";
_str += _userid;
_str += "&key=";
_str += _key;
_str += " HTTP/1.1\r\n";
_str += "Host: api.iottweet.com\r\n";
_str += "Connection: keep-alive\r\n\r\n";
//Push data to api.iottweet.com
client.print(_str);
delay(50);
//Check response back
while(client.available()){
_controlpanelstatus = client.readStringUntil('\r');
}
//--------- Start parse json message ---------------
DynamicJsonDocument doc(1024);
auto error = deserializeJson(doc, _controlpanelstatus);
if (error) {
Serial.print(F("deserializeJson() failed with code "));
Serial.println(error.c_str());
return "deserializeJson() failed with code ";
}
_sw1status = doc["sw1"];
_sw2status = doc["sw2"];
_sw3status = doc["sw3"];
_sw4status = doc["sw4"];
_sw5status = doc["sw5"];
switch(_sw){
case 1:
return _sw1status;
break;
case 2:
return _sw2status;
break;
case 3:
return _sw3status;
break;
case 4:
return _sw4status;
break;
case 5:
return _sw5status;
break;
}
}
}
//Connect to dashboard and read status each of specified number slider
float IoTtweet::ReadAnalogSlider(const char *userid, const char *key, uint8_t slider)
{
//Transfer variable
_userid = userid;
_key = key;
_slider = slider;
//Create connection TCP to api.iottweet.com
WiFiClient client;
if (client.connect(IoTtweet_HOST, IoTtweet_PORT))
{
//Start sending data package
_str = "GET /read/?userid=";
_str += _userid;
_str += "&key=";
_str += _key;
_str += " HTTP/1.1\r\n";
_str += "Host: api.iottweet.com\r\n";
_str += "Connection: keep-alive\r\n\r\n";
//Push data to api.iottweet.com
client.print(_str);
delay(50);
//Check response back
while(client.available()){
_controlpanelstatus = client.readStringUntil('\r');
}
//--------- Start parse json message ---------------
DynamicJsonDocument doc(1024);
auto error = deserializeJson(doc, _controlpanelstatus);
if (error) {
Serial.print(F("deserializeJson() failed with code "));
Serial.println(error.c_str());
}
_sl1status = doc["slide1"];
f_sl1status = String(_sl1status).toFloat();
_sl2status = doc["slide2"];
f_sl2status = String(_sl2status).toFloat();
_sl3status = doc["slide3"];
f_sl3status = String(_sl3status).toFloat();
switch(_slider){
case 1:
return f_sl1status;
break;
case 2:
return f_sl2status;
break;
case 3:
return f_sl3status;
break;
}
}
}
//Get IoTtweet library version information
String IoTtweet::getVersion()
{
return IoTtweet_libVersion;
}
/*
library for IoTtweet.com, using via api.iottweet.com
Created by : IoTtweet tech. team
Date : 2016.Sep.22
Updated : 2016.Sep.23, 01:35
Updated : 2016.Sep.23, 16:50
Updated : 2016.Sep.24, 20:40
Updated : 2016.Sep.25, 12:03
>> Release GITHUB : v0.1.0 on 25.Sep, 12:04
Updated : 2016.Oct.3, 23:00
>> Release GITHUB : v0.2.0 on 3.Oct, 23:06
>> Features - Read status of specified switch name
- Read status of specified slider name
Updated : 2016.Dec.17, 17:31
>> Release GITHUB : v0.2.9
Updated : 2017.Jan.15, 22:01
>> Able sending tweet as string type.
>> Able tweet by have whitespace string.
>> Release GITHUB : v0.3.0
*/
#ifndef IoTtweet_h
#define IoTtweet_h
#include "Arduino.h"
#include "ESP8266WiFi.h"
#include "ArduinoJson.h"
class IoTtweet
{
public:
bool begin(const char *ssid, const char *passw);
String WriteDashboard(const char *userid, const char *key, float slot0, float slot1, float slot2, float slot3, String tw, String twpb);
String ReadControlPanel(const char *userid, const char *key);
String ReadDigitalSwitch(const char *userid, const char *key, uint8_t sw);
String getVersion();
float ReadAnalogSlider(const char *userid, const char *key, uint8_t slider);
private:
const char *_ssid, *_passw;
const char *_userid, *_key;
String _tw, _twpb;
float _slot0, _slot1, _slot2, _slot3;
const char *_libversion;
String _str, _response, _controlpanelstatus;
const char *_sw1status, *_sw2status, *_sw3status, *_sw4status, *_sw5status, *_sl1status, *_sl2status, *_sl3status;
float f_sl1status, f_sl2status, f_sl3status;
String _allcontrol;
uint8_t _sw, _slider;
};
#endif
# IoTtweetESP32
Library for esp32 using on IoTtweet.com
Support Espino32 from ThaiEasyElec
/*
* This example demonstrate how to write data from your "Internet of Things" to IoTtweet dashboard
* coding from IoTtweet.com
* Created : 2017.Nov.29
* By Isaranu Janthong
* IoTtweet Founder.
* Visit us at : www.iottweet.com
*/
#include <IoTtweetESP32.h>
const char *userid = "your-user-id"; //IoTtweet account user ID (6 digits, included zero pre-fix)
const char *key = "your-iot-key"; //IoTtweet registered device key in "MY IOT Garage"
const char *ssid = "your-ssid"; //Your-WiFi-router-SSID
const char *password = "your-wifi-password"; //Your-WiFi-password
IoTtweetESP32 myiot; /*naming your devices*/
void setup() {
Serial.begin(115200);
/*Get IoTtweet Library version*/
String libvers = myiot.getVersion();
Serial.println("IoTtweet Library vesion : " + String(libvers));
/*Connect WiFi*/
Serial.println("\nConnect wifi...");
bool conn = myiot.begin(ssid,password);
if(!conn)
{
Serial.println("WiFi connection failed.");
}else
{
Serial.println("WiFi connected !");
}
}
void loop() {
/*Read control panel status (Switch and Slider)*/
String control_panel_status = myiot.ReadControlPanel(userid,key);
/*Print out result: Output is JSON format*/
Serial.println(control_panel_status);
/*Delay interval time for read control status*/
delay(1000);
}
/*
* This example demonstrate how to write data from your "Internet of Things" to IoTtweet dashboard
* Compatible with "Espino32" by ThaiEasyElec.
* Created : 2017.Aug.28
* By Isaranu Janthong
* IoTtweet Founder.
* Visit us at : www.iottweet.com
*/
#include <IoTtweetESP32.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"
/* -- Espino32 : IoTtweet -- */
const char *userid = "userid"; //IoTtweet account user ID (6 digits, included zero pre-fix)
const char *key = "device-key"; //IoTtweet registered device key in "MY IOT Garage"
const char *ssid = "ssid"; //Your-WiFi-router-SSID
const char *password = "password"; //Your-WiFi-password
float data0, data1, data2, data3; //Your sending data variable.
String message_1 = "Hello from Espino32"; //Your private tweet meassage to dashboard
String message_2 = "ThaieasyElec na ja :)"; //Your public tweet message to dashboard
IoTtweetESP32 myiot; //naming your devices
/* -- Espino32 : OLED -- */
#define OLED_RESET 0
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
static const unsigned char PROGMEM thermo16_glcd_bmp[] =
{ B00000001, B10000000,
B00000010, B01000000,
B00000100, B00100000,
B11000100, B00100000,
B00000100, B00100000,
B00000100, B00100000,
B11000111, B11100000,
B00000111, B11100000,
B00000111, B11100000,
B00001111, B11110000,
B00011111, B11111000,
B00011111, B11111000,
B00011111, B11111000,
B00001111, B11110000,
B00001111, B11110000,
B00000011, B11000000 };
static const unsigned char PROGMEM drop16_glcd_bmp[] =
{ B00000001, B10000000,
B00000001, B10000000,
B00000001, B10000000,
B00000011, B11000000,
B00000111, B11100000,
B00001111, B11110000,
B00011111, B11111000,
B00011111, B11111000,
B00111111, B11111100,
B00111111, B11111100,
B00111111, B11111100,
B00111111, B11111100,
B00111111, B11111100,
B00011111, B11111000,
B00001111, B11110000,
B00000011, B11000000 };
static const unsigned char PROGMEM blank16_glcd_bmp[] =
{ B00000000, B00000000,
B00000000, B00000000,
B00000000, B00000000,
B00000000, B00000000,
B00000000, B00000000,
B00000000, B00000000,
B00000000, B00000000,
B00000000, B00000000,
B00000000, B00000000,
B00000000, B00000000,
B00000000, B00000000,
B00000000, B00000000,
B00000000, B00000000,
B00000000, B00000000,
B00000000, B00000000,
B00000000, B00000000 };
#if (SSD1306_LCDHEIGHT != 64)
//#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
/* - OLED Display position offset. - */
int thermo_pos_x = 10, thermo_pos_y = 5;
/* -- Espino32 : DHT11 -- */
#define DHTPIN 10
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float h,t,f,hif,hic;
float t_offset = 0.0, h_offset = 0.0;
void setup() {
Serial.begin(115200);
dht.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); /* initialize with the I2C addr 0x3D (for the 128x64) */
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Connecting to IoTtweet.com...");
display.display();
/* Get IoTtweet Library version */
String libvers = myiot.getVersion();
Serial.println("IoTtweet Library vesion : " + String(libvers));
/* Connect WiFi */
Serial.println("\nConnect wifi...");
bool conn = myiot.begin(ssid,password);
display.println("Lib.ver: " + String(libvers));
if(!conn)
{
display.println("connect fails.");
}else
{ display.println("connected !.");
}
display.display();
delay(2000);
display.clearDisplay();
}
void loop() {
/* - DHT sensor reading - */
float h = dht.readHumidity() + h_offset;
float t = dht.readTemperature() + t_offset;
float f = dht.readTemperature(true);
delay(2000);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
/* - Draw icon -*/
display.clearDisplay();
display.drawBitmap(thermo_pos_x,thermo_pos_y, thermo16_glcd_bmp,16,16, WHITE);
display.drawBitmap(thermo_pos_x,thermo_pos_y+30, drop16_glcd_bmp,16,16, WHITE);
/* - Display Temperature - */
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(thermo_pos_x + 30, thermo_pos_y);
display.println(String(t));
/* - Display Humidity - */
display.setCursor(thermo_pos_x + 30, thermo_pos_y + 30);
display.println(String(h));
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(110, thermo_pos_y + 5);
display.println("c.");
display.setCursor(110, thermo_pos_y + 35);
display.println("%RH");
/* - Setup tweet message to display on IoTtweet dashboard - */
message_1 = "อุณหภูมิตอนนี้ " + String(t) + " c";
message_2 = "ความชื้นตอนนี้ " + String(h) + " %";
/* - Send data from your iot to Dashboard - */
String response = myiot.WriteDashboard(userid,key,t,h,data2,data3,message_1,message_2);
display.setTextColor(WHITE);
display.setCursor(17, 56);
display.println("Uploading IoTtweet");
display.display();
delay(15000);
/* OLED screen */
display.setTextColor(BLACK);
display.setCursor(17, 56);
display.println("Uploading IoTtweet");
display.display();
display.setTextColor(WHITE);
display.setCursor(40, 56);
display.println("by Espino32");
display.display();
delay(2000);
display.setTextColor(BLACK);
display.setCursor(40, 56);
display.println("by Espino32");
display.display();
display.setTextColor(WHITE);
display.setCursor(40, 56);
display.println("Read sensor..");
display.display();
}
#include <Adafruit_NeoPixel.h>
int neoPixelPin = 9;
int numPixels = 16;
int max_brightness = 32;
int i;
int r,g,b;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(numPixels, neoPixelPin, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.setBrightness(8); /* max : 32 */
}
void loop() {
/* - Draw a color - */
for(i=0; i< numPixels; i++){
r = random(0,255);
g = random(0,255);
b = random(0,255);
strip.setPixelColor(i, r, g, b);
delay(20);
strip.show();
}
delay(1000);
}
/*
* This example demonstrate how to write data from your "Internet of Things" to IoTtweet dashboard
* Compatible with "Espino32" by ThaiEasyElec.
* Created : 2017.Aug.28
* By Isaranu Janthong
* IoTtweet Founder.
* Visit us at : www.iottweet.com
*/
#include <IoTtweetESP32.h>
/* -- Espino32 : IoTtweet -- */
const char *userid = "userid"; //IoTtweet account user ID (6 digits, included zero pre-fix)
const char *key = "device-key"; //IoTtweet registered device key in "MY IOT Garage"
const char *ssid = "ssid"; //Your-WiFi-router-SSID
const char *password = "password"; //Your-WiFi-password
float data0, data1, data2, data3; //Your sending data variable.
String message_1 = "Hello from Espino32"; //Your private tweet meassage to dashboard
String message_2 = "ThaieasyElec na ja :)"; //Your public tweet message to dashboard
IoTtweetESP32 myiot; //naming your devices
void setup() {
Serial.begin(115200);
/* Get IoTtweet Library version */
String libvers = myiot.getVersion();
Serial.println("IoTtweet Library vesion : " + String(libvers));
/* Connect WiFi */
Serial.println("\nConnect wifi...");
bool conn = myiot.begin(ssid,password);
Serial.println("Lib.ver: " + String(libvers));
if(!conn)
{
Serial.println("connect fails.");
}else
{
Serial.println("connected !.");
}
}
void loop() {
/* - Example data generating -*/
data0 = random(20,80);
data1 = random(30,70);
data2 = random(40,60);
data3 = random(50,55);
/*Send data from your iot to Dashboard */
String response = myiot.WriteDashboard(userid,key,data0,data1,data2,data3,message_1,message_2);
delay(15000);
}
/*
* This example demonstrate how to write data from your "Internet of Things" to IoTtweet dashboard
* Compatible with "Espino32" by ThaiEasyElec.
* Created : 2017.Aug.28
* By Isaranu Janthong
* IoTtweet Founder.
* Visit us at : www.iottweet.com
*/
#include "DHT.h"
/* -- Espino32 : DHT11 -- */
#define DHTPIN 10
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float h,t,f,hif,hic;
float t_offset = 0.0, h_offset = 0.0;
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
/* - DHT sensor reading - */
float h = dht.readHumidity() + h_offset;
float t = dht.readTemperature() + t_offset;
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.println("Temperature is " + String(t) + " celcuis");
Serial.println("Humidity is " + String(h) + " %RH");
Serial.println("--------------------");
delay(2000);
}
/*
* This example demonstrate code.
* Compatible with "Espino32" by ThaiEasyElec.
* Created : 2017.Aug.28
* By Isaranu Janthong
* IoTtweet Founder.
* Visit us at : www.iottweet.com
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
/* -- Espino32 : OLED -- */
#define OLED_RESET 0
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#if (SSD1306_LCDHEIGHT != 64)
//#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
void setup() {
Serial.begin(115200);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); /* initialize with the I2C addr 0x3D (for the 128x64) */
display.display();
delay(2000);
/* clear screen */
display.clearDisplay();
/* Display message 1 */
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Hello Espino32 !");
display.display();
/* Display message 2 */
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,10);
display.println("ThaiEasyElec na ja");
display.display();
}
void loop() {
// code..
}
/*
* This example demonstrate how to write data from your "Internet of Things" to IoTtweet dashboard
* coding from IoTtweet.com
* Created : 2017.Feb.14
* By Isaranu Janthong
* IoTtweet Founder.
* Visit us at : www.iottweet.com
*/
#include <IoTtweetESP32.h>
const char *userid = "your-user-id"; //IoTtweet account user ID (6 digits, included zero pre-fix)
const char *key = "your-iot-key"; //IoTtweet registered device key in "MY IOT Garage"
const char *ssid = "your-ssid"; //Your-WiFi-router-SSID
const char *password = "your-wifi-password"; //Your-WiFi-password
float data0, data1, data2, data3; //Your sending data variable.
String private_tweet = "Hello Node32s"; //Your private tweet meassage to dashboard
String public_tweet = "Welceome IoTtweetESP32.h"; //Your public tweet message to dashboard
IoTtweetESP32 myiot; //naming your devices
void setup() {
Serial.begin(115200);
//Get IoTtweet Library version
String libvers = myiot.getVersion();
Serial.println("IoTtweet Library vesion : " + String(libvers));
//Connect WiFi
Serial.println("\nConnect wifi...");
bool conn = myiot.begin(ssid,password);
if(!conn)
{
Serial.println("WiFi connection failed.");
}else
{
Serial.println("WiFi connected !");
}
}
void loop() {
//Example data generating
data0 = random(20,80);
data1 = random(30,70);
data2 = random(40,60);
data3 = random(50,55);
//Send data from your iot to Dashboard
String response = myiot.WriteDashboard(userid,key,data0,data1,data2,data3,private_tweet,public_tweet);
Serial.println(response); //Show response JSON from www.iottweet.com
//Waiting storage data on IoTtweet cloud 15 sec.
delay(15000);
}
name=IoTtweetESP32
version=1.0.0
author=Isaranu Janthong <isaranujanthong@gmail.com>
maintainer=Isaranu Janthong <isaranujanthong@gmail.com>
sentence=A library that makes Internet of Things send data and control on IoTtweet.com.
paragraph=Support ESP8266WiFi.h.
category=Communication
url=https://www.iottweet.com/
architectures=esp32
includes=IoTtweetESP32.h
/*
library for IoTtweet.com, using via api.iottweet.com
Compatible use with ESP32 WiFi and BLE chip from espressif.
Created by : IoTtweet tech. team
Date : 2016.Oct.15
*/
#include "IoTtweetESP32.h"
#define IoTtweet_HOST "api.iottweet.com"
#define IoTtweet_PORT 80
#define IoTtweet_libVersion "v1.0.0"
#define LAG_TIME 50
//Connect WiFi router
bool IoTtweetESP32::begin(const char *ssid, const char *passw)
{
//Transfer variable
_ssid = ssid;
_passw = passw;
int _cnt=0, _retry=30;
bool _conn = false;
//Connect WiFi
WiFi.begin(_ssid,_passw);
Serial.print("IoTweetESP32 wifi start connection...");
//Connecting WiFi
while ((WiFi.status() != WL_CONNECTED) && (_cnt <= _retry))
{
delay(500);
Serial.print(".");
_cnt++;
}
if(WiFi.status() == WL_CONNECTED)
{
_conn = true;
Serial.println("\nWiFi for IoTtweet Connected !");
}else{
_conn = false;
}
return _conn;
}
//Connect to api.iottweet.com & send data to dashboard
String IoTtweetESP32::WriteDashboard(const char *userid, const char *key, float slot0, float slot1, float slot2, float slot3, String tw, String twpb)
{
//Transfer variable
_userid = userid;
_key = key;
_slot0 = slot0;
_slot1 = slot1;
_slot2 = slot2;
_slot3 = slot3;
_tw = tw;
_twpb = twpb;
//whitespace string replace to %20
_tw.replace(" ","%20");
_twpb.replace(" ","%20");
//Create connection TCP to api.iottweet.com
WiFiClient client;
if (client.connect(IoTtweet_HOST, IoTtweet_PORT))
{
//Start sending data package
_str = "GET /?userid=";
_str += _userid;
_str += "&key=";
_str += _key;
_str += "&slot0=";
_str += _slot0;
_str += "&slot1=";
_str += _slot1;
_str += "&slot2=";
_str += _slot2;
_str += "&slot3=";
_str += _slot3;
_str += "&tw=";
_str += _tw;
_str += "&twpb=";
_str += _twpb;
_str += " HTTP/1.1\r\n";
_str += "Host: api.iottweet.com\r\n";
_str += "Connection: keep-alive\r\n\r\n";
//Push data to api.iottweet.com
client.print(_str);
//Check response back
while(client.available()){
_response = client.readStringUntil('\r');
}
return _response;
}
}
//Connect to dashboard and read status of all switch and slide
String IoTtweetESP32::ReadControlPanel(const char *userid, const char *key)
{
//Transfer variable
_userid = userid;
_key = key;
//Create connection TCP to api.iottweet.com
WiFiClient client;
if (client.connect(IoTtweet_HOST, IoTtweet_PORT))
{
//Start sending data package
_str = "GET /read/?userid=";
_str += _userid;
_str += "&key=";
_str += _key;
_str += " HTTP/1.1\r\n";
_str += "Host: api.iottweet.com\r\n";
_str += "Connection: keep-alive\r\n\r\n";
//Push data to api.iottweet.com
client.print(_str);
delay(LAG_TIME);
//Check response back
while(client.available()){
_controlpanelstatus = client.readStringUntil('\r');
}
//--------- Start parse json message ---------------
StaticJsonBuffer<2000> jsonBuffer;
JsonObject& _root = jsonBuffer.parseObject(_controlpanelstatus);
if (!_root.success())
{
Serial.println("parseObject() failed");
}
_sw1status = _root["sw1"];
_sw2status = _root["sw2"];
_sw3status = _root["sw3"];
_sw4status = _root["sw4"];
_sw5status = _root["sw5"];
_sl1status = _root["slide1"];
f_sl1status = String(_sl1status).toFloat();
_sl2status = _root["slide2"];
f_sl2status = String(_sl2status).toFloat();
_sl3status = _root["slide3"];
f_sl3status = String(_sl3status).toFloat();
_allcontrol = "{";
_allcontrol += "\"sw1\":\"";
_allcontrol += _sw1status;
_allcontrol += "\",";
_allcontrol += "\"sw2\":\"";
_allcontrol += _sw2status;
_allcontrol += "\",";
_allcontrol += "\"sw3\":\"";
_allcontrol += _sw3status;
_allcontrol += "\",";
_allcontrol += "\"sw4\":\"";
_allcontrol += _sw4status;
_allcontrol += "\",";
_allcontrol += "\"sw5\":\"";
_allcontrol += _sw5status;
_allcontrol += "\",";
_allcontrol += "\"slider1\":\"";
_allcontrol += f_sl1status;
_allcontrol += "\",";
_allcontrol += "\"slider2\":\"";
_allcontrol += f_sl2status;
_allcontrol += "\",";
_allcontrol += "\"slider3\":\"";
_allcontrol += f_sl3status;
_allcontrol += "\"}";
return _allcontrol;
}
}
//Connect to dashboard and read status of specified number of switch
String IoTtweetESP32::ReadDigitalSwitch(const char *userid, const char *key, uint8_t sw)
{
//Transfer variable
_userid = userid;
_key = key;
_sw = sw;
//Create connection TCP to api.iottweet.com
WiFiClient client;
if (client.connect(IoTtweet_HOST, IoTtweet_PORT))
{
//Start sending data package
_str = "GET /read/?userid=";
_str += _userid;
_str += "&key=";
_str += _key;
_str += " HTTP/1.1\r\n";
_str += "Host: api.iottweet.com\r\n";
_str += "Connection: keep-alive\r\n\r\n";
//Push data to api.iottweet.com
client.print(_str);
delay(LAG_TIME);
//Check response back
while(client.available()){
_controlpanelstatus = client.readStringUntil('\r');
}
//--------- Start parse json message ---------------
StaticJsonBuffer<2000> jsonBuffer;
Serial.print("read : " + _controlpanelstatus);
JsonObject& _root = jsonBuffer.parseObject(_controlpanelstatus);
if (!_root.success())
{
Serial.println("parseObject() failed");
}
_sw1status = _root["sw1"];
_sw2status = _root["sw2"];
_sw3status = _root["sw3"];
_sw4status = _root["sw4"];
_sw5status = _root["sw5"];
switch(_sw){
case 1:
return _sw1status;
break;
case 2:
return _sw2status;
break;
case 3:
return _sw3status;
break;
case 4:
return _sw4status;
break;
case 5:
return _sw5status;
break;
}
}
}
//Connect to dashboard and read status each of specified number slider
float IoTtweetESP32::ReadAnalogSlider(const char *userid, const char *key, uint8_t slider)
{
//Transfer variable
_userid = userid;
_key = key;
_slider = slider;
//Create connection TCP to api.iottweet.com
WiFiClient client;
if (client.connect(IoTtweet_HOST, IoTtweet_PORT))
{
//Start sending data package
_str = "GET /read/?userid=";
_str += _userid;
_str += "&key=";
_str += _key;
_str += " HTTP/1.1\r\n";
_str += "Host: api.iottweet.com\r\n";
_str += "Connection: keep-alive\r\n\r\n";
//Push data to api.iottweet.com
client.print(_str);
delay(LAG_TIME);
//Check response back
while(client.available()){
_controlpanelstatus = client.readStringUntil('\r');
}
//--------- Start parse json message ---------------
StaticJsonBuffer<2000> jsonBuffer;
JsonObject& _root = jsonBuffer.parseObject(_controlpanelstatus);
if (!_root.success())
{
Serial.println("parseObject() failed");
}
_sl1status = _root["slide1"];
f_sl1status = String(_sl1status).toFloat();
_sl2status = _root["slide2"];
f_sl2status = String(_sl2status).toFloat();
_sl3status = _root["slide3"];
f_sl3status = String(_sl3status).toFloat();
switch(_slider){
case 1:
return f_sl1status;
break;
case 2:
return f_sl2status;
break;
case 3:
return f_sl3status;
break;
}
}
}
//Get IoTtweet library version information
String IoTtweetESP32::getVersion()
{
return IoTtweet_libVersion;
}
/*
library for IoTtweet.com, using via api.iottweet.com
Compatible use with ESP32 WiFi and BLE chip from espressif.
Created by : IoTtweet tech. team
Date : 2016.Oct.15
*/
#ifndef IoTtweetESP32_h
#define IoTtweetESP32_h
#include "Arduino.h"
#include "WiFi.h"
#include "ArduinoJson.h"
class IoTtweetESP32
{
public:
bool begin(const char *ssid, const char *passw);
String WriteDashboard(const char *userid, const char *key, float slot0, float slot1, float slot2, float slot3, String tw, String twpb);
String ReadControlPanel(const char *userid, const char *key);
String ReadDigitalSwitch(const char *userid, const char *key, uint8_t sw);
String getVersion();
float ReadAnalogSlider(const char *userid, const char *key, uint8_t slider);
private:
const char *_ssid, *_passw;
const char *_userid, *_key;
String _tw, _twpb;
float _slot0, _slot1, _slot2, _slot3;
const char *_libversion;
String _str, _response, _controlpanelstatus;
const char *_sw1status, *_sw2status, *_sw3status, *_sw4status, *_sw5status, *_sl1status, *_sl2status, *_sl3status;
float f_sl1status, f_sl2status, f_sl3status;
String _allcontrol;
uint8_t _sw, _slider;
};
#endif
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// The MIT License (MIT)
//
// Copyright (c) 2019 Sergey Zawislak
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2019
//
// ,
// ( ), ,
// , , , , , ,
// / , , , :
//
// .
//
// ܻ, - , ,
// , , .
// - , , , , ,
// - .
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <Arduino.h>
#ifndef Oregon_TM_h
#define Oregon_TM_h
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This Arduino code is for receive and transmit data using Oregon Scientific RF protocol version 2.1 and 3.0.
//
// Last updated: 14 October 2019
//
// The folowed sensors data format are supported.
//
// Receive and transmit:
// THGN132N (THGR122N, THGN123N),
// RTGN318,
// THGR810.
// Receive only:
// THN132N,
// WGR800,
// UVN800.
//
// Aslo supported self-developed sensors. Please contact author for additional infromation.
//
// This file is part of the Arduino OREGON_NR library.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// The MIT License (MIT)
//
// Copyright (c) 2019 Sergey Zawislak
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Oregon Scientific v2.1 v3.0
//
// 14 2019
//
//
//
// :
// THGN132N (THGR122N, THGN123N),
// RTGN318,
// THGR810.
// :
// THN132N,
// WGR800,
// UVN800.
//
// ( )
//
// - OREGON_NR
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2019
//
// ,
// ( ), ,
// , , , , , ,
// / , , , :
//
// .
//
// ܻ, - , ,
// , , .
// - , , , , ,
// - .
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define TR_TIME 488
#define TWOTR_TIME 976
#define PULSE_SHORTEN_2 80
#define PULSE_SHORTEN_3 120
#define THGN132 0x1D20
#define THGR810 0xF824
#define RTGN318 0xDCC3
#define THP 0x5500
static byte TX_PIN = 4;
static const int buffersize = 10;
class Oregon_TM
{
public:
byte SendBuffer[buffersize];
byte protocol = 2;
word sens_type = 0x0000;
int timing_corrector2 = 4;
int timing_corrector3 = 2;
Oregon_TM(byte);
Oregon_TM();
void setType(word);
void setChannel( byte);
void setId(byte);
void setBatteryFlag(bool);
void setStartCount(byte);
void setTemperature(float);
void setHumidity(byte);
void setComfort(float, byte);
bool transmit();
void SendPacket();
void setErrorTHP();
void setPressureTHP(float);
void setTemperatureTHP(float);
void setBatteryTHP(word);
void setChannelTHP(byte);
void setHumidityTHP(float);
private:
void sendZero(void);
void sendOne(void);
void sendMSB(const byte);
void sendLSB(const byte);
void sendData();
void sendOregon();
void sendPreamble();
void calculateAndSetChecksum132();
void calculateAndSetChecksum318();
void calculateAndSetChecksum810();
void calculateAndSetChecksumTHP();
unsigned long time_marker = 0;
unsigned long time_marker_send = 0;
unsigned long send_time = 0;
bool prevbit = 1;
bool prevstate = 1;
};
#endif
# Oregon_NR
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This Arduino code is for receive and transmit data using Oregon Scientific RF protocol version 2.1 and 3.0.
//
// Last updated: 14 Оctober 2019
//
// The folowed sensors data format are supported.
//
// Receive and transmit:
// THGN132N (THGR122N, THGN123N),
// RTGN318,
// THGR810.
// Receive only:
// THN132N,
// WGR800,
// UVN800.
//
// Aslo supported self-developed sensors. Please contact author for additional infromation.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Данная библиотека Ардуино предназначена для приема и передачи данных в формате беспроводного протокола Oregon Scientific v2.1 и v3.0
//
// Последнее обновление 14 Октября 2019
//
// Поддерживается формат следующих датчиков
//
// Приём и передача:
// THGN132N (THGR122N, THGN123N),
// RTGN318,
// THGR810.
// Тольок приём:
// THN132N,
// WGR800,
// UVN800.
//
// Также поддерживаются датчики собственной разработки (за дополнительной документацей обращаться к автору)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Код приёмника протестирован на оригинальных датчиках THGN132N, THN132N и WGR800.
//
// Код передатчика протетстирована на погодных станциях BAR206, BAR208 эмуляцией сигнала THGN132N
// Для успешного приёма погодной станцией сигнала необходимо соблюдать следующие условия при передаче данных:
// Влажность 2-98%
// Температура -50...+70С
// При создании энергосберегающих датчиков с режимом "глубокого сна" нужно учесть, что интервалы между пакетами для успешного приёма погодной станцией
// должны отличаться от номинальных не более чем на +-1сек. Например для THGN132:
// Канал 1 - 39 (38 - 40) c
// Канал 2 - 41 (40 - 42) c
// Канал 3 - 43 (42 - 44) c
//
// Если пришёл пакет с корректной CRC и контрольной суммой, но значение температуры и влажности некорректные, например +3.0С переданы не как 0300, а A200
// то датчик может быть заблокирован до смены ID или до сброса погодной станциии.
// Блокировка навсегда возможна и при неправильном сочетании номера канала и ID датчика. Этот вопрос пока до конца не изучен
//
// Передача сигналов в формате RTGN318 и THGR810 до конца не протестирована. Поэтому возможны проблемы с приёмом этих сигналов погодной станцией
// на отдельных каналах
\ No newline at end of file
#include <Wire.h>
#include <Oregon_TM.h>
#include <BME280I2C.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Скетч для устройства, передающего данные датчика BME280 в формате Oregon Scientific THGN132N
//Принципиальная схема прилагается.
//Для работы необходима библиотека https://github.com/finitespace/BME280/
//Устройство работает от 3-ех пальчиковых батареек, для экономии электричества заливать скетч нужно через ISP
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Также возможна передача данных в формате - THP (температура, влажность, давление, напряжение батареи)
//Пример с приёмником поддерживает расшифоовку THP
////////////////////////////////////////////////////////////////////////////////////////////////////////////
# define THGN_SEND 0 // Передавать ли данные в формате THGN132
# define THP_SEND 1 // Передавать ли данные в формате THP
# define DEVICE_LOG 0 //Писать ли лог В Serial
# define DONE_PIN 15 // вывод сигнала об окончании работы на таймер
# define BME_WAIT 10 // Сколько мс ожидать датчик BME
# define BATTERY_THR 3.5 // Порог напряжения для выставляения флага разряда батарейки (THGN)
/////////////////////////////////////////////////////////////////////////////////////////////////
//Ниблы датчика THP
//Во всех полях младшие ниблы идут вперёд!!!
// 1-2 - тип (55)
// 3 - канал (0-7)
// 4-6 - (температура от -100С) * 10. Т.е. +25.1С = 1251 = 4E3h
// 7-9 - Влажность *10 Т.е. 25.1% = 251 = 0FBh
// 10-12 - (давление от 500ммртст) * 10. Т.е. 765мм = 2650 = A5Ah
// 13-15 - данные с АЦП (A0)
// 16-17 - CheckSUM
// 18-19 - CRC8 (poly 0x07 start 0x00)
/////////////////////////////////////////////////////////////////////////////////////////////////
Oregon_TM transmitter(4);
BME280I2C bme;
bool bme_present = false;
float bme_temp(NAN), bme_hum(NAN), bme_pres(NAN);
/////////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
digitalWrite(DONE_PIN, LOW);
pinMode(DONE_PIN, OUTPUT);
#ifdef DEVICE_LOG
Serial.begin(115200);
Serial.println("Waiting for BMEsensor...");
#endif
//Обмен данными с BME//////////////////////////////////
Wire.begin();
while(!bme.begin())
{
if (millis() > BME_WAIT) break;
}
if (!bme.begin())
{
#ifdef DEVICE_LOG
Serial.println("No BME sensor found");
#endif
bme_present = false;
}
else
{
switch(bme.chipModel())
{
case BME280::ChipModel_BME280:
bme_present = true;
bme.read(bme_pres, bme_temp, bme_hum);
#ifdef DEVICE_LOG
Serial.println("Found BME280 sensor! Success.");
Serial.print("Temperature = ");
Serial.print(bme_temp, 1);
Serial.println("C");
Serial.print("Humidity = ");
Serial.print(bme_hum, 1);
Serial.println("%");
Serial.print("Pressure = ");
Serial.print(bme_pres * 0.75, 1);
Serial.println("mmHg");
#endif
break;
default:
#ifdef DEVICE_LOG
Serial.println("Found UNKNOWN sensor! Error!");
#endif
bme_present = false;
}
}
//Напряжения батареи///////////////////////////////////////////
word battvotage = (word)(((float)(1.1 * 16368) / Vbg()) * 100);
#ifdef DEVICE_LOG
Serial.print("Battery voltage = ");
Serial.println(battvotage,HEX);
#endif
//Подготовка и отправка данных THGN//////////////////////////////////////
transmitter.protocol == 2;
if (THGN_SEND)
{
transmitter.setType(THGN132);
transmitter.setChannel(3);
transmitter.setBatteryFlag(battvotage < BATTERY_THR);
if (bme_present)
{
if (bme_hum > 98) bme_hum = 98;
if (bme_hum < 2) bme_hum = 2;
if (bme_temp > 70) bme_temp = 70;
if (bme_temp < -50) bme_temp = -50;
transmitter.setTemperature(bme_temp);
transmitter.setHumidity(bme_hum);
transmitter.setComfort(bme_temp, bme_hum);
}
else
{
transmitter.setTemperature(-49.9);
transmitter.setHumidity(2);
transmitter.setComfort(-49.9, 2);
}
transmitter.SendPacket();
}
// Если отправляются оба формата пакетов, межу ними надо выдержать паузу
if (THP_SEND && THGN_SEND) delay(100);
//Подготовка и отправка данных THP//////////////////////////////////////
if (THP_SEND)
{
transmitter.setType(THP);
transmitter.setChannelTHP(1);
transmitter.setBatteryTHP( battvotage);
if (bme_present)
{
transmitter.setTemperatureTHP(bme_temp);
transmitter.setHumidityTHP(bme_hum);
transmitter.setPressureTHP(bme_pres * 0.75); // перевод Pa в mmHg
}
else
{
transmitter.setErrorTHP();
}
transmitter.SendPacket();
}
#ifdef DEVICE_LOG
Serial.println();
Serial.print(millis());
Serial.println("ms");
Serial.println();
#endif
//Команда на отключение питания
digitalWrite(DONE_PIN, HIGH);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void loop(){}
/////////////////////////////////////////////////////////////////////////////////////////////////
int Vbg() {
ADMUX = (1<<REFS0)|(0<<REFS1)|(1<<MUX3)|(1<<MUX2)|(1<<MUX1)|(0<<MUX0);
long buffersamp=0;
for (int n=0x0; n<=0xff; n++ ) {
ADCSRA = 0xc7;
while (bit_is_set(ADCSRA,ADSC));
buffersamp += ADC; }
buffersamp >>=4; //16368 full scale 14bit
ADCSRA &= ~(1 << ADEN); // отключаем АЦП
return buffersamp;
}
#include <Oregon_NR.h>
//#include <Oregon_TR.h>
#define UNO
#ifdef UNO
//Oregon_NR oregon(2, 0, 13); // Для Arduino UNO/Nano - приёмник на выводе D2, Прерывание 0, Светодиод приёма на вывод 13
Oregon_NR oregon(2, 0); // Если светодиод не нужен
#endif
#ifdef WEMOS_D1
Oregon_NR oregon(13, 13, 2, true); // для Wemos D1 - датчик на выводе D7 (GPIO13), Светодиод на D2 подтянут к +пит.
#endif
void setup() {
Serial.begin(115200);
//вкючение прослушивания радиоканала
oregon.start();
oregon.receiver_dump = 0; //true - Включает "осциллограф" - отображение данных, полученных с приёмника
//Если не распознаются последние байты принятого пакета можно попробовать подстроить частоту захвата данных
oregon.timing_correction = 0; // коррекция частоты завхвата данных. Обычно достаоточно подобрать значение от -5 до 5
oregon.decode_method = 3; //или использовать метод 3 для декодирования
}
void loop() {
//////////////////////////////////////////////////////////////////////
//Захват пакета,/////////////////////////////////////////////////////
oregon.capture(0); // 1 - выводить в Serial сервисную информацию
//Захваченные данные годны до следующего вызова capture
//ОБработка полученного пакета//////////////////////////////////////////////
if (oregon.captured) {
//Вывод информации в Serial
Serial.print ((float) millis() / 1000, 1); //Время
Serial.print ("s\t\t");
//Версия протокола
if (oregon.ver == 2) Serial.print(" ");
if (oregon.ver == 3) Serial.print("3 ");
//Информация о восстановлени пакета
if (oregon.restore_sign & 0x01) Serial.print("s"); //восстановлены одиночные такты
else Serial.print(" ");
if (oregon.restore_sign & 0x02) Serial.print("d"); //восстановлены двойные такты
else Serial.print(" ");
if (oregon.restore_sign & 0x04) Serial.print("p "); //исправленна ошибка при распознавании версии пакета
else Serial.print(" ");
//Вывод полученного пакета. Точки - это ниблы, содержащие сомнительные биты
for (int q = 0;q < PACKET_LENGTH - 1; q++)
if (oregon.valid_p[q] == 0x0F) Serial.print(oregon.packet[q], HEX);
else Serial.print(".");
//Время обработки пакета
Serial.print(" ");
Serial.print(oregon.work_time);
Serial.print("ms ");
if ((oregon.sens_type == THGN132 || (oregon.sens_type & 0x0FFF) == RTGN318 || oregon.sens_type == THGR810 || oregon.sens_type == THN132) && oregon.crc_c){
Serial.print("\t");
Serial.print(" TYPE: ");
if (oregon.sens_type == THGN132) Serial.print("THGN132N");
if (oregon.sens_type == THGR810) Serial.print("THGR810 ");
if ((oregon.sens_type & 0x0FFF) == RTGN318) Serial.print("RTGN318");
if (oregon.sens_type == THN132) Serial.print("THN132N ");
Serial.print(" CHNL: ");
Serial.print(oregon.sens_chnl);
if (oregon.sens_tmp >= 0 && oregon.sens_tmp < 10) Serial.print(" TMP: ");
if (oregon.sens_tmp < 0 && oregon.sens_tmp >-10) Serial.print(" TMP: ");
if (oregon.sens_tmp <= -10) Serial.print(" TMP:");
if (oregon.sens_tmp >= 10) Serial.print(" TMP: ");
Serial.print(oregon.sens_tmp, 1);
Serial.print("C ");
if (oregon.sens_type == THGN132 || oregon.sens_type == THGR810 || (oregon.sens_type & 0x0FFF) == RTGN318) {
Serial.print("HUM: ");
Serial.print(oregon.sens_hmdty, 0);
Serial.print("%");
}
else Serial.print(" ");
Serial.print(" BAT: ");
if (oregon.sens_battery) Serial.print("F "); else Serial.print("e ");
Serial.print("ID: ");
Serial.print(oregon.sens_id, HEX);
}
if (oregon.sens_type == WGR800 && oregon.crc_c){
Serial.print("\t");
float wnddata;
Serial.print(" TYPE: ");
Serial.print("WGR800");
Serial.print(" AVG WS: ");
Serial.print(oregon.sens_avg_ws, 1);
Serial.print("m/s MAX WS: ");
Serial.print(oregon.sens_max_ws, 1);
Serial.print("m/s WDIR: "); //N = 0, E = 4, S = 8, W = 12
Serial.print(oregon.sens_wdir);
Serial.print(" BAT: ");
if (oregon.sens_battery) Serial.print("F "); else Serial.print("e ");
Serial.print("ID: ");
Serial.print(oregon.sens_id, HEX);
}
if (oregon.sens_type == UVN800 && oregon.crc_c){
Serial.print("\t");
float wnddata;
Serial.print(" TYPE: ");
Serial.print("UVN800");
Serial.print(" UV IDX: ");
Serial.print(oregon.UV_index);
Serial.print(" BAT: ");
if (oregon.sens_battery) Serial.print("F "); else Serial.print("e ");
Serial.print("ID: ");
Serial.print(oregon.sens_id, HEX);
}
#if ADD_SENS_SUPPORT == 1
if ((oregon.sens_type & 0xFF00) == THP && oregon.crc_c) {
Serial.print("\t");
Serial.print(" TYPE: ");
Serial.print("THP");
Serial.print(" CHNL: ");
Serial.print(oregon.sens_chnl);
if (oregon.sens_tmp > 0 && oregon.sens_tmp < 10) Serial.print(" TMP: ");
if (oregon.sens_tmp < 0 && oregon.sens_tmp > -10) Serial.print(" TMP: ");
if (oregon.sens_tmp <= -10) Serial.print(" TMP:");
if (oregon.sens_tmp >= 10) Serial.print(" TMP: ");
Serial.print(oregon.sens_tmp, 1);
Serial.print("C ");
Serial.print("HUM: ");
Serial.print(oregon.sens_hmdty, 1);
Serial.print("% ");
Serial.print("PRESS: ");
Serial.print(oregon.sens_pressure, 1);
Serial.print("Hgmm");
Serial.print(" BAT: ");
Serial.print(oregon.sens_voltage, 2);
Serial.print("V");
}
#endif
Serial.println();
}
}
\ No newline at end of file
#include <Oregon_NR.h>
#include <Oregon_TM.h>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// В данном примере описан ретранслятор пакетов
// Может быть полезен для увеличения дальности приёма сигналов от некоторых датчиков
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Oregon_NR oregon(2, 0); // Приёмник на D2, прерывание 0
Oregon_TM transmitter(4); // Передатчик на D4
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
oregon.start(); // Включаем приёмник
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
oregon.capture(0);
if (oregon.captured) {
for (int q = 0;q < PACKET_LENGTH; q++) Serial.print(oregon.packet[q], HEX);
//Проверяем, нужно ли этот пакет ретранслировать
//Например, нам нужно ретранслировать пакет, если первые два нибла пакета 19h (датчик WGR800)
if (oregon.packet[0] == 0x01 && oregon.packet[1] == 0x09)
{
Serial.println(" -> ");
delay(500);
//Подготавливаем буфер передачи
for ( int q = 0;q < 10; q++){
transmitter.SendBuffer[q] = oregon.packet[q*2+1] + oregon.packet[q*2]*16;
}
//Будем передавать в том же протоколе, что и приняли
transmitter.protocol = oregon.ver;
//Передаём данные
transmitter.SendPacket();
}
else Serial.println(' ');
}
}
#include <Oregon_TM.h>
//Имитатор датчиков температуры и влажности Oregon Scientific
//В данном примере иммитируются пакеты следующих типов датчиков:
//THGN132N (тип 1D20h, протокол v2, 3 канала)
//RTGN318 (тип ХСС3h, протокол v2, 5 каналов)
//THGR810 (тип F824h, протокол v3, 10 каналов)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Вывод передатчика один. Указывается только при создании первого объекта. В данном примере передатчик подключен к D4
Oregon_TM transmitter(4), transmitter2, transmitter3;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
Serial.begin(115200);
//Первый передатчик
transmitter.setType(THGN132);
transmitter.setChannel(3); // Номер канала для THGN132 - 1...3
transmitter.setBatteryFlag(1); // Флаг разряженной батарейки
transmitter.setTemperature(24.2); // -50C...+70C
transmitter.setHumidity(13); // 2...98%
transmitter.setComfort(24.2, 13); // Расчёт передаваемого индекса комфорта
//Второй передатчик
transmitter2.setType(THGR810);
transmitter2.setChannel(1); // Номер канала для THGR810 - 1...10 (Возможно приниматься базой будут тольок первые 5 каналов. Проверить мне не на чем...)
transmitter2.setBatteryFlag(1);
transmitter2.setTemperature(+49.9);
transmitter2.setHumidity(98);
transmitter2.setComfort(+49.9, 98);
//Третий передатчик
transmitter3.setType(RTGN318);
transmitter3.setChannel(5); // Номер канала для RTGN318 - 1...5.
transmitter3.setBatteryFlag(0);
transmitter3.setTemperature(-31);
transmitter3.setHumidity(50);
transmitter3.setComfort(-31,50);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
//передача осуществляется по таймеру, который определяется типом датчика и номером канала
if (transmitter.transmit()) PrintSentData(transmitter.SendBuffer);
if (transmitter2.transmit()) PrintSentData(transmitter2.SendBuffer);
if (transmitter3.transmit()) PrintSentData(transmitter3.SendBuffer);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void PrintSentData(byte* buf)
{
Serial.print(millis() / 1000);
Serial.print("s \t\t");
for (byte i = 0; i < 10; ++i)
{
byte trmbuf = *buf;
Serial.print(trmbuf >> 4, HEX);
if (i < (10 - 1) )
Serial.print(trmbuf & 0x0F, HEX);
buf++;
}
Serial.println();
}
Oregon_NR KEYWORD1
start KEYWORD2
stop KEYWORD2
capture KEYWORD2
sens_type KEYWORD3
sens_tmp KEYWORD3
sens_hmdty KEYWORD3
sens_chnl KEYWORD3
sens_id KEYWORD3
sens_battery KEYWORD3
ver KEYWORD3
crc_c KEYWORD3
captured KEYWORD3
work_time KEYWORD3
packet KEYWORD3
valid_p KEYWORD3
packets_received KEYWORD3
received_CRC KEYWORD3
catch2 KEYWORD3
catch3 KEYWORD3
sens_avg_ws KEYWORD3
sens_max_ws; KEYWORD3
sens_wdir KEYWORD3
UV_index KEYWORD3
lightness KEYWORD3
restore_sign KEYWORD3
receiver_dump KEYWORD3
THGN132 LITERAL1
THN132 LITERAL1
RTGN318 LITERAL1
THGR810 LITERAL1
WGR800 LITERAL1
UVN800 LITERAL1
Oregon_TM KEYWORD1
SendBuffer KEYWORD3
protocol KEYWORD3
setType KEYWORD3
setChannel KEYWORD3
setId KEYWORD3
setBatteryFlag KEYWORD3
setTemperature KEYWORD3
setHumidity KEYWORD3
setComfort KEYWORD3
transmit KEYWORD3
SendPacket KEYWORD3
timing_correction KEYWORD3
decode_method KEYWORD3
\ No newline at end of file
name=Oregon_NR
version=19.10.11
author=Sergey Zawislak <invandy@mail.ru>
maintainer=Sergey Zawislak <invandy@mail.ru>
sentence=This code is for receive and transmit data using Oregon Scientific RF protocol version 2.1 and 3.0.
paragraph=
category=Sensors
url=https://github.com/invandy/Oregon_NR
architectures=*
MIT License
Copyright (c) 2018 Joao Lopes
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
\ No newline at end of file
////////
// Library: Remote debug - debug over WiFi - for Esp8266 (NodeMCU) or ESP32
// Author : Joao Lopes
// File : RemoteDebug_Basic.ino
// Notes :
//
// Attention: This library is only for help development. Please not use this in production
//
// First sample to show how to use it - basic one
//
// Example of use:
//
//#ifndef DEBUG_DISABLED
// if (Debug.isActive(Debug.<level>)) { // <--- This is very important to reduce overheads and work of debug levels
// Debug.printf("bla bla bla: %d %s\n", number, str);
// Debug.println("bla bla bla");
// }
//#endif
//
// Or short way (prefered if only one debug at time)
//
// debugA("This is a any (always showed) - var %d", var);
//
// debugV("This is a verbose - var %d", var);
// debugD("This is a debug - var %d", var);
// debugI("This is a information - var %d", var);
// debugW("This is a warning - var %d", var);
// debugE("This is a error - var %d", var);
//
// debugV("This is println");
//
//
///////
////// Defines
// Host name (please change it)
#define HOST_NAME "remotedebug"
// Board especific libraries
#if defined ESP8266 || defined ESP32
// Use mDNS ? (comment this do disable it)
#define USE_MDNS true
// Arduino OTA (uncomment this to enable)
//#define USE_ARDUINO_OTA true
#else
// RemoteDebug library is now only to Espressif boards,
// as ESP32 and ESP82266,
// If need for another WiFi boards,
// please add an issue about this
// and we will see if it is possible made the port for your board.
// access: https://github.com/JoaoLopesF/RemoteDebug/issues
#error "The board must be ESP8266 or ESP32"
#endif // ESP
//////// Libraries
#if defined ESP8266
// Includes of ESP8266
#include <ESP8266WiFi.h>
#ifdef USE_MDNS
#include <DNSServer.h>
#include <ESP8266mDNS.h>
#endif
#elif defined ESP32
// Includes of ESP32
#include <WiFi.h>
#ifdef USE_MDNS
#include <DNSServer.h>
#include "ESPmDNS.h"
#endif
#endif // ESP
// Remote debug over WiFi - not recommended for production, only for development
#include "RemoteDebug.h" //https://github.com/JoaoLopesF/RemoteDebug
RemoteDebug Debug;
// SSID and password
const char* ssid = "........";
const char* password = "........";
// Time
uint32_t mLastTime = 0;
uint32_t mTimeSeconds = 0;
////// Setup
void setup() {
// Initialize the Serial (use only in setup codes)
Serial.begin(230400);
// Buildin led of ESP
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
// Debug
Serial.println("**** Setup: initializing ...");
// WiFi connection
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());
// Register host name in WiFi and mDNS
String hostNameWifi = HOST_NAME;
hostNameWifi.concat(".local");
#ifdef ESP8266 // Only for it
WiFi.hostname(hostNameWifi);
#endif
#ifdef USE_MDNS // Use the MDNS ?
if (MDNS.begin(HOST_NAME)) {
Serial.print("* MDNS responder started. Hostname -> ");
Serial.println(HOST_NAME);
}
MDNS.addService("telnet", "tcp", 23);
#endif
// Initialize RemoteDebug
Debug.begin(HOST_NAME); // Initialize the WiFi server
Debug.setResetCmdEnabled(true); // Enable the reset command
Debug.showProfiler(true); // Profiler (Good to measure times, to optimize codes)
Debug.showColors(true); // Colors
// End off setup
Serial.println("* Arduino RemoteDebug Library");
Serial.println("*");
Serial.print("* WiFI connected. IP address: ");
Serial.println(WiFi.localIP());
Serial.println("*");
Serial.println("* Please use the telnet client (telnet for Mac/Unix or putty and others for Windows)");
Serial.println("* or the RemoteDebugApp (in browser: http://joaolopesf.net/remotedebugapp)");
Serial.println("*");
Serial.println("* This sample will send messages of debug in all levels.");
Serial.println("*");
Serial.println("* Please try change debug level in client (telnet or web app), to see how it works");
Serial.println("*");
}
void loop()
{
// Each second
if ((millis() - mLastTime) >= 1000) {
// Time
mLastTime = millis();
mTimeSeconds++;
// Blink the led
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
// Debug the time (verbose level)
debugV("* Time: %u seconds (VERBOSE)", mTimeSeconds);
if (mTimeSeconds % 5 == 0) { // Each 5 seconds
// Debug levels
debugV("* This is a message of debug level VERBOSE");
debugD("* This is a message of debug level DEBUG");
debugI("* This is a message of debug level INFO");
debugW("* This is a message of debug level WARNING");
debugE("* This is a message of debug level ERROR");
// Call a function
foo();
}
}
// RemoteDebug handle
Debug.handle();
// Give a time for ESP
yield();
}
// Function example to show a new auto function name of debug* macros
void foo() {
uint8_t var = 1;
debugV("this is a debug - var %u", var);
debugV("This is a println");
}
/////////// End
{
"name": "RemoteDebug",
"keywords": "Debug, Remote, Telnet, WiFi",
"description": "A library for Arduino to debug projects over WiFi, with web app or telnet client, with Print commands like Serial Monitor.",
"repository":
{
"type": "git",
"url": "https://github.com/JoaoLopesF/RemoteDebug.git"
},
"version": "3.0.5",
"frameworks": "arduino",
"platforms": "*"
}
name=RemoteDebug
version=3.0.5
author=Joao Lopes
maintainer=Joao Lopes
sentence=A library for Arduino to debug projects over WiFi, with web app or telnet client, with Print commands like Serial Monitor.
paragraph=
category=Communication
url=https://github.com/JoaoLopesF/RemoteDebug
architectures=*
/*
* Header for RemoteDebugCfg
*
* MIT License
*
* Copyright (c) 2019 Joao Lopes
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
/*
* All configurations have moved to RemoteDebugCfg.h,
* to facilitate changes
*/
///////////// User config, to not lost in library updates
#ifndef REMOTEDEBUGCFG_H_
#define REMOTEDEBUGCFG_H_
#pragma once
///////////// For RemoteDebug ///////////////////
///// Debug disable for compile to production/release
///// as nothing of RemotedDebug is compiled, zero overhead :-)
//#define DEBUG_DISABLED true
// Debug enabled ?
#ifndef DEBUG_DISABLED
///// Port for telnet server
#define TELNET_PORT 23
// Disable auto function for debug macros? (uncomment this if not want this)
//#define DEBUG_DISABLE_AUTO_FUNC true
// Simple password request - left commented if not need this - 18/07/18
// Notes:
// It is very simple feature, only text, no cryptography,
// and the password is echoed in screen (I not discovery yet how disable it)
// telnet use advanced authentication (kerberos, etc.)
// Such now as RemoteDebug now is not for production releases,
// this kind of authentication will not be done now.
// Can be by project, just call setPassword method
#define REMOTEDEBUG_PWD_ATTEMPTS 3
// Maximum time for inactivity (em milliseconds)
// Default: 10 minutes
// Comment it if you not want this
// Can be by project, just define it before include this file
#define MAX_TIME_INACTIVE 600000
// Buffered print write to WiFi -> length of buffer
// Can be by project, just define it before include this file
#define BUFFER_PRINT 150
// Should the help text be displayed on connection.
// Enabled by default, comment to disable
#define SHOW_HELP true
// Buffering (sends in interval of time to avoid ESP misterious delays)
// Uncomment this to disable it
#define CLIENT_BUFFERING true
#ifdef CLIENT_BUFFERING
#define DELAY_TO_SEND 10 // Time to send buffer
#define MAX_SIZE_SEND 1460 // Maximum size of packet (limit of TCP/IP)
#endif
// Enable if you test features yet in development
//#define ALPHA_VERSION true
// Debugger support enabled ?
// Comment this to disable it
#define DEBUGGER_ENABLED true
#ifdef DEBUGGER_ENABLED
#define DEBUGGER_HANDLE_TIME 850 // Interval to call handle of debugger - equal to implemented in debugger
// App have debugger elements on screen ?
// Note: app not have it yet
//#define DEBUGGER_SEND_INFO true
#endif
///// Websocket server to support debug over web browser (RemoteDebugApp)
// Uncomment this to disable it
//#define WEBSOCKET_DISABLED true
///////////// For RemoteDebugWS ///////////////////
#ifndef WEBSOCKET_DISABLED
// Websocket port
#define WEBSOCKET_PORT 8232
// Library arduinoWebSockets already installed, uncomment to use it
// Do this if you receive errors of multiple definition ...
//#define USE_LIB_WEBSOCKET true
#endif
///////////// For RemoteDebugger ///////////////////
// Enable Flash variables support - F()
// Used internally in SerialDebug and in public API
// If is a low memory board, like AVR, all strings in SerialDebug is using flash memory
// If have RAM memory, this is more fast than flash
//#define DEBUG_USE_FLASH_F true
// For Espressif boards, default is not flash support for printf,
// due it have a lot of memory and Serial.printf is not compatible with it
// If you need more memory, can force it:
//#define DEBUG_USE_FLASH_F true
#endif /* DEBUG_DISABLED */
#endif /* REMOTEDEBUGCFG_H_ */
////// End
/*
* Libraries Arduino
* *****************
* Library : Remote debug - debug over telnet - for Esp8266 (NodeMCU) or ESP32
* Author : Joao Lopes
* File : RemoteDebugWS - web socket server to RemoteDebugAapp
* Comments: Web socket server uses the arduinWebSockets library (https://github.com/Links2004/arduinoWebSockets)
* The author uses Eclise IDE (sloeber) to made this source
* License : See RemoteDebugWS.h
*
* Versions:
* ------ ---------- -----------------
* 0.2.0 2019-03-19 All public configurations (#defines) have moved to RemoteDebugCfg.h, to facilitate changes for anybody.
* Several adjustments.
* 0.1.0 2019-03-11 Fist version
*
*/
/* TODO:
*/
///// RemoteDebug configuration
#include "RemoteDebugCfg.h"
// Debug enabled ?
#ifndef DEBUG_DISABLED
/////// Includes
#include "RemoteDebugWS.h"
// Only if web socket (RemoteDebugApp) is enabled
#ifndef WEBSOCKET_DISABLED
#include "Arduino.h"
#ifdef USE_LIB_WEBSOCKET // This library already installed
#include <WebSockets.h> // https://github.com/Links2004/arduinoWebSockets
#include <WebSocketsClient.h>
#include <WebSocketsServer.h>
#else // local copy
#include "utility/WebSockets.h" // https://github.com/Links2004/arduinoWebSockets
#include "utility/WebSocketsClient.h"
#include "utility/WebSocketsServer.h"
#endif
#include "RemoteDebug.h"
/////// Defines
// Version
#define REMOTEDEBUGWS_VERSION "0.1.1"
// Internal debug macro - recommended stay disable
#define D(fmt, ...) // Without this
//#define D(fmt, ...) Serial.printf("rdws: " fmt "\n", ##__VA_ARGS__) // Serial debug
/////// Variables
// Arduino websocket server (to comunicate with RemoteDebugApp)
static WebSocketsServer WebSocketServer(WEBSOCKET_PORT); // Websocket server on port 81
// Variables used by webSocketEvent
static int8_t _webSocketConnected = WS_NOT_CONNECTED; // Client is connected ?
static RemoteDebugWSCallbacks* _callbacks; // callbacks to RemoteDebug
////// Methods
// Initialize the socket server
void RemoteDebugWS::begin(RemoteDebugWSCallbacks* callbacks) {
// Initialize web socket (RemoteDebugApp)
WebSocketServer.begin(); // start the websocket server
WebSocketServer.onEvent(webSocketEvent); // if there's an incomming websocket message, go to function 'webSocketEvent'
// Set callbacks
_callbacks = callbacks;
// Debug
D("socket server started");
}
// Finalize the socket server
void RemoteDebugWS::stop() {
// Finalize web socket (RemoteDebugApp)
WebSocketServer.close();
_webSocketConnected = WS_NOT_CONNECTED;
}
void RemoteDebugWS::disconnectAllClients() {
// Disconnect all clients
WebSocketServer.disconnect();
// Disconnected
_webSocketConnected = WS_NOT_CONNECTED;
// Callback
if (_callbacks) {
_callbacks->onDisconnect();
}
}
void RemoteDebugWS::disconnect() {
// Disconnect actual clients
if (_webSocketConnected != WS_NOT_CONNECTED) {
WebSocketServer.disconnect(_webSocketConnected);
// Disconnected
_webSocketConnected = WS_NOT_CONNECTED;
// Callback
if (_callbacks) {
_callbacks->onDisconnect();
}
}
}
// Handle
void RemoteDebugWS::handle() {
WebSocketServer.loop();
// // Test
//
// if (_webSocketConnected) {
// WebSocketServer.sendTXT(_webSocketConnected, "AAAAAAAAA\n");
// }
}
// Is connected ?
boolean RemoteDebugWS::isConnected() {
return (_webSocketConnected != WS_NOT_CONNECTED);
}
// Print
size_t RemoteDebugWS::write(const uint8_t *buffer, size_t size) {
// Process buffer
for(size_t i=0; i<size; i++) {
write((uint8_t) buffer[i]);
}
return size;
}
size_t RemoteDebugWS::write(uint8_t character) {
static String buffer = "";
size_t ret = 0;
// Process
if (character == '\n') {
if (_webSocketConnected != WS_NOT_CONNECTED) {
D("write send buf[%u]: %s", buffer.length(), buffer.c_str());
WebSocketServer.sendTXT(_webSocketConnected, buffer.c_str(), buffer.length());
}
// Empty the buffer
ret = buffer.length();
buffer = "";
} else if (character != '\r' && isPrintable(character)) {
// Append
buffer.concat((char)character);
}
// Return
return ret;
}
// Destructor
RemoteDebugWS::~RemoteDebugWS() {
// Stop
stop();
}
/////// WebSocket to comunicate with RemoteDebugApp
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t payloadlength) { // When a WebSocket message is received
int blk_count = 0;
char ipaddr[26];
IPAddress localip;
switch (type) {
case WStype_DISCONNECTED: // if the websocket is disconnected
D("[%u] Disconnected!", num);
// Disconnected
_webSocketConnected = WS_NOT_CONNECTED;
// Callback
if (_callbacks) {
_callbacks->onDisconnect();
}
break;
case WStype_CONNECTED: // if a new websocket connection is established
{
#ifdef D // serial debug
IPAddress ip = WebSocketServer.remoteIP(num);
D("[%u] Connected from %d.%d.%d.%d url: %s", num, ip[0], ip[1], ip[2], ip[3],
payload);
#endif
// Have in another connection
// One connection to reduce overheads
if (num != _webSocketConnected) {
WebSocketServer.sendTXT(_webSocketConnected, "* Closing client connection ...");
WebSocketServer.disconnect(_webSocketConnected);
}
// Set as connected
_webSocketConnected = num;
// Send initial message
WebSocketServer.sendTXT(_webSocketConnected, "$app:I");
// Callback
if (_callbacks) {
_callbacks->onConnect();
}
}
break;
case WStype_TEXT: // if new text data is received
{
// if (payloadlength == 0) {
// return;
// }
D("[%u] get Text: %s", num, payload);
// Callback
if (_callbacks) {
_callbacks->onReceive((const char*)payload);
}
}
break;
case WStype_ERROR: // if new text data is received
D("Error");
// Disconnected
_webSocketConnected = WS_NOT_CONNECTED;
// Callback
if (_callbacks) {
_callbacks->onDisconnect();
}
break;
default:
// Disconnected
_webSocketConnected = WS_NOT_CONNECTED;
// Callback
if (_callbacks) {
_callbacks->onDisconnect();
}
D("WStype %x not handled ", type);
}
}
#endif // WEBSOCKET_DISABLED
#endif // DEBUG_DISABLED
/// End
/*
* Header for RemoteDebugWS
*
* Copyright (C) 2018 Joao Lopes
* MIT License
*
* Copyright (c) 2019 Joao Lopes https://github.com/JoaoLopesF/RemoteDebug
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#ifndef REMOTEDEBUGWS_H_
#define REMOTEDEBUGWS_H_
#pragma once
///// RemoteDebug configuration
#include "RemoteDebugCfg.h"
// Debug enabled ?
#ifndef DEBUG_DISABLED
// Only if Web socket enabled (RemoteDebugApp)
#ifndef WEBSOCKET_DISABLED
///////// Defines
// Connected
#define WS_NOT_CONNECTED -1 // Not connected
//////// Includes
#ifdef USE_LIB_WEBSOCKET // This library already installed
#include <WebSockets.h> // https://github.com/Links2004/arduinoWebSockets
#include <WebSocketsClient.h>
#include <WebSocketsServer.h>
#else // local copy
#include "utility/WebSockets.h" // https://github.com/Links2004/arduinoWebSockets
#include "utility/WebSocketsClient.h"
#include "utility/WebSocketsServer.h"
#endif
///// Callbacks class - based in Kolban BLE callback example code
class RemoteDebugWSCallbacks {
public:
virtual ~RemoteDebugWSCallbacks() {}
virtual void onConnect() = 0;
virtual void onDisconnect() = 0;
virtual void onReceive(const char* message) = 0;
};
///// Main Class
class RemoteDebugWS: public Print
{
public:
// Constructor
//RemoteDebugWS();
// Methods
void begin(RemoteDebugWSCallbacks* callbacks);
void stop();
void disconnectAllClients();
void disconnect();
boolean isConnected();
void handle();
// Print
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *buffer, size_t size);
// Destructor
~RemoteDebugWS();
private:
//////// Variables
//////// Privates
};
/////// Prototypes
// Websocket (RemoteDebugApp)
// Note: in is out of class, do not cause errors on onEvent
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t payloadlength);
#endif // WEBSOCKET_DISABLED
#else
#error "aaaah"
#endif // DEBUG_DISABLED
#endif /* REMOTEDEBUGWS_H_ */
///// End
/*
* telnet.h - Telnet Defines
*
* Note: only used is uncommented
* */
#ifndef TELNET_H
#define TELNET_TELNET_H
#define TELNET_IAC 255
#define TELNET_DONT 254
#define TELNET_DO 253
#define TELNET_WONT 252
#define TELNET_WILL 251
// #define TELNET_SE 240 // Subnegotiation End
// #define TELNET_NOP 241 // No Operation
// #define TELNET_DM 242 // Data Mark
// #define TELNET_BRK 243 // Break
// #define TELNET_IP 244 // Interrupt process
// #define TELNET_AO 245 // Abort output
// #define TELNET_AYT 246 // Are You There
// #define TELNET_EC 247 // Erase Character
// #define TELNET_EL 248 // Erase Line
//#define TELNET_GA 249 // Go Ahead
// #define TELNET_SB 250 // Subnegotiation Begin
// #define TELNET_BINARY 0 // 8-bit data path
#define TELNET_ECHO 1 // echo
// #define TELNET_RCP 2 // prepare to reconnect
#define TELNET_SGA 3 // suppress go ahead
// #define TELNET_NAMS 4 // approximate message size
// #define TELNET_STATUS 5 // give status
// #define TELNET_TM 6 // timing mark
// #define TELNET_RCTE 7 // remote controlled transmission and echo
// #define TELNET_NAOL 8 // negotiate about output line width
// #define TELNET_NAOP 9 // negotiate about output page size
// #define TELNET_NAOCRD 10 // negotiate about CR disposition
// #define TELNET_NAOHTS 11 // negotiate about horizontal tabstops
// #define TELNET_NAOHTD 12 // negotiate about horizontal tab disposition
// #define TELNET_NAOFFD 13 // negotiate about formfeed disposition
// #define TELNET_NAOVTS 14 // negotiate about vertical tab stops
// #define TELNET_NAOVTD 15 // negotiate about vertical tab disposition
// #define TELNET_NAOLFD 16 // negotiate about output LF disposition
// #define TELNET_XASCII 17 // extended ascii character set
// #define TELNET_LOGOUT 18 // force logout
// #define TELNET_BM 19 // byte macro
// #define TELNET_DET 20 // data entry terminal
// #define TELNET_SUPDUP 21 // supdup protocol
// #define TELNET_SUPDUPOUTPUT 22 // supdup output
// #define TELNET_SNDLOC 23 // send location
// #define TELNET_TTYPE 24 // terminal type
// #define TELNET_EOR 25 // end or record
// #define TELNET_TUID 26 // TACACS user identification
// #define TELNET_OUTMRK 27 // output marking
// #define TELNET_TTYLOC 28 // terminal location number
// #define TELNET_VT3270REGIME 29 // 3270 regime
// #define TELNET_X3PAD 30 // X.3 PAD
// #define TELNET_NAWS 31 // window size
// #define TELNET_TSPEED 32 // terminal speed
// #define TELNET_LFLOW 33 // remote flow control
// #define TELNET_LINEMODE 34 // Linemode option
// #define TELNET_XDISPLOC 35 // X Display Location
// #define TELNET_OLD_ENVIRON 36 // Old - Environment variables
// #define TELNET_AUTHENTICATION 37 // Authenticate
// #define TELNET_ENCRYPT 38 // Encryption option
// #define TELNET_NEW_ENVIRON 39 // New - Environment variables
// #define TELNET_TN3270E 40 // TN3270E
// #define TELNET_XAUTH 41 // XAUTH
// #define TELNET_CHARSET 42 // CHARSET
// #define TELNET_RSP 43 // Telnet Remote Serial Port
// #define TELNET_COM_PORT_OPTION 44 // Com Port Control Option
#define TELNET_SUPPRESS_LOCAL_ECHO 45 // Telnet Suppress Local Echo
// #define TELNET_TLS 46 // Telnet Start TLS
// #define TELNET_KERMIT 47 // KERMIT
// #define TELNET_SEND_URL 48 // SEND-URL
// #define TELNET_FORWARD_X 49 // FORWARD_X
// #define TELNET_PRAGMA_LOGON 138 // TELOPT PRAGMA LOGON
// #define TELNET_SSPI_LOGON 139 // TELOPT SSPI LOGON
// #define TELNET_PRAGMA_HEARTBEAT 140 // TELOPT PRAGMA HEARTBEAT
// #define TELNET_EXOPL 255 // Extended-Options-List
// #define TELNET_NOOPT 0
// #define TELNET_IS 0
// #define TELNET_SEND 1
#endif // TELNET_H
/**
* @file WebSockets.h
* @date 20.05.2015
* @author Markus Sattler
*
* Copyright (c) 2015 Markus Sattler. All rights reserved.
* This file is part of the WebSockets for Arduino.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef WEBSOCKETS_H_
#define WEBSOCKETS_H_
#ifdef STM32_DEVICE
#include <application.h>
#define bit(b) (1UL << (b)) // Taken directly from Arduino.h
#else
#include <Arduino.h>
#include <IPAddress.h>
#endif
#ifdef ARDUINO_ARCH_AVR
#error Version 2.x.x currently does not support Arduino with AVR since there is no support for std namespace of c++.
#error Use Version 1.x.x. (ATmega branch)
#else
#include <functional>
#endif
#ifndef NODEBUG_WEBSOCKETS
#ifdef DEBUG_ESP_PORT
#define DEBUG_WEBSOCKETS(...) DEBUG_ESP_PORT.printf( __VA_ARGS__ )
#else
//#define DEBUG_WEBSOCKETS(...) os_printf( __VA_ARGS__ )
#endif
#endif
#ifndef DEBUG_WEBSOCKETS
#define DEBUG_WEBSOCKETS(...)
#define NODEBUG_WEBSOCKETS
#endif
#if defined(ESP8266) || defined(ESP32)
#define WEBSOCKETS_MAX_DATA_SIZE (15*1024)
#define WEBSOCKETS_USE_BIG_MEM
#define GET_FREE_HEAP ESP.getFreeHeap()
// moves all Header strings to Flash (~300 Byte)
//#define WEBSOCKETS_SAVE_RAM
#elif defined(STM32_DEVICE)
#define WEBSOCKETS_MAX_DATA_SIZE (15*1024)
#define WEBSOCKETS_USE_BIG_MEM
#define GET_FREE_HEAP System.freeMemory()
#else
//atmega328p has only 2KB ram!
#define WEBSOCKETS_MAX_DATA_SIZE (1024)
// moves all Header strings to Flash
#define WEBSOCKETS_SAVE_RAM
#endif
#define WEBSOCKETS_TCP_TIMEOUT (2000)
#define NETWORK_ESP8266_ASYNC (0)
#define NETWORK_ESP8266 (1)
#define NETWORK_W5100 (2)
#define NETWORK_ENC28J60 (3)
#define NETWORK_ESP32 (4)
// max size of the WS Message Header
#define WEBSOCKETS_MAX_HEADER_SIZE (14)
#if !defined(WEBSOCKETS_NETWORK_TYPE)
// select Network type based
#if defined(ESP8266) || defined(ESP31B)
#define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP8266
//#define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP8266_ASYNC
//#define WEBSOCKETS_NETWORK_TYPE NETWORK_W5100
#elif defined(ESP32)
#define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP32
#else
#define WEBSOCKETS_NETWORK_TYPE NETWORK_W5100
#endif
#endif
// Includes and defined based on Network Type
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
// Note:
// No SSL/WSS support for client in Async mode
// TLS lib need a sync interface!
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#include <WiFiClientSecure.h>
#elif defined(ESP31B)
#include <ESP31BWiFi.h>
#else
#error "network type ESP8266 ASYNC only possible on the ESP mcu!"
#endif
#include <ESPAsyncTCP.h>
#include <ESPAsyncTCPbuffer.h>
#define WEBSOCKETS_NETWORK_CLASS AsyncTCPbuffer
#define WEBSOCKETS_NETWORK_SERVER_CLASS AsyncServer
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
#if !defined(ESP8266) && !defined(ESP31B)
#error "network type ESP8266 only possible on the ESP mcu!"
#endif
#ifdef ESP8266
#include <ESP8266WiFi.h>
#else
#include <ESP31BWiFi.h>
#endif
#define WEBSOCKETS_NETWORK_CLASS WiFiClient
#define WEBSOCKETS_NETWORK_SERVER_CLASS WiFiServer
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_W5100)
#ifdef STM32_DEVICE
#define WEBSOCKETS_NETWORK_CLASS TCPClient
#define WEBSOCKETS_NETWORK_SERVER_CLASS TCPServer
#else
#include <Ethernet.h>
#include <SPI.h>
#define WEBSOCKETS_NETWORK_CLASS EthernetClient
#define WEBSOCKETS_NETWORK_SERVER_CLASS EthernetServer
#endif
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_ENC28J60)
#include <UIPEthernet.h>
#define WEBSOCKETS_NETWORK_CLASS UIPClient
#define WEBSOCKETS_NETWORK_SERVER_CLASS UIPServer
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
#include <WiFi.h>
#include <WiFiClientSecure.h>
#define WEBSOCKETS_NETWORK_CLASS WiFiClient
#define WEBSOCKETS_NETWORK_SERVER_CLASS WiFiServer
#else
#error "no network type selected!"
#endif
// moves all Header strings to Flash (~300 Byte)
#ifdef WEBSOCKETS_SAVE_RAM
#define WEBSOCKETS_STRING(var) F(var)
#else
#define WEBSOCKETS_STRING(var) var
#endif
typedef enum {
WSC_NOT_CONNECTED,
WSC_HEADER,
WSC_CONNECTED
} WSclientsStatus_t;
typedef enum {
WStype_ERROR,
WStype_DISCONNECTED,
WStype_CONNECTED,
WStype_TEXT,
WStype_BIN,
WStype_FRAGMENT_TEXT_START,
WStype_FRAGMENT_BIN_START,
WStype_FRAGMENT,
WStype_FRAGMENT_FIN,
} WStype_t;
typedef enum {
WSop_continuation = 0x00, ///< %x0 denotes a continuation frame
WSop_text = 0x01, ///< %x1 denotes a text frame
WSop_binary = 0x02, ///< %x2 denotes a binary frame
///< %x3-7 are reserved for further non-control frames
WSop_close = 0x08, ///< %x8 denotes a connection close
WSop_ping = 0x09, ///< %x9 denotes a ping
WSop_pong = 0x0A ///< %xA denotes a pong
///< %xB-F are reserved for further control frames
} WSopcode_t;
typedef struct {
bool fin;
bool rsv1;
bool rsv2;
bool rsv3;
WSopcode_t opCode;
bool mask;
size_t payloadLen;
uint8_t * maskKey;
} WSMessageHeader_t;
typedef struct {
uint8_t num; ///< connection number
WSclientsStatus_t status;
WEBSOCKETS_NETWORK_CLASS * tcp;
bool isSocketIO; ///< client for socket.io server
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
bool isSSL; ///< run in ssl mode
WiFiClientSecure * ssl;
#endif
String cUrl; ///< http url
uint16_t cCode; ///< http code
bool cIsClient = false; ///< will be used for masking
bool cIsUpgrade; ///< Connection == Upgrade
bool cIsWebsocket; ///< Upgrade == websocket
String cSessionId; ///< client Set-Cookie (session id)
String cKey; ///< client Sec-WebSocket-Key
String cAccept; ///< client Sec-WebSocket-Accept
String cProtocol; ///< client Sec-WebSocket-Protocol
String cExtensions; ///< client Sec-WebSocket-Extensions
uint16_t cVersion; ///< client Sec-WebSocket-Version
uint8_t cWsRXsize; ///< State of the RX
uint8_t cWsHeader[WEBSOCKETS_MAX_HEADER_SIZE]; ///< RX WS Message buffer
WSMessageHeader_t cWsHeaderDecode;
String base64Authorization; ///< Base64 encoded Auth request
String plainAuthorization; ///< Base64 encoded Auth request
String extraHeaders;
bool cHttpHeadersValid; ///< non-websocket http header validity indicator
size_t cMandatoryHeadersCount; ///< non-websocket mandatory http headers present count
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
String cHttpLine; ///< HTTP header lines
#endif
} WSclient_t;
class WebSockets {
protected:
#ifdef __AVR__
typedef void (*WSreadWaitCb)(WSclient_t * client, bool ok);
#else
typedef std::function<void(WSclient_t * client, bool ok)> WSreadWaitCb;
#endif
virtual void clientDisconnect(WSclient_t * client) = 0;
virtual bool clientIsConnected(WSclient_t * client) = 0;
virtual void messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin) = 0;
void clientDisconnect(WSclient_t * client, uint16_t code, char * reason = NULL, size_t reasonLen = 0);
bool sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * payload = NULL, size_t length = 0, bool fin = true, bool headerToPayload = false);
void headerDone(WSclient_t * client);
void handleWebsocket(WSclient_t * client);
bool handleWebsocketWaitFor(WSclient_t * client, size_t size);
void handleWebsocketCb(WSclient_t * client);
void handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t * payload);
String acceptKey(String & clientKey);
String base64_encode(uint8_t * data, size_t length);
bool readCb(WSclient_t * client, uint8_t *out, size_t n, WSreadWaitCb cb);
virtual size_t write(WSclient_t * client, uint8_t *out, size_t n);
size_t write(WSclient_t * client, const char *out);
};
#ifndef UNUSED
#define UNUSED(var) (void)(var)
#endif
#endif /* WEBSOCKETS_H_ */
/**
* @file WebSocketsClient.h
* @date 20.05.2015
* @author Markus Sattler
*
* Copyright (c) 2015 Markus Sattler. All rights reserved.
* This file is part of the WebSockets for Arduino.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef WEBSOCKETSCLIENT_H_
#define WEBSOCKETSCLIENT_H_
#include "WebSockets.h"
class WebSocketsClient: private WebSockets {
public:
#ifdef __AVR__
typedef void (*WebSocketClientEvent)(WStype_t type, uint8_t * payload, size_t length);
#else
typedef std::function<void (WStype_t type, uint8_t * payload, size_t length)> WebSocketClientEvent;
#endif
WebSocketsClient(void);
virtual ~WebSocketsClient(void);
void begin(const char *host, uint16_t port, const char * url = "/", const char * protocol = "arduino");
void begin(String host, uint16_t port, String url = "/", String protocol = "arduino");
void begin(IPAddress host, uint16_t port, const char * url = "/", const char * protocol = "arduino");
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
void beginSSL(const char *host, uint16_t port, const char * url = "/", const char * = "", const char * protocol = "arduino");
void beginSSL(String host, uint16_t port, String url = "/", String fingerprint = "", String protocol = "arduino");
#endif
void beginSocketIO(const char *host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
void beginSocketIO(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
void beginSocketIOSSL(const char *host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
void beginSocketIOSSL(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
#endif
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
void loop(void);
#else
// Async interface not need a loop call
void loop(void) __attribute__ ((deprecated)) {}
#endif
void onEvent(WebSocketClientEvent cbEvent);
bool sendTXT(uint8_t * payload, size_t length = 0, bool headerToPayload = false);
bool sendTXT(const uint8_t * payload, size_t length = 0);
bool sendTXT(char * payload, size_t length = 0, bool headerToPayload = false);
bool sendTXT(const char * payload, size_t length = 0);
bool sendTXT(String & payload);
bool sendBIN(uint8_t * payload, size_t length, bool headerToPayload = false);
bool sendBIN(const uint8_t * payload, size_t length);
bool sendPing(uint8_t * payload = NULL, size_t length = 0);
bool sendPing(String & payload);
void disconnect(void);
void setAuthorization(const char * user, const char * password);
void setAuthorization(const char * auth);
void setExtraHeaders(const char * extraHeaders = NULL);
void setReconnectInterval(unsigned long time);
protected:
String _host;
uint16_t _port;
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
String _fingerprint;
#endif
WSclient_t _client;
WebSocketClientEvent _cbEvent;
unsigned long _lastConnectionFail;
unsigned long _reconnectInterval;
void messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin);
void clientDisconnect(WSclient_t * client);
bool clientIsConnected(WSclient_t * client);
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
void handleClientData(void);
#endif
void sendHeader(WSclient_t * client);
void handleHeader(WSclient_t * client, String * headerLine);
void connectedCb();
void connectFailedCb();
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
void asyncConnect();
#endif
/**
* called for sending a Event to the app
* @param type WStype_t
* @param payload uint8_t *
* @param length size_t
*/
virtual void runCbEvent(WStype_t type, uint8_t * payload, size_t length) {
if(_cbEvent) {
_cbEvent(type, payload, length);
}
}
};
#endif /* WEBSOCKETSCLIENT_H_ */
/**
* @file WebSocketsServer.h
* @date 20.05.2015
* @author Markus Sattler
*
* Copyright (c) 2015 Markus Sattler. All rights reserved.
* This file is part of the WebSockets for Arduino.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef WEBSOCKETSSERVER_H_
#define WEBSOCKETSSERVER_H_
#include "WebSockets.h"
#ifndef WEBSOCKETS_SERVER_CLIENT_MAX
#define WEBSOCKETS_SERVER_CLIENT_MAX (5)
#endif
class WebSocketsServer: protected WebSockets {
public:
#ifdef __AVR__
typedef void (*WebSocketServerEvent)(uint8_t num, WStype_t type, uint8_t * payload, size_t length);
typedef bool (*WebSocketServerHttpHeaderValFunc)(String headerName, String headerValue);
#else
typedef std::function<void (uint8_t num, WStype_t type, uint8_t * payload, size_t length)> WebSocketServerEvent;
typedef std::function<bool (String headerName, String headerValue)> WebSocketServerHttpHeaderValFunc;
#endif
WebSocketsServer(uint16_t port, String origin = "", String protocol = "arduino");
virtual ~WebSocketsServer(void);
void begin(void);
void close(void);
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
void loop(void);
#else
// Async interface not need a loop call
void loop(void) __attribute__ ((deprecated)) {}
#endif
void onEvent(WebSocketServerEvent cbEvent);
void onValidateHttpHeader(
WebSocketServerHttpHeaderValFunc validationFunc,
const char* mandatoryHttpHeaders[],
size_t mandatoryHttpHeaderCount);
bool sendTXT(uint8_t num, uint8_t * payload, size_t length = 0, bool headerToPayload = false);
bool sendTXT(uint8_t num, const uint8_t * payload, size_t length = 0);
bool sendTXT(uint8_t num, char * payload, size_t length = 0, bool headerToPayload = false);
bool sendTXT(uint8_t num, const char * payload, size_t length = 0);
bool sendTXT(uint8_t num, String & payload);
bool broadcastTXT(uint8_t * payload, size_t length = 0, bool headerToPayload = false);
bool broadcastTXT(const uint8_t * payload, size_t length = 0);
bool broadcastTXT(char * payload, size_t length = 0, bool headerToPayload = false);
bool broadcastTXT(const char * payload, size_t length = 0);
bool broadcastTXT(String & payload);
bool sendBIN(uint8_t num, uint8_t * payload, size_t length, bool headerToPayload = false);
bool sendBIN(uint8_t num, const uint8_t * payload, size_t length);
bool broadcastBIN(uint8_t * payload, size_t length, bool headerToPayload = false);
bool broadcastBIN(const uint8_t * payload, size_t length);
bool sendPing(uint8_t num, uint8_t * payload = NULL, size_t length = 0);
bool sendPing(uint8_t num, String & payload);
bool broadcastPing(uint8_t * payload = NULL, size_t length = 0);
bool broadcastPing(String & payload);
void disconnect(void);
void disconnect(uint8_t num);
void setAuthorization(const char * user, const char * password);
void setAuthorization(const char * auth);
int connectedClients(bool ping = false);
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
IPAddress remoteIP(uint8_t num);
#endif
protected:
uint16_t _port;
String _origin;
String _protocol;
String _base64Authorization; ///< Base64 encoded Auth request
String * _mandatoryHttpHeaders;
size_t _mandatoryHttpHeaderCount;
WEBSOCKETS_NETWORK_SERVER_CLASS * _server;
WSclient_t _clients[WEBSOCKETS_SERVER_CLIENT_MAX];
WebSocketServerEvent _cbEvent;
WebSocketServerHttpHeaderValFunc _httpHeaderValidationFunc;
bool _runnning;
bool newClient(WEBSOCKETS_NETWORK_CLASS * TCPclient);
void messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin);
void clientDisconnect(WSclient_t * client);
bool clientIsConnected(WSclient_t * client);
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
void handleNewClients(void);
void handleClientData(void);
#endif
void handleHeader(WSclient_t * client, String * headerLine);
/**
* called if a non Websocket connection is coming in.
* Note: can be override
* @param client WSclient_t * ptr to the client struct
*/
virtual void handleNonWebsocketConnection(WSclient_t * client) {
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] no Websocket connection close.\n", client->num);
client->tcp->write("HTTP/1.1 400 Bad Request\r\n"
"Server: arduino-WebSocket-Server\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: 32\r\n"
"Connection: close\r\n"
"Sec-WebSocket-Version: 13\r\n"
"\r\n"
"This is a Websocket server only!");
clientDisconnect(client);
}
/**
* called if a non Authorization connection is coming in.
* Note: can be override
* @param client WSclient_t * ptr to the client struct
*/
virtual void handleAuthorizationFailed(WSclient_t *client) {
client->tcp->write("HTTP/1.1 401 Unauthorized\r\n"
"Server: arduino-WebSocket-Server\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: 45\r\n"
"Connection: close\r\n"
"Sec-WebSocket-Version: 13\r\n"
"WWW-Authenticate: Basic realm=\"WebSocket Server\""
"\r\n"
"This Websocket server requires Authorization!");
clientDisconnect(client);
}
/**
* called for sending a Event to the app
* @param num uint8_t
* @param type WStype_t
* @param payload uint8_t *
* @param length size_t
*/
virtual void runCbEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
if(_cbEvent) {
_cbEvent(num, type, payload, length);
}
}
/*
* Called at client socket connect handshake negotiation time for each http header that is not
* a websocket specific http header (not Connection, Upgrade, Sec-WebSocket-*)
* If the custom httpHeaderValidationFunc returns false for any headerName / headerValue passed, the
* socket negotiation is considered invalid and the upgrade to websockets request is denied / rejected
* This mechanism can be used to enable custom authentication schemes e.g. test the value
* of a session cookie to determine if a user is logged on / authenticated
*/
virtual bool execHttpHeaderValidation(String headerName, String headerValue) {
if(_httpHeaderValidationFunc) {
//return the value of the custom http header validation function
return _httpHeaderValidationFunc(headerName, headerValue);
}
//no custom http header validation so just assume all is good
return true;
}
private:
/*
* returns an indicator whether the given named header exists in the configured _mandatoryHttpHeaders collection
* @param headerName String ///< the name of the header being checked
*/
bool hasMandatoryHeader(String headerName);
};
#endif /* WEBSOCKETSSERVER_H_ */
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!