Skip to content

Commit

Permalink
Merge pull request #2 from Clovel/research
Browse files Browse the repository at this point in the history
[Research] Web example, ESP, ESP example code, electronics
  • Loading branch information
Clovel authored Sep 4, 2019
2 parents cf4d0dd + 35536bd commit d943087
Show file tree
Hide file tree
Showing 11 changed files with 847 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,16 @@ src_file_list*.txt
# pkg-config generated files --------------------
*.pc

# Binary files ----------------------------------
a.out
*.exe
*.dll
*.dylib
*.a
*.so

web-example
!web-example/

# macOS files -----------------------------------
.DS_Store
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# Let There Be Light !

Connected lamp project

# Ressources & Links

https://www.instructables.com/id/Controlling-AC-light-using-Arduino-with-relay-modu/
https://tttapa.github.io/ESP8266/Chap01%20-%20ESP8266.html
https://forum.arduino.cc/index.php?topic=590593.0
https://developer.myconstellation.io/tutorials/creer-une-prise-connectee-avec-un-esp8266/

___
Binary file added elec/logic-level-converter.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added meca/base-drawing.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions research/esp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# ESP

## Model

### ESP8266
Has
- 17 GPIO pins, but 6 of these pins (6-11) are used for communication with the on-board flash memory chip. So 11 GPIO pins
- WiFi Communication
- An analog input

#### Price
3$-6$ according to https://www.makeradvisor.com

#### Where to buy in France

15.99€ for 3 boards (5.33€ per board) :
- https://www.amazon.fr/AZ-Delivery-NodeMCU-ESP8266-développement-development/dp/B074Q2WM1Y/ref=sr_1_3?__mk_fr_FR=ÅMÅŽÕÑ&keywords=NodeMCU&qid=1567073501&s=gateway&sr=8-3

### ESP32
More expensive, more functionnalities, may be overkill.

## Programming

Several option are available :
- Arduino IDE
- SDK for C-programming
- LUA Interpreter firmware
- MicroPython firmware

The easiest method seems to be the Arduino IDE method.

## Requirements
- ESP8266 board
- A computer that can run Arduino IDE (Windows, Mac or Linux)
- A USB-To-Serial converter. It must be a **3.3V** model
- A USB Cable
- A 3.3V Power supply or voltage regulator\*
- A WiFi network to connect to

(\*) Your board may already include these.
3 changes: 3 additions & 0 deletions research/example1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Example 1

Code taken from : https://arduino.stackexchange.com/questions/36330/how-to-make-a-5-volt-relay-work-with-nodemcu
125 changes: 125 additions & 0 deletions research/example1/example1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

const char *ssid = "said.";
const char *password = "secret";
ESP8266WebServer server(80);
const int led = 13;
const int outputLed = 12;

void turnOnRelayOne(void)
{
digitalWrite(outputLed, 1);
digitalWrite(5, HIGH); //GPIO 5 // Relay 1
server.send(200, "text/html", "Relay 1 turned on.");
digitalWrite(outputLed, 0);
}

void turnOnRelayTwo(void)
{
digitalWrite(outputLed, 1);
digitalWrite(4, HIGH); //GPIO 4 // Relay 2
server.send(200, "text/html", "Relay 2 turned on.");
digitalWrite(outputLed, 0);
}

void handleRoot()
{
digitalWrite(led, 1);

char temp[400];
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
int h = dht.readHumidity();
int t = dht.readTemperature();

snprintf(temp, 400,
"<html>\
<head>\
<meta http-equiv='refresh' content='5'/>\
<title>NodeMCU hooked to Relay Board</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h1>Hello from NodeMCU!</h1>\
<p>Uptime: %02d:%02d:%02d</p>\
</body>\
</html>",

hr, min % 60, sec % 60
);

server.send(200, "text/html", temp);
digitalWrite(led, 0);
}

void handleNotFound(void)
{
digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (HTTP_GET == server.method()) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";

for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}

server.send(404, "text/plain", message);
digitalWrite(led, 0);
}

void setup(void)
{
pinMode(led, OUTPUT);
pinMode(10, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(outputLed, OUTPUT);
digitalWrite(led, 0);
Serial.begin(9600);
WiFi.begin(ssid, password);
Serial.println("");

// Wait for connection
while (WL_CONNECTED != WiFi.status()) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}

server.on("/", handleRoot);

server.on("/inline", [] () {
server.send(200, "text/plain", "this works as well");
});

server.on("/relay1", turnOnRelayOne);
server.on("/relay2", turnOnRelayTwo);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}

void loop(void)
{
server.handleClient();
}
7 changes: 7 additions & 0 deletions research/web-example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
all: web-example

web-example: main.cxx remoteRequest.cxx
g++ main.cxx remoteRequest.cxx -o web-example

clean:
rm web-example
128 changes: 128 additions & 0 deletions research/web-example/main.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* Copyright Clovis Durand
* Let There Be Light project
*
* @file main.cxx
* @brief This file is a test for running a minimalist
* web page to change a variable in a C/C++ program.
*/

/* Includes -------------------------------------------- */
#include "remoteRequest.hxx"

/* C++ System */
#include <iostream>

/* C Networking */
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>

#include <pthread.h>

#include <unistd.h> /* cloe(int fd) */

/* Defines --------------------------------------------- */
#define MAX_REQUEST_NB 5U

/* Variable declarations ------------------------------- */
std::string sProgName;
static int sVar = 0;
std::string sWebStr = "HTTP/1.0 200 Ok\r\n"
"Constant_Type: text/html\r\n"
"\r\n"
"<html lang=\"en\">\r\n"
" <head>\r\n"
" <title>web-example</title>\r\n"
" </head>\r\n"
" <body>\r\n"
" Hello there !\r\nGeneral Kenobi !\r\n"
" </body>\r\n"
"</html>\r\n";

/* Support funtions ------------------------------------ */
void usage(void) {
std::cout << "[USAGE] " << sProgName << " usage : " <<std ::endl;
std::cout << " " << sProgName << " <port>" << std::endl;
}

/* Main function --------------------------------------- */
int main(const int argc, const char * const * const argv) {
std::cout << "[DEBUG] Hello there ! General Kenobi !" << std::endl << std::endl;

sProgName = std::string(argv[0]);

if(2 != argc) {
std::cerr << "[ERROR] Wrong number of arguments !" << std::cerr;
usage();
exit(EXIT_FAILURE);
}

/* Get the port number specified in the argument */
int lPort = std::atoi(argv[1]);

/* Server socket definition */
int lServerSocket = -1; /* Socket descriptor */
struct sockaddr_in lServerSocketName; /* Socket internet structure */
struct hostent *lServerHostPtr = nullptr; /* Server information */
socklen_t lServerSocketNameLen = sizeof(lServerSocketName);

/* Input socket definition */
int lClientSocket = -1;
struct sockaddr_in lClientSocketName; /* Socket internet structure */
struct hostent *lClientHostPtr = nullptr; /* Client information */
socklen_t lClientSocketNameLen = sizeof(lClientSocketName);

/* Initializing the sockaddr structure w/ formatted information */
lServerSocketName.sin_family = AF_INET; /* Address family */
lServerSocketName.sin_port = htons(lPort); /* Port */
lServerSocketName.sin_addr.s_addr = htonl(INADDR_ANY);

/* Declaring thread */
pthread_t lThread;

/* Creating the TCP socket */
if(0 > (lServerSocket = socket(AF_INET, SOCK_STREAM, 0))) {
std::cerr << "[ERROR] Failed to create the socket (lServerSocket = " << lServerSocket << ") !" << std::endl;
exit(EXIT_FAILURE);
}

/* Bind socket */
if(-1 == bind(lServerSocket, (struct sockaddr *)&lServerSocketName, sizeof(lServerSocketName))) {
std::cerr << "[ERROR] Failed to bind the socket !" << std::endl;
exit(EXIT_FAILURE);
}

/* Initializing the request queue */
listen(lServerSocket, MAX_REQUEST_NB);

/* Enter main loop */
std::cout << "[INFO ] Server is listening..." << std::endl;
int lError = 0;
while(true) {
/* Get client socket */
lClientSocket = accept(lServerSocket, (struct sockaddr *)&lClientSocketName, &lClientSocketNameLen);

if(-1 == lClientSocket) {
std::cerr << "[ERROR] Failed to \"accept\" client socket" << std::endl;
exit(EXIT_FAILURE);
}

lError = acceptRequest(lClientSocket, &sVar);
if(0 != lError) {
std::cerr << "[ERROR] Failed to process remote request w/ acceptRequest !" << std::endl;
} else {
std::cout << "[DEBUG] Processed acceptRequest successfully !" << std::endl;
}
}

/* Printout the web page contents */
std::cout << "[DEBUG] Web page contents : " << std::endl << sWebStr << std::endl;

/* Close the socket */
close(lServerSocket);

std::cout << "[INFO ] Server ending. Bye..." << std::endl;

return EXIT_SUCCESS;
}
Loading

0 comments on commit d943087

Please sign in to comment.