Skip to content

Instantly share code, notes, and snippets.

@skyrocknroll
Last active December 16, 2020 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skyrocknroll/61cb9fbe827ef51160d84ce238dc362a to your computer and use it in GitHub Desktop.
Save skyrocknroll/61cb9fbe827ef51160d84ce238dc362a to your computer and use it in GitHub Desktop.
[nodemcu] #oled @nodemcu
#include <Wire.h>
#include "OLED.h"
#include <ESP8266WiFi.h>
char* ssid = "Sadhu";
char* password = "xxxx";
char* host = "192.168.0.120";
//SDA -- D4
//SCL -- D5
//RST -- D2
OLED display(SDA, SCL);
void setup() {
pinMode(D2, OUTPUT);
digitalWrite(D2, LOW); // turn D2 low to reset OLED
delay(50);
digitalWrite(D2, HIGH); // while OLED is running, must set D2 in high
delay(10);
Serial.begin(9600);
Serial.println("OLED Booting Up!");
// Initialize display
display.begin();
Serial.print("Connecting to ");
display.print("Connecting to");
delay(1000);
display.print(ssid,1);
delay(1000);
WiFi.begin(ssid, password);
int i=0;
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
display.print(".",2,i++);
}
display.clear();
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
display.print("",0);
display.print("WiFi connected",1);
display.print("IP address: ",2);
String localIpStr = WiFi.localIP().toString();
char localIp_array[localIpStr.length()+1];
localIpStr.toCharArray(localIp_array, localIpStr.length()+1);
char* localIp = strtok(localIp_array, " ");
display.print(localIp,3);
}
int value = 0;
void loop() {
delay(5000);
display.clear();
Serial.print("connecting to host ");
display.print("connecting to host ");
Serial.println(host);
display.print(host,0);
display.print("port: 5678",1);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 5678;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
display.print("connection failed",2);
return;
}
String url = "/data";
Serial.print("Requesting URL: ");
Serial.println(url);
display.clear();
display.print("Requesting URL: ");
display.print("/data",1);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 10000) {
Serial.println(">>> Client Timeout !");
display.print(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
if (line == "\n") {
Serial.println("Found Body begin! ");
String line = client.readStringUntil('\r');
Serial.print("Content --> " + line);
line.trim();
char line_array[line.length()+1];
line.toCharArray(line_array, line.length()+1);
char* line_chr = strtok(line_array, "");
display.print(line_chr,3);
}
}
Serial.println();
Serial.println("closing connection");
}
package main
import (
"fmt"
"html/template"
"net/http"
"net/http/httputil"
)
type ContactDetails struct {
Name string
//Subject string
//Message string
}
var data string = "RAYS"
func main() {
tmpl := template.Must(template.ParseFiles("forms.html"))
http.HandleFunc("/form", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
tmpl.Execute(w, nil)
return
}
details := ContactDetails{
Name: r.FormValue("name"),
//Subject: r.FormValue("subject"),
//Message: r.FormValue("message"),
}
data = details.Name
tmpl.Execute(w, struct{ Data string }{data})
})
http.HandleFunc("/data", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Request received!")
requestDump, err := httputil.DumpRequest(r, true)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(requestDump))
if r.Method == http.MethodGet {
fmt.Fprint(w, data)
}
})
http.ListenAndServe(":5678", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment