ESP8266-tcpip

Migrate from https://cmakerhk.wordpress.com/2018/11/07/esp8266_tcp-ip/


Test Plan

for example:
Ubuntu Server IP: 192.168.31.217
ESP8266 IP : 192.168.31.210

Related Command on ESP8266

AT+CWMODE_DEF=3
AT+CWJAP_DEF="mySSID","myPassword"
AT+CWJAP? 
AT+CIFSR

Use Ubuntu to Create a server:
nc -l 1234
–Then waiting for the connection

ESP8266:
Setup single connection

AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.31.217",1234

AT+CIPSEND=10
>>1234567890
Recv 10 BYTE
SEND OK

+IPD,6:Hello

AT+CIPSENDEX=10   # it will send when \0 is saw or length is met


///Throughput, 
AT+CIPMODE=1
AT+CIPSEND
>


send "+++" together to break the loop

telegram-more

Migrate from https://cmakerhk.wordpress.com/2018/11/06/more-on-telegram/


https://github.com/rahiel/telegram-send#installation
easy to use to send photoes, and text back to the telegram and also right-click send file to telegram

sudo pip install telegram-send

telegram-send --configure



#paste the token and enter the password from the app >> Done

apt-get moo | telegram-send –pre –stdin
telegram-send –image photo.jpg –caption “The Moon at night”
telegram-send –file document.pdf

Use inside python script
https://www.rahielkasim.com/telegram-send/docs/api/



https://github.com/dschep/ntfy
receive notification

pip install ntfy[telegram]

((Command not found –> not working


https://core.telegram.org/bots/samples


Example used: HTTPSRequest

/*
HTTP over TLS (HTTPS) example sketch

This example demonstrates how to use
WiFiClientSecure class to access HTTPS API.
We fetch and display the status of
esp8266/Arduino project continuous integration
build.

Limitations:
only RSA certificates
no support of Perfect Forward Secrecy (PFS)
TLSv1.2 is supported since version 2.4.0-rc1

Created by Ivan Grokhotkov, 2015.
This example is in public domain.
*/

telegram:
get Info of bot: https://api.telegram.org/bot/getme

SendMessage: http://api.telegram.org/bot/sendMessage?chat_id=&text=HelloFromESP8266

ReceiveMessage: https://api.telegram.org/bot/getUpdates

On the NodeMCU: Since it is HTTPS –> I cannot find a way to used it with AT COMMAND

Programming to NodeMCU:
HTTPSRequest:

–>fingerprint[] –> inside the Chrome –> certificate –> detail –> thumb print


telegram:
get Info of bot: https://api.telegram.org/bot/getme

SendMessage: http://api.telegram.org/bot/sendMessage?chat_id=&text=HelloFromESP8266

ReceiveMessage: https://api.telegram.org/bot/getUpdates

reply a message: /sendmessage?chat_id=1234567&text=yoyo&reply_to_message_id=454
sendphoto: –> get the file id first!!!
/sendphoto?chat_id=1234567&photo=AgADBQADxagxG6PpUVdHAwvYecQlGt2C-TIABAEAAwIAA20AA4XvAgABFgQ

Sendphoto with URL:
/sendphoto?chat_id=1234567&photo=https://www.canva.com/learn/wp-content/uploads/2018/11/best-free-stock-photos-tb-2640x1485.jpg

https://stackoverflow.com/questions/33126743/how-do-i-add-my-bot-to-a-channel
sendMessage?chat_id=@test&text=Testing%20if%20this%20works

bash-socket

Migrate from https://cmakerhk.wordpress.com/2018/11/05/socket-on-shell/


http://www.tutorialspoint.com/unix_commands/nc.htm
NC: netcat

$nc -l 1234

When you enter http://localhost:1234
you will see

GET / HTTP/1.1
Host: localhost:1234
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-GB,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1

–> How to do a response?

For not a Web client

Server
nc -l 1234 >> Listen TCP on port 1234
nc 127.0.0.1 1234 >> connect to TCP port 1234

then you make a chat room xd

nc -l 1234 > filename.out
nc host.example.com 1234 >>>>>>
echo -n “GET / HTTP/1.0\r\n\r\n” | nc host.example.com 80

-v: display all the information

File Transfer
$ cat happy.txt | ncat -v -l -p 5555
Ncat: Version 5.21 ( http://nmap.org/ncat )
Ncat: Listening on 0.0.0.0:5555
$ ncat localhost 5555 > happy_copy.txt

Port Scanning
TCP
$ nc -v -n -z -w 1 127.0.0.1 8080-8088
nc: connect to 127.0.0.1 port 8080 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 8081 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 8082 (tcp) failed: Connection refused
Connection to 127.0.0.1 8083 port [tcp/*] succeeded!
nc: connect to 127.0.0.1 port 8084 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 8085 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 8086 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 8087 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 8088 (tcp) failed: Connection refused

UDP
$ nc -ul 8083 -v
Listening on [0.0.0.0] (family 0, port 8083)
Connection from localhost 50944 received!
XXXXX
$ nc -u -v -n -z 127.0.0.1 8080-8088
Connection to 127.0.0.1 8083 port [udp/*] succeeded!

“-n” parameter here prevents DNS lookup,
“-z” makes nc not receive any data from the server
“-w 1” makes the connection timeout after 1 second of inactivity

nodejs-simple-webserver

Migrate from https://cmakerhk.wordpress.com/2018/10/31/nodejs-simple-webserver/


var http = require('http');

//create a server object
http.createServer(
  function(req, res){
    res.writeHead(200,{'Content-Type':'text/html'});
    res.write('Hello World'); //write the response to the client
    res.end(); //end the response
  }
).listen(8080); // the server object listen on port 8080

http.createServer().listen(8080)

when you run that command on the node shell, it will create a server on local host and print hello world.

or node demo_http.js

The Query String

http://localhost:8080/summer
^——- the /summer is the query string

rpi-wifi-config-cli

Migrate from https://cmakerhk.wordpress.com/2018/10/29/config-wifi-on-terminal-on-pi/


Set Up Network on the Raspberry Pi

If you will use a Ethernet cable to connect your Raspberry Pi to the internet, you can skip this step.

For this section we will assume you have a Raspberry Pi 3, with built in WiFi.

Start by scanning for wireless networks:

pi@raspberrypi:~ $ sudo iwlist wlan0 scan
This will list all of the available WiFi networks. (It also confirms that your WiFi is working)

Now we need to open the wpa-supplicant file, to add the network you want to connect to:

pi@raspberrypi:~ $ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
This will open the file in the Nano editor. Add the following to the bottom of the file (change wifiName andwifiPassword with the actual network name and password):
network={ ssid="wifiName" psk="wifiPassword" }
Press "Ctrl+x" to save the code. Confirm with "y", and confirm the name with "Enter".

And reboot the Raspberry Pi:

pi@raspberrypi:~ $ sudo reboot
After reboot, log in again, and confirm that the WiFi is connected and working:
pi@raspberrypi:~ $ ifconfig wlan0
If the WiFi is working propery, the information displayed should include an IP address, similar to this:
inet addr:192.168.1.50
Write down that IP address, as we will use it to connect to the Raspberry Pi via SSH.

more-on-wifi

Migrate from https://cmakerhk.wordpress.com/2018/10/22/more-on-wifi/


RSSI: Received Signal Strength Indicator
unit: dBm and RSSI(relative index)

Signal Strength TL;DR Required for
-30 dBm Amazing Max achievable signal strength. The client can only be a few feet from the AP to achieve this. Not typical or desirable in the real world. N/A
-67 dBm Very Good Minimum signal strength for applications that require very reliable, timely delivery of data packets. VoIP/VoWiFi, streaming video
-70 dBm Okay Minimum signal strength for reliable packet delivery. Email, web
-80 dBm Not Good Minimum signal strength for basic connectivity. Packet delivery may be unreliable. N/A
-90 dBm Unusable Approaching or drowning in the noise floor. Any functionality is highly unlikely. N/A
ref: https://www.metageek.com/training/resources/understanding-rssi.html

WPS(Wi-Fi Protected Setup)

http://kisslink.com/

arduino-string

Migrate from https://cmakerhk.wordpress.com/2018/10/22/string-on-arduino/


Ref: https://www.arduino.cc/en/Tutorial/StringComparisonOperators

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Test of String");

  Serial.print("\"Hello\" == \"Hello\" : ");
  Serial.println("Hello" == "Hello");     //return 1: true

  Serial.print("\"Hello\" > \"Hell\" : ");
  Serial.println("Hello" > "Hell");     //return 0: false

  Serial.print("\"Hello\" < \"Hell\" : ");
  Serial.println("Hello" < "Hell");     //return 1: true

  Serial.print("\"AB\" < \"CD\" : ");
  Serial.println("AB" < "CD");     //return 1: true

  Serial.print("\"AB\" < \"ABCD\" : ");
  Serial.println("AB" < "ABCD");     //return 1: true

  Serial.print("\"99\" < \"100\" : ");
  Serial.println("999" < "1000");     //return 1: true

  Serial.print("99 < \"100\" : ");
  Serial.println(99 < "100");     //return 1: true
}

It doesn’t match what the website said. Conclusion: except for two identical string. Don’t use compare in String.

arduino-timer-programming

Migrate from https://cmakerhk.wordpress.com/2018/10/19/timer-programming-on-arduino/


ul millis() Returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.
ul micros() Returns the number of microseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 70 minutes. On 16 MHz Arduino boards (e.g. Duemilanove and Nano), this function has a resolution of four microseconds (i.e. the value returned is always a multiple of four). On 8 MHz Arduino boards (e.g. the LilyPad), this function has a resolution of eight microseconds
delay(ul ms) Pauses the program for the amount of time (in milliseconds) specified as parameter
delayMicroseconds(ui us) Pauses the program for the amount of time (in microseconds) specified as parameter. There are a thousand microseconds in a millisecond, and a million microseconds in a second.

Currently, the largest value that will produce an accurate delay is 16383. This could change in future Arduino releases. For delays longer than a few thousand microseconds, you should use delay() instead

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    ledState = !ledState;

    // set the LED with the ledState of the variable:
    digitalWrite(LED_BUILTIN, ledState);
  }
}

Programing Flow of Cenz

#define led_pin 13

void setup() {
  // put your setup code here, to run once:
  pinMode(led_pin, OUTPUT);
}

void loop() {
  //variable declaration
  unsigned long current_millis = millis();
  static unsigned long led_previous_millis = 0;
  static bool led_toggle_flag = false;


  //=========checkFlag()======
  if(current_millis - led_previous_millis >= 1000){
    //toggle LED function flag on
    led_toggle_flag = true;
  }

  //===========do()===========
  if(led_toggle_flag){
    led_toggle(led_pin);

    //reset the flag and timer
    led_toggle_flag = false;
    led_previous_millis = current_millis;
  }
}

void led_toggle(unsigned char pin){
  digitalWrite(pin, !digitalRead(pin));
}
#define led_pin 13

typedef struct EventRegister{
  unsigned long previous_millis;
  bool check_flag;
  unsigned long duration;
};

EventRegister ledToggle{0, false, 1000};

void setup() {
  // put your setup code here, to run once:
  pinMode(led_pin, OUTPUT);
}

void loop() {
  //variable declaration
  unsigned long current_millis = millis();

  //=========checkFlag()======
  if(current_millis - ledToggle.previous_millis >= ledToggle.duration){
    //toggle LED function flag on
    ledToggle.check_flag = true;
  }

  //===========do()===========
  if(ledToggle.check_flag){
    led_toggle(led_pin);
    //reset the flag and timer
    ledToggle.check_flag = false;
    ledToggle.previous_millis = current_millis;
  }
}

void led_toggle(unsigned char pin){
  digitalWrite(pin, !digitalRead(pin));
}

Ref:
https://www.arduino.cc/reference/en/language/functions/time/millis/
https://www.arduino.cc/reference/en/language/functions/time/micros/
https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

devcon-USB-cli

Migrate from https://cmakerhk.wordpress.com/2018/10/18/devcon/


https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon

Since the VBus And GND pin is longer, there exist even if the D+, D- is still connecting, but lost conntion. But thhe VBus, GND is still Connecting making the PC think it is still alive..

You can unplug and replug to solve this problem

Or go to device manager, disable and enable that COM

OR by the following:
Check the devcon.exe

And use the following command
Rescan: devcon restart “@USB\VID_2341&PID_0043\75633313133351019140”
get hardware: devcon hwids “@USB*” >> hardware.txt

Zip: https://mega.nz/#F!xJl00QDR!Ndk2bc2lnQtUU2842q–UQ

arduino-using-vscode

Migrate from https://cmakerhk.wordpress.com/2018/10/17/using-vscode-on-arduino/


http://skybow.pixnet.net/blog/post/118553532-%5Barduino%5D-%E4%BD%BF%E7%94%A8vscode-ide%E6%90%AD%E9%85%8D-arduino-extension%E9%80%B2%E8%A1%8C%E9%96%8B

https://www.arduino.cc/en/Guide/Linux/

Via Ubuntu Software Center or apt
WARNING!!! THESE INSTRUCTIONS WILL INSTALL AN EXTREMELY OUTDATED AND NON-STANDARD VERSION OF THE ARDUINO IDE. NOT RECOMMENDED!!!!! Open Ubuntu Software Center and search for Arduino. Alternatively, you can install via the command line by running the following in a Terminal
VSCode
F1: Quick Command