Go Down

Topic: strtol naar (Read 1 time) previous topic - next topic

RBA2000

Dec 04, 2016, 11:41 pm Last Edit: Dec 06, 2016, 08:38 pm by RBA2000
Wie heeft er een idee over het volgende probleempje.
Ik ben momenteel een beetje aan het stoeien met een webserver voorbeeld.
Hier is een strtol gebruikt om waarde door te geven aan een pin.
Het werkt, echter ik zou de waarde in een int willen hebben om verder mee te werken.
Helaas ben ik dus nog niet zo goed met het vinden van de juiste manier en na vele malen proberen en vele error's zou ik graag hier willen vragen voor suggesties.

Het gaat om het volgende stukje code:

Code: [Select]
if(payload[0] == '#') {
// we get RGB data like #ff0000

// decode rgb data
uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);

analogWrite(LED_RED, map(((rgb >> 16) & 0xFF),0,255,0,1023));
analogWrite(LED_GREEN, map(((rgb >> 8) & 0xFF),0,255,0,1023));
analogWrite(LED_BLUE, map(((rgb >> 0) & 0xFF),0,255,0,1023));
}


De waarde die analogwrite(LED_RED) in deze dus als voorbeeld krijgt.

Wellicht een hele stomme vraag.
Bedankt.

nicoverduin

Je zou bijv. het volgende kunnen doen:
Code: [Select]

union {
   uint32_t rgb;
   struct {
      uint8_t blue;
      uint8_t green;
      uint8_t red;
      uint8_t notUsed;
   }
} RGB;




Hier leg je dus een template over het 32 bits getal. En dan 
Code: [Select]

uint32_t RGB.rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);

analogWrite(LED_RED, RGB.red);
analogWrite(LED_GREEN, RGB.green);
analogWrite(LED_BLUE, RGB.blue);


mappen naar 10bits heeft weinig zin omdat de analogWrites maar 8 bits zijn.
Met vriendelijke groet / kindest regards
Nico Verduin
www.verelec.nl
Do not PM me for personal consultancy unless you are willing to pay for it.

RBA2000

Bedankt voor de suggestie.
Echter heb ik, wellicht door blindstaren, verzuimd op te geven dat de code is aangepast voor een ESP8266.
De analogWrite funtie van een ESP8266 defaults is 1023 full scale dus de originele code stuurt maar 25% duty cycle max..

originele code voor Arduino.

Code: [Select]

if(payload[0] == '#') {
                // we get RGB data

                // decode rgb data
                uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);

                analogWrite(LED_RED,    ((rgb >> 16) & 0xFF));
                analogWrite(LED_GREEN,  ((rgb >> 8) & 0xFF));
                analogWrite(LED_BLUE,   ((rgb >> 0) & 0xFF));


Vandaar deze manier van mappen.
Sorry.

RBA2000

Oeps!
Code: [Select]
Arduino: 1.6.12 (Linux), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 115200, 4M (3M SPIFFS)"

/home/pi/Documents/Node mcu 8266 Pro/WebCtrl_oled_rgb7/WebCtrl_oled_rgb7.ino: In function 'void webSocketEvent(uint8_t, WStype_t, uint8_t*, size_t)':
WebCtrl_oled_rgb7:136: error: expected ';' after struct definition
    }
    ^
WebCtrl_oled_rgb7:140: error: expected initializer before '.' token
 uint32_t RGB.rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);
             ^
exit status 1
expected ';' after struct definition

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


Waarom?

nicoverduin

Zet je hele sketch ff hier neer. Dan kijken we wel ff verder.
Met vriendelijke groet / kindest regards
Nico Verduin
www.verelec.nl
Do not PM me for personal consultancy unless you are willing to pay for it.

RBA2000

Beste Nico,

Bedankt voor u reactie.
De sketch is nogal rommelig daar ik in de test en leer fase zit (slecht excuus, ik weet het).
Hopelijk is het wel een beetje duidelijk door middel van de extra tekst in de sketch.
Code: [Select]
/*
* WebCtrl_oled_rgb7.ino
* Created on: 4.12.2016
*
*/
//Modified to act as AP


#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsServer.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <Hash.h>

#include "FS.h"

#define LED_RED 15
#define LED_GREEN 12
#define LED_BLUE 13

#define USE_SERIAL Serial
#define OLED_RESET LED_BUILTIN //4
Adafruit_SSD1306 display(OLED_RESET);

const unsigned char ad3dlogo [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};

ESP8266WiFiMulti WiFiMulti;

ESP8266WebServer server = ESP8266WebServer(80);
WebSocketsServer webSocket = WebSocketsServer(81);

String html_home;

void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {

switch(type) {
case WStype_DISCONNECTED:
USE_SERIAL.printf("[%u] Disconnected!\n", num);
  display.fillCircle(120, 3, 3, BLACK);
  display.display();
break;
case WStype_CONNECTED: {
IPAddress ip = webSocket.remoteIP(num);
USE_SERIAL.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
  display.fillCircle(120, 3, 3, WHITE);
  display.display();
 
// send message to client
webSocket.sendTXT(num, "Connected");
}
break;
case WStype_TEXT:
USE_SERIAL.printf("[%u] get Text: %s\n", num, payload);

if(payload[0] == '#') {
// we get RGB data

union {
   uint32_t rgb;
   struct {
      uint8_t blue;
      uint8_t green;
      uint8_t red;
      uint8_t notUsed;
   }
} RGB;

// decode rgb data
uint32_t RGB.rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);

analogWrite(LED_RED, RGB.red);
analogWrite(LED_GREEN, RGB.green);
analogWrite(LED_BLUE, RGB.blue);
}

break;
}

}

void prepareFile(){
 
  Serial.println("Prepare file system");
  SPIFFS.begin();
 
  File file = SPIFFS.open("/home.html", "r");
  if (!file) {
    Serial.println("file open failed"); 
  } else{
    Serial.println("file open success");

    html_home = "";
    while (file.available()) {
      //Serial.write(file.read());
      String line = file.readStringUntil('\n');
      html_home += line + "\n";
    }
    file.close();

    Serial.print(html_home);
  }
}

void setup() {

// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done
  // Clear the buffer.
  display.clearDisplay();

  // Draw the bitmap:
  // drawBitmap(x position, y position, bitmap data, bitmap width, bitmap height, color)
  display.drawBitmap(0, 0, ad3dlogo, 128, 64, WHITE);

  // Update the display
  display.display();
 
//USE_SERIAL.begin(921600);
USE_SERIAL.begin(115200);

//USE_SERIAL.setDebugOutput(true);

USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();

for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}

pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);

digitalWrite(LED_RED, 1);
digitalWrite(LED_GREEN, 1);
digitalWrite(LED_BLUE, 1);

prepareFile();

WiFi.softAP("arduino-er", "12345678");
IPAddress myIP = WiFi.softAPIP();
USE_SERIAL.print("AP IP address: ");
USE_SERIAL.println(myIP);

// start webSocket server
webSocket.begin();
webSocket.onEvent(webSocketEvent);

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

// handle index
server.on("/", []() {
// send home.html
server.send(200, "text/html", html_home);
});

server.begin();

// Add service to MDNS
MDNS.addService("http", "tcp", 80);
MDNS.addService("ws", "tcp", 81);

digitalWrite(LED_RED, 125); // led 50 procent on after boot
digitalWrite(LED_GREEN, 125);
digitalWrite(LED_BLUE, 125);

// -------------------------------------------------------------
// just a (bad way of programming) test for oled
// need a way to make a routine for this

// text display
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
 
  display.setCursor(0,0);
  display.print("ADCtrl-v1");
  display.println(".3");
 
  display.setCursor(3,16);
  display.print("R");
  display.fillRect(11, 21, 3, 2, WHITE); // 20 procent led on
  display.fillRect(15, 20, 3, 3, WHITE); // 40 procent led on
  display.fillRect(19, 19, 3, 4, WHITE); // 60 procent led on
  // display.fillRect(23, 18, 3, 5, WHITE); // 80 procent led on
  // display.fillRect(27, 17, 3, 6, WHITE); // 100 procent led on

  display.setCursor(3,28);
  display.print("G");
  display.fillRect(11, 33, 3, 2, WHITE);
  display.fillRect(15, 32, 3, 3, WHITE);
  display.fillRect(19, 31, 3, 4, WHITE);
  // display.fillRect(23, 30, 3, 5, WHITE);
  // display.fillRect(27, 29, 3, 6, WHITE);

  display.setCursor(3,41);
  display.print("B");
  display.fillRect(11, 46, 3, 2, WHITE);
  display.fillRect(15, 45, 3, 3, WHITE);
  display.fillRect(19, 44, 3, 4, WHITE);
  // display.fillRect(23, 43, 3, 5, WHITE);
  // display.fillRect(27, 42, 3, 6, WHITE);
 
  display.drawCircle(120, 3, 3, WHITE);
 
  display.drawLine(0, 10, 127, 10, WHITE);
 
  display.drawLine(33, 10, 33, 54, WHITE);
  display.setCursor(38,19);
  display.print("Switch L: OFF");
  display.setCursor(38,39);
  display.print("Switch R: OFF");

  display.drawLine(0, 54, 127, 54, WHITE);

  display.setCursor(0,57);
  display.print("AP-IP:");
  // display.setCursor(36,57);
  display.println(myIP);
 
  display.display();
// -------------------------------------------------------------

  USE_SERIAL.printf("Server Start\n");

}

void loop() {
webSocket.loop();
server.handleClient();
}

Een rommeltje al om en gezien de maximum allowed lenght (9000 characters) de code van het logo deels verwijderd.
Bij voorbaat dank dat u er naar wil kijken.
Ik zie het waarschijnlijk gewoon niet meer.

nicoverduin

Welke (link) voor websocketsServer gebruik jij?
Met vriendelijke groet / kindest regards
Nico Verduin
www.verelec.nl
Do not PM me for personal consultancy unless you are willing to pay for it.

RBA2000

#7
Dec 06, 2016, 05:01 pm Last Edit: Dec 06, 2016, 05:13 pm by RBA2000
Ik heb een home.html op de esp8266 staan. (flash file system)
De bedoeling is dat hierdoor er geen internet nodig is om het zo maar te zeggen.
De home.html maakt geen gebruik van een externe script.
Code: [Select]
<!DOCTYPE html>
<html dir="ltr" lang="en-US"><head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"><!-- Created by AlDay-3D -->
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale = 1.0, maximum-scale = 1.0, user-scalable = no, width = device-width">
    <meta name="description" content="allday-3d.come. When more direct methods become necessary.">
    <meta name="keywords" content="3D design, 3D objects, 3D printing, ESP8266, Arduino, Raspberry Pi, Poser, OBJ, DAZ3D Studio, Rain-to-Art, Aineke.nl,">
    <title>ADCtrl v1.3</title>

<script>
var connection = new WebSocket('ws://'+location.hostname+':81/', ['arduino']);
connection.onopen = function(){
    connection.send('Connect ' + new Date());
};

connection.onerror = function(error){
    console.log('WebSocket Error ', error);
};

connection.onmessage = function(e){
    console.log('Server: ', e.data);
};

function sendRGB(){
    var r = parseInt(document.getElementById('r').value).toString(16);
    var g = parseInt(document.getElementById('g').value).toString(16);
    var b = parseInt(document.getElementById('b').value).toString(16);
   
    if(r.length < 2){
        r = '0' + r;
    }   
   
    if(g.length < 2){
        g = '0' + g;
    }   
   
    if(b.length < 2){
        b = '0' + b;
    }   
   
    var rgb = '#'+r+g+b;   
    console.log('RGB: ' + rgb);
    connection.send(rgb);
};

function sendS1(){
var s1 = parseInt(document.getElementById('s1').value).toString(16);
var bt1 ='@'+s1;
console.log('Switch 1: ' + bt1);
    connection.send(bt1);
};

function sendS2(){
var s2 = parseInt(document.getElementById('s2').value).toString(16);
var bt2 ='@'+s2;
console.log('Switch 2: ' + bt2);
    connection.send(bt2);
}
</script>

<style>
input[type=range] {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  margin: 10px 0;
  width: 80%;
}
input[type=range]:focus {
  outline: none;
}
input[type=range]::-webkit-slider-runnable-track {
  width: 100%;
  height: 4px;
  cursor: pointer;
  animate: 0.2s;
  box-shadow: 0px 0px 0px #000000;
  background: #C0C0C0;
  border-radius: 0px;
  border: 0px solid #000000;
}
input[type=range]::-webkit-slider-thumb {
  box-shadow: 1px 1px 1px #000000;
  border: 1px solid #A9A9A9;
  height: 30px;
  width: 18px;
  border-radius: 25px;
  background: #778899;
  cursor: pointer;
  -webkit-appearance: none;
  margin-top: -13.5px;
}
input[type=range]:focus::-webkit-slider-runnable-track {
  background: #C0C0C0;
}
input[type=range]::-moz-range-track {
  width: 100%;
  height: 3px;
  cursor: pointer;
  animate: 0.2s;
  box-shadow: 0px 0px 0px #000000;
  background: #C0C0C0;
  border-radius: 0px;
  border: 0px solid #000000;
}
input[type=range]::-moz-range-thumb {
  box-shadow: 1px 1px 1px #000000;
  border: 1px solid #A9A9A9;
  height: 30px;
  width: 18px;
  border-radius: 25px;
  background: #778899;
  cursor: pointer;
}
input[type=range]::-ms-track {
  width: 100%;
  height: 3px;
  cursor: pointer;
  animate: 0.2s;
  background: transparent;
  border-color: transparent;
  color: transparent;
}
input[type=range]::-ms-fill-lower {
  background: #C0C0C0;
  border: 0px solid #000000;
  border-radius: 0px;
  box-shadow: 0px 0px 0px #000000;
}
input[type=range]::-ms-fill-upper {
  background: #C0C0C0;
  border: 0px solid #000000;
  border-radius: 0px;
  box-shadow: 0px 0px 0px #000000;
}
input[type=range]::-ms-thumb {
  box-shadow: 1px 1px 1px #000000;
  border: 1px solid #A9A9A9;
  height: 30px;
  width: 18px;
  border-radius: 25px;
  background: #778899;
  cursor: pointer;
}
input[type=range]:focus::-ms-fill-lower {
  background: #C0C0C0;
}
input[type=range]:focus::-ms-fill-upper {
  background: #C0C0C0;
}
</style>

<style>
  input[type="button"]{
  background-color: #778899;
  color: #000000;
  width: 120px;
  height: 30px;
  border-radius: 6px;
  cursor: pointer;
  -webkit-appearance: none;
  margin-top: -13.5px;
}
</style>

</head>
<body style="background-color:Gainsboro;">
<div id="product"><span style="color: #2F4F4F; font-size: 20pt; text-align: center; font-weight: bold;"">ADCtrl </span><span style="color: #C0C0C0">v1.3</span></div>
<hr>

<!-- sliders and buttons -->
Outlets control
   <p style="text-align: center;">
<input id="s1" type="button" value="1" onclick="sendS1();" /<br/>
<input id="s2" type="button" value="2" onclick="sendS2();" /<br/>
<br></p>
<hr>

RGB control
   <p style="text-align: center;">
R: <input id="r" type="range" min="0" max="255" step="1" onchange="sendRGB();" /><br/><br/>
G: <input id="g" type="range" min="0" max="255" step="1" onchange="sendRGB();" /><br/><br/>
B: <input id="b" type="range" min="0" max="255" step="1" onchange="sendRGB();" /><br/><br/>
<br></p>
<hr>



<table class="RBA2000-article" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; width: 100%;">
        <tbody>
        <tr>
        <td style="width: 20%; text-align: right;"><br></td>
        <td style="color:#2F4F4F; width: 50%;"><span style="text-align: right; font-weight: bold;">Instructions</span><br></td>
        </tr>
        <tr>
        <td style="color:Silver; width: 20%; text-align: right;"><span style="text-align: right; font-weight: bold;">Product:</span><br></td>
        <td style="color:#2F4F4F; width: 50%;">ADCtrl Remote</td>
        </tr>
        <tr>
        <td style="color:Silver; width: 20%; text-align: right; font-weight: bold;"><span style="font-weight: bold; text-align: right;"></span>Slider R:<br></td>
        <td style="color:#2F4F4F; width: 50%;">Handle on slider R controls the red led.<br></td>
        </tr>
        <tr>
        <td style="color:Silver; width: 20%; text-align: right;"><span style="font-weight: bold; text-align: right;">Slider G:</span><br></td>
        <td style="color:#2F4F4F; width: 50%;">Handle on slider G controls the green led.<br></td>
        </tr>
        <tr>
        <td style="color:Silver; width: 20%; text-align: right;"><span style="font-weight: bold; text-align: right;">Slider B:</span><br></td>
        <td style="color:#2F4F4F; width: 50%;">Handle on slider B controls the blue led.<br></td>
        </tr>
        <tr>
        <td style="color:Silver; width: 20%; text-align: right;"><span style="font-weight: bold; text-align: right;">Switch 1:</span><br></td>
        <td style="color:#2F4F4F; width: 50%;">Button 1 controls switch 1.<br></td>
        </tr>
        <tr>
        <td style="color:Silver; width: 20%; text-align: right;"><span style="font-weight: bold; text-align: right;">Switch 2:</span><br></td>
        <td style="color:#2F4F4F; width: 50%;">Button 2 controls switch 2.<br></td>
</tr>
        <tr>
        <td style="color:Silver; width: 20%; text-align: right;"><span style="font-weight: bold; text-align: right;">Note:</span><br></td>
        <td style="color:#2F4F4F; width: 50%;">Check the display for info.<br></td>
        </tr>
        <tr>
        <td style="width: 20%; text-align: right;"><span style="font-weight: bold; text-align: right;"> </span><br></td>
        <td style="color:#2F4F4F; width: 50%;">Based on an example by Arduino-er.<br></td>
        </tr>
        </tbody>
        </table>

<footer class="RBA2000-footer clearfix">
<p style=color:Silver>Copyright © 2016, All Day 3D. All Rights Reserved.</p>
</footer>

</body>
</html>

Wellicht ook handig om te weten.
Sorry.

nicoverduin

Ik vind die link belangrijker want op dit moment kan ik niet compileren omdat ik die lib mis.
Met vriendelijke groet / kindest regards
Nico Verduin
www.verelec.nl
Do not PM me for personal consultancy unless you are willing to pay for it.

RBA2000

Ah, die link.
https://github.com/Links2004/arduinoWebSockets

nicoverduin

#10
Dec 06, 2016, 07:31 pm Last Edit: Dec 06, 2016, 07:31 pm by nicoverduin
Zat nog een klein foutje in mijn voorbeeld. Moet zijn:
Code: [Select]

union {
   uint32_t rgb;
   struct {
      uint8_t blue;
      uint8_t green;
      uint8_t red;
      uint8_t notUsed;
   };
} RGB;

// decode rgb data
RGB.rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);

analogWrite(LED_RED, RGB.red);
analogWrite(LED_GREEN, RGB.green);
analogWrite(LED_BLUE, RGB.blue);


RGB is al gedefinieerd dus moet er geen uint32_t voor en bij de struct ontbrak de ;[/code]
Met vriendelijke groet / kindest regards
Nico Verduin
www.verelec.nl
Do not PM me for personal consultancy unless you are willing to pay for it.

RBA2000

Bedankt voor u reactie.
Tja, die semi-colon was me ook opgevallen.
En nu na die kleine verandering zitten we weer op de juiste weg.

Echter, daar de analogWrite funtie van een ESP8266 defaults 1023 full scale is stuurt deze code maar weer 25% naar de led indien bijv. RGB.red=255.

Zou ik nu weer moeten gaan mappen?
of...

nicoverduin

Gewoon gaan mappen....
Met vriendelijke groet / kindest regards
Nico Verduin
www.verelec.nl
Do not PM me for personal consultancy unless you are willing to pay for it.

Go Up