Skip to content

Instantly share code, notes, and snippets.

View pgsamila's full-sized avatar
:octocat:
W I P

Amila Sampath pgsamila

:octocat:
W I P
View GitHub Profile
boolean isJavaFun = true;
System.out.println(isJavaFun);// Outputs true
Math.max(50, 100);
Math.min(50, 100);
Math.sqrt(100);
Math.random();
int num1 = 200 + 50; // 250 (200 + 50)
int num2 = num1 + 250; // 500 (250 + 250)
int num3 = num2 + num2; // 1000 (500 + 500)
int myInt = 9;
double myDouble = myInt;
// This is a single line comment
System.out.println("Hello World");
System.out.println("Hello World"); // This is a single comment
/* This is a multiple line comment,
The code below will print the words Hello World to the screen, and it is amazing */
System.out.println("Hello World");
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
void setup() {
// Initialize NodeMCU
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void reconnect() {
// Loop until we’ re reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
@pgsamila
pgsamila / gist:fac96596c4b59458701de598387fc9c3
Created May 1, 2021 05:55
MQTT subscribe callback function
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Do something with node MCU