{"action":"opened","issue":{"url":"https://api.github.com/repos/mysensors/Arduino/issues/223","labels_url":"https://api.github.com/repos/mysensors/Arduino/issues/223/labels{/name}","comments_url":"https://api.github.com/repos/mysensors/Arduino/issues/223/comments","events_url":"https://api.github.com/repos/mysensors/Arduino/issues/223/events","html_url":"https://github.com/mysensors/Arduino/issues/223","id":110502203,"number":223,"title":"Use portable datatypes in message definition","user":{"login":"Yveaux","id":7716005,"avatar_url":"https://avatars.githubusercontent.com/u/7716005?v=3","gravatar_id":"","url":"https://api.github.com/users/Yveaux","html_url":"https://github.com/Yveaux","followers_url":"https://api.github.com/users/Yveaux/followers","following_url":"https://api.github.com/users/Yveaux/following{/other_user}","gists_url":"https://api.github.com/users/Yveaux/gists{/gist_id}","starred_url":"https://api.github.com/users/Yveaux/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Yveaux/subscriptions","organizations_url":"https://api.github.com/users/Yveaux/orgs","repos_url":"https://api.github.com/users/Yveaux/repos","events_url":"https://api.github.com/users/Yveaux/events{/privacy}","received_events_url":"https://api.github.com/users/Yveaux/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-10-08T17:20:55Z","updated_at":"2015-10-08T17:20:55Z","closed_at":null,"body":"MyMessage, used for message data transfer, is defined as follows:\r\n```\r\n\tunion {\r\n\t\tuint8_t bValue;\r\n\t\tunsigned long ulValue;\r\n\t\tlong lValue;\r\n\t\tunsigned int uiValue;\r\n\t\tint iValue;\r\n\t\tstruct { // Float messages\r\n\t\t\tfloat fValue;\r\n\t\t\tuint8_t fPrecision;   // Number of decimals when serializing\r\n\t\t};\r\n\t\tstruct {  // Presentation messages\r\n\t\t\tuint8_t version; \t  // Library version\r\n   \t\t    uint8_t sensorType;   // Sensor type hint for controller, see table above\r\n\t\t};\r\n\t\tchar data[MAX_PAYLOAD + 1];\r\n\t} __attribute__((packed));\r\n```\r\n\r\nThe (unsigned) int types are non-portable when going from 8 bit AVR architecture to 32 bit (e.g. ESP8266, Cortex-M), which is a problem when architectures are mixed within a network.\r\nOn AVR an (unsigned) int is 16bits, on ESP it is 32 bits.\r\nOn AVR a double is 32bits (equal to float), on ESP it is 64 bits.\r\n\r\nThis probably lead to a problem described in the forum: http://forum.mysensors.org/topic/2118/esp8266-wifi-gateway-issue\r\n\r\nFor portability use uint16_t and int16_t for ints.\r\nFor consistency also use uint32_t and int32_t for longs, uint8_t and int8_t for chars.\r\n\r\n--------------------\r\nBackground\r\n\r\nFollowing simple sketch can be run on different targets to identify the sizes of their datatypes:\r\n\r\n```\r\n#define SIZEOF(x) \\\r\n  Serial.print(\"sizeof(\" #x \")=\"); Serial.println(sizeof(x));\r\n\r\nvoid setup() {\r\n  Serial.begin(115200);\r\n  SIZEOF(char);\r\n  SIZEOF(int);\r\n  SIZEOF(short);\r\n  SIZEOF(long);\r\n  SIZEOF(float);\r\n  SIZEOF(double);\r\n  SIZEOF(uint8_t);\r\n  SIZEOF(uint16_t);\r\n  SIZEOF(uint32_t);\r\n}\r\n\r\nvoid loop() {\r\n}\r\n```\r\n\r\nOutput on ATMega328 (8bit)\r\n```\r\nsizeof(char)=1\r\nsizeof(int)=2\r\nsizeof(short)=2\r\nsizeof(long)=4\r\nsizeof(float)=4\r\nsizeof(double)=4\r\nsizeof(uint8_t)=1\r\nsizeof(uint16_t)=2\r\nsizeof(uint32_t)=4\r\n```\r\n\r\nOutput on ESP8266 (32bit)\r\n```\r\nsizeof(char)=1\r\nsizeof(int)=4            <-- Problematic\r\nsizeof(short)=2\r\nsizeof(long)=4\r\nsizeof(float)=4\r\nsizeof(double)=8         <-- Problematic, but not used in MyMessage\r\nsizeof(uint8_t)=1\r\nsizeof(uint16_t)=2\r\nsizeof(uint32_t)=4\r\n```\r\n"}}